-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
160 lines (137 loc) · 3.33 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
package main
import (
"crypto/tls"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
// hop-by-hop headers
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
var hopHeaders = []string{
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te",
"Trailers",
"Transfer-Encoding",
"Upgrade",
}
type RewriteTransport struct {
Transport http.RoundTripper
}
func (t *RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.Transport.RoundTrip(req)
}
func getProxyClient(proxy string, ignoreSsl bool) *http.Client {
proxyUrl, _ := url.Parse(proxy)
myClient := &http.Client{
Transport: &RewriteTransport{
&http.Transport{
Proxy: http.ProxyURL(proxyUrl),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: ignoreSsl,
},
},
},
}
return myClient
}
func containsHeader(s string, list []string) bool {
for _, v := range list {
if v == s {
return true
}
}
return false
}
func newProxyHandler(httpClient *http.Client, targetHost string, headersMap map[string]string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// get path from request and append to target url
target, _ := url.Parse(targetHost + r.URL.Path)
if r.URL.RawQuery != "" {
target.RawQuery = r.URL.RawQuery
}
// create new request
req, err := http.NewRequest(r.Method, target.String(), r.Body)
if err != nil {
log.Println(err)
return
}
// add default headers
for k, v := range headersMap {
req.Header.Set(k, v)
}
// copy headers from original request to new request
for k, v := range r.Header {
if !containsHeader(k, hopHeaders) {
req.Header[k] = v
}
}
// send request to target url
resp, err := httpClient.Do(req)
if err != nil {
log.Println(err)
return
}
// copy headers from response to original response
for k, v := range resp.Header {
w.Header()[k] = v
}
// copy status code from response to original response
w.WriteHeader(resp.StatusCode)
// copy body from response to original response using io.Copy
_, err = io.Copy(w, resp.Body)
if err != nil {
log.Println(err)
return
}
// close response body
err = resp.Body.Close()
if err != nil {
log.Println(err)
return
}
})
}
func main() {
// get server url from env
serverUrl := os.Getenv("SERVER_URL")
if serverUrl == "" {
serverUrl = "0.0.0.0:8080"
}
// get socks5 proxy from env
proxyUrl := os.Getenv("PROXY_DSN")
if proxyUrl == "" {
log.Fatal("PROXY_DSN is not set")
}
targetHost := os.Getenv("TARGET_HOST")
if targetHost == "" {
log.Fatal("TARGET_HOST is not set")
}
ignoreSsl := os.Getenv("IGNORE_SSL")
if ignoreSsl == "" {
ignoreSsl = "false"
}
ignoreSslBool, err := strconv.ParseBool(ignoreSsl)
if err != nil {
log.Fatal(err)
}
// get default headers from env
// example: "Content-Type:application/json,Authorization...."
headersMap := make(map[string]string)
headers := os.Getenv("DEFAULT_HEADERS")
if headers != "" {
for _, header := range strings.Split(headers, ",") {
headerParts := strings.Split(header, ":")
headersMap[headerParts[0]] = headerParts[1]
}
}
httpClient := getProxyClient(proxyUrl, ignoreSslBool)
//proxy all outgoing http requests to socks5 proxy
log.Fatal(http.ListenAndServe(serverUrl, newProxyHandler(httpClient, targetHost, headersMap)))
}