|
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 (
|
|
2
|
163 rootDir string
|
|
|
164 rootShow bool
|
|
|
165 ndex []string
|
|
|
166 fcgiAddr string
|
|
|
167 fcgiPat string
|
|
|
168 rprxAddr string
|
|
|
169 rdirCode int
|
|
|
170 rdirURL string
|
|
0
|
171 )
|
|
|
172
|
|
|
173 for _, d := range dirs {
|
|
|
174 switch d.Key {
|
|
|
175 case "root":
|
|
|
176 rootDir = safeArg(d.Args, 0)
|
|
2
|
177 rootShow = safeArg(d.Args, 1) == "show"
|
|
|
178 case "ndex":
|
|
|
179 ndex = d.Args
|
|
0
|
180 case "fcgi":
|
|
|
181 fcgiAddr = safeArg(d.Args, 0)
|
|
|
182 fcgiPat = safeArg(d.Args, 1)
|
|
|
183 if fcgiPat == "" {
|
|
|
184 fcgiPat = "*"
|
|
|
185 }
|
|
|
186 case "rprx":
|
|
|
187 rprxAddr = safeArg(d.Args, 0)
|
|
2
|
188 case "rdir":
|
|
|
189 rdirCode, _ = strconv.Atoi(safeArg(d.Args, 0))
|
|
|
190 rdirURL = safeArg(d.Args, 1)
|
|
0
|
191 }
|
|
|
192 }
|
|
|
193
|
|
2
|
194 if rdirURL != "" {
|
|
|
195 if rdirCode == 0 {
|
|
|
196 rdirCode = http.StatusFound
|
|
|
197 }
|
|
|
198 http.Redirect(w, r, rdirURL, rdirCode)
|
|
|
199 return
|
|
|
200 }
|
|
0
|
201 if rprxAddr != "" {
|
|
|
202 serveReverseProxy(w, r, rprxAddr)
|
|
|
203 return
|
|
|
204 }
|
|
|
205 if fcgiAddr != "" && matchGlob(fcgiPat, r.URL.Path) {
|
|
|
206 if err := serveFCGI(w, r, fcgiAddr, rootDir); err != nil {
|
|
|
207 log.Printf("d2o: fcgi error: %v", err)
|
|
|
208 http.Error(w, "gateway error", http.StatusBadGateway)
|
|
|
209 }
|
|
|
210 return
|
|
|
211 }
|
|
|
212 if rootDir != "" {
|
|
2
|
213 serveStatic(w, r, rootDir, rootShow, ndex)
|
|
0
|
214 return
|
|
|
215 }
|
|
|
216
|
|
|
217 http.Error(w, "not found", http.StatusNotFound)
|
|
|
218 }
|
|
|
219
|
|
|
220 // --- Static -----------------------------------------------------------------
|
|
2
|
221 func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, show bool, ndex []string) {
|
|
0
|
222 fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path)))
|
|
|
223
|
|
|
224 info, err := os.Stat(fpath)
|
|
|
225 if os.IsNotExist(err) {
|
|
|
226 http.Error(w, "not found", http.StatusNotFound)
|
|
|
227 return
|
|
|
228 }
|
|
|
229 if err != nil {
|
|
|
230 http.Error(w, "internal error", http.StatusInternalServerError)
|
|
|
231 return
|
|
|
232 }
|
|
1
|
233
|
|
0
|
234 if info.IsDir() {
|
|
2
|
235 for _, idx := range ndex {
|
|
1
|
236 idxPath := filepath.Join(fpath, idx)
|
|
|
237 if _, err := os.Stat(idxPath); err == nil {
|
|
|
238 http.ServeFile(w, r, idxPath)
|
|
|
239 return
|
|
|
240 }
|
|
|
241 }
|
|
2
|
242 if !show {
|
|
|
243 http.Error(w, "forbidden", http.StatusForbidden)
|
|
|
244 return
|
|
|
245 }
|
|
0
|
246 listDir(w, r, fpath, r.URL.Path)
|
|
|
247 return
|
|
|
248 }
|
|
1
|
249
|
|
0
|
250 http.ServeFile(w, r, fpath)
|
|
|
251 }
|
|
|
252
|
|
|
253 func listDir(w http.ResponseWriter, r *http.Request, dir, urlPath string) {
|
|
|
254 entries, err := os.ReadDir(dir)
|
|
|
255 if err != nil {
|
|
|
256 http.Error(w, "cannot read directory", http.StatusInternalServerError)
|
|
|
257 return
|
|
|
258 }
|
|
|
259 w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
260 fmt.Fprintf(w, "<html><head><title>Index of %s</title></head><body>\n", urlPath)
|
|
|
261 fmt.Fprintf(w, "<h2>Index of %s</h2><hr><pre>\n", urlPath)
|
|
|
262 if urlPath != "/" {
|
|
|
263 fmt.Fprintf(w, "<a href=\"..\">..</a>\n")
|
|
|
264 }
|
|
|
265 for _, e := range entries {
|
|
|
266 name := e.Name()
|
|
|
267 if e.IsDir() {
|
|
|
268 name += "/"
|
|
|
269 }
|
|
|
270 fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", path.Join(urlPath, name), name)
|
|
|
271 }
|
|
|
272 fmt.Fprintf(w, "</pre><hr><i>d2o webserver</i></body></html>")
|
|
|
273 }
|
|
|
274
|
|
|
275 // --- Reverse proxy ----------------------------------------------------------
|
|
|
276
|
|
|
277 func serveReverseProxy(w http.ResponseWriter, r *http.Request, target string) {
|
|
|
278 if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") {
|
|
|
279 target = "http://" + target
|
|
|
280 }
|
|
|
281 u, err := url.Parse(target)
|
|
|
282 if err != nil {
|
|
|
283 http.Error(w, "bad gateway config", http.StatusInternalServerError)
|
|
|
284 return
|
|
|
285 }
|
|
|
286 proxy := httputil.NewSingleHostReverseProxy(u)
|
|
|
287 proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
288 log.Printf("d2o: rprx error: %v", err)
|
|
|
289 http.Error(w, "bad gateway", http.StatusBadGateway)
|
|
|
290 }
|
|
|
291 proxy.ServeHTTP(w, r)
|
|
|
292 }
|
|
|
293
|
|
|
294 // --- FastCGI ----------------------------------------------------------------
|
|
|
295
|
|
|
296 func serveFCGI(w http.ResponseWriter, r *http.Request, addr, docRoot string) error {
|
|
|
297 network, address := parseFCGIAddr(addr)
|
|
|
298 conn, err := net.Dial(network, address)
|
|
|
299 if err != nil {
|
|
|
300 return fmt.Errorf("connect %s: %w", addr, err)
|
|
|
301 }
|
|
|
302 defer conn.Close()
|
|
|
303
|
|
|
304 scriptPath := r.URL.Path
|
|
|
305 if docRoot != "" {
|
|
|
306 scriptPath = filepath.Join(docRoot, filepath.FromSlash(r.URL.Path))
|
|
|
307 }
|
|
|
308
|
|
|
309 params := map[string]string{
|
|
|
310 "REQUEST_METHOD": r.Method,
|
|
|
311 "SCRIPT_FILENAME": scriptPath,
|
|
|
312 "SCRIPT_NAME": r.URL.Path,
|
|
|
313 "REQUEST_URI": r.URL.RequestURI(),
|
|
|
314 "QUERY_STRING": r.URL.RawQuery,
|
|
|
315 "SERVER_PROTOCOL": r.Proto,
|
|
|
316 "SERVER_NAME": stripPort(r.Host),
|
|
|
317 "DOCUMENT_ROOT": docRoot,
|
|
|
318 "GATEWAY_INTERFACE": "CGI/1.1",
|
|
|
319 "SERVER_SOFTWARE": "d2o/1.0",
|
|
|
320 }
|
|
|
321 if r.TLS != nil {
|
|
|
322 params["HTTPS"] = "on"
|
|
|
323 }
|
|
|
324 for k, vs := range r.Header {
|
|
|
325 key := "HTTP_" + strings.ToUpper(strings.ReplaceAll(k, "-", "_"))
|
|
|
326 params[key] = strings.Join(vs, ", ")
|
|
|
327 }
|
|
|
328 if ct := r.Header.Get("Content-Type"); ct != "" {
|
|
|
329 params["CONTENT_TYPE"] = ct
|
|
|
330 }
|
|
|
331 if r.ContentLength >= 0 {
|
|
|
332 params["CONTENT_LENGTH"] = strconv.FormatInt(r.ContentLength, 10)
|
|
|
333 }
|
|
|
334
|
|
|
335 return fcgi.Do(w, r, conn, params)
|
|
|
336 }
|
|
|
337
|
|
|
338 func parseFCGIAddr(addr string) (network, address string) {
|
|
|
339 if strings.HasPrefix(addr, "unix:") {
|
|
|
340 return "unix", strings.TrimPrefix(addr, "unix:")
|
|
|
341 }
|
|
|
342 return "tcp", addr
|
|
|
343 }
|
|
|
344
|
|
|
345 // --- Helpers ----------------------------------------------------------------
|
|
|
346
|
|
|
347 func stripPort(host string) string {
|
|
|
348 if h, _, err := net.SplitHostPort(host); err == nil {
|
|
|
349 return h
|
|
|
350 }
|
|
|
351 return host
|
|
|
352 }
|
|
|
353
|
|
|
354 func safeArg(args []string, i int) string {
|
|
|
355 if i < len(args) {
|
|
|
356 return args[i]
|
|
|
357 }
|
|
|
358 return ""
|
|
|
359 }
|
|
|
360
|
|
|
361 func matchGlob(pattern, s string) bool {
|
|
|
362 if pattern == "*" {
|
|
|
363 return true
|
|
|
364 }
|
|
|
365 regPat := "^" + strings.ReplaceAll(regexp.QuoteMeta(pattern), regexp.QuoteMeta("*"), ".*") + "$"
|
|
|
366 matched, err := regexp.MatchString(regPat, s)
|
|
|
367 return err == nil && matched
|
|
2
|
368 } |