github.com/metacubex/sing-shadowsocks@v0.2.6/none.go (about) 1 package shadowsocks 2 3 import ( 4 "context" 5 "net" 6 "net/netip" 7 8 "github.com/sagernet/sing/common" 9 "github.com/sagernet/sing/common/buf" 10 M "github.com/sagernet/sing/common/metadata" 11 N "github.com/sagernet/sing/common/network" 12 "github.com/sagernet/sing/common/udpnat" 13 ) 14 15 const MethodNone = "none" 16 17 type NoneMethod struct{} 18 19 func NewNone() Method { 20 return &NoneMethod{} 21 } 22 23 func (m *NoneMethod) Name() string { 24 return MethodNone 25 } 26 27 func (m *NoneMethod) DialConn(conn net.Conn, destination M.Socksaddr) (net.Conn, error) { 28 shadowsocksConn := &noneConn{ 29 Conn: conn, 30 handshake: true, 31 destination: destination, 32 } 33 return shadowsocksConn, shadowsocksConn.clientHandshake() 34 } 35 36 func (m *NoneMethod) DialEarlyConn(conn net.Conn, destination M.Socksaddr) net.Conn { 37 return &noneConn{ 38 Conn: conn, 39 destination: destination, 40 } 41 } 42 43 func (m *NoneMethod) DialPacketConn(conn net.Conn) N.NetPacketConn { 44 return &nonePacketConn{conn} 45 } 46 47 type noneConn struct { 48 net.Conn 49 50 handshake bool 51 destination M.Socksaddr 52 } 53 54 func (c *noneConn) clientHandshake() error { 55 err := M.SocksaddrSerializer.WriteAddrPort(c.Conn, c.destination) 56 if err != nil { 57 return err 58 } 59 c.handshake = true 60 return nil 61 } 62 63 func (c *noneConn) Write(b []byte) (n int, err error) { 64 if c.handshake { 65 return c.Conn.Write(b) 66 } 67 err = M.SocksaddrSerializer.WriteAddrPort(c.Conn, c.destination) 68 if err != nil { 69 return 70 } 71 c.handshake = true 72 return c.Conn.Write(b) 73 } 74 75 func (c *noneConn) WriteBuffer(buffer *buf.Buffer) error { 76 defer buffer.Release() 77 if c.handshake { 78 return common.Error(c.Conn.Write(buffer.Bytes())) 79 } 80 81 header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(c.destination))) 82 err := M.SocksaddrSerializer.WriteAddrPort(header, c.destination) 83 if err != nil { 84 return err 85 } 86 c.handshake = true 87 return common.Error(c.Conn.Write(buffer.Bytes())) 88 } 89 90 func (c *noneConn) FrontHeadroom() int { 91 if !c.handshake { 92 return M.SocksaddrSerializer.AddrPortLen(c.destination) 93 } 94 return 0 95 } 96 97 func (c *noneConn) RemoteAddr() net.Addr { 98 return c.destination.TCPAddr() 99 } 100 101 func (c *noneConn) Upstream() any { 102 return c.Conn 103 } 104 105 func (c *noneConn) ReaderReplaceable() bool { 106 return true 107 } 108 109 func (c *noneConn) WriterReplaceable() bool { 110 return c.handshake 111 } 112 113 type nonePacketConn struct { 114 net.Conn 115 } 116 117 func (c *nonePacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) { 118 _, err := buffer.ReadOnceFrom(c) 119 if err != nil { 120 return M.Socksaddr{}, err 121 } 122 return M.SocksaddrSerializer.ReadAddrPort(buffer) 123 } 124 125 func (c *nonePacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { 126 defer buffer.Release() 127 header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination))) 128 err := M.SocksaddrSerializer.WriteAddrPort(header, destination) 129 if err != nil { 130 return err 131 } 132 return common.Error(buffer.WriteTo(c)) 133 } 134 135 func (c *nonePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { 136 n, err = c.Read(p) 137 if err != nil { 138 return 139 } 140 buffer := buf.With(p[:n]) 141 destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer) 142 if err != nil { 143 return 144 } 145 if destination.IsFqdn() { 146 addr = destination 147 } else { 148 addr = destination.UDPAddr() 149 } 150 n = copy(p, buffer.Bytes()) 151 return 152 } 153 154 func (c *nonePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { 155 destination := M.SocksaddrFromNet(addr) 156 buffer := buf.NewSize(M.SocksaddrSerializer.AddrPortLen(destination) + len(p)) 157 defer buffer.Release() 158 err = M.SocksaddrSerializer.WriteAddrPort(buffer, destination) 159 if err != nil { 160 return 161 } 162 _, err = buffer.Write(p) 163 if err != nil { 164 return 165 } 166 return len(p), nil 167 } 168 169 func (c *nonePacketConn) Headroom() int { 170 return M.MaxSocksaddrLength 171 } 172 173 type NoneService struct { 174 handler Handler 175 udpNat *udpnat.Service[netip.AddrPort] 176 } 177 178 func NewNoneService(udpTimeout int64, handler Handler) Service { 179 s := &NoneService{ 180 handler: handler, 181 } 182 s.udpNat = udpnat.New[netip.AddrPort](udpTimeout, handler) 183 return s 184 } 185 186 func (s *NoneService) Name() string { 187 return MethodNone 188 } 189 190 func (s *NoneService) Password() string { 191 return "" 192 } 193 194 func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { 195 destination, err := M.SocksaddrSerializer.ReadAddrPort(conn) 196 if err != nil { 197 return err 198 } 199 metadata.Protocol = "shadowsocks" 200 metadata.Destination = destination 201 return s.handler.NewConnection(ctx, conn, metadata) 202 } 203 204 func (s *NoneService) WriteIsThreadUnsafe() { 205 } 206 207 func (s *NoneService) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error { 208 destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer) 209 if err != nil { 210 return err 211 } 212 metadata.Protocol = "shadowsocks" 213 metadata.Destination = destination 214 s.udpNat.NewPacket(ctx, metadata.Source.AddrPort(), buffer, metadata, func(natConn N.PacketConn) N.PacketWriter { 215 return &nonePacketWriter{conn, natConn} 216 }) 217 return nil 218 } 219 220 type nonePacketWriter struct { 221 source N.PacketConn 222 nat N.PacketConn 223 } 224 225 func (w *nonePacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { 226 header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination))) 227 err := M.SocksaddrSerializer.WriteAddrPort(header, destination) 228 if err != nil { 229 buffer.Release() 230 return err 231 } 232 return w.source.WritePacket(buffer, M.SocksaddrFromNet(w.nat.LocalAddr())) 233 } 234 235 func (w *nonePacketWriter) Upstream() any { 236 return w.source 237 } 238 239 func (w *nonePacketWriter) FrontHeadroom() int { 240 return M.MaxSocksaddrLength 241 } 242 243 func (s *NoneService) NewError(ctx context.Context, err error) { 244 s.handler.NewError(ctx, err) 245 }