github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/direct/tcp_linux.go (about) 1 package direct 2 3 import ( 4 "net" 5 6 "github.com/database64128/shadowsocks-go/conn" 7 "github.com/database64128/shadowsocks-go/zerocopy" 8 ) 9 10 // TCPTransparentServer is a transparent proxy server. 11 // 12 // TCPTransparentServer implements the zerocopy TCPServer interface. 13 type TCPTransparentServer struct{} 14 15 func NewTCPTransparentServer() (zerocopy.TCPServer, error) { 16 return TCPTransparentServer{}, nil 17 } 18 19 // Info implements the zerocopy.TCPServer Info method. 20 func (TCPTransparentServer) Info() zerocopy.TCPServerInfo { 21 return zerocopy.TCPServerInfo{ 22 NativeInitialPayload: false, 23 DefaultTCPConnCloser: zerocopy.JustClose, 24 } 25 } 26 27 // Accept implements the zerocopy.TCPServer Accept method. 28 func (TCPTransparentServer) Accept(rawRW zerocopy.DirectReadWriteCloser) (rw zerocopy.ReadWriter, targetAddr conn.Addr, payload []byte, username string, err error) { 29 tc, ok := rawRW.(*net.TCPConn) 30 if !ok { 31 return nil, conn.Addr{}, nil, "", zerocopy.ErrAcceptRequiresTCPConn 32 } 33 return &DirectStreamReadWriter{rw: rawRW}, conn.AddrFromIPPort(tc.LocalAddr().(*net.TCPAddr).AddrPort()), nil, "", nil 34 }