1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| package shadowsocksr
import ( "context" "errors" "fmt" "net"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter/outbound" "github.com/sagernet/sing-box/common/dialer" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/transport/clashssr/obfs" "github.com/sagernet/sing-box/transport/clashssr/protocol" "github.com/sagernet/sing/common/bufio" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/logger" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network"
"github.com/Dreamacro/clash/transport/shadowsocks/core" "github.com/Dreamacro/clash/transport/shadowsocks/shadowstream" "github.com/Dreamacro/clash/transport/socks5" )
func RegisterOutbound(registry *outbound.Registry) { outbound.Register[option.ShadowsocksROutboundOptions](registry, C.TypeShadowsocksR, NewOutbound) }
type Outbound struct { outbound.Adapter logger logger.ContextLogger dialer N.Dialer serverAddr M.Socksaddr cipher core.Cipher obfs obfs.Obfs protocol protocol.Protocol }
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksROutboundOptions) (adapter.Outbound, error) { outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain()) if err != nil { return nil, err } outbound := &Outbound{ logger: logger, Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowsocksR, tag, options.Network.Build(), options.DialerOptions), dialer: outboundDialer, serverAddr: options.ServerOptions.Build(), } var cipher string switch options.Method { case "none": cipher = "dummy" default: cipher = options.Method } outbound.cipher, err = core.PickCipher(cipher, nil, options.Password) if err != nil { return nil, err } var ( ivSize int key []byte ) if cipher == "dummy" { ivSize = 0 key = core.Kdf(options.Password, 16) } else { streamCipher, ok := outbound.cipher.(*core.StreamCipher) if !ok { return nil, fmt.Errorf("%s is not none or a supported stream cipher in ssr", cipher) } ivSize = streamCipher.IVSize() key = streamCipher.Key } obfs, obfsOverhead, err := obfs.PickObfs(options.Obfs, &obfs.Base{ Host: options.Server, Port: int(options.ServerPort), Key: key, IVSize: ivSize, Param: options.ObfsParam, }) if err != nil { return nil, E.Cause(err, "initialize obfs") } protocol, err := protocol.PickProtocol(options.Protocol, &protocol.Base{ Key: key, Overhead: obfsOverhead, Param: options.ProtocolParam, }) if err != nil { return nil, E.Cause(err, "initialize protocol") } outbound.obfs = obfs outbound.protocol = protocol
return outbound, nil }
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.Tag() metadata.Destination = destination switch network { case N.NetworkTCP: h.logger.InfoContext(ctx, "outbound connection to ", destination) conn, err := h.dialer.DialContext(ctx, network, h.serverAddr) if err != nil { return nil, err } conn = h.cipher.StreamConn(h.obfs.StreamConn(conn)) writeIv, err := conn.(*shadowstream.Conn).ObtainWriteIV() if err != nil { conn.Close() return nil, err } conn = h.protocol.StreamConn(conn, writeIv) err = M.SocksaddrSerializer.WriteAddrPort(conn, destination) if err != nil { conn.Close() return nil, E.Cause(err, "write request") } return conn, nil case N.NetworkUDP: conn, err := h.ListenPacket(ctx, destination) if err != nil { return nil, err } return bufio.NewBindPacketConn(conn, destination), nil default: return nil, E.Extend(N.ErrUnknownNetwork, network) } }
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.Tag() metadata.Destination = destination h.logger.InfoContext(ctx, "outbound packet connection to ", destination) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) if err != nil { return nil, err } packetConn := h.cipher.PacketConn(bufio.NewUnbindPacketConn(outConn)) packetConn = h.protocol.PacketConn(packetConn) packetConn = &ssPacketConn{packetConn, outConn.RemoteAddr()} return packetConn, nil }
func (h *Outbound) InterfaceUpdated() { return }
func (h *Outbound) Close() error { return nil }
type ssPacketConn struct { net.PacketConn rAddr net.Addr }
func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b) if err != nil { return } return spc.PacketConn.WriteTo(packet[3:], spc.rAddr) }
func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { n, _, e := spc.PacketConn.ReadFrom(b) if e != nil { return 0, nil, e }
addr := socks5.SplitAddr(b[:n]) if addr == nil { return 0, nil, errors.New("parse addr error") }
udpAddr := addr.UDPAddr() if udpAddr == nil { return 0, nil, errors.New("parse addr error") }
copy(b, b[len(addr):]) return n - len(addr), udpAddr, e }
|