forked from ContentSquare/chproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (269 loc) · 7.72 KB
/
main.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
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
290
291
292
293
294
295
296
297
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/Vertamedia/chproxy/config"
"github.com/Vertamedia/chproxy/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/acme/autocert"
)
var (
configFile = flag.String("config", "", "Proxy configuration filename")
version = flag.Bool("version", false, "Prints current version and exits")
)
var (
proxy = newReverseProxy()
// networks allow lists
allowedNetworksHTTP atomic.Value
allowedNetworksHTTPS atomic.Value
allowedNetworksMetrics atomic.Value
)
func main() {
flag.Parse()
if *version {
fmt.Printf("%s\n", versionString())
os.Exit(0)
}
log.Infof("%s", versionString())
log.Infof("Loading config: %s", *configFile)
cfg, err := loadConfig()
if err != nil {
log.Fatalf("error while loading config: %s", err)
}
if err = applyConfig(cfg); err != nil {
log.Fatalf("error while applying config: %s", err)
}
log.Infof("Loading config %q: successful", *configFile)
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP)
go func() {
for {
switch <-c {
case syscall.SIGHUP:
log.Infof("SIGHUP received. Going to reload config %s ...", *configFile)
if err := reloadConfig(); err != nil {
log.Errorf("error while reloading config: %s", err)
continue
}
log.Infof("Reloading config %s: successful", *configFile)
}
}
}()
server := cfg.Server
if len(server.HTTP.ListenAddr) == 0 && len(server.HTTPS.ListenAddr) == 0 {
panic("BUG: broken config validation - `listen_addr` is not configured")
}
if server.HTTP.ForceAutocertHandler {
autocertManager = newAutocertManager(server.HTTPS.Autocert)
}
if len(server.HTTPS.ListenAddr) != 0 {
go serveTLS(server.HTTPS)
}
if len(server.HTTP.ListenAddr) != 0 {
go serve(server.HTTP)
}
select {}
}
var autocertManager *autocert.Manager
func newAutocertManager(cfg config.Autocert) *autocert.Manager {
if len(cfg.CacheDir) > 0 {
if err := os.MkdirAll(cfg.CacheDir, 0700); err != nil {
log.Fatalf("error while creating folder %q: %s", cfg.CacheDir, err)
}
}
var hp autocert.HostPolicy
if len(cfg.AllowedHosts) != 0 {
allowedHosts := make(map[string]struct{}, len(cfg.AllowedHosts))
for _, v := range cfg.AllowedHosts {
allowedHosts[v] = struct{}{}
}
hp = func(_ context.Context, host string) error {
if _, ok := allowedHosts[host]; ok {
return nil
}
return fmt.Errorf("host %q doesn't match `host_policy` configuration", host)
}
}
return &autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(cfg.CacheDir),
HostPolicy: hp,
}
}
func newListener(listenAddr string) net.Listener {
ln, err := net.Listen("tcp4", listenAddr)
if err != nil {
log.Fatalf("cannot listen for %q: %s", listenAddr, err)
}
return ln
}
func serveTLS(cfg config.HTTPS) {
ln := newListener(cfg.ListenAddr)
h := http.HandlerFunc(serveHTTP)
tlsCfg := newTLSConfig(cfg)
tln := tls.NewListener(ln, tlsCfg)
log.Infof("Serving https on %q", cfg.ListenAddr)
if err := listenAndServe(tln, h, cfg.TimeoutCfg); err != nil {
log.Fatalf("TLS server error on %q: %s", cfg.ListenAddr, err)
}
}
func serve(cfg config.HTTP) {
var h http.Handler
ln := newListener(cfg.ListenAddr)
h = http.HandlerFunc(serveHTTP)
if cfg.ForceAutocertHandler {
if autocertManager == nil {
panic("BUG: autocertManager is not inited")
}
addr := ln.Addr().String()
parts := strings.Split(addr, ":")
if parts[len(parts)-1] != "80" {
log.Fatalf("`letsencrypt` specification requires http server to listen on :80 port to satisfy http-01 challenge. " +
"Otherwise, certificates will be impossible to generate")
}
h = autocertManager.HTTPHandler(h)
}
log.Infof("Serving http on %q", cfg.ListenAddr)
if err := listenAndServe(ln, h, cfg.TimeoutCfg); err != nil {
log.Fatalf("HTTP server error on %q: %s", cfg.ListenAddr, err)
}
}
func newTLSConfig(cfg config.HTTPS) *tls.Config {
tlsCfg := tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
}
if len(cfg.KeyFile) > 0 && len(cfg.CertFile) > 0 {
cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.KeyFile)
if err != nil {
log.Fatalf("cannot load cert for `https.cert_file`=%q, `https.key_file`=%q: %s",
cfg.CertFile, cfg.KeyFile, err)
}
tlsCfg.Certificates = []tls.Certificate{cert}
} else {
if autocertManager == nil {
panic("BUG: autocertManager is not inited")
}
tlsCfg.GetCertificate = autocertManager.GetCertificate
}
return &tlsCfg
}
func listenAndServe(ln net.Listener, h http.Handler, cfg config.TimeoutCfg) error {
s := &http.Server{
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
Handler: h,
ReadTimeout: time.Duration(cfg.ReadTimeout),
WriteTimeout: time.Duration(cfg.WriteTimeout),
IdleTimeout: time.Duration(cfg.IdleTimeout),
// Suppress error logging from the server, since chproxy
// must handle all these errors in the code.
ErrorLog: log.NilLogger,
}
return s.Serve(ln)
}
var promHandler = promhttp.Handler()
func serveHTTP(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodPost:
// Only GET and POST methods are supported.
case http.MethodOptions:
// This is required for CORS shit :)
rw.Header().Set("Allow", "GET,POST")
return
default:
err := fmt.Errorf("%q: unsupported method %q", r.RemoteAddr, r.Method)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusMethodNotAllowed)
return
}
switch r.URL.Path {
case "/favicon.ico":
case "/metrics":
an := allowedNetworksMetrics.Load().(*config.Networks)
if !an.Contains(r.RemoteAddr) {
err := fmt.Errorf("connections to /metrics are not allowed from %s", r.RemoteAddr)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusForbidden)
return
}
proxy.refreshCacheMetrics()
promHandler.ServeHTTP(rw, r)
case "/", "/query":
var err error
var an *config.Networks
if r.TLS != nil {
an = allowedNetworksHTTPS.Load().(*config.Networks)
err = fmt.Errorf("https connections are not allowed from %s", r.RemoteAddr)
} else {
an = allowedNetworksHTTP.Load().(*config.Networks)
err = fmt.Errorf("http connections are not allowed from %s", r.RemoteAddr)
}
if !an.Contains(r.RemoteAddr) {
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusForbidden)
return
}
proxy.ServeHTTP(rw, r)
default:
badRequest.Inc()
err := fmt.Errorf("%q: unsupported path: %q", r.RemoteAddr, r.URL.Path)
rw.Header().Set("Connection", "close")
respondWith(rw, err, http.StatusBadRequest)
}
}
func loadConfig() (*config.Config, error) {
if *configFile == "" {
log.Fatalf("Missing -config flag")
}
cfg, err := config.LoadFile(*configFile)
if err != nil {
configSuccess.Set(0)
return nil, fmt.Errorf("can't load config %q: %s", *configFile, err)
}
configSuccess.Set(1)
configSuccessTime.Set(float64(time.Now().Unix()))
return cfg, nil
}
func applyConfig(cfg *config.Config) error {
if err := proxy.applyConfig(cfg); err != nil {
return err
}
allowedNetworksHTTP.Store(&cfg.Server.HTTP.AllowedNetworks)
allowedNetworksHTTPS.Store(&cfg.Server.HTTPS.AllowedNetworks)
allowedNetworksMetrics.Store(&cfg.Server.Metrics.AllowedNetworks)
log.SetDebug(cfg.LogDebug)
log.Infof("Loaded config:\n%s", cfg)
return nil
}
func reloadConfig() error {
cfg, err := loadConfig()
if err != nil {
return err
}
return applyConfig(cfg)
}
var (
buildTag = "unknown"
buildRevision = "unknown"
buildTime = "unknown"
)
func versionString() string {
ver := buildTag
if len(ver) == 0 {
ver = "unknown"
}
return fmt.Sprintf("chproxy ver. %s, rev. %s, built at %s", ver, buildRevision, buildTime)
}