/
mlbamproxy.go
279 lines (224 loc) · 5.75 KB
/
mlbamproxy.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const version = "1.1.1"
type baseHandle struct{}
var (
_port int
_destination string
_sources []string
_debug bool
_version bool
)
func contains(domains []string, url string) bool {
for _, domain := range domains {
if strings.HasSuffix(url, domain) {
return true
}
}
return false
}
func logError(err error) bool {
if err != nil {
log.Printf("[MLBAMProxy] Error: %v", err)
return true
}
return false
}
func printf(format string, args ...interface{}) {
log.Printf("[MLBAMProxy] "+format, args...)
}
func logf(format string, args ...interface{}) {
if _debug {
log.Printf("[MLBAMProxy] Debug: "+format, args...)
}
}
func canRedirect() bool {
return len(_sources) > 0 && _destination != ""
}
func getScheme(r *http.Request) string {
if r.Method == http.MethodConnect {
return "https://"
}
return "http://"
}
func getPort(r *http.Request) string {
if r.URL.Port() != "" {
return r.URL.Port()
}
if r.Method == http.MethodConnect {
return "433"
}
return "80"
}
func getURL(r *http.Request, isDestination bool) (*url.URL, error) {
hostname := r.URL.Hostname()
port := getPort(r)
if isDestination {
hostname = _destination
}
raw := fmt.Sprintf("%v%v:%v", getScheme(r), hostname, port)
if port == "" {
raw = fmt.Sprintf("%v%v", getScheme(r), hostname)
}
if r.URL.Path != "" {
raw = fmt.Sprintf("%v%v", raw, r.URL.Path)
}
newURL, err := url.Parse(raw)
if logError(err) {
return nil, err
}
return newURL, nil
}
func initParameters() {
_sources = []string{}
flag.BoolVar(&_debug, "debug", false, "Debug mode")
flag.BoolVar(&_version, "v", false, "Version")
flag.IntVar(&_port, "p", 17070, "Port used by the local proxy")
flag.StringVar(&_destination, "d", "", "Destination domain to forward source domains requests to.")
sources := flag.String("s", "", "Source domains to redirect requests from, separated by commas. (e.g.: --s google.com,facebook.com)")
flag.Parse()
if _version {
fmt.Printf("version %v", version)
os.Exit(1)
}
for _, hostname := range strings.Split(*sources, ",") {
if hostname != "" {
_sources = append(_sources, hostname)
}
}
if !canRedirect() {
printf("Proxy will act as a default proxy and won't redirect a domain to another, no sources and/or destination were specified")
}
}
func copyRequest(u *url.URL, r *http.Request) (*http.Request, error) {
target, _ := getURL(r, false)
target.Scheme = u.Scheme
target.Host = u.Host
req, err := http.NewRequest(r.Method, target.String(), r.Body)
if logError(err) {
return nil, err
}
for key := range r.Header {
req.Header.Set(key, r.Header.Get(key))
}
req.Header.Set("Host", u.Host)
if r.Referer() != "" {
req.Header.Set("Referer", strings.Replace(r.Referer(), r.Host, u.Host, -1))
}
req.Header.Set("X-Forwarded-Proto", "http")
if r.TLS != nil {
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set(http.CanonicalHeaderKey("X-Forwarded-Proto"), "https")
req.Header.Set(http.CanonicalHeaderKey("X-Forwarded-Port"), fmt.Sprintf("%v", _port))
}
req.Header.Del("Accept-Encoding")
return req, nil
}
func setupResponse(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, accessToken, Authorization, Accept, Range")
}
func dialTLS(network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if logError(err) {
return nil, err
}
host, _, err := net.SplitHostPort(addr)
if logError(err) {
conn.Close()
return nil, err
}
cfg := &tls.Config{
ServerName: host,
InsecureSkipVerify: true,
NextProtos: []string{"h2", "http/1.1"},
}
tlsConn := tls.Client(conn, cfg)
err = tlsConn.Handshake()
if logError(err) {
conn.Close()
tlsConn.Close()
return nil, err
}
cs := tlsConn.ConnectionState()
cert := cs.PeerCertificates[0]
cert.VerifyHostname(host)
logf("%v", cert.Subject)
return tlsConn, nil
}
func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
proxyURL, err := getURL(r, false)
if logError(err) {
return
}
logf("Request URL: %v", proxyURL)
if canRedirect() && contains(_sources, r.URL.Hostname()) {
url, err := getURL(r, true)
if logError(err) {
return
}
proxyURL = url
logf("Request URL redirected to: %v", proxyURL)
}
proxy := NewReverseProxy(proxyURL)
proxy.Timeout = 60 * time.Minute
r, _ = copyRequest(proxyURL, r)
setupResponse(&w)
proxy.Transport = &http.Transport{
DialTLS: dialTLS,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"h2", "http/1.1"},
},
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
Proxy: http.ProxyURL(proxyURL),
Dial: (&net.Dialer{
Timeout: 3 * time.Minute,
KeepAlive: 30 * time.Second,
FallbackDelay: 300 * time.Millisecond,
Deadline: time.Now().Add(60 * time.Second),
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
proxy.Director = func(r *http.Request) {
r.URL.Host = proxyURL.Host
r.Host = proxyURL.Host
}
proxy.ServeHTTP(w, r)
}
func runProxyServer() {
h := &baseHandle{}
http.Handle("/", h)
server := &http.Server{
Addr: fmt.Sprintf(":%d", _port),
Handler: h,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
ReadHeaderTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
printf("Proxy server listening on port: %d", _port)
err := server.ListenAndServe()
if logError(err) {
return
}
}
func main() {
log.SetOutput(os.Stdout)
initParameters()
runProxyServer()
}