/
server.go
190 lines (163 loc) · 4.18 KB
/
server.go
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
// Copyright (C) 2022 Shanhu Tech Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package doorway
import (
"context"
"crypto/tls"
"log"
"net"
"net/http"
"net/http/httputil"
"strings"
"golang.org/x/crypto/acme/autocert"
"shanhu.io/aries"
"shanhu.io/misc/certutil"
"shanhu.io/misc/errcode"
)
// ServerConfig is the config for serving the reverse proxy
// server.
type ServerConfig struct {
HostMap map[string]string
AutoCertCache autocert.Cache
Home aries.Service
ManualCerts map[string]*tls.Certificate
IPWhitelist []string
}
type server struct {
home aries.Service
hostMap hostMap
proxy *httputil.ReverseProxy
autoCertCache autocert.Cache
manualCerts map[string]*tls.Certificate
ipWhitelist []*net.IPNet
}
func makeDefaultHome() aries.Service {
r := aries.NewRouter()
r.Index(aries.StringFunc("hi"))
r.File("health", aries.StringFunc("ok"))
return r
}
func newServer(config *ServerConfig) (*server, error) {
var ipWhitelist []*net.IPNet
for _, w := range config.IPWhitelist {
_, n, err := net.ParseCIDR(w)
if err != nil {
return nil, errcode.Annotatef(
err, "invalid whitelist entry: %q", w,
)
}
ipWhitelist = append(ipWhitelist, n)
}
s := &server{
hostMap: newMemHostMap(config.HostMap),
autoCertCache: config.AutoCertCache,
ipWhitelist: ipWhitelist,
manualCerts: config.ManualCerts,
}
if config.Home == nil {
s.home = makeDefaultHome()
} else {
s.home = config.Home
}
s.proxy = &httputil.ReverseProxy{
Director: s.director,
ModifyResponse: setStrictTransportSecurity,
}
return s, nil
}
func (s *server) checkIP(c *aries.C) error {
if len(s.ipWhitelist) == 0 {
return nil
}
ip := aries.RemoteIP(c)
if ip == nil {
return errcode.InvalidArgf("cannot determine IP address")
}
for _, n := range s.ipWhitelist {
if n.Contains(ip) {
return nil
}
}
return errcode.Unauthorizedf("not authorized")
}
func (s *server) Serve(c *aries.C) error {
host := strings.TrimSuffix(c.Req.Host, ".")
entry := s.hostMap.mapHost(host)
if entry == nil {
return aries.NotFound
}
if err := s.checkIP(c); err != nil {
return err
}
switch entry.typ {
default:
return aries.NotFound
case hostHome:
return s.serveHome(c)
case hostRedirect:
u := *c.Req.URL
u.Host = entry.host
c.Redirect(u.String())
return nil
case hostProxy:
s.proxy.ServeHTTP(c.Resp, c.Req)
return nil
}
}
func (s *server) serveHome(c *aries.C) error {
return s.home.Serve(c)
}
func (s *server) director(req *http.Request) {
// swap the scheme to http
req.Header.Set("X-Forwarded-Proto", "https")
host := strings.TrimSuffix(req.Host, ".")
mapped := hostMapToProxy(s.hostMap, host)
if mapped == "" {
if host == "" {
log.Println("empty host")
} else {
log.Printf("unexpected host: %q", host)
}
req.URL = sinkURL
return
}
forwardToHTTP(req, mapped)
}
func setStrictTransportSecurity(resp *http.Response) error {
resp.Header.Set(
"Strict-Transport-Security",
"max-age=15552000; includeSubDomains",
)
return nil
}
// hostPolicy determines which hosts are whitelisted for autocert.
func (s *server) hostPolicy(_ context.Context, host string) error {
if !hostMapHas(s.hostMap, host) {
return errcode.NotFoundf("%q not in whitelist", host)
}
return nil
}
func (s *server) autoTLSConfig() *tls.Config {
autoCert := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: s.hostPolicy,
Cache: s.autoCertCache,
}
tlsConfig := autoCert.TLSConfig()
tlsConfig.GetCertificate = certutil.WrapAutoCert(
tlsConfig.GetCertificate, s.manualCerts,
)
return tlsConfig
}