github.com/MerlinKodo/sing-shadowsocks2@v0.1.6/cipher/method_none.go (about)

     1  package cipher
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/sagernet/sing/common"
     8  	"github.com/sagernet/sing/common/buf"
     9  	"github.com/sagernet/sing/common/bufio"
    10  	M "github.com/sagernet/sing/common/metadata"
    11  	N "github.com/sagernet/sing/common/network"
    12  )
    13  
    14  const MethodNone = "none"
    15  
    16  func init() {
    17  	RegisterMethod([]string{MethodNone}, func(ctx context.Context, method string, options MethodOptions) (Method, error) {
    18  		return &noneMethod{}, nil
    19  	})
    20  }
    21  
    22  var _ Method = (*noneMethod)(nil)
    23  
    24  type noneMethod struct{}
    25  
    26  func (m *noneMethod) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error) {
    27  	err := M.SocksaddrSerializer.WriteAddrPort(conn, destination)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return conn, nil
    32  }
    33  
    34  func (m *noneMethod) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn {
    35  	return &noneConn{
    36  		ExtendedConn: bufio.NewExtendedConn(conn),
    37  		destination:  destination,
    38  	}
    39  }
    40  
    41  func (m *noneMethod) DialPacketConn(conn net.Conn) N.NetPacketConn {
    42  	return &nonePacketConn{
    43  		ExtendedConn: bufio.NewExtendedConn(conn),
    44  	}
    45  }
    46  
    47  var (
    48  	_ N.ExtendedConn       = (*noneConn)(nil)
    49  	_ N.FrontHeadroom      = (*noneConn)(nil)
    50  	_ N.ReaderWithUpstream = (*noneConn)(nil)
    51  	_ N.WriterWithUpstream = (*noneConn)(nil)
    52  	_ common.WithUpstream  = (*noneConn)(nil)
    53  )
    54  
    55  type noneConn struct {
    56  	N.ExtendedConn
    57  	destination    M.Socksaddr
    58  	requestWritten bool
    59  }
    60  
    61  func (c *noneConn) Write(p []byte) (n int, err error) {
    62  	if !c.requestWritten {
    63  		buffer := buf.NewSize(M.SocksaddrSerializer.AddrPortLen(c.destination) + len(p))
    64  		defer buffer.Release()
    65  		err = M.SocksaddrSerializer.WriteAddrPort(buffer, c.destination)
    66  		if err != nil {
    67  			return
    68  		}
    69  		common.Must1(buffer.Write(p))
    70  		_, err = c.ExtendedConn.Write(buffer.Bytes())
    71  		if err != nil {
    72  			return
    73  		}
    74  		c.requestWritten = true
    75  		n = len(p)
    76  		return
    77  	}
    78  	return c.ExtendedConn.Write(p)
    79  }
    80  
    81  func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error {
    82  	if !c.requestWritten {
    83  		header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(c.destination)))
    84  		err := M.SocksaddrSerializer.WriteAddrPort(header, c.destination)
    85  		if err != nil {
    86  			return err
    87  		}
    88  		c.requestWritten = true
    89  	}
    90  	return c.ExtendedConn.WriteBuffer(buffer)
    91  }
    92  
    93  func (c *noneConn) FrontHeadroom() int {
    94  	return M.MaxSocksaddrLength
    95  }
    96  
    97  func (c *noneConn) ReaderReplaceable() bool {
    98  	return true
    99  }
   100  
   101  func (c *noneConn) WriterReplaceable() bool {
   102  	return c.requestWritten
   103  }
   104  
   105  func (c *noneConn) Upstream() any {
   106  	return c.ExtendedConn
   107  }
   108  
   109  var (
   110  	_ N.NetPacketConn     = (*nonePacketConn)(nil)
   111  	_ N.FrontHeadroom     = (*nonePacketConn)(nil)
   112  	_ common.WithUpstream = (*nonePacketConn)(nil)
   113  )
   114  
   115  type nonePacketConn struct {
   116  	N.ExtendedConn
   117  }
   118  
   119  func (c *nonePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
   120  	n, err = c.ExtendedConn.Read(p)
   121  	if err != nil {
   122  		return
   123  	}
   124  	buffer := buf.With(p[:n])
   125  	destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer)
   126  	if err != nil {
   127  		return
   128  	}
   129  	if destination.IsFqdn() {
   130  		addr = destination
   131  	} else {
   132  		addr = destination.UDPAddr()
   133  	}
   134  	n = copy(p, buffer.Bytes())
   135  	return
   136  }
   137  
   138  func (c *nonePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
   139  	destination := M.SocksaddrFromNet(addr)
   140  	buffer := buf.NewSize(M.SocksaddrSerializer.AddrPortLen(destination) + len(p))
   141  	defer buffer.Release()
   142  	err = M.SocksaddrSerializer.WriteAddrPort(buffer, destination)
   143  	if err != nil {
   144  		return
   145  	}
   146  	common.Must1(buffer.Write(p))
   147  	_, err = c.ExtendedConn.Write(buffer.Bytes())
   148  	if err != nil {
   149  		return
   150  	}
   151  	n = len(p)
   152  	return
   153  }
   154  
   155  func (c *nonePacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
   156  	err = c.ExtendedConn.ReadBuffer(buffer)
   157  	if err != nil {
   158  		return
   159  	}
   160  	return M.SocksaddrSerializer.ReadAddrPort(buffer)
   161  }
   162  
   163  func (c *nonePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
   164  	header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination)))
   165  	err := M.SocksaddrSerializer.WriteAddrPort(header, destination)
   166  	if err != nil {
   167  		return err
   168  	}
   169  	return c.ExtendedConn.WriteBuffer(buffer)
   170  }
   171  
   172  func (c *nonePacketConn) FrontHeadroom() int {
   173  	return M.MaxSocksaddrLength
   174  }
   175  
   176  func (c *nonePacketConn) Upstream() any {
   177  	return c.ExtendedConn
   178  }