github.com/sagernet/sing-shadowsocks2@v0.2.0/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 extendedConn := bufio.NewExtendedConn(conn) 43 return &nonePacketConn{extendedConn, extendedConn} 44 } 45 46 var ( 47 _ N.ExtendedConn = (*noneConn)(nil) 48 _ N.FrontHeadroom = (*noneConn)(nil) 49 _ N.ReaderWithUpstream = (*noneConn)(nil) 50 _ N.WriterWithUpstream = (*noneConn)(nil) 51 _ common.WithUpstream = (*noneConn)(nil) 52 ) 53 54 type noneConn struct { 55 N.ExtendedConn 56 destination M.Socksaddr 57 requestWritten bool 58 } 59 60 func (c *noneConn) Write(p []byte) (n int, err error) { 61 if !c.requestWritten { 62 buffer := buf.NewSize(M.SocksaddrSerializer.AddrPortLen(c.destination) + len(p)) 63 defer buffer.Release() 64 err = M.SocksaddrSerializer.WriteAddrPort(buffer, c.destination) 65 if err != nil { 66 return 67 } 68 common.Must1(buffer.Write(p)) 69 _, err = c.ExtendedConn.Write(buffer.Bytes()) 70 if err != nil { 71 return 72 } 73 c.requestWritten = true 74 n = len(p) 75 return 76 } 77 return c.ExtendedConn.Write(p) 78 } 79 80 func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error { 81 if !c.requestWritten { 82 header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(c.destination))) 83 err := M.SocksaddrSerializer.WriteAddrPort(header, c.destination) 84 if err != nil { 85 return err 86 } 87 c.requestWritten = true 88 } 89 return c.ExtendedConn.WriteBuffer(buffer) 90 } 91 92 func (c *noneConn) FrontHeadroom() int { 93 return M.MaxSocksaddrLength 94 } 95 96 func (c *noneConn) ReaderReplaceable() bool { 97 return true 98 } 99 100 func (c *noneConn) WriterReplaceable() bool { 101 return c.requestWritten 102 } 103 104 func (c *noneConn) Upstream() any { 105 return c.ExtendedConn 106 } 107 108 var ( 109 _ N.NetPacketConn = (*nonePacketConn)(nil) 110 _ N.FrontHeadroom = (*nonePacketConn)(nil) 111 _ common.WithUpstream = (*nonePacketConn)(nil) 112 ) 113 114 type nonePacketConn struct { 115 N.AbstractConn 116 conn N.ExtendedConn 117 } 118 119 func (c *nonePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { 120 n, err = c.conn.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.conn.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.conn.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.conn.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.conn 178 }