github.com/metacubex/mihomo@v1.18.5/listener/shadowsocks/utils.go (about) 1 package shadowsocks 2 3 import ( 4 "bytes" 5 "errors" 6 "net" 7 "net/url" 8 9 "github.com/metacubex/mihomo/transport/socks5" 10 ) 11 12 type packet struct { 13 pc net.PacketConn 14 rAddr net.Addr 15 payload []byte 16 put func() 17 } 18 19 func (c *packet) Data() []byte { 20 return c.payload 21 } 22 23 // WriteBack wirtes UDP packet with source(ip, port) = `addr` 24 func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) { 25 if addr == nil { 26 err = errors.New("address is invalid") 27 return 28 } 29 packet := bytes.Join([][]byte{socks5.ParseAddrToSocksAddr(addr), b}, []byte{}) 30 return c.pc.WriteTo(packet, c.rAddr) 31 } 32 33 // LocalAddr returns the source IP/Port of UDP Packet 34 func (c *packet) LocalAddr() net.Addr { 35 return c.rAddr 36 } 37 38 func (c *packet) Drop() { 39 if c.put != nil { 40 c.put() 41 c.put = nil 42 } 43 c.payload = nil 44 } 45 46 func (c *packet) InAddr() net.Addr { 47 return c.pc.LocalAddr() 48 } 49 50 func ParseSSURL(s string) (addr, cipher, password string, err error) { 51 u, err := url.Parse(s) 52 if err != nil { 53 return 54 } 55 56 addr = u.Host 57 if u.User != nil { 58 cipher = u.User.Username() 59 password, _ = u.User.Password() 60 } 61 return 62 }