-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
85 lines (79 loc) · 1.78 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
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"flag"
"golang.org/x/net/proxy"
"io"
"log"
"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" {
serverConn, err := socks5proxy.Dial("tcp", req.Host)
log.Println("Got server connection")
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)
} else {
// Server-Only field; we get an error fi we pass this to `client.Do`.
req.RequestURI = ""
resp, err := client.Do(req)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error() + "\n"))
return
}
hdr := w.Header()
for k, v := range resp.Header {
hdr[k] = v
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
})
log.Fatal(http.ListenAndServe(*laddr, hndl))
}