github.com/MicalKarl/sing-shadowsocks@v0.0.5/shadowsocks.go (about) 1 package shadowsocks 2 3 import ( 4 "crypto/md5" 5 "net" 6 7 "github.com/sagernet/sing/common" 8 E "github.com/sagernet/sing/common/exceptions" 9 F "github.com/sagernet/sing/common/format" 10 M "github.com/sagernet/sing/common/metadata" 11 N "github.com/sagernet/sing/common/network" 12 ) 13 14 var ( 15 ErrBadKey = E.New("bad key") 16 ErrMissingPassword = E.New("missing password") 17 ErrNoUsers = E.New("no users") 18 ) 19 20 type Method interface { 21 Name() string 22 DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error) 23 DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn 24 DialPacketConn(conn net.Conn) N.NetPacketConn 25 } 26 27 type Service interface { 28 Name() string 29 Password() string 30 N.TCPConnectionHandler 31 N.UDPHandler 32 E.Handler 33 } 34 35 type MultiService[U comparable] interface { 36 Name() string 37 UpdateUsers(userList []U, keyList [][]byte) error 38 UpdateUsersWithPasswords(userList []U, passwordList []string) error 39 N.TCPConnectionHandler 40 N.UDPHandler 41 E.Handler 42 } 43 44 type Handler interface { 45 N.TCPConnectionHandler 46 N.UDPConnectionHandler 47 E.Handler 48 } 49 50 type ServerConnError struct { 51 net.Conn 52 Source M.Socksaddr 53 Cause error 54 } 55 56 func (e *ServerConnError) Close() error { 57 if conn, ok := common.Cast[*net.TCPConn](e.Conn); ok { 58 conn.SetLinger(0) 59 } 60 return e.Conn.Close() 61 } 62 63 func (e *ServerConnError) Unwrap() error { 64 return e.Cause 65 } 66 67 func (e *ServerConnError) Error() string { 68 return F.ToString("shadowsocks: serve TCP from ", e.Source, ": ", e.Cause) 69 } 70 71 type ServerPacketError struct { 72 Source M.Socksaddr 73 Cause error 74 } 75 76 func (e *ServerPacketError) Unwrap() error { 77 return e.Cause 78 } 79 80 func (e *ServerPacketError) Error() string { 81 return F.ToString("shadowsocks: serve UDP from ", e.Source, ": ", e.Cause) 82 } 83 84 func Key(password []byte, keySize int) []byte { 85 var b, prev []byte 86 h := md5.New() 87 for len(b) < keySize { 88 h.Write(prev) 89 h.Write(password) 90 b = h.Sum(b) 91 prev = b[len(b)-h.Size():] 92 h.Reset() 93 } 94 return b[:keySize] 95 }