-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpproxy.go
More file actions
218 lines (199 loc) · 5.52 KB
/
httpproxy.go
File metadata and controls
218 lines (199 loc) · 5.52 KB
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
package fcbreak
import (
"encoding/base64"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
type HTTPProxy struct {
s *HTTPService
tp http.Transport
dialer proxy.Dialer
}
func NewHTTPProxy(s *HTTPService) *HTTPProxy {
p := &HTTPProxy{
s: s,
tp: http.Transport{},
dialer: proxy.Direct,
}
// proxy
if s.GetCfg().ChainProxy != "" {
httpProxyURI, err := url.Parse(s.GetCfg().ChainProxy)
if err != nil {
log.Fatalf("%v", err)
}
p.dialer, err = proxy.FromURL(httpProxyURI, proxy.Direct)
if err != nil {
log.Fatalf("%v", err)
}
p.tp.Dial = p.dialer.Dial
}
return p
}
func (hp *HTTPProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Info
if req.Method != http.MethodConnect && !req.URL.IsAbs() {
if ok := hp.Auth(req, "Authorization"); !ok {
rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted API", charset="UTF-8"`)
http.Error(rw, "Unauthorized", http.StatusUnauthorized)
return
}
hp.InfoHandler(rw, req)
return
}
// Proxy
if ok := hp.Auth(req, "Proxy-Authorization"); !ok {
rw.Header().Set("Proxy-Authenticate", "Basic")
rw.WriteHeader(http.StatusProxyAuthRequired)
return
}
if req.Method == http.MethodConnect {
hp.ConnectHandler(rw, req)
} else {
hp.HTTPHandler(rw, req)
}
}
func (hp *HTTPProxy) InfoHandler(rw http.ResponseWriter, req *http.Request) {
info := hp.s.GetInfo()
cfg := hp.s.GetCfg()
clashTemp := `proxies:
- {name: %[1]s, type: http, server: %[3]s, port: %[4]s, tls: %[7]t, sni: %[2]s, username: %[8]s, password: %[9]s, skip-cert-verify: %[10]t}
- {name: %[1]s (via Server), type: http, server: %[5]s, port: %[6]s, tls: %[7]t, sni: %[2]s, username: %[8]s, password: %[9]s, skip-cert-verify: %[10]t}`
quanxTemp := `http=%[3]s:%[4]s, username=%[8]s, password=%[9]s, over-tls=%[7]t, tls-host=%[2]s, tls-verification=%[10]t, fast-open=false, udp-relay=false, tag=%[1]s
http=%[5]s:%[6]s, username=%[8]s, password=%[9]s, over-tls=%[7]t, tls-host=%[2]s, tls-verification=%[10]t, fast-open=false, udp-relay=false, tag=%[1]s (via Server)`
name := info.Name
scheme := info.Scheme
eip, eportStr, err := net.SplitHostPort(info.ExposedAddr)
if err != nil {
http.Error(rw, err.Error(), 500)
return
}
if !req.URL.IsAbs() {
req.URL.Host = req.Host
}
tls := scheme == "https"
rip := req.URL.Hostname()
rportStr := req.URL.Port()
if rportStr == "" {
if tls {
rportStr = "443"
} else {
rportStr = "80"
}
}
user := cfg.Username
pass := cfg.Password
tlsInsecure := cfg.ProxyInsecure
switch req.URL.Path {
case "/clash":
fmt.Fprintf(rw, clashTemp, name, rip, eip, eportStr, rip, rportStr, tls, user, pass, tlsInsecure)
case "/quan":
fallthrough
case "/quanx":
fmt.Fprintf(rw, quanxTemp, name, rip, eip, eportStr, rip, rportStr, tls, user, pass, !tlsInsecure)
default:
http.NotFound(rw, req)
}
}
func (hp *HTTPProxy) HTTPHandler(rw http.ResponseWriter, req *http.Request) {
removeProxyHeaders(req)
resp, err := hp.tp.RoundTrip(req)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
copyHeaders(rw.Header(), resp.Header)
rw.WriteHeader(resp.StatusCode)
_, err = io.Copy(rw, resp.Body)
if err != nil && err != io.EOF {
return
}
}
// deprecated
// Hijack needs to SetReadDeadline on the Conn of the request, but if we use stream compression here,
// we may always get i/o timeout error.
func (hp *HTTPProxy) ConnectHandler(rw http.ResponseWriter, req *http.Request) {
remote, err := hp.dialer.Dial("tcp", req.URL.Host)
if err != nil {
http.Error(rw, "Failed", http.StatusBadRequest)
return
}
defer remote.Close()
hj, ok := rw.(http.Hijacker)
if !ok {
rw.WriteHeader(http.StatusInternalServerError)
return
}
client, _, err := hj.Hijack()
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
client.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
transport(remote, client)
client.Close()
}
func (hp *HTTPProxy) Auth(req *http.Request, header string) bool {
if hp.s.GetCfg().Username == "" && hp.s.GetCfg().Password == "" {
return true
}
u, p, ok := basicAuth(req, header)
if !ok {
log.Printf("Authenication Failed: Failed to get auth info.\n")
return false
}
if u != hp.s.GetCfg().Username || p != hp.s.GetCfg().Password {
log.Printf("Authenication Failed: %s:%s not matched.\n", u, p)
return false
}
return true
}
func basicAuth(req *http.Request, header string) (username, password string, ok bool) {
auth := req.Header.Get(header)
if auth == "" {
return "", "", false
}
return parseBasicAuth(auth)
}
// parseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic "
if len(auth) < len(prefix) {
return "", "", false
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return "", "", false
}
cs := string(c)
username, password, ok = strings.Cut(cs, ":")
if !ok {
return "", "", false
}
return username, password, true
}
func copyHeaders(dst, src http.Header) {
for key, values := range src {
for _, value := range values {
dst.Add(key, value)
}
}
}
func removeProxyHeaders(req *http.Request) {
req.RequestURI = ""
req.Header.Del("Proxy-Connection")
req.Header.Del("Connection")
req.Header.Del("Proxy-Authenticate")
req.Header.Del("Proxy-Authorization")
req.Header.Del("TE")
req.Header.Del("Trailers")
req.Header.Del("Transfer-Encoding")
req.Header.Del("Upgrade")
}