-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlocal_http.go
153 lines (134 loc) · 3.74 KB
/
local_http.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
package easyss
import (
"encoding/base64"
"errors"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"github.com/nange/easyss/v2/log"
"github.com/nange/easyss/v2/util/bytespool"
"github.com/nange/easyss/v2/util/netpipe"
"github.com/wzshiming/sysproxy"
)
type bufferPool struct{}
func (bp *bufferPool) Get() []byte {
return bytespool.Get(RelayBufferSize)
}
func (bp *bufferPool) Put(buf []byte) {
bytespool.MustPut(buf)
}
func (ss *Easyss) LocalHttp() {
var addr string
if ss.BindAll() {
addr = ":" + strconv.Itoa(ss.LocalHTTPPort())
} else {
addr = "127.0.0.1:" + strconv.Itoa(ss.LocalHTTPPort())
}
log.Info("[HTTP_PROXY] starting local http-proxy server at", "addr", addr)
server := &http.Server{Addr: addr, Handler: newHTTPProxy(ss)}
ss.SetHttpProxyServer(server)
if err := server.ListenAndServe(); err != nil {
log.Warn("[HTTP_PROXY] local http-proxy server", "err", err)
}
}
func (ss *Easyss) SetSysProxyOnHTTP() error {
if err := sysproxy.OnHTTP(ss.LocalHttpAddr()); err != nil {
return err
}
return sysproxy.OnHTTPS(ss.LocalHttpAddr())
}
func (ss *Easyss) SetSysProxyOffHTTP() error {
if err := sysproxy.OffHTTP(); err != nil {
return err
}
return sysproxy.OffHTTPS()
}
type httpProxy struct {
ss *Easyss
rp *httputil.ReverseProxy
}
func newHTTPProxy(ss *Easyss) *httpProxy {
return &httpProxy{
ss: ss,
rp: &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {},
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return url.Parse(ss.Socks5ProxyAddr())
},
TLSHandshakeTimeout: ss.TLSTimeout(),
},
BufferPool: &bufferPool{},
ErrorHandler: func(rw http.ResponseWriter, r *http.Request, err error) {
log.Warn("[HTTP_PROXY] reverse proxy request", "err", err)
http.Error(rw, "Service unavailable", http.StatusServiceUnavailable)
},
},
}
}
func (h *httpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.ss.AuthUsername() != "" && h.ss.AuthPassword() != "" {
username, password, ok := basicAuth(r)
if !ok {
log.Warn("[HTTP_PROXY] username and password not provided")
http.Error(w, "Proxy auth required", http.StatusProxyAuthRequired)
return
}
if username != h.ss.AuthUsername() || password != h.ss.AuthPassword() {
log.Warn("[HTTP_PROXY] username or password is invalid")
http.Error(w, "Request unauthorized", http.StatusUnauthorized)
return
}
}
if r.Method == "CONNECT" {
h.doWithHijack(w, r)
return
}
h.rp.ServeHTTP(w, r)
}
func (h *httpProxy) doWithHijack(w http.ResponseWriter, r *http.Request) {
rc := http.NewResponseController(w)
hijConn, _, err := rc.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error("[HTTP_PROXY] get hijack conn", "err", err)
return
}
defer hijConn.Close()
if _, err := hijConn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n")); err != nil {
log.Error("[HTTP_PROXY] write hijack ok", "err", err)
return
}
if err := h.ss.localRelay(hijConn, r.URL.Host); err != nil && !errors.Is(err, netpipe.ErrPipeClosed) {
log.Warn("[HTTP_PROXY] local relay", "err", err)
}
}
func basicAuth(r *http.Request) (username, password string, ok bool) {
username, password, ok = r.BasicAuth()
if ok {
return
}
auth := r.Header.Get("Proxy-Authorization")
if auth == "" {
return
}
return parseBasicAuth(auth)
}
func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic "
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], 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
}