/
proxy.go
64 lines (53 loc) · 1.02 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
package jumpway
import (
"context"
"net"
"sync"
"github.com/wzshiming/anyproxy"
"github.com/wzshiming/bridge"
)
func RunProxy(ctx context.Context, listener net.Listener, dialer bridge.Dialer) error {
address := listener.Addr().String()
proxies := []string{
"http://" + address,
"socks5://" + address,
"socks4://" + address,
"ssh://" + address,
"view://" + address,
}
proxy, err := anyproxy.NewAnyProxy(ctx, proxies, dialer, nil, BytesPool)
if err != nil {
return err
}
host := proxy.Match(address)
for {
conn, err := listener.Accept()
if err != nil {
return err
}
go host.ServeConn(conn)
}
return nil
}
var DefaultSize = 32 * 1024
type bytesPool struct {
sync.Pool
}
func (b *bytesPool) Get() []byte {
buf := b.Pool.Get().([]byte)
buf = buf[:cap(buf)]
return buf
}
func (b *bytesPool) Put(d []byte) {
if d == nil || len(d) < DefaultSize {
return
}
b.Pool.Put(d)
}
var BytesPool = &bytesPool{
Pool: sync.Pool{
New: func() interface{} {
return make([]byte, DefaultSize)
},
},
}