~emersion/kimchi

kimchi/directives.go -rw-r--r-- 6.6 KiB
1ec4ec62Simon Ser man: update example with kimchi directives 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main

import (
	"crypto/subtle"
	"fmt"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"git.sr.ht/~emersion/go-scfg"
)

func loadConfig(srv *Server, filename string) error {
	cfg, err := scfg.Load(filename)
	if err != nil {
		return err
	}

	cfg, err = resolveImports(cfg, filename)
	if err != nil {
		return err
	}

	return parseConfig(srv, cfg)
}

func parseConfig(srv *Server, cfg scfg.Block) error {
	for _, dir := range cfg {
		switch dir.Name {
		case "site":
			if err := parseSite(srv, dir); err != nil {
				return err
			}
		default:
			return fmt.Errorf("unknown directive %q", dir.Name)
		}
	}
	return nil
}

func parseSite(srv *Server, dir *scfg.Directive) error {
	for _, site := range dir.Params {
		uriStr := site
		if !strings.Contains(uriStr, "//") {
			uriStr = "//" + uriStr
		}

		u, err := url.Parse(uriStr)
		if err != nil {
			return fmt.Errorf("site %q: %v", site, err)
		}

		var ln *Listener
		var host, port string
		switch u.Scheme {
		case "", "http", "http+insecure":
			if host, port, err = net.SplitHostPort(u.Host); err != nil {
				host = u.Host
				port = "http"
			}
			ln = srv.AddListener("tcp", ":"+port)
			if u.Scheme == "http+insecure" {
				ln.Insecure = true
			}
		default:
			return fmt.Errorf("site %q: unknown URI scheme %q", site, u.Scheme)
		}

		path := u.Path
		if path == "" {
			path = "/"
		}
		if !strings.HasPrefix(path, "/") {
			return fmt.Errorf("site %q: invalid path %q", site, path)
		}

		pattern := host + path

		// First process backend directives
		var backend http.Handler
		for _, child := range dir.Children {
			f, ok := backends[child.Name]
			if !ok {
				continue
			}

			if backend != nil {
				return fmt.Errorf("site %q: multiple HTTP backend directives provided", site)
			}

			backend, err = f(child)
			if err != nil {
				return fmt.Errorf("site %q: %v", site, err)
			}
		}
		if backend == nil {
			return fmt.Errorf("site %q: missing backend directive", site)
		}

		// Then process middleware directives
		handler := backend
		for _, child := range dir.Children {
			if _, ok := backends[child.Name]; ok {
				// Backend directive already processed above
				continue
			}

			handler, err = parseMiddleware(child, handler)
			if err != nil {
				return fmt.Errorf("site %q: directive %q: %v", site, child.Name, err)
			}
		}

		ln.Mux.Handle(pattern, http.StripPrefix(path, handler))
	}
	return nil
}

type parseBackendFunc func(dir *scfg.Directive) (http.Handler, error)

var backends = map[string]parseBackendFunc{
	"file_server": func(dir *scfg.Directive) (http.Handler, error) {
		var dirname string
		if err := dir.ParseParams(&dirname); err != nil {
			return nil, err
		}
		var fs http.FileSystem = http.Dir(dirname)
		if dir.Children.Get("browse") == nil {
			fs = noBrowseFileSystem{fs}
		}
		return http.FileServer(fs), nil
	},
	"reverse_proxy": func(dir *scfg.Directive) (http.Handler, error) {
		var urlStr string
		if err := dir.ParseParams(&urlStr); err != nil {
			return nil, err
		}
		target, err := url.Parse(urlStr)
		if err != nil {
			return nil, err
		}
		proxy := httputil.NewSingleHostReverseProxy(target)
		director := proxy.Director
		proxy.Director = func(req *http.Request) {
			forwarded := fmt.Sprintf("for=%q;host=%q;proto=%q", req.RemoteAddr, req.Host, req.URL.Scheme)
			director(req)
			req.Header.Set("Forwarded", forwarded)
			req.Header.Set("X-Forwarded-For", req.RemoteAddr)
			req.Header.Set("X-Forwarded-Host", req.Host)
			req.Header.Set("X-Forwarded-Proto", req.URL.Scheme)
		}
		return proxy, nil
	},
	"redirect": func(dir *scfg.Directive) (http.Handler, error) {
		var to string
		if err := dir.ParseParams(&to); err != nil {
			return nil, err
		}
		return http.RedirectHandler(to, http.StatusFound), nil
	},
}

func parseMiddleware(dir *scfg.Directive, next http.Handler) (http.Handler, error) {
	switch dir.Name {
	case "header":
		// TODO: allow adding and removing fields
		setFields := make(map[string]string)
		if len(dir.Params) > 0 {
			if len(dir.Params) != 2 {
				return nil, fmt.Errorf("expected exactly two parameters")
			}
			setFields[dir.Params[0]] = dir.Params[1]
		} else {
			for _, child := range dir.Children {
				if len(child.Params) != 1 {
					return nil, fmt.Errorf("expected exactly one parameter for child directive")
				}
				if _, ok := setFields[child.Name]; ok {
					return nil, fmt.Errorf("duplicate child directive %q", child.Name)
				}
				setFields[child.Name] = child.Params[0]
			}
		}

		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			for k, v := range setFields {
				w.Header().Set(k, v)
			}
			next.ServeHTTP(w, r)
		}), nil
	case "basic_auth":
		var username, password string
		if err := dir.ParseParams(&username, &password); err != nil {
			return nil, err
		}

		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			u, p, ok := r.BasicAuth()
			usernameOK := subtle.ConstantTimeCompare([]byte(username), []byte(u))
			passwordOK := subtle.ConstantTimeCompare([]byte(password), []byte(p))
			if !ok || (usernameOK&passwordOK) != 1 {
				w.Header().Set("WWW-Authenticate", "Basic")
				http.Error(w, "Unauthorized", http.StatusUnauthorized)
				return
			}

			next.ServeHTTP(w, r)
		}), nil
	default:
		return nil, fmt.Errorf("unknown directive")
	}
}

func resolveImports(input scfg.Block, filename string) (scfg.Block, error) {
	dirname := filepath.Dir(filename)

	output := make(scfg.Block, 0, len(input))
	for _, dir := range input {
		switch dir.Name {
		case "import":
			var pattern string
			if err := dir.ParseParams(&pattern); err != nil {
				return nil, err
			}
			if !filepath.IsAbs(pattern) {
				pattern = filepath.Join(dirname, pattern)
			}

			matches, err := filepath.Glob(pattern)
			if err != nil {
				return nil, fmt.Errorf("failed to import %q: %v", pattern, err)
			}

			for _, filename := range matches {
				block, err := scfg.Load(filename)
				if err != nil {
					return nil, err
				}

				block, err = resolveImports(block, filename)
				if err != nil {
					return nil, err
				}

				output = append(output, block...)
			}
		default:
			if len(dir.Children) > 0 {
				children, err := resolveImports(dir.Children, filename)
				if err != nil {
					return nil, err
				}

				dirCopy := *dir
				dirCopy.Children = children
				dir = &dirCopy
			}

			output = append(output, dir)
			continue
		}
	}

	return output, nil
}

type noBrowseFileSystem struct {
	http.FileSystem
}

func (fs noBrowseFileSystem) Open(name string) (http.File, error) {
	f, err := fs.FileSystem.Open(name)
	if err != nil {
		return nil, err
	}
	return noBrowseFile{f}, nil
}

type noBrowseFile struct {
	http.File
}

func (f noBrowseFile) Readdir(count int) ([]os.FileInfo, error) {
	return nil, os.ErrPermission
}