-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
167 lines (144 loc) · 3.69 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
package main
import (
"bufio"
"fmt"
"github.com/sdvcrx/cuttlefish/config"
"io"
"net"
"net/http"
"strings"
"time"
)
var (
INTERNAL_URL_PREFIX = "/-/"
FILTER_HEADERS = []string{
"Prxoy-Authenticate",
"Proxy-Connection",
"Transfer-Encoding",
"Upgrade",
"X-Forwarded-For",
}
)
func transfer(dst io.WriteCloser, src io.ReadCloser) {
defer dst.Close()
defer src.Close()
io.Copy(dst, src)
}
func copyHeader(dst, src http.Header) {
for key, vv := range src {
for _, val := range vv {
dst.Add(key, val)
}
}
}
func filterProxyHeaders(header http.Header) {
for _, key := range FILTER_HEADERS {
header.Del(key)
}
}
func connectTunnelHandler(w http.ResponseWriter, r *http.Request) {
parentProxy, err := SelectProxy(r)
var proxy_conn net.Conn
if parentProxy != nil {
proxy_conn, err = net.DialTimeout("tcp4", parentProxy.Host, 10*time.Second)
// TODO remove current unavailable proxy from proxies pool
} else {
proxy_conn, err = net.DialTimeout("tcp4", r.Host, 10*time.Second)
}
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
// if parentProxy is not null, send CONNECT request to parent proxy server
if parentProxy != nil {
connectReq := &http.Request{
Method: http.MethodConnect,
URL: r.URL,
Host: r.Host,
Header: make(http.Header),
}
connectReq.Write(proxy_conn)
br := bufio.NewReader(proxy_conn)
resp, err := http.ReadResponse(br, connectReq)
if err != nil {
proxy_conn.Close()
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
// expect 200 Connection established
if resp.StatusCode != http.StatusOK {
proxy_conn.Close()
// TODO remove current unavailable proxy from proxies pool
http.Error(w, "Read Proxy Response error", http.StatusServiceUnavailable)
return
}
}
// Send 200 Connection established
w.WriteHeader(http.StatusOK)
// hijack connnection
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Proxy hijack error", http.StatusServiceUnavailable)
return
}
client_conn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
go transfer(proxy_conn, client_conn)
go transfer(client_conn, proxy_conn)
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
filterProxyHeaders(r.Header)
proxy_resp, err := DefaultProxyTransport.RoundTrip(r)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer proxy_resp.Body.Close()
copyHeader(w.Header(), proxy_resp.Header)
w.WriteHeader(proxy_resp.StatusCode)
io.Copy(w, proxy_resp.Body)
}
// Handle internal proxy server commands
func InternalHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(400)
return
}
switch command := strings.Trim(r.URL.Path, INTERNAL_URL_PREFIX); command {
case "reload":
config.Reload()
default:
logger.Warn().Msgf("[internal] No command available: %s", command)
}
}
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, INTERNAL_URL_PREFIX) {
InternalHandler(w, r)
return
}
if r.URL.Path == "/-/reload" {
config.Reload()
return
}
if r.Method == http.MethodConnect {
logger.Info().Msgf("%s %s", r.Method, r.Host)
connectTunnelHandler(w, r)
} else {
logger.Info().Msgf("%s %s", r.Method, r.URL)
httpHandler(w, r)
}
}
func NewProxyServer() http.Server {
appConfig := config.GetInstance()
authUser := appConfig.AuthUser
authPassword := appConfig.AuthPassword
addr := fmt.Sprintf("%s:%d", appConfig.Host, appConfig.Port)
proxyHandler := ProxyAuthenticateHandler(ProxyHandler, authUser, authPassword)
return http.Server{
Addr: addr,
Handler: proxyHandler,
}
}