-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
127 lines (108 loc) · 3.28 KB
/
router.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
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type proxy struct {
internalPort int
externalPort int
scheme, host string
internal *http.Server
external *http.Server
client *http.Client
}
func (app *proxy) init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
zerolog.SetGlobalLevel(zerolog.TraceLevel)
external := mux.NewRouter()
external.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool {
// external - exclusive (ban list)
match := forbidden.Match(r.URL.Path)
log.Debug().Str("path", r.URL.Path).Bool("match", match).Msg("external")
return !match
}).HandlerFunc(app.forwardHandler)
internal := mux.NewRouter()
internal.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool {
// internal - inclusive
match := allowed.Match(r.URL.Path)
log.Debug().Str("path", r.URL.Path).Bool("match", match).Msg("internal")
return match
}).HandlerFunc(app.forwardHandler)
app.external = newServer(app.externalPort, external)
app.internal = newServer(app.internalPort, internal)
app.client = newClient()
}
func (app *proxy) run() {
go func() {
log.Info().Int("port", app.internalPort).Msg("starting internal")
if err := app.internal.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal().Err(err)
}
}()
log.Info().Int("port", app.externalPort).Msg("starting external")
if err := app.external.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal().Err(err)
}
}
func (app *proxy) shutdown() {
log.Warn().Msg("shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.internal.Shutdown(ctx); err != nil && err != context.Canceled {
panic(err)
}
if err := app.external.Shutdown(ctx); err != nil && err != context.Canceled {
panic(err)
}
app.client.CloseIdleConnections()
log.Warn().Msg("done.")
}
func (app *proxy) forwardHandler(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error().Err(err).Msg("read request body")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req.Body = ioutil.NopCloser(bytes.NewReader(body))
url := fmt.Sprintf("%s://%s%s", app.scheme, app.host, req.RequestURI)
proxyReq, err := http.NewRequestWithContext(req.Context(), req.Method, url, bytes.NewReader(body))
if err != nil {
log.Error().Err(err).Msg("new request")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
proxyReq.Header = make(http.Header)
for h, val := range req.Header {
proxyReq.Header[h] = val
}
resp, err := app.client.Do(proxyReq)
if err != nil {
log.Error().Err(err).Msg("execute request")
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
delete(req.Header, "Content-Length")
for k, hvv := range req.Header {
for _, hv := range hvv {
w.Header().Add(k, hv)
}
}
w.WriteHeader(resp.StatusCode)
if _, err = io.Copy(w, resp.Body); err != nil {
log.Error().Err(err).Msg("write response body")
}
}