-
Notifications
You must be signed in to change notification settings - Fork 688
/
proxy.go
120 lines (103 loc) · 2.26 KB
/
proxy.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
package proxy
import (
"io"
"log"
"net"
"golang.org/x/net/proxy"
"github.com/datawire/ambassador/pkg/tpu"
)
type Proxy struct {
listener net.Listener
router func(*net.TCPConn) (string, error)
}
func NewProxy(address string, router func(*net.TCPConn) (string, error)) (proxy *Proxy, err error) {
tpu.Rlimit()
ln, err := net.Listen("tcp", ":1234")
if err == nil {
proxy = &Proxy{ln, router}
}
return
}
func (p *Proxy) log(line string, args ...interface{}) {
log.Printf("PXY: "+line+"\n", args...)
}
func (p *Proxy) Start(limit int) {
p.log("listening limit=%v", limit)
go func() {
sem := tpu.NewSemaphore(limit)
for {
conn, err := p.listener.Accept()
if err != nil {
p.log(err.Error())
} else {
switch conn := conn.(type) {
case *net.TCPConn:
p.log("CAPACITY: %v", len(sem))
sem.Acquire()
go func() {
defer sem.Release()
p.handleConnection(conn)
}()
default:
p.log("unknown connection type: %v", conn)
}
}
}
}()
}
func (p *Proxy) handleConnection(conn *net.TCPConn) {
host, err := p.router(conn)
if err != nil {
p.log("router error: %v", err)
return
}
p.log("CONNECT %s %s", conn.RemoteAddr(), host)
// setting up an ssh tunnel with dynamic socks proxy at this end
// seems faster than connecting directly to a socks proxy
dialer, err := proxy.SOCKS5("tcp", "localhost:1080", nil, proxy.Direct)
// dialer, err := proxy.SOCKS5("tcp", "localhost:9050", nil, proxy.Direct)
if err != nil {
p.log(err.Error())
conn.Close()
return
}
_proxy, err := dialer.Dial("tcp", host)
if err != nil {
p.log(err.Error())
conn.Close()
return
}
proxy := _proxy.(*net.TCPConn)
done := tpu.NewLatch(2)
go p.pipe(conn, proxy, done)
go p.pipe(proxy, conn, done)
done.Wait()
}
func (p *Proxy) pipe(from, to *net.TCPConn, done tpu.Latch) {
defer func() {
p.log("CLOSED WRITE %v", to.RemoteAddr())
to.CloseWrite()
}()
defer func() {
p.log("CLOSED READ %v", from.RemoteAddr())
from.CloseRead()
}()
defer done.Notify()
const size = 64 * 1024
var buf [size]byte
for {
n, err := from.Read(buf[0:size])
if err != nil {
if err != io.EOF {
p.log(err.Error())
}
break
} else {
_, err := to.Write(buf[0:n])
if err != nil {
p.log(err.Error())
break
}
}
}
}