|
0
|
1 package main
|
|
|
2
|
|
|
3 import (
|
|
|
4 "crypto/tls"
|
|
|
5 "fmt"
|
|
|
6 "log"
|
|
|
7 "net"
|
|
|
8 "net/http"
|
|
|
9 "net/http/httputil"
|
|
|
10 "net/url"
|
|
|
11 "os"
|
|
|
12 "path"
|
|
|
13 "path/filepath"
|
|
|
14 "regexp"
|
|
|
15 "runtime"
|
|
|
16 "strconv"
|
|
|
17 "strings"
|
|
|
18
|
|
|
19 "d2o/fcgi"
|
|
|
20 "d2o/icf"
|
|
|
21 )
|
|
|
22
|
|
|
23 func main() {
|
|
|
24 cfgPath := "/etc/d2obase"
|
|
|
25 if len(os.Args) > 1 {
|
|
|
26 cfgPath = os.Args[1]
|
|
|
27 }
|
|
|
28
|
|
|
29 f, err := os.Open(cfgPath)
|
|
|
30 if err != nil {
|
|
|
31 log.Fatalf("d2o: cannot open config: %v", err)
|
|
|
32 }
|
|
|
33 cfg, err := icf.Parse(f)
|
|
|
34 f.Close()
|
|
|
35 if err != nil {
|
|
|
36 log.Fatalf("d2o: config error: %v", err)
|
|
|
37 }
|
|
|
38
|
|
|
39 for _, d := range cfg.Abstract("d2o") {
|
|
|
40 switch d.Key {
|
|
|
41 case "threads":
|
|
|
42 n, err := strconv.Atoi(safeArg(d.Args, 0))
|
|
|
43 if err == nil && n > 0 {
|
|
|
44 runtime.GOMAXPROCS(n)
|
|
|
45 log.Printf("d2o: GOMAXPROCS = %d", n)
|
|
|
46 }
|
|
|
47 }
|
|
|
48 }
|
|
|
49
|
|
|
50 ports := collectPorts(cfg)
|
|
|
51 if len(ports) == 0 {
|
|
|
52 log.Fatal("d2o: no port directives found in config")
|
|
|
53 }
|
|
|
54
|
|
|
55 h := &handler{cfg: cfg}
|
|
|
56 errCh := make(chan error, len(ports))
|
|
|
57
|
|
|
58 for _, pc := range ports {
|
|
|
59 go func(pc portConfig) {
|
|
|
60 errCh <- pc.listen(h)
|
|
|
61 }(pc)
|
|
|
62 }
|
|
|
63
|
|
|
64 log.Fatal(<-errCh)
|
|
|
65 }
|
|
|
66
|
|
|
67 // --- Port collection --------------------------------------------------------
|
|
|
68
|
|
|
69 type portConfig struct {
|
|
|
70 addr string
|
|
|
71 certFile string
|
|
|
72 keyFile string
|
|
|
73 isTLS bool
|
|
|
74 }
|
|
|
75
|
|
|
76 func (pc portConfig) listen(h http.Handler) error {
|
|
|
77 if !pc.isTLS {
|
|
|
78 log.Printf("d2o: listening on %s (http)", pc.addr)
|
|
|
79 return http.ListenAndServe(pc.addr, h)
|
|
|
80 }
|
|
|
81 cert, err := tls.LoadX509KeyPair(pc.certFile, pc.keyFile)
|
|
|
82 if err != nil {
|
|
|
83 return fmt.Errorf("d2o: tls: %w", err)
|
|
|
84 }
|
|
|
85 ln, err := tls.Listen("tcp", pc.addr, &tls.Config{
|
|
|
86 Certificates: []tls.Certificate{cert},
|
|
|
87 MinVersion: tls.VersionTLS12,
|
|
|
88 })
|
|
|
89 if err != nil {
|
|
|
90 return fmt.Errorf("d2o: listen %s: %w", pc.addr, err)
|
|
|
91 }
|
|
|
92 log.Printf("d2o: listening on %s (https)", pc.addr)
|
|
|
93 return http.Serve(ln, h)
|
|
|
94 }
|
|
|
95
|
|
|
96 func collectPorts(cfg *icf.Config) []portConfig {
|
|
|
97 seen := make(map[string]bool)
|
|
|
98 var out []portConfig
|
|
|
99
|
|
|
100 for _, b := range cfg.Blocks {
|
|
|
101 dirs := cfg.ResolveBlock(b, nil)
|
|
|
102
|
|
|
103 var cert, key string
|
|
|
104 for _, d := range dirs {
|
|
|
105 if d.Key == "tls" {
|
|
|
106 cert = safeArg(d.Args, 0)
|
|
|
107 key = safeArg(d.Args, 1)
|
|
|
108 }
|
|
|
109 }
|
|
|
110
|
|
|
111 for _, d := range dirs {
|
|
|
112 switch d.Key {
|
|
|
113 case "port":
|
|
|
114 addr := ":" + safeArg(d.Args, 0)
|
|
|
115 if !seen[addr] {
|
|
|
116 seen[addr] = true
|
|
|
117 out = append(out, portConfig{addr: addr})
|
|
|
118 }
|
|
|
119 case "port+tls":
|
|
|
120 addr := ":" + safeArg(d.Args, 0)
|
|
|
121 c := safeArg(d.Args, 1)
|
|
|
122 k := safeArg(d.Args, 2)
|
|
|
123 if c == "" {
|
|
|
124 c = cert
|
|
|
125 }
|
|
|
126 if k == "" {
|
|
|
127 k = key
|
|
|
128 }
|
|
|
129 if !seen[addr] {
|
|
|
130 seen[addr] = true
|
|
|
131 out = append(out, portConfig{addr: addr, certFile: c, keyFile: k, isTLS: true})
|
|
|
132 }
|
|
|
133 }
|
|
|
134 }
|
|
|
135 }
|
|
|
136 return out
|
|
|
137 }
|
|
|
138
|
|
|
139 // --- HTTP Handler -----------------------------------------------------------
|
|
|
140
|
|
|
141 type handler struct {
|
|
|
142 cfg *icf.Config
|
|
|
143 }
|
|
|
144
|
|
|
145 func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
146 host := stripPort(r.Host)
|
|
|
147 reqPath := path.Clean(r.URL.Path)
|
|
|
148
|
|
|
149 dirs, caps := h.cfg.Match(host + reqPath)
|
|
|
150 if dirs == nil {
|
|
|
151 dirs, caps = h.cfg.Match(host)
|
|
|
152 }
|
|
|
153 if dirs == nil {
|
|
|
154 http.Error(w, "not found", http.StatusNotFound)
|
|
|
155 return
|
|
|
156 }
|
|
|
157
|
|
|
158 h.serve(w, r, dirs, caps)
|
|
|
159 }
|
|
|
160
|
|
|
161 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) {
|
|
|
162 var (
|
|
1
|
163 rootDir string
|
|
|
164 rootIndex []string
|
|
|
165 fcgiAddr string
|
|
|
166 fcgiPat string
|
|
|
167 rprxAddr string
|
|
0
|
168 )
|
|
|
169
|
|
|
170 for _, d := range dirs {
|
|
|
171 switch d.Key {
|
|
|
172 case "root":
|
|
|
173 rootDir = safeArg(d.Args, 0)
|
|
|
174 switch safeArg(d.Args, 1) {
|
|
|
175 case "show":
|
|
1
|
176 if len(d.Args) >= 3 {
|
|
|
177 rootIndex = d.Args[2:]
|
|
|
178 } else {
|
|
|
179 rootIndex = []string{"index.html"}
|
|
|
180 }
|
|
0
|
181 case "hide", "":
|
|
1
|
182 rootIndex = nil
|
|
0
|
183 default:
|
|
|
184 log.Printf("d2o: root: unknown mode %q (want show|hide)", safeArg(d.Args, 1))
|
|
|
185 }
|
|
|
186 case "fcgi":
|
|
|
187 fcgiAddr = safeArg(d.Args, 0)
|
|
|
188 fcgiPat = safeArg(d.Args, 1)
|
|
|
189 if fcgiPat == "" {
|
|
|
190 fcgiPat = "*"
|
|
|
191 }
|
|
|
192 case "rprx":
|
|
|
193 rprxAddr = safeArg(d.Args, 0)
|
|
|
194 }
|
|
|
195 }
|
|
|
196
|
|
|
197 if rprxAddr != "" {
|
|
|
198 serveReverseProxy(w, r, rprxAddr)
|
|
|
199 return
|
|
|
200 }
|
|
|
201 if fcgiAddr != "" && matchGlob(fcgiPat, r.URL.Path) {
|
|
|
202 if err := serveFCGI(w, r, fcgiAddr, rootDir); err != nil {
|
|
|
203 log.Printf("d2o: fcgi error: %v", err)
|
|
|
204 http.Error(w, "gateway error", http.StatusBadGateway)
|
|
|
205 }
|
|
|
206 return
|
|
|
207 }
|
|
|
208 if rootDir != "" {
|
|
1
|
209 serveStatic(w, r, rootDir, rootIndex)
|
|
0
|
210 return
|
|
|
211 }
|
|
|
212
|
|
|
213 http.Error(w, "not found", http.StatusNotFound)
|
|
|
214 }
|
|
|
215
|
|
|
216 // --- Static -----------------------------------------------------------------
|
|
|
217
|
|
1
|
218 func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, rootIndex []string) {
|
|
0
|
219 fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path)))
|
|
|
220
|
|
|
221 info, err := os.Stat(fpath)
|
|
|
222 if os.IsNotExist(err) {
|
|
|
223 http.Error(w, "not found", http.StatusNotFound)
|
|
|
224 return
|
|
|
225 }
|
|
|
226 if err != nil {
|
|
|
227 http.Error(w, "internal error", http.StatusInternalServerError)
|
|
|
228 return
|
|
|
229 }
|
|
1
|
230
|
|
0
|
231 if info.IsDir() {
|
|
1
|
232 if rootIndex == nil {
|
|
0
|
233 http.Error(w, "forbidden", http.StatusForbidden)
|
|
|
234 return
|
|
|
235 }
|
|
1
|
236 for _, idx := range rootIndex {
|
|
|
237 idxPath := filepath.Join(fpath, idx)
|
|
|
238 if _, err := os.Stat(idxPath); err == nil {
|
|
|
239 http.ServeFile(w, r, idxPath)
|
|
|
240 return
|
|
|
241 }
|
|
|
242 }
|
|
0
|
243 listDir(w, r, fpath, r.URL.Path)
|
|
|
244 return
|
|
|
245 }
|
|
1
|
246
|
|
0
|
247 http.ServeFile(w, r, fpath)
|
|
|
248 }
|
|
|
249
|
|
|
250 func listDir(w http.ResponseWriter, r *http.Request, dir, urlPath string) {
|
|
|
251 entries, err := os.ReadDir(dir)
|
|
|
252 if err != nil {
|
|
|
253 http.Error(w, "cannot read directory", http.StatusInternalServerError)
|
|
|
254 return
|
|
|
255 }
|
|
|
256 w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
257 fmt.Fprintf(w, "<html><head><title>Index of %s</title></head><body>\n", urlPath)
|
|
|
258 fmt.Fprintf(w, "<h2>Index of %s</h2><hr><pre>\n", urlPath)
|
|
|
259 if urlPath != "/" {
|
|
|
260 fmt.Fprintf(w, "<a href=\"..\">..</a>\n")
|
|
|
261 }
|
|
|
262 for _, e := range entries {
|
|
|
263 name := e.Name()
|
|
|
264 if e.IsDir() {
|
|
|
265 name += "/"
|
|
|
266 }
|
|
|
267 fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", path.Join(urlPath, name), name)
|
|
|
268 }
|
|
|
269 fmt.Fprintf(w, "</pre><hr><i>d2o webserver</i></body></html>")
|
|
|
270 }
|
|
|
271
|
|
|
272 // --- Reverse proxy ----------------------------------------------------------
|
|
|
273
|
|
|
274 func serveReverseProxy(w http.ResponseWriter, r *http.Request, target string) {
|
|
|
275 if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") {
|
|
|
276 target = "http://" + target
|
|
|
277 }
|
|
|
278 u, err := url.Parse(target)
|
|
|
279 if err != nil {
|
|
|
280 http.Error(w, "bad gateway config", http.StatusInternalServerError)
|
|
|
281 return
|
|
|
282 }
|
|
|
283 proxy := httputil.NewSingleHostReverseProxy(u)
|
|
|
284 proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
285 log.Printf("d2o: rprx error: %v", err)
|
|
|
286 http.Error(w, "bad gateway", http.StatusBadGateway)
|
|
|
287 }
|
|
|
288 proxy.ServeHTTP(w, r)
|
|
|
289 }
|
|
|
290
|
|
|
291 // --- FastCGI ----------------------------------------------------------------
|
|
|
292
|
|
|
293 func serveFCGI(w http.ResponseWriter, r *http.Request, addr, docRoot string) error {
|
|
|
294 network, address := parseFCGIAddr(addr)
|
|
|
295 conn, err := net.Dial(network, address)
|
|
|
296 if err != nil {
|
|
|
297 return fmt.Errorf("connect %s: %w", addr, err)
|
|
|
298 }
|
|
|
299 defer conn.Close()
|
|
|
300
|
|
|
301 scriptPath := r.URL.Path
|
|
|
302 if docRoot != "" {
|
|
|
303 scriptPath = filepath.Join(docRoot, filepath.FromSlash(r.URL.Path))
|
|
|
304 }
|
|
|
305
|
|
|
306 params := map[string]string{
|
|
|
307 "REQUEST_METHOD": r.Method,
|
|
|
308 "SCRIPT_FILENAME": scriptPath,
|
|
|
309 "SCRIPT_NAME": r.URL.Path,
|
|
|
310 "REQUEST_URI": r.URL.RequestURI(),
|
|
|
311 "QUERY_STRING": r.URL.RawQuery,
|
|
|
312 "SERVER_PROTOCOL": r.Proto,
|
|
|
313 "SERVER_NAME": stripPort(r.Host),
|
|
|
314 "DOCUMENT_ROOT": docRoot,
|
|
|
315 "GATEWAY_INTERFACE": "CGI/1.1",
|
|
|
316 "SERVER_SOFTWARE": "d2o/1.0",
|
|
|
317 }
|
|
|
318 if r.TLS != nil {
|
|
|
319 params["HTTPS"] = "on"
|
|
|
320 }
|
|
|
321 for k, vs := range r.Header {
|
|
|
322 key := "HTTP_" + strings.ToUpper(strings.ReplaceAll(k, "-", "_"))
|
|
|
323 params[key] = strings.Join(vs, ", ")
|
|
|
324 }
|
|
|
325 if ct := r.Header.Get("Content-Type"); ct != "" {
|
|
|
326 params["CONTENT_TYPE"] = ct
|
|
|
327 }
|
|
|
328 if r.ContentLength >= 0 {
|
|
|
329 params["CONTENT_LENGTH"] = strconv.FormatInt(r.ContentLength, 10)
|
|
|
330 }
|
|
|
331
|
|
|
332 return fcgi.Do(w, r, conn, params)
|
|
|
333 }
|
|
|
334
|
|
|
335 func parseFCGIAddr(addr string) (network, address string) {
|
|
|
336 if strings.HasPrefix(addr, "unix:") {
|
|
|
337 return "unix", strings.TrimPrefix(addr, "unix:")
|
|
|
338 }
|
|
|
339 return "tcp", addr
|
|
|
340 }
|
|
|
341
|
|
|
342 // --- Helpers ----------------------------------------------------------------
|
|
|
343
|
|
|
344 func stripPort(host string) string {
|
|
|
345 if h, _, err := net.SplitHostPort(host); err == nil {
|
|
|
346 return h
|
|
|
347 }
|
|
|
348 return host
|
|
|
349 }
|
|
|
350
|
|
|
351 func safeArg(args []string, i int) string {
|
|
|
352 if i < len(args) {
|
|
|
353 return args[i]
|
|
|
354 }
|
|
|
355 return ""
|
|
|
356 }
|
|
|
357
|
|
|
358 func matchGlob(pattern, s string) bool {
|
|
|
359 if pattern == "*" {
|
|
|
360 return true
|
|
|
361 }
|
|
|
362 regPat := "^" + strings.ReplaceAll(regexp.QuoteMeta(pattern), regexp.QuoteMeta("*"), ".*") + "$"
|
|
|
363 matched, err := regexp.MatchString(regPat, s)
|
|
|
364 return err == nil && matched
|
|
|
365 }
|