This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tcp_conn.go
106 lines (96 loc) · 2.16 KB
/
tcp_conn.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
package shadowsocks_stream
import (
"fmt"
"io"
"github.com/daeuniverse/softwind/ciphers"
"github.com/daeuniverse/softwind/netproxy"
"github.com/daeuniverse/softwind/pool"
)
// TcpConn the struct that override the netproxy.Conn methods
type TcpConn struct {
netproxy.Conn
cipher *ciphers.StreamCipher
init bool
}
func NewTcpConn(c netproxy.Conn, cipher *ciphers.StreamCipher) *TcpConn {
return &TcpConn{
Conn: c,
cipher: cipher,
}
}
func (c *TcpConn) Read(b []byte) (n int, err error) {
if !c.cipher.DecryptInited() {
buf := b
if len(buf) < c.cipher.InfoIVLen() {
buf = pool.Get(c.cipher.InfoIVLen() + len(b))
defer pool.Put(buf)
}
n, err = io.ReadAtLeast(c.Conn, buf, c.cipher.InfoIVLen())
if err != nil {
return 0, fmt.Errorf("invalid ivLen:%v, actual length:%v: %w", c.cipher.InfoIVLen(), n, err)
}
//log.Println("n1", n)
iv := buf[:c.cipher.InfoIVLen()]
if err = c.cipher.InitDecrypt(iv); err != nil {
return 0, err
}
if c.cipher.IV() == nil {
c.cipher.SetIV(iv)
}
if n == c.cipher.InfoIVLen() {
//log.Println("here")
return 0, nil
}
//log.Println("there")
n = copy(b, buf[c.cipher.InfoIVLen():n])
c.cipher.Decrypt(b[:n], b[:n])
//log.Println("n2", n)
} else {
n, err = c.Conn.Read(b)
if err != nil {
return n, err
}
c.cipher.Decrypt(b[:n], b[:n])
}
return n, nil
}
func (c *TcpConn) Write(b []byte) (n int, err error) {
lenToWrite := len(b)
ivLen := 0
if !c.cipher.EncryptInited() {
_, err = c.cipher.InitEncrypt()
if err != nil {
return 0, err
}
}
if !c.init {
c.init = true
iv := c.cipher.IV()
buf := pool.Get(len(b) + len(iv))
defer pool.Put(buf)
ivLen = len(iv)
copy(buf, iv)
copy(buf[ivLen:], b)
b = buf
// For SSR obfs.
if innerConn, ok := c.Conn.(interface {
SetCipher(cipher *ciphers.StreamCipher)
}); ok {
innerConn.SetCipher(c.cipher)
}
if innerConn, ok := c.Conn.(interface {
SetAddrLen(addrLen int)
}); ok {
innerConn.SetAddrLen(lenToWrite)
}
}
c.cipher.Encrypt(b[ivLen:], b[ivLen:])
_, err = c.Conn.Write(b)
if err != nil {
return 0, err
}
return lenToWrite, nil
}
func (c *TcpConn) Cipher() *ciphers.StreamCipher {
return c.cipher
}