-
Notifications
You must be signed in to change notification settings - Fork 261
/
goproxy.go
210 lines (174 loc) · 4.42 KB
/
goproxy.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
package main
import (
"flag"
"net"
"net/http"
"strings"
"./dns"
"./src"
"./sutils"
"./cryptconn"
)
var cipher string
var keyfile string
var listenaddr string
var username string
var password string
var passfile string
var blackfile string
var runmode string
var logger *sutils.Logger
func init() {
var logfile string
var loglevel string
flag.StringVar(&runmode, "mode", "", "server/client/httproxy mode")
flag.StringVar(&cipher, "cipher", "aes", "aes/des/tripledes/rc4")
flag.StringVar(&keyfile, "keyfile", "", "key and iv file")
flag.StringVar(&listenaddr, "listen", ":5233", "listen address")
flag.StringVar(&username, "username", "", "username for connect")
flag.StringVar(&password, "password", "", "password for connect")
flag.StringVar(&passfile, "passfile", "", "password file")
flag.StringVar(&blackfile, "black", "", "blacklist file")
flag.StringVar(&logfile, "logfile", "", "log file")
flag.StringVar(&loglevel, "loglevel", "WARNING", "log level")
flag.Parse()
lv, err := sutils.GetLevelByName(loglevel)
if err != nil { panic(err.Error()) }
err = sutils.SetupLog(logfile, lv, 16)
if err != nil { panic(err.Error()) }
logger = sutils.NewLogger("goproxy")
}
var cryptWrapper func (net.Conn) (net.Conn, error) = nil
func run_server () {
var err error
if passfile != "" {
err = src.LoadPassfile(passfile)
if err != nil { panic(err.Error()) }
}
err = sutils.TcpServer(listenaddr, func (conn net.Conn) (err error) {
defer conn.Close()
err = src.QsocksHandler(conn)
if err != nil { sutils.Err(err) }
return nil
})
if err != nil { sutils.Err(err) }
}
func run_client () {
var err error
if cryptWrapper == nil {
sutils.Warning("client mode without keyfile")
}
if len(flag.Args()) < 1 {
panic("args not enough")
}
serveraddr := flag.Args()[0]
err = dns.LoadConfig("resolv.conf")
if err != nil {
err = dns.LoadConfig("/etc/goproxy/resolv.conf")
if err != nil { panic(err.Error()) }
}
src.InitDail(blackfile, serveraddr, cryptWrapper, username, password)
err = sutils.TcpServer(listenaddr, func (conn net.Conn) (err error) {
defer conn.Close()
srcconn, dstconn, err := src.SocksHandler(conn)
if err != nil { return }
sutils.CopyLink(srcconn, dstconn)
return
})
if err != nil { sutils.Err(err) }
}
var tspt http.Transport
type Proxy struct {}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sutils.Info(r.Method, r.URL)
if r.Method == "CONNECT" {
p.Connect(w, r)
return
}
r.RequestURI = ""
r.Header.Del("Accept-Encoding")
r.Header.Del("Proxy-Connection")
r.Header.Del("Connection")
resp, err := tspt.RoundTrip(r)
if err != nil {
sutils.Err(err)
return
}
defer resp.Body.Close()
resp.Header.Del("Content-Length")
for k, vv := range resp.Header {
for _, v := range vv { w.Header().Add(k, v) }
}
w.WriteHeader(resp.StatusCode)
_, err = sutils.CoreCopy(w, resp.Body)
if err != nil {
sutils.Err(err)
return
}
return
}
func (p *Proxy) Connect(w http.ResponseWriter, r *http.Request) {
hij, ok := w.(http.Hijacker)
if !ok {
sutils.Err("httpserver does not support hijacking")
return
}
srcconn, _, err := hij.Hijack()
if err != nil {
sutils.Err("Cannot hijack connection ", err)
return
}
defer srcconn.Close()
host := r.URL.Host
if !strings.Contains(host, ":") {
host += ":80"
}
dstconn, err := src.DialConn("tcp", host)
if err != nil {
sutils.Err(err)
srcconn.Write([]byte("HTTP/1.0 502 OK\r\n\r\n"))
return
}
defer dstconn.Close()
srcconn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
sutils.CopyLink(srcconn, dstconn)
return
}
func run_httproxy() {
if cryptWrapper == nil {
sutils.Warning("client mode without keyfile")
}
if len(flag.Args()) < 1 {
panic("args not enough")
}
serveraddr := flag.Args()[0]
err := dns.LoadConfig("resolv.conf")
if err != nil {
err = dns.LoadConfig("/etc/goproxy/resolv.conf")
if err != nil { panic(err.Error()) }
}
src.InitDail(blackfile, serveraddr, cryptWrapper, username, password)
tspt = http.Transport{Dial: src.DialConn}
http.ListenAndServe(listenaddr, &Proxy{})
}
func main() {
var err error
if len(keyfile) > 0 {
cryptWrapper, err = cryptconn.NewCryptWrapper(cipher, keyfile)
if err != nil {
sutils.Err("crypto not work, cipher or keyfile wrong.")
return
}
}
switch runmode {
case "server":
sutils.Info("server mode")
run_server()
case "client":
sutils.Info("client mode")
run_client()
case "httproxy":
sutils.Info("httproxy mode")
run_httproxy()
}
}