|
| 1 | +package listeners |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + socks5Version = 0x05 |
| 14 | + socks5AuthNone = 0x00 |
| 15 | + socks5AuthUserPass = 0x02 |
| 16 | + socks5AuthNoMethods = 0xFF |
| 17 | + |
| 18 | + socks5CmdConnect = 0x01 |
| 19 | + |
| 20 | + socks5AtypIPv4 = 0x01 |
| 21 | + socks5AtypDomain = 0x03 |
| 22 | + socks5AtypIPv6 = 0x04 |
| 23 | + |
| 24 | + socks5ReplySucceeded = 0x00 |
| 25 | + socks5ReplyGeneralFailure = 0x01 |
| 26 | + socks5ReplyConnectionNotAllow = 0x02 |
| 27 | + socks5ReplyHostUnreachable = 0x04 |
| 28 | + socks5ReplyCommandNotSupport = 0x07 |
| 29 | + socks5ReplyAddressNotSupport = 0x08 |
| 30 | +) |
| 31 | + |
| 32 | +// SOCKS5AuthConfig controls handshake authentication behavior. |
| 33 | +type SOCKS5AuthConfig struct { |
| 34 | + Username string |
| 35 | + Password string |
| 36 | +} |
| 37 | + |
| 38 | +func (c SOCKS5AuthConfig) RequiresUserPass() bool { |
| 39 | + return c.Username != "" || c.Password != "" |
| 40 | +} |
| 41 | + |
| 42 | +// SOCKS5ConnectRequest is the parsed destination from a CONNECT request. |
| 43 | +type SOCKS5ConnectRequest struct { |
| 44 | + ATYP byte |
| 45 | + Host string |
| 46 | + Port int |
| 47 | + Target string |
| 48 | +} |
| 49 | + |
| 50 | +// PerformSOCKS5Handshake negotiates auth and parses a CONNECT request. |
| 51 | +func PerformSOCKS5Handshake(conn net.Conn, auth SOCKS5AuthConfig) (SOCKS5ConnectRequest, error) { |
| 52 | + reader := bufio.NewReader(conn) |
| 53 | + |
| 54 | + method, err := negotiateSOCKS5Method(reader, conn, auth) |
| 55 | + if err != nil { |
| 56 | + return SOCKS5ConnectRequest{}, err |
| 57 | + } |
| 58 | + if method == socks5AuthUserPass { |
| 59 | + if err := authenticateSOCKS5UserPass(reader, conn, auth); err != nil { |
| 60 | + return SOCKS5ConnectRequest{}, err |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + request, err := readSOCKS5ConnectRequest(reader) |
| 65 | + if err != nil { |
| 66 | + return SOCKS5ConnectRequest{}, err |
| 67 | + } |
| 68 | + return request, nil |
| 69 | +} |
| 70 | + |
| 71 | +// WriteSOCKS5ConnectReply writes a reply and bind address to the client. |
| 72 | +func WriteSOCKS5ConnectReply(conn net.Conn, status byte, bindAddr net.Addr) error { |
| 73 | + atyp := byte(socks5AtypIPv4) |
| 74 | + addrBytes := []byte{0, 0, 0, 0} |
| 75 | + portBytes := []byte{0, 0} |
| 76 | + |
| 77 | + host, port, err := splitHostPort(bindAddr) |
| 78 | + if err == nil { |
| 79 | + if ip := net.ParseIP(host); ip != nil { |
| 80 | + if v4 := ip.To4(); v4 != nil { |
| 81 | + atyp = socks5AtypIPv4 |
| 82 | + addrBytes = v4 |
| 83 | + } else { |
| 84 | + atyp = socks5AtypIPv6 |
| 85 | + addrBytes = ip.To16() |
| 86 | + } |
| 87 | + } else { |
| 88 | + atyp = socks5AtypDomain |
| 89 | + addrBytes = append([]byte{byte(len(host))}, []byte(host)...) |
| 90 | + } |
| 91 | + portBytes = []byte{byte(port >> 8), byte(port)} |
| 92 | + } |
| 93 | + |
| 94 | + reply := []byte{socks5Version, status, 0x00, atyp} |
| 95 | + reply = append(reply, addrBytes...) |
| 96 | + reply = append(reply, portBytes...) |
| 97 | + _, err = conn.Write(reply) |
| 98 | + return err |
| 99 | +} |
| 100 | + |
| 101 | +func negotiateSOCKS5Method(reader *bufio.Reader, conn net.Conn, auth SOCKS5AuthConfig) (byte, error) { |
| 102 | + header := make([]byte, 2) |
| 103 | + if _, err := io.ReadFull(reader, header); err != nil { |
| 104 | + return 0, err |
| 105 | + } |
| 106 | + if header[0] != socks5Version { |
| 107 | + return 0, fmt.Errorf("unsupported socks version %d", header[0]) |
| 108 | + } |
| 109 | + |
| 110 | + methods := make([]byte, int(header[1])) |
| 111 | + if _, err := io.ReadFull(reader, methods); err != nil { |
| 112 | + return 0, err |
| 113 | + } |
| 114 | + |
| 115 | + wantMethod := byte(socks5AuthNone) |
| 116 | + if auth.RequiresUserPass() { |
| 117 | + wantMethod = socks5AuthUserPass |
| 118 | + } |
| 119 | + |
| 120 | + for _, method := range methods { |
| 121 | + if method == wantMethod { |
| 122 | + if _, err := conn.Write([]byte{socks5Version, wantMethod}); err != nil { |
| 123 | + return 0, err |
| 124 | + } |
| 125 | + return wantMethod, nil |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + _, _ = conn.Write([]byte{socks5Version, socks5AuthNoMethods}) |
| 130 | + return 0, errors.New("no compatible socks5 auth method") |
| 131 | +} |
| 132 | + |
| 133 | +func authenticateSOCKS5UserPass(reader *bufio.Reader, conn net.Conn, auth SOCKS5AuthConfig) error { |
| 134 | + header := make([]byte, 2) |
| 135 | + if _, err := io.ReadFull(reader, header); err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + if header[0] != 0x01 { |
| 139 | + _, _ = conn.Write([]byte{0x01, 0x01}) |
| 140 | + return fmt.Errorf("unsupported auth version %d", header[0]) |
| 141 | + } |
| 142 | + |
| 143 | + user := make([]byte, int(header[1])) |
| 144 | + if _, err := io.ReadFull(reader, user); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + plen := make([]byte, 1) |
| 148 | + if _, err := io.ReadFull(reader, plen); err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + pass := make([]byte, int(plen[0])) |
| 152 | + if _, err := io.ReadFull(reader, pass); err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + if string(user) != auth.Username || string(pass) != auth.Password { |
| 157 | + _, _ = conn.Write([]byte{0x01, 0x01}) |
| 158 | + return errors.New("invalid socks5 username/password") |
| 159 | + } |
| 160 | + |
| 161 | + _, err := conn.Write([]byte{0x01, 0x00}) |
| 162 | + return err |
| 163 | +} |
| 164 | + |
| 165 | +func readSOCKS5ConnectRequest(reader *bufio.Reader) (SOCKS5ConnectRequest, error) { |
| 166 | + head := make([]byte, 4) |
| 167 | + if _, err := io.ReadFull(reader, head); err != nil { |
| 168 | + return SOCKS5ConnectRequest{}, err |
| 169 | + } |
| 170 | + if head[0] != socks5Version { |
| 171 | + return SOCKS5ConnectRequest{}, fmt.Errorf("unsupported request version %d", head[0]) |
| 172 | + } |
| 173 | + if head[1] != socks5CmdConnect { |
| 174 | + return SOCKS5ConnectRequest{}, fmt.Errorf("unsupported socks5 command %d", head[1]) |
| 175 | + } |
| 176 | + |
| 177 | + host, err := readSOCKS5Address(reader, head[3]) |
| 178 | + if err != nil { |
| 179 | + return SOCKS5ConnectRequest{}, err |
| 180 | + } |
| 181 | + portBytes := make([]byte, 2) |
| 182 | + if _, err := io.ReadFull(reader, portBytes); err != nil { |
| 183 | + return SOCKS5ConnectRequest{}, err |
| 184 | + } |
| 185 | + port := int(portBytes[0])<<8 | int(portBytes[1]) |
| 186 | + |
| 187 | + return SOCKS5ConnectRequest{ |
| 188 | + ATYP: head[3], |
| 189 | + Host: host, |
| 190 | + Port: port, |
| 191 | + Target: net.JoinHostPort(host, strconv.Itoa(port)), |
| 192 | + }, nil |
| 193 | +} |
| 194 | + |
| 195 | +func readSOCKS5Address(reader *bufio.Reader, atyp byte) (string, error) { |
| 196 | + switch atyp { |
| 197 | + case socks5AtypIPv4: |
| 198 | + buf := make([]byte, 4) |
| 199 | + if _, err := io.ReadFull(reader, buf); err != nil { |
| 200 | + return "", err |
| 201 | + } |
| 202 | + return net.IP(buf).String(), nil |
| 203 | + case socks5AtypDomain: |
| 204 | + size := make([]byte, 1) |
| 205 | + if _, err := io.ReadFull(reader, size); err != nil { |
| 206 | + return "", err |
| 207 | + } |
| 208 | + buf := make([]byte, int(size[0])) |
| 209 | + if _, err := io.ReadFull(reader, buf); err != nil { |
| 210 | + return "", err |
| 211 | + } |
| 212 | + return string(buf), nil |
| 213 | + case socks5AtypIPv6: |
| 214 | + buf := make([]byte, 16) |
| 215 | + if _, err := io.ReadFull(reader, buf); err != nil { |
| 216 | + return "", err |
| 217 | + } |
| 218 | + return net.IP(buf).String(), nil |
| 219 | + default: |
| 220 | + return "", fmt.Errorf("unsupported socks5 atyp %d", atyp) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +func splitHostPort(addr net.Addr) (string, int, error) { |
| 225 | + if addr == nil { |
| 226 | + return "", 0, errors.New("nil addr") |
| 227 | + } |
| 228 | + host, portStr, err := net.SplitHostPort(addr.String()) |
| 229 | + if err != nil { |
| 230 | + return "", 0, err |
| 231 | + } |
| 232 | + port, err := strconv.Atoi(portStr) |
| 233 | + if err != nil { |
| 234 | + return "", 0, err |
| 235 | + } |
| 236 | + return host, port, nil |
| 237 | +} |
0 commit comments