-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
201 lines (182 loc) · 4.81 KB
/
proxy.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
package proxy
import (
"crypto/tls"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"regexp"
"github.com/anduintransaction/oauth-proxy/utils"
"gottb.io/goru/config"
"gottb.io/goru/log"
)
type whilelist struct {
method string
path *regexp.Regexp
}
type Proxy struct {
Provider string `config:"provider"`
Scheme string `config:"scheme"`
RedirectURI string `config:"redirect_uri"`
RequestHost string `config:"request_host"`
EndPoint string `config:"end_point"`
PreserveHost bool `config:"preserve_host"`
ClientID string `config:"client_id"`
ClientSecret string `config:"client_secret"`
CallbackURI string `config:"callback_uri"`
Organizations []string `config:"organizations"`
Teams []string `config:"teams"`
Whitelists []string `config:"whitelists"`
organizations utils.StringSet
teams utils.StringSet
target *url.URL
whitelists []*whilelist
reverseProxy *httputil.ReverseProxy
}
func (p *Proxy) HasOrg(org string) bool {
return p.organizations.Has(org)
}
func (p *Proxy) HasTeam(team string) bool {
return p.teams.Has(team)
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.reverseProxy.ServeHTTP(w, r)
}
func (p *Proxy) IsWhiteList(method, path string) bool {
for _, w := range p.whitelists {
if w.method != "ANY" && w.method != method {
continue
}
path = strings.TrimRight(path, "/")
if path == "" {
path = "/"
}
matched := w.path.MatchString(path)
if matched {
return true
}
}
return false
}
func (p *Proxy) createReverseProxy() {
transport := http.DefaultTransport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
p.reverseProxy = &httputil.ReverseProxy{
Director: p.transformRequest,
Transport: transport,
}
}
func (p *Proxy) transformRequest(req *http.Request) {
req.URL.Scheme = p.target.Scheme
req.URL.Host = p.target.Host
req.URL.Path = p.singleJoiningSlash(p.target.Path, req.URL.Path)
targetQuery := p.target.RawQuery
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
if !p.PreserveHost {
req.Header.Set("Host", p.target.Host)
req.Host = p.target.Host
}
}
func (p *Proxy) singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
var Config struct {
Provider string `config:"provider"`
ClientID string `config:"client_id"`
ClientSecret string `config:"client_secret"`
CallbackURI string `config:"callback_uri"`
StateTimeout int `config:"state_timeout"`
CookieTimeout int `config:"cookie_timeout"`
CookieName string `config:"cookie_name"`
CheckVersion bool `config:"check_version"`
Version int64
}
var proxies []*Proxy
var proxyMap map[string]*Proxy
func Start(config *config.Config) error {
oauthConfig, err := config.Get("oauth")
if err != nil {
return err
}
err = oauthConfig.Unmarshal(&Config)
if err != nil {
return err
}
proxyConfig, err := config.Get("proxy")
if err != nil {
return err
}
err = proxyConfig.Unmarshal(&proxies)
if err != nil {
return err
}
proxyMap = make(map[string]*Proxy)
for _, proxy := range proxies {
if proxy.Provider == "" {
proxy.Provider = Config.Provider
}
if proxy.ClientID == "" {
proxy.ClientID = Config.ClientID
}
if proxy.ClientSecret == "" {
proxy.ClientSecret = Config.ClientSecret
}
if proxy.CallbackURI == "" {
proxy.CallbackURI = Config.CallbackURI
}
proxy.organizations = utils.NewStringSet(proxy.Organizations)
proxy.teams = utils.NewStringSet(proxy.Teams)
proxy.target, err = url.Parse(proxy.EndPoint)
if err != nil {
return err
}
proxy.whitelists = []*whilelist{}
for _, wl := range proxy.Whitelists {
w := &whilelist{}
pieces := strings.SplitN(wl, ":", 2)
if len(pieces) == 1 {
w.method = "ANY"
w.path, err = regexp.Compile("^" + pieces[0] + "$")
} else {
w.method = strings.ToUpper(pieces[0])
w.path, err = regexp.Compile("^" + pieces[1] + "$")
}
if err != nil {
return err
}
proxy.whitelists = append(proxy.whitelists, w)
}
proxy.createReverseProxy()
proxyMap[proxy.RequestHost] = proxy
log.Debug(proxy)
}
rand.Seed(time.Now().UnixNano())
Config.Version = rand.Int63()
defaultStateMap = newStateMap(Config.StateTimeout)
return nil
}
func Stop(config *config.Config) error {
defaultStateMap.quit()
return nil
}
func GetProxy(requestHost string) *Proxy {
return proxyMap[requestHost]
}