-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
90 lines (74 loc) · 1.94 KB
/
connector.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
package ss
import (
"context"
"fmt"
"net"
"time"
"github.com/jxo-me/netx/core/connector"
md "github.com/jxo-me/netx/core/metadata"
"github.com/jxo-me/netx/x/internal/util/relay"
"github.com/jxo-me/netx/x/internal/util/ss"
"github.com/shadowsocks/go-shadowsocks2/core"
)
type ssuConnector struct {
cipher core.Cipher
md metadata
options connector.Options
}
func NewConnector(opts ...connector.Option) connector.IConnector {
options := connector.Options{}
for _, opt := range opts {
opt(&options)
}
return &ssuConnector{
options: options,
}
}
func (c *ssuConnector) Init(md md.IMetaData) (err error) {
if err = c.parseMetadata(md); err != nil {
return
}
if c.options.Auth != nil {
method := c.options.Auth.Username()
password, _ := c.options.Auth.Password()
c.cipher, err = ss.ShadowCipher(method, password, c.md.key)
}
return
}
func (c *ssuConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
log := c.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"network": network,
"address": address,
})
log.Debugf("connect %s/%s", address, network)
switch network {
case "udp", "udp4", "udp6":
default:
err := fmt.Errorf("network %s is unsupported", network)
log.Error(err)
return nil, err
}
if c.md.connectTimeout > 0 {
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
defer conn.SetDeadline(time.Time{})
}
taddr, _ := net.ResolveUDPAddr(network, address)
if taddr == nil {
taddr = &net.UDPAddr{}
}
pc, ok := conn.(net.PacketConn)
if ok {
if c.cipher != nil {
pc = c.cipher.PacketConn(pc)
}
// standard UDP relay
return ss.UDPClientConn(pc, conn.RemoteAddr(), taddr, c.md.bufferSize), nil
}
if c.cipher != nil {
conn = ss.ShadowConn(c.cipher.StreamConn(conn), nil)
}
// UDP over TCP
return relay.UDPTunClientConn(conn, taddr), nil
}