-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
211 lines (177 loc) · 4.69 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/hagen1778/chproxy/config"
"github.com/hagen1778/chproxy/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/acme/autocert"
)
var configFile = flag.String("config", "testdata/http.conf.yml", "Proxy configuration filename")
var (
proxy = newReverseProxy()
allowedNetworks atomic.Value
)
func main() {
flag.Parse()
log.Infof("Loading config: %s", *configFile)
cfg, err := reloadConfig()
if err != nil {
log.Fatalf("error while loading config: %s", err)
}
log.Infof("Loading config %s: 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)
}
}
}()
if cfg.IsTLS {
if err := serveTLS(cfg.ListenAddr, &cfg.TLSConfig); err != nil {
log.Fatalf("TLS server error on %q: %s", cfg.ListenAddr, err)
}
return
}
if err := serve(cfg.ListenAddr); err != nil {
log.Fatalf("Server error on %q: %s", cfg.ListenAddr, err)
}
}
func serveTLS(addr string, tlsConfig *config.TLSConfig) error {
ln := newTLSListener(addr, tlsConfig)
log.Infof("Serving https on %q", addr)
return listenAndServe(ln)
}
func serve(addr string) error {
ln := newListener(addr)
log.Infof("Serving http on %q", addr)
return listenAndServe(ln)
}
func newListener(laddr string) net.Listener {
ln, err := net.Listen("tcp4", laddr)
if err != nil {
log.Fatalf("cannot listen for %q: %s", laddr, err)
}
return &netListener{
ln,
}
}
func newTLSListener(laddr string, cfg *config.TLSConfig) net.Listener {
tlsConfig := 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 `tls_config.cert_file`=%q, `tls_config.key_file`=%q: %s",
cfg.CertFile, cfg.KeyFile, err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
} else {
if len(cfg.CertCacheDir) > 0 {
if err := os.MkdirAll(cfg.CertCacheDir, 0700); err != nil {
log.Fatalf("error while creating folder %q: %s", cfg.CertCacheDir, err)
}
}
var hp autocert.HostPolicy
if len(cfg.HostPolicy) != 0 {
allowedHosts := make(map[string]struct{}, len(cfg.HostPolicy))
for _, v := range cfg.HostPolicy {
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)
}
}
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(cfg.CertCacheDir),
HostPolicy: hp,
}
tlsConfig.GetCertificate = m.GetCertificate
}
ln := newListener(laddr)
return tls.NewListener(ln, &tlsConfig)
}
func listenAndServe(ln net.Listener) error {
s := &http.Server{
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
Handler: http.HandlerFunc(serveHTTP),
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Minute * 10,
ErrorLog: log.ErrorLogger,
}
return s.Serve(ln)
}
var promHandler = promhttp.Handler()
func serveHTTP(rw http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/favicon.ico":
case "/metrics":
promHandler.ServeHTTP(rw, r)
case "/":
goodRequest.Inc()
proxy.ServeHTTP(rw, r)
default:
badRequest.Inc()
log.Debugf("Unsupported path: %s", r.URL.Path)
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "Unsupported path: %s", r.URL.Path)
}
}
type netListener struct {
net.Listener
}
func (ln *netListener) Accept() (net.Conn, error) {
for {
conn, err := ln.Listener.Accept()
if err != nil {
return nil, err
}
remoteAddr := conn.RemoteAddr().String()
an := allowedNetworks.Load().(*config.Networks)
if !an.Contains(remoteAddr) {
log.Errorf("connections are not allowed from %s", remoteAddr)
conn.Close()
continue
}
return conn, nil
}
}
func reloadConfig() (*config.Server, error) {
cfg, err := config.LoadFile(*configFile)
if err != nil {
return nil, fmt.Errorf("can't load config %q: %s", *configFile, err)
}
if err := proxy.ApplyConfig(cfg); err != nil {
return nil, err
}
allowedNetworks.Store(&cfg.Networks)
log.SetDebug(cfg.LogDebug)
return &cfg.Server, nil
}