-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
160 lines (142 loc) · 3.36 KB
/
worker.go
File metadata and controls
160 lines (142 loc) · 3.36 KB
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
package resock
import (
"context"
"errors"
"net"
)
type Worker func(conn net.Conn) (net.Conn, error)
type Pipeline struct {
in []Worker
out []Worker
ctx context.Context
}
func (p *Pipeline) Filter(conn net.Conn, isSrv bool) (net.Conn, error) {
var err error
var pipe = p.out
if isSrv {
pipe = p.in
}
for _, worker := range pipe {
conn, err = worker(conn)
if err != nil {
return nil, err
}
}
return conn, nil
}
func (p *Pipeline) AddOut(w ...Worker) {
p.out = append(p.out, w...)
}
func (p *Pipeline) AddIn(w ...Worker) {
p.in = append(p.in, w...)
}
func (p *Pipeline) Add(in []Worker, out []Worker) {
if in != nil {
p.AddIn(in...)
}
if out != nil {
p.AddOut(out...)
}
}
func socksLocalPipe() *Pipeline {
p := &Pipeline{}
logical1Worker := func(conn net.Conn) (net.Conn, error) {
host, err := Socks5Handshake(conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
p.ctx = context.WithValue(context.Background(), "host", host)
return conn, nil
}
logical2Worker := func(conn net.Conn) (net.Conn, error) {
host := p.ctx.Value("host").(Addr)
_, err := conn.Write(host)
if err != nil {
return nil, err
}
return conn, nil
}
p.AddOut(logical1Worker, basicTCPToSrvWorker, chacha20Worker, logical2Worker)
return p
}
func socksSrvPipe() *Pipeline {
p := &Pipeline{}
p.AddIn(chacha20Worker)
p.AddOut(func(conn net.Conn) (net.Conn, error) {
buf := GetBuf()
defer PutBuf(buf)
addr, err := readAddr(buf, conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
p.ctx = context.WithValue(context.Background(), "host", addr)
return conn, nil
},
func(conn net.Conn) (net.Conn, error) {
host := p.ctx.Value("host").(Addr)
if "" == host.String() {
return nil, errors.New("dst addr parse error")
}
return net.Dial("tcp", host.String())
})
return p
}
func wsLocalPipe(isTLS bool) *Pipeline {
p := &Pipeline{}
if isTLS {
p.AddOut(wssLocalWorker)
} else {
p.AddOut(wsLocalWorker)
}
return p
}
func chacha20Worker(conn net.Conn) (net.Conn, error) {
return NewChacha20Stream(GetCfg().Key, conn)
}
func basicTCPToSrvWorker(conn net.Conn) (net.Conn, error) {
return net.Dial("tcp", GetCfg().Server)
}
func socks5ServerWorker(conn net.Conn) (net.Conn, error) {
buf := GetBuf()
defer PutBuf(buf)
addr, err := readAddr(buf, conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
return net.Dial("tcp", addr.String())
}
func socks5ClientWorker(conn net.Conn) (net.Conn, error) {
dial, err := net.Dial("tcp", GetCfg().Server)
if err != nil {
return nil, err
}
host, err := Socks5Handshake(conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
dstStream, err := NewChacha20Stream(GetCfg().Key, dial)
if err != nil {
return nil, err
}
_, err = dstStream.Write(host)
if err != nil {
return nil, err
}
return dstStream, nil
}
func wsLocalWorker(conn net.Conn) (net.Conn, error) {
ws := NewWebsock()
host, err := Socks5Handshake(conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
return ws.Dial(host.String(), "ws://"+GetCfg().Server)
}
func wssLocalWorker(conn net.Conn) (net.Conn, error) {
ws := NewWebsock()
host, err := Socks5Handshake(conn)
if err != nil {
return nil, errors.New("SOCKS error:" + err.Error())
}
return ws.DialTLS(host.String(), "wss://"+GetCfg().Server+"/wss")
}