forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclash.go
311 lines (280 loc) · 6.83 KB
/
clash.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package libcore
import (
"context"
"encoding/json"
"fmt"
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/adapter/outbound"
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/component/nat"
"github.com/Dreamacro/clash/constant"
clashC "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/socks"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/xtls/xray-core/common/task"
"io"
"net"
"sync"
"time"
)
type ClashBasedInstance struct {
access sync.Mutex
socksPort int32
ctx chan constant.ConnContext
in *socks.Listener
udpIn *socks.UDPListener
udpCtx chan *inbound.PacketAdapter
out clashC.ProxyAdapter
nat nat.Table
started bool
}
func (s *ClashBasedInstance) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
dest, err := addrToMetadata(address)
if err != nil {
return nil, err
}
dest.NetWork = networkForClash(network)
return s.out.DialContext(ctx, dest)
}
func newClashBasedInstance(socksPort int32, out clashC.ProxyAdapter) *ClashBasedInstance {
return &ClashBasedInstance{
socksPort: socksPort,
ctx: make(chan constant.ConnContext, 64),
udpCtx: make(chan *inbound.PacketAdapter, 64),
out: out,
}
}
func (s *ClashBasedInstance) Start() error {
s.access.Lock()
defer s.access.Unlock()
if s.started {
return errors.New("already started")
}
addr := fmt.Sprintf("127.0.0.1:%d", s.socksPort)
in, err := socks.New(addr, s.ctx)
if err != nil {
return errors.WithMessage(err, "create socks inbound")
}
udpIn, err := socks.NewUDP(addr, s.udpCtx)
if err != nil {
return errors.WithMessage(err, "create socks udp inbound")
}
s.in = in
s.udpIn = udpIn
s.started = true
go s.loop()
go s.loopUdp()
return nil
}
func (s *ClashBasedInstance) Close() error {
s.access.Lock()
defer s.access.Unlock()
if !s.started {
return errors.New("not started")
}
closeIgnore(s.in, s.udpIn, s.out, s.ctx, s.udpCtx)
return nil
}
func (s *ClashBasedInstance) loop() {
for conn := range s.ctx {
conn := conn
metadata := conn.Metadata()
go func() {
ctx := context.Background()
remote, err := s.out.DialContext(ctx, metadata)
if err != nil {
fmt.Printf("Dial error: %s\n", err.Error())
return
}
_ = task.Run(ctx, func() error {
_, _ = io.Copy(remote, conn.Conn())
return io.EOF
}, func() error {
_, _ = io.Copy(conn.Conn(), remote)
return io.EOF
})
_ = remote.Close()
_ = conn.Conn().Close()
}()
}
}
var udpTimeout = 60 * time.Second
func (s *ClashBasedInstance) loopUdp() {
for packet := range s.udpCtx {
metadata := packet.Metadata()
if !metadata.Valid() {
logrus.Warnln("[Metadata] not valid: ", metadata)
continue
}
packet := packet
go func() {
key := packet.LocalAddr().String()
handle := func() bool {
pc := s.nat.Get(key)
if pc != nil {
_, err := pc.WriteTo(packet.Data(), metadata.UDPAddr())
if err != nil {
packet.Drop()
_ = pc.Close()
}
return true
}
return false
}
if handle() {
packet.Drop()
return
}
lockKey := key + "-lock"
cond, loaded := s.nat.GetOrCreateLock(lockKey)
if loaded {
cond.L.Lock()
cond.Wait()
handle()
cond.L.Unlock()
return
}
ctx := context.Background()
remote, err := s.out.DialUDP(metadata)
if err != nil {
fmt.Printf("Dial UDP error: %s\n", err.Error())
return
}
s.nat.Set(key, remote)
go handle()
buf := pool.Get(pool.RelayBufferSize)
_ = task.Run(ctx, func() error {
for {
_ = remote.SetReadDeadline(time.Now().Add(udpTimeout))
n, from, err := remote.ReadFrom(buf)
if err == nil {
_, err = packet.WriteBack(buf[:n], from)
}
if err != nil {
return err
}
}
})
_ = pool.Put(buf)
_ = remote.Close()
packet.Drop()
s.nat.Delete(lockKey)
cond.Broadcast()
}()
}
}
func addrToMetadata(rawAddress string) (addr *clashC.Metadata, err error) {
host, port, err := net.SplitHostPort(rawAddress)
if err != nil {
err = fmt.Errorf("addrToMetadata failed: %w", err)
return
}
ip := net.ParseIP(host)
if ip == nil {
addr = &clashC.Metadata{
AddrType: clashC.AtypDomainName,
Host: host,
DstIP: nil,
DstPort: port,
}
return
} else if ip4 := ip.To4(); ip4 != nil {
addr = &clashC.Metadata{
AddrType: clashC.AtypIPv4,
Host: "",
DstIP: ip4,
DstPort: port,
}
return
}
addr = &clashC.Metadata{
AddrType: clashC.AtypIPv6,
Host: "",
DstIP: ip,
DstPort: port,
}
return
}
func networkForClash(network string) clashC.NetWork {
switch network {
case "tcp", "tcp4", "tcp6":
return clashC.TCP
case "udp", "udp4", "udp6":
return clashC.UDP
}
logrus.Fatalln("unexpected network name", network)
return 0
}
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
_ = tcp.SetKeepAlivePeriod(30 * time.Second)
}
}
func safeConnClose(c net.Conn, err error) {
if err != nil {
_ = c.Close()
}
}
func NewShadowsocksInstance(socksPort int32, server string, port int32, password string, cipher string, plugin string, pluginOpts string) (*ClashBasedInstance, error) {
if cipher == "none" {
cipher = "dummy"
}
if plugin == "obfs-local" || plugin == "simple-obfs" {
plugin = "obfs"
}
opts := map[string]interface{}{}
err := json.Unmarshal([]byte(pluginOpts), &opts)
if err != nil {
return nil, err
}
out, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Server: server,
Port: int(port),
Password: password,
Cipher: cipher,
Plugin: plugin,
PluginOpts: opts,
})
if err != nil {
return nil, err
}
return newClashBasedInstance(socksPort, out), nil
}
func NewShadowsocksRInstance(socksPort int32, server string, port int32, password string, cipher string, obfs string, obfsParam string, protocol string, protocolParam string) (*ClashBasedInstance, error) {
if cipher == "none" {
cipher = "dummy"
}
out, err := outbound.NewShadowSocksR(outbound.ShadowSocksROption{
Server: server,
Port: int(port),
Password: password,
Cipher: cipher,
Obfs: obfs,
ObfsParam: obfsParam,
Protocol: protocol,
ProtocolParam: protocolParam,
UDP: true,
})
if err != nil {
return nil, err
}
return newClashBasedInstance(socksPort, out), nil
}
func NewSnellInstance(socksPort int32, server string, port int32, psk string, obfsMode string, obfsHost string, version int32) (*ClashBasedInstance, error) {
obfs := map[string]interface{}{}
obfs["mode"] = obfsMode
obfs["host"] = obfsHost
out, err := outbound.NewSnell(outbound.SnellOption{
Server: server,
Port: int(port),
Psk: psk,
Version: int(version),
ObfsOpts: obfs,
})
if err != nil {
return nil, err
}
return newClashBasedInstance(socksPort, out), nil
}