-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (63 loc) · 1.35 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
package main
import (
"flag"
"golang.org/x/net/proxy"
"io"
"net"
"net/http"
)
var (
laddr = flag.String("laddr", ":8000", "Address to listen on")
raddr = flag.String("raddr", ":1080", "Socks proxy address to connect to.")
)
var (
socks5proxy proxy.Dialer
client *http.Client
)
func newClient(dialer proxy.Dialer) *http.Client {
return &http.Client{
Transport: &http.Transport{
Dial: func(net, addr string) (net.Conn, error) {
return socks5proxy.Dial(net, addr)
},
},
}
}
func main() {
flag.Parse()
var err error
socks5proxy, err = proxy.SOCKS5("tcp", *raddr, nil, proxy.Direct)
if err != nil {
panic(err)
}
client = newClient(socks5proxy)
hndl := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != "CONNECT" {
return
}
serverConn, err := socks5proxy.Dial("tcp", req.Host)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error() + "\n"))
return
}
hijacker, ok := w.(http.Hijacker)
if !ok {
serverConn.Close()
w.WriteHeader(500)
w.Write([]byte("Failed cast to Hijacker\n"))
return
}
w.WriteHeader(200)
_, bio, err := hijacker.Hijack()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error() + "\n"))
serverConn.Close()
return
}
go io.Copy(serverConn, bio)
go io.Copy(bio, serverConn)
})
http.ListenAndServe(*laddr, hndl)
}