|
| 1 | +package socks5 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "strconv" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type UDPUpstream struct { |
| 14 | + ctrl net.Conn |
| 15 | + relay net.Conn |
| 16 | + hdr []byte |
| 17 | +} |
| 18 | + |
| 19 | +func DialUpstreamUDP(ctx context.Context, cfg ClientConfig, dstIP net.IP, dstPort int) (*UDPUpstream, error) { |
| 20 | + if cfg.Host == "" || cfg.Port < 1 || cfg.Port > 65535 { |
| 21 | + return nil, fmt.Errorf("invalid upstream config") |
| 22 | + } |
| 23 | + if dstPort < 1 || dstPort > 65535 { |
| 24 | + return nil, fmt.Errorf("invalid target port") |
| 25 | + } |
| 26 | + if dstIP == nil { |
| 27 | + return nil, fmt.Errorf("invalid target ip") |
| 28 | + } |
| 29 | + |
| 30 | + timeout := cfg.Timeout |
| 31 | + if timeout <= 0 { |
| 32 | + timeout = dialTimeout |
| 33 | + } |
| 34 | + |
| 35 | + d := net.Dialer{Timeout: timeout} |
| 36 | + ApplyBypassMark(&d, cfg.BypassMark) |
| 37 | + ctrlAddr := net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port)) |
| 38 | + ctrl, err := d.DialContext(ctx, "tcp", ctrlAddr) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("dial upstream: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + _ = ctrl.SetDeadline(time.Now().Add(timeout)) |
| 44 | + if err := clientGreet(ctrl, cfg.Username, cfg.Password); err != nil { |
| 45 | + ctrl.Close() |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + relayHost, relayPort, err := clientUDPAssociate(ctrl) |
| 49 | + if err != nil { |
| 50 | + ctrl.Close() |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + _ = ctrl.SetDeadline(time.Time{}) |
| 54 | + |
| 55 | + if ip := net.ParseIP(relayHost); ip == nil || ip.IsUnspecified() { |
| 56 | + relayHost = cfg.Host |
| 57 | + } |
| 58 | + |
| 59 | + ud := net.Dialer{Timeout: timeout} |
| 60 | + ApplyBypassMark(&ud, cfg.BypassMark) |
| 61 | + relayAddr := net.JoinHostPort(relayHost, strconv.Itoa(relayPort)) |
| 62 | + relay, err := ud.DialContext(ctx, "udp", relayAddr) |
| 63 | + if err != nil { |
| 64 | + ctrl.Close() |
| 65 | + return nil, fmt.Errorf("dial relay: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + u := &UDPUpstream{ |
| 69 | + ctrl: ctrl, |
| 70 | + relay: relay, |
| 71 | + hdr: buildUDPHeader(&net.UDPAddr{IP: dstIP, Port: dstPort}), |
| 72 | + } |
| 73 | + go u.watchCtrl() |
| 74 | + return u, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (u *UDPUpstream) watchCtrl() { |
| 78 | + buf := make([]byte, 1) |
| 79 | + _, _ = u.ctrl.Read(buf) |
| 80 | + u.Close() |
| 81 | +} |
| 82 | + |
| 83 | +func (u *UDPUpstream) Write(payload []byte) (int, error) { |
| 84 | + pkt := make([]byte, 0, len(u.hdr)+len(payload)) |
| 85 | + pkt = append(pkt, u.hdr...) |
| 86 | + pkt = append(pkt, payload...) |
| 87 | + if _, err := u.relay.Write(pkt); err != nil { |
| 88 | + return 0, err |
| 89 | + } |
| 90 | + return len(payload), nil |
| 91 | +} |
| 92 | + |
| 93 | +func (u *UDPUpstream) Read(buf []byte) (int, error) { |
| 94 | + n, err := u.relay.Read(buf) |
| 95 | + if err != nil { |
| 96 | + return 0, err |
| 97 | + } |
| 98 | + if n < 4 { |
| 99 | + return 0, fmt.Errorf("short datagram") |
| 100 | + } |
| 101 | + if buf[0] != 0 || buf[1] != 0 { |
| 102 | + return 0, fmt.Errorf("bad rsv") |
| 103 | + } |
| 104 | + if buf[2] != 0 { |
| 105 | + return 0, fmt.Errorf("fragmented datagram") |
| 106 | + } |
| 107 | + _, off, perr := parseUDPAddress(buf[:n]) |
| 108 | + if perr != nil { |
| 109 | + return 0, perr |
| 110 | + } |
| 111 | + copy(buf, buf[off:n]) |
| 112 | + return n - off, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (u *UDPUpstream) SetReadDeadline(t time.Time) error { |
| 116 | + return u.relay.SetReadDeadline(t) |
| 117 | +} |
| 118 | + |
| 119 | +func (u *UDPUpstream) Close() error { |
| 120 | + if u.ctrl != nil { |
| 121 | + u.ctrl.Close() |
| 122 | + } |
| 123 | + if u.relay != nil { |
| 124 | + return u.relay.Close() |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func clientUDPAssociate(conn net.Conn) (host string, port int, err error) { |
| 130 | + req := []byte{socks5Version, cmdUDPAssociate, 0x00, atypIPv4, 0, 0, 0, 0, 0, 0} |
| 131 | + if _, err := conn.Write(req); err != nil { |
| 132 | + return "", 0, fmt.Errorf("udp associate write: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + head := make([]byte, 4) |
| 136 | + if _, err := io.ReadFull(conn, head); err != nil { |
| 137 | + return "", 0, fmt.Errorf("udp associate reply head: %w", err) |
| 138 | + } |
| 139 | + if head[0] != socks5Version { |
| 140 | + return "", 0, fmt.Errorf("upstream bad version in reply: %d", head[0]) |
| 141 | + } |
| 142 | + if head[1] != repSuccess { |
| 143 | + return "", 0, fmt.Errorf("upstream udp associate rejected: code=%d", head[1]) |
| 144 | + } |
| 145 | + |
| 146 | + var hostBuf []byte |
| 147 | + switch head[3] { |
| 148 | + case atypIPv4: |
| 149 | + hostBuf = make([]byte, 4) |
| 150 | + case atypIPv6: |
| 151 | + hostBuf = make([]byte, 16) |
| 152 | + case atypDomain: |
| 153 | + l := make([]byte, 1) |
| 154 | + if _, err := io.ReadFull(conn, l); err != nil { |
| 155 | + return "", 0, fmt.Errorf("udp associate reply addr len: %w", err) |
| 156 | + } |
| 157 | + hostBuf = make([]byte, int(l[0])) |
| 158 | + default: |
| 159 | + return "", 0, fmt.Errorf("upstream bad atyp in reply: %d", head[3]) |
| 160 | + } |
| 161 | + if _, err := io.ReadFull(conn, hostBuf); err != nil { |
| 162 | + return "", 0, fmt.Errorf("udp associate reply addr: %w", err) |
| 163 | + } |
| 164 | + portBuf := make([]byte, 2) |
| 165 | + if _, err := io.ReadFull(conn, portBuf); err != nil { |
| 166 | + return "", 0, fmt.Errorf("udp associate reply port: %w", err) |
| 167 | + } |
| 168 | + port = int(binary.BigEndian.Uint16(portBuf)) |
| 169 | + |
| 170 | + switch head[3] { |
| 171 | + case atypIPv4, atypIPv6: |
| 172 | + host = net.IP(hostBuf).String() |
| 173 | + case atypDomain: |
| 174 | + host = string(hostBuf) |
| 175 | + } |
| 176 | + return host, port, nil |
| 177 | +} |
0 commit comments