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