github.com/database64128/shadowsocks-go@v1.7.0/http/tcp.go (about) 1 package http 2 3 import ( 4 "github.com/database64128/shadowsocks-go/conn" 5 "github.com/database64128/shadowsocks-go/zerocopy" 6 "github.com/database64128/tfo-go/v2" 7 "go.uber.org/zap" 8 ) 9 10 // ProxyClient implements the zerocopy TCPClient interface. 11 type ProxyClient struct { 12 name string 13 address string 14 dialer tfo.Dialer 15 } 16 17 func NewProxyClient(name, address string, dialer tfo.Dialer) *ProxyClient { 18 return &ProxyClient{ 19 name: name, 20 address: address, 21 dialer: dialer, 22 } 23 } 24 25 // Info implements the zerocopy.TCPClient Info method. 26 func (c *ProxyClient) Info() zerocopy.TCPClientInfo { 27 return zerocopy.TCPClientInfo{ 28 Name: c.name, 29 NativeInitialPayload: false, 30 } 31 } 32 33 // Dial implements the zerocopy.TCPClient Dial method. 34 func (c *ProxyClient) Dial(targetAddr conn.Addr, payload []byte) (rawRW zerocopy.DirectReadWriteCloser, rw zerocopy.ReadWriter, err error) { 35 nc, err := c.dialer.Dial("tcp", c.address, nil) 36 if err != nil { 37 return 38 } 39 rawRW = nc.(zerocopy.DirectReadWriteCloser) 40 41 rw, err = NewHttpStreamClientReadWriter(rawRW, targetAddr) 42 if err != nil { 43 rawRW.Close() 44 return 45 } 46 47 if len(payload) > 0 { 48 if _, err = rw.WriteZeroCopy(payload, 0, len(payload)); err != nil { 49 rawRW.Close() 50 } 51 } 52 return 53 } 54 55 // ProxyServer implements the zerocopy TCPServer interface. 56 type ProxyServer struct { 57 logger *zap.Logger 58 } 59 60 func NewProxyServer(logger *zap.Logger) *ProxyServer { 61 return &ProxyServer{logger} 62 } 63 64 // Info implements the zerocopy.TCPServer Info method. 65 func (s *ProxyServer) Info() zerocopy.TCPServerInfo { 66 return zerocopy.TCPServerInfo{ 67 NativeInitialPayload: false, 68 DefaultTCPConnCloser: zerocopy.JustClose, 69 } 70 } 71 72 // Accept implements the zerocopy.TCPServer Accept method. 73 func (s *ProxyServer) Accept(rawRW zerocopy.DirectReadWriteCloser) (rw zerocopy.ReadWriter, targetAddr conn.Addr, payload []byte, username string, err error) { 74 rw, targetAddr, err = NewHttpStreamServerReadWriter(rawRW, s.logger) 75 return 76 }