-
Notifications
You must be signed in to change notification settings - Fork 30
/
shadowsocks.go
95 lines (80 loc) · 1.9 KB
/
shadowsocks.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
package shadowsocks
import (
"crypto/md5"
"net"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var (
ErrBadKey = E.New("bad key")
ErrMissingPassword = E.New("missing password")
ErrNoUsers = E.New("no users")
)
type Method interface {
Name() string
DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error)
DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn
DialPacketConn(conn net.Conn) N.NetPacketConn
}
type Service interface {
Name() string
Password() string
N.TCPConnectionHandler
N.UDPHandler
E.Handler
}
type MultiService[U comparable] interface {
Name() string
UpdateUsers(userList []U, keyList [][]byte) error
UpdateUsersWithPasswords(userList []U, passwordList []string) error
N.TCPConnectionHandler
N.UDPHandler
E.Handler
}
type Handler interface {
N.TCPConnectionHandler
N.UDPConnectionHandler
E.Handler
}
type ServerConnError struct {
net.Conn
Source M.Socksaddr
Cause error
}
func (e *ServerConnError) Close() error {
if conn, ok := common.Cast[*net.TCPConn](e.Conn); ok {
conn.SetLinger(0)
}
return e.Conn.Close()
}
func (e *ServerConnError) Unwrap() error {
return e.Cause
}
func (e *ServerConnError) Error() string {
return F.ToString("shadowsocks: serve TCP from ", e.Source, ": ", e.Cause)
}
type ServerPacketError struct {
Source M.Socksaddr
Cause error
}
func (e *ServerPacketError) Unwrap() error {
return e.Cause
}
func (e *ServerPacketError) Error() string {
return F.ToString("shadowsocks: serve UDP from ", e.Source, ": ", e.Cause)
}
func Key(password []byte, keySize int) []byte {
var b, prev []byte
h := md5.New()
for len(b) < keySize {
h.Write(prev)
h.Write(password)
b = h.Sum(b)
prev = b[len(b)-h.Size():]
h.Reset()
}
return b[:keySize]
}