github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/tunnel/socks/conn.go (about) 1 package socks 2 3 import ( 4 "context" 5 "net" 6 7 "github.com/laof/lite-speed-test/common" 8 "github.com/laof/lite-speed-test/tunnel" 9 ) 10 11 type Conn struct { 12 net.Conn 13 metadata *tunnel.Metadata 14 } 15 16 func (c *Conn) Metadata() *tunnel.Metadata { 17 return c.metadata 18 } 19 20 type packetInfo struct { 21 metadata *tunnel.Metadata 22 payload []byte 23 } 24 25 type PacketConn struct { 26 net.PacketConn 27 input chan *packetInfo 28 output chan *packetInfo 29 src net.Addr 30 ctx context.Context 31 cancel context.CancelFunc 32 } 33 34 func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { 35 panic("implement me") 36 } 37 38 func (c *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { 39 panic("implement me") 40 } 41 42 func (c *PacketConn) Close() error { 43 c.cancel() 44 return nil 45 } 46 47 func (c *PacketConn) WriteWithMetadata(p []byte, m *tunnel.Metadata) (int, error) { 48 select { 49 case c.output <- &packetInfo{ 50 metadata: m, 51 payload: p, 52 }: 53 return len(p), nil 54 case <-c.ctx.Done(): 55 return 0, common.NewError("socks packet conn closed") 56 } 57 } 58 59 func (c *PacketConn) ReadWithMetadata(p []byte) (int, *tunnel.Metadata, error) { 60 select { 61 case info := <-c.input: 62 n := copy(p, info.payload) 63 return n, info.metadata, nil 64 case <-c.ctx.Done(): 65 return 0, nil, common.NewError("socks packet conn closed") 66 } 67 }