/
main.go
336 lines (294 loc) · 7.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"go.uber.org/atomic"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tg123/go-htpasswd"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/crypto/bcrypt"
"golang.org/x/sync/errgroup"
)
func main() {
logger, _ := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stdout"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}.Build()
log := zapr.NewLogger(logger)
defer logger.Sync() // nolint: errcheck
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
EnvVars: []string{"ADDR"},
Value: ":3311",
},
&cli.StringFlag{
Name: "metrics-addr",
EnvVars: []string{"METRICS_ADDR"},
Value: ":3000",
},
&cli.BoolFlag{
Name: "enable-pprof",
EnvVars: []string{"ENABLE_PPROF"},
Value: false,
},
&cli.StringFlag{
Name: "pprof-addr",
EnvVars: []string{"PPROF_ADDR"},
Value: ":6060",
},
&cli.StringFlag{
Name: "target-uri",
EnvVars: []string{"TARGET_URI"},
Required: true,
},
&cli.StringFlag{
Name: "auth",
Usage: "basic auth credentials, format: username:(bcrypt hash of password)",
EnvVars: []string{"AUTH"},
Required: true,
},
&cli.DurationFlag{
Name: "auth-timeout",
EnvVars: []string{"AUTH_TIMEOUT"},
Value: 24 * time.Hour,
},
&cli.Uint64Flag{
Name: "rate-limit-max",
EnvVars: []string{"RATE_LIMIT_MAX"},
Value: 5,
},
&cli.DurationFlag{
Name: "rate-limit-duration",
EnvVars: []string{"RATE_LIMIT_DURATION"},
Value: time.Minute,
},
},
Action: func(c *cli.Context) error {
eg, ctx := errgroup.WithContext(c.Context)
u, err := url.Parse(c.String("target-uri"))
if err != nil {
log.Error(err, "parse target")
return err
}
auth := c.String("auth")
user := auth[:strings.IndexByte(auth, ':')]
pass := auth[strings.IndexByte(auth, ':')+1:]
md5Pass, err := htpasswd.AcceptMd5(pass)
if err != nil {
log.Error(err, "md5 password")
}
if md5Pass == nil {
log.Info("md5 password is nil")
}
if c.Bool("enable-pprof") {
eg.Go(func() error {
return runHTTP(ctx, log, c.String("pprof-addr"), "pprof", http.DefaultServeMux)
})
}
eg.Go(func() error {
r := mux.NewRouter()
r.Path("/metrics").Handler(promhttp.Handler())
return runHTTP(ctx, log, c.String("metrics-addr"), "metrics", r)
})
var unhashed atomic.String
eg.Go(func() error {
proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.SetURL(u)
r.Out.Host = u.Host
ui := u.User
if ui != nil {
r.Out.Header.Set("authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(ui.String()))))
}
},
}
limiter := newRateLimiter(ctx, c.Uint64("rate-limit-max"), c.Duration("rate-limit-duration"))
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := userIP(r)
username, password, ok := r.BasicAuth()
if !ok {
if err := limiter.Wait(r.Context(), ip); err != nil {
log.Error(errors.New("rate limit exceeded"), "no auth provided", "ip", ip)
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
unh := unhashed.Load()
if unh != "" && unh == password && username == user {
proxy.ServeHTTP(w, r)
return
}
if err := limiter.Wait(r.Context(), ip); err != nil {
log.Error(errors.New("rate limit exceeded"), "incorrect auth", "ip", ip)
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}
err := bcrypt.CompareHashAndPassword([]byte(pass), []byte(password))
if err == nil && username == user {
unhashed.Store(password)
proxy.ServeHTTP(w, r)
return
}
if err != nil {
log.Error(err, "bycrypt incorrect auth", "ip", ip)
}
if md5Pass != nil && md5Pass.MatchesPassword(password) && username == user {
unhashed.Store(password)
proxy.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
return runHTTP(ctx, log, c.String("addr"), "server", handler)
})
eg.Go(func() error {
t := time.NewTicker(c.Duration("auth-timeout"))
defer t.Stop()
for {
select {
case <-t.C:
unhashed.Store("")
case <-ctx.Done():
return ctx.Err()
}
}
})
eg.Go(func() error {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigs:
log.Info("terminating signal received", "sig", sig)
return fmt.Errorf("signal %s received", sig.String())
case <-ctx.Done():
return ctx.Err()
}
})
return eg.Wait()
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err, "run")
os.Exit(1)
}
}
func runHTTP(ctx context.Context, log logr.Logger, addr, name string, handler http.Handler) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("could not listen for %s on address %w", name, err)
}
s := &http.Server{
Handler: handler,
}
go func() {
<-ctx.Done()
shutdownContext, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
log.Info(fmt.Sprintf("initiated a graceful shutdown of the %s server", name))
err := s.Shutdown(shutdownContext)
if errors.Is(err, context.DeadlineExceeded) {
log.Info(fmt.Sprintf("%s server terminated abruptly, necessitating a forced closure.", name))
s.Close() // nolint: errcheck
}
}()
log.Info(fmt.Sprintf("%s server is up and running", name), "addr", l.Addr().String())
return s.Serve(l)
}
type rateLimiterTimestamp struct {
count uint64
timestamp time.Time
}
type rateLimiter struct {
keys map[string]*rateLimiterTimestamp
max uint64
dur time.Duration
mux *sync.Mutex
}
func newRateLimiter(ctx context.Context, max uint64, d time.Duration) *rateLimiter {
r := rateLimiter{
keys: make(map[string]*rateLimiterTimestamp),
max: max,
mux: &sync.Mutex{},
dur: d,
}
go func() {
ticker := time.NewTicker(d / 2)
for ctx.Err() == nil {
<-ticker.C
r.clearOldKeys()
}
}()
return &r
}
func (i *rateLimiter) clearOldKeys() {
i.mux.Lock()
defer i.mux.Unlock()
for k, v := range i.keys {
if time.Since(v.timestamp) > i.dur {
delete(i.keys, k)
}
}
}
func (i *rateLimiter) Wait(ctx context.Context, key string) error {
i.mux.Lock()
defer i.mux.Unlock()
l, exists := i.keys[key]
if !exists {
i.keys[key] = &rateLimiterTimestamp{
count: 1,
timestamp: time.Now(),
}
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
for l.count >= i.max {
return fmt.Errorf("rate limit exceeded for %s", key)
}
l.count++
return nil
}
func userIP(r *http.Request) string {
a := r.Header.Get("X-Forwarded-For")
if a == "" {
a = r.RemoteAddr
}
return strings.Split(a, ",")[0]
}