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
|
package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
)
type multipleReverseProxyServer struct {
rules []rewriteRule
}
type rewriteRule struct {
fromHost string
fromPath string
toUrl *url.URL
proxy http.Handler
}
func (r *rewriteRule) matches(host, path string) bool {
if r.fromHost != "" {
if r.fromHost != host {
return false
}
}
if r.fromPath != "" {
if !strings.HasPrefix(path+"/", r.fromPath) {
return false
}
}
return true
}
func basicAuthHandler(handler http.Handler, realm, username, passwordHash string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
inputUsername, inputPassword, ok := r.BasicAuth()
if !ok || inputUsername != username || !VerifyPassword(inputPassword, passwordHash) {
w.Header().Set(
"WWW-Authenticate",
fmt.Sprintf("Basic realm=\"%s\"", realm),
)
http.Error(w, "401 unauthorized", http.StatusUnauthorized)
return
}
handler.ServeHTTP(w, r)
})
}
func newMultipleReverseProxyServer(ps []ProxyConfig) (*multipleReverseProxyServer, error) {
var rules []rewriteRule
for _, p := range ps {
targetUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", p.To.Host, p.To.Port))
if err != nil {
return nil, err
}
var proxy http.Handler = &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(targetUrl)
r.SetXForwarded()
},
ModifyResponse: func(r *http.Response) error {
if r.StatusCode < 300 || 400 <= r.StatusCode {
return nil
}
// If the response is redirect and has location header, rewrite it.
location := r.Header.Get("Location")
if location == "" {
return nil
}
locationUrl, err := url.Parse(location)
if err != nil {
return nil
}
if !locationUrl.IsAbs() {
return nil
}
if locationUrl.Hostname() != targetUrl.Hostname() {
return nil
}
locationUrl.Host = p.From.Host
if locationUrl.Scheme == "http" {
locationUrl.Scheme = "https"
}
r.Header.Set("Location", locationUrl.String())
return nil
},
}
if p.BasicAuth != nil {
credentialFileContent, err := os.ReadFile(p.BasicAuth.CredentialFile)
if err != nil {
return nil, err
}
usernameAndPasswordHash := strings.Split(strings.TrimSuffix(string(credentialFileContent), "\n"), ":")
if len(usernameAndPasswordHash) != 2 {
return nil, fmt.Errorf("invalid credential file format")
}
username := usernameAndPasswordHash[0]
passwordHash := usernameAndPasswordHash[1]
proxy = basicAuthHandler(
proxy,
p.BasicAuth.Realm,
username,
passwordHash,
)
}
rules = append(rules, rewriteRule{
fromHost: p.From.Host,
fromPath: p.From.Path,
toUrl: targetUrl,
proxy: proxy,
})
}
return &multipleReverseProxyServer{
rules: rules,
}, nil
}
func (s *multipleReverseProxyServer) tryServeHTTP(
w http.ResponseWriter,
r *http.Request,
) bool {
for _, rule := range s.rules {
if rule.matches(r.Host, r.URL.Path) {
rule.proxy.ServeHTTP(w, r)
return true
}
}
return false
}
type Server struct {
s http.Server
tlsEnabled bool
}
func NewServer(cfg *ServerConfig) (*Server, error) {
h := http.NewServeMux()
if cfg.ACMEChallenge != nil {
h.Handle(
"/.well-known/acme-challenge/",
http.FileServer(http.Dir(cfg.ACMEChallenge.Root)),
)
}
if cfg.RedirectToHTTPS {
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
target := r.URL
target.Scheme = "https"
target.Host = r.Host
http.Redirect(w, r, target.String(), http.StatusMovedPermanently)
})
} else {
reverseProxyServer, err := newMultipleReverseProxyServer(cfg.Proxies)
if err != nil {
return nil, err
}
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
found := reverseProxyServer.tryServeHTTP(w, r)
if !found {
http.NotFound(w, r)
}
})
}
var tlsConfig *tls.Config
if cfg.TLSCertFile != "" && cfg.TLSKeyFile != "" {
cert, err := tls.LoadX509KeyPair(cfg.TLSCertFile, cfg.TLSKeyFile)
if err != nil {
return nil, err
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
return &Server{
tlsEnabled: cfg.Protocol == "https",
s: http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Handler: h,
TLSConfig: tlsConfig,
},
}, nil
}
func (s *Server) Label() string {
return s.s.Addr
}
func (s *Server) Serve(listener net.Listener) error {
if s.tlsEnabled {
return s.s.ServeTLS(listener, "", "")
} else {
return s.s.Serve(listener)
}
}
func (s *Server) Shutdown(ctx context.Context) {
s.s.Shutdown(ctx)
}
func NewListener(cfg *ServerConfig) (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
}
|