-
Notifications
You must be signed in to change notification settings - Fork 17
/
tcp.go
75 lines (64 loc) · 2.93 KB
/
tcp.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
package ss2022
import (
"net"
"github.com/database64128/shadowsocks-go/conn"
"github.com/database64128/shadowsocks-go/zerocopy"
)
// TCPClient implements the zerocopy TCPClient interface.
type TCPClient struct {
tco *zerocopy.TCPConnOpener
cipherConfig *CipherConfig
eihPSKHashes [][IdentityHeaderLength]byte
unsafeRequestStreamPrefix []byte
unsafeResponseStreamPrefix []byte
}
func NewTCPClient(address string, dialerTFO bool, dialerFwmark int, cipherConfig *CipherConfig, eihPSKHashes [][IdentityHeaderLength]byte, unsafeRequestStreamPrefix, unsafeResponseStreamPrefix []byte) *TCPClient {
return &TCPClient{
tco: zerocopy.NewTCPConnOpener(conn.NewDialer(dialerTFO, dialerFwmark), "tcp", address),
cipherConfig: cipherConfig,
eihPSKHashes: eihPSKHashes,
unsafeRequestStreamPrefix: unsafeRequestStreamPrefix,
unsafeResponseStreamPrefix: unsafeResponseStreamPrefix,
}
}
// Dial implements the zerocopy.TCPClient Dial method.
func (c *TCPClient) Dial(targetAddr conn.Addr, payload []byte) (tc *net.TCPConn, rw zerocopy.ReadWriter, err error) {
rw, rawRW, err := NewShadowStreamClientReadWriter(c.tco, c.cipherConfig, c.eihPSKHashes, targetAddr, payload, c.unsafeRequestStreamPrefix, c.unsafeResponseStreamPrefix)
if err == nil {
tc = rawRW.(*net.TCPConn)
}
return
}
// NativeInitialPayload implements the zerocopy.TCPClient NativeInitialPayload method.
func (c *TCPClient) NativeInitialPayload() bool {
return true
}
// TCPServer implements the zerocopy TCPServer interface.
type TCPServer struct {
cipherConfig *CipherConfig
saltPool *SaltPool[string]
uPSKMap map[[IdentityHeaderLength]byte]*CipherConfig
unsafeRequestStreamPrefix []byte
unsafeResponseStreamPrefix []byte
}
func NewTCPServer(cipherConfig *CipherConfig, uPSKMap map[[IdentityHeaderLength]byte]*CipherConfig, unsafeRequestStreamPrefix, unsafeResponseStreamPrefix []byte) *TCPServer {
return &TCPServer{
cipherConfig: cipherConfig,
saltPool: NewSaltPool[string](ReplayWindowDuration),
uPSKMap: uPSKMap,
unsafeRequestStreamPrefix: unsafeRequestStreamPrefix,
unsafeResponseStreamPrefix: unsafeResponseStreamPrefix,
}
}
// Accept implements the zerocopy.TCPServer Accept method.
func (s *TCPServer) Accept(tc *net.TCPConn) (rw zerocopy.ReadWriter, targetAddr conn.Addr, payload []byte, err error) {
return NewShadowStreamServerReadWriter(tc, s.cipherConfig, s.saltPool, s.uPSKMap, s.unsafeRequestStreamPrefix, s.unsafeResponseStreamPrefix)
}
// NativeInitialPayload implements the zerocopy.TCPServer NativeInitialPayload method.
func (s *TCPServer) NativeInitialPayload() bool {
return true
}
// DefaultTCPConnCloser implements the zerocopy.TCPServer DefaultTCPConnCloser method.
func (s *TCPServer) DefaultTCPConnCloser() zerocopy.TCPConnCloser {
return zerocopy.ForceReset
}