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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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
'use strict'

const { EventEmitter } = require('node:events')
const { Buffer } = require('node:buffer')
const { InvalidArgumentError, Socks5ProxyError } = require('./errors')
const { debuglog } = require('node:util')
const { parseAddress } = require('./socks5-utils')

const debug = debuglog('undici:socks5')
const EMPTY_BUFFER = Buffer.alloc(0)

// SOCKS5 constants
const SOCKS_VERSION = 0x05

// Authentication methods
const AUTH_METHODS = {
  NO_AUTH: 0x00,
  GSSAPI: 0x01,
  USERNAME_PASSWORD: 0x02,
  NO_ACCEPTABLE: 0xFF
}

// SOCKS5 commands
const COMMANDS = {
  CONNECT: 0x01,
  BIND: 0x02,
  UDP_ASSOCIATE: 0x03
}

// Address types
const ADDRESS_TYPES = {
  IPV4: 0x01,
  DOMAIN: 0x03,
  IPV6: 0x04
}

// Reply codes
const REPLY_CODES = {
  SUCCEEDED: 0x00,
  GENERAL_FAILURE: 0x01,
  CONNECTION_NOT_ALLOWED: 0x02,
  NETWORK_UNREACHABLE: 0x03,
  HOST_UNREACHABLE: 0x04,
  CONNECTION_REFUSED: 0x05,
  TTL_EXPIRED: 0x06,
  COMMAND_NOT_SUPPORTED: 0x07,
  ADDRESS_TYPE_NOT_SUPPORTED: 0x08
}

// State machine states
const STATES = {
  INITIAL: 'initial',
  HANDSHAKING: 'handshaking',
  AUTHENTICATING: 'authenticating',
  AUTHENTICATED: 'authenticated',
  CONNECTING: 'connecting',
  CONNECTED: 'connected',
  ERROR: 'error',
  CLOSED: 'closed'
}

/**
 * SOCKS5 client implementation
 * Handles SOCKS5 protocol negotiation and connection establishment
 */
class Socks5Client extends EventEmitter {
  constructor (socket, options = {}) {
    super()

    if (!socket) {
      throw new InvalidArgumentError('socket is required')
    }

    this.socket = socket
    this.options = options
    this.state = STATES.INITIAL
    this.buffer = EMPTY_BUFFER
    this.onSocketData = this.onData.bind(this)
    this.onSocketError = this.onError.bind(this)
    this.onSocketClose = this.onClose.bind(this)

    // Authentication settings
    this.authMethods = []
    if (options.username && options.password) {
      this.authMethods.push(AUTH_METHODS.USERNAME_PASSWORD)
    }
    this.authMethods.push(AUTH_METHODS.NO_AUTH)

    // Socket event handlers
    this.socket.on('data', this.onSocketData)
    this.socket.on('error', this.onSocketError)
    this.socket.on('close', this.onSocketClose)
  }

  /**
   * Handle incoming data from the socket
   */
  onData (data) {
    debug('received data', data.length, 'bytes in state', this.state)
    this.buffer = Buffer.concat([this.buffer, data])

    try {
      switch (this.state) {
        case STATES.HANDSHAKING:
          this.handleHandshakeResponse()
          break
        case STATES.AUTHENTICATING:
          this.handleAuthResponse()
          break
        case STATES.CONNECTING:
          this.handleConnectResponse()
          break
      }
    } catch (err) {
      this.onError(err)
    }
  }

  /**
   * Handle socket errors
   */
  onError (err) {
    debug('socket error', err)
    this.state = STATES.ERROR
    this.emit('error', err)
    this.destroy()
  }

  /**
   * Handle socket close
   */
  onClose () {
    debug('socket closed')
    this.state = STATES.CLOSED
    this.emit('close')
  }

  /**
   * Destroy the client and underlying socket
   */
  destroy () {
    if (this.socket && !this.socket.destroyed) {
      this.socket.destroy()
    }
  }

  markAuthenticated () {
    this.state = STATES.AUTHENTICATED
    this.emit('authenticated')
  }

  /**
   * Start the SOCKS5 handshake
   */
  handshake () {
    if (this.state !== STATES.INITIAL) {
      throw new InvalidArgumentError('Handshake already started')
    }

    debug('starting handshake with', this.authMethods.length, 'auth methods')
    this.state = STATES.HANDSHAKING

    // Build handshake request
    // +----+----------+----------+
    // |VER | NMETHODS | METHODS  |
    // +----+----------+----------+
    // | 1  |    1     | 1 to 255 |
    // +----+----------+----------+
    const request = Buffer.alloc(2 + this.authMethods.length)
    request[0] = SOCKS_VERSION
    request[1] = this.authMethods.length
    this.authMethods.forEach((method, i) => {
      request[2 + i] = method
    })

    this.socket.write(request)
  }

  /**
   * Handle handshake response from server
   */
  handleHandshakeResponse () {
    if (this.buffer.length < 2) {
      return // Not enough data yet
    }

    const version = this.buffer[0]
    const method = this.buffer[1]

    if (version !== SOCKS_VERSION) {
      throw new Socks5ProxyError(`Invalid SOCKS version: ${version}`, 'UND_ERR_SOCKS5_VERSION')
    }

    if (method === AUTH_METHODS.NO_ACCEPTABLE) {
      throw new Socks5ProxyError('No acceptable authentication method', 'UND_ERR_SOCKS5_AUTH_REJECTED')
    }

    this.buffer = this.buffer.subarray(2)
    debug('server selected auth method', method)

    if (method === AUTH_METHODS.NO_AUTH) {
      this.markAuthenticated()
    } else if (method === AUTH_METHODS.USERNAME_PASSWORD) {
      this.state = STATES.AUTHENTICATING
      this.sendAuthRequest()
    } else {
      throw new Socks5ProxyError(`Unsupported authentication method: ${method}`, 'UND_ERR_SOCKS5_AUTH_METHOD')
    }
  }

  /**
   * Send username/password authentication request
   */
  sendAuthRequest () {
    const { username, password } = this.options

    if (!username || !password) {
      throw new InvalidArgumentError('Username and password required for authentication')
    }

    debug('sending username/password auth')

    // Username/Password authentication request (RFC 1929)
    // +----+------+----------+------+----------+
    // |VER | ULEN |  UNAME   | PLEN |  PASSWD  |
    // +----+------+----------+------+----------+
    // | 1  |  1   | 1 to 255 |  1   | 1 to 255 |
    // +----+------+----------+------+----------+
    const usernameBuffer = Buffer.from(username)
    const passwordBuffer = Buffer.from(password)

    if (usernameBuffer.length > 255 || passwordBuffer.length > 255) {
      throw new InvalidArgumentError('Username or password too long')
    }

    const request = Buffer.alloc(3 + usernameBuffer.length + passwordBuffer.length)
    request[0] = 0x01 // Sub-negotiation version
    request[1] = usernameBuffer.length
    usernameBuffer.copy(request, 2)
    request[2 + usernameBuffer.length] = passwordBuffer.length
    passwordBuffer.copy(request, 3 + usernameBuffer.length)

    this.socket.write(request)
  }

  /**
   * Handle authentication response
   */
  handleAuthResponse () {
    if (this.buffer.length < 2) {
      return // Not enough data yet
    }

    const version = this.buffer[0]
    const status = this.buffer[1]

    if (version !== 0x01) {
      throw new Socks5ProxyError(`Invalid auth sub-negotiation version: ${version}`, 'UND_ERR_SOCKS5_AUTH_VERSION')
    }

    if (status !== 0x00) {
      throw new Socks5ProxyError('Authentication failed', 'UND_ERR_SOCKS5_AUTH_FAILED')
    }

    this.buffer = this.buffer.subarray(2)
    debug('authentication successful')
    this.markAuthenticated()
  }

  /**
   * Send CONNECT command
   * @param {string} address - Target address (IP or domain)
   * @param {number} port - Target port
   */
  connect (address, port) {
    if (this.state === STATES.CONNECTING || this.state === STATES.CONNECTED) {
      throw new InvalidArgumentError('Connection already in progress')
    }

    if (this.state !== STATES.AUTHENTICATED) {
      throw new InvalidArgumentError('Client must be authenticated before CONNECT')
    }

    debug('connecting to', address, port)
    this.state = STATES.CONNECTING

    const request = this.buildConnectRequest(COMMANDS.CONNECT, address, port)
    this.socket.write(request)
  }

  /**
   * Build a SOCKS5 request
   */
  buildConnectRequest (command, address, port) {
    // Parse address to determine type and buffer
    const { type: addressType, buffer: addressBuffer } = parseAddress(address)

    // Build request
    // +----+-----+-------+------+----------+----------+
    // |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
    // +----+-----+-------+------+----------+----------+
    // | 1  |  1  | X'00' |  1   | Variable |    2     |
    // +----+-----+-------+------+----------+----------+
    const request = Buffer.alloc(4 + addressBuffer.length + 2)
    request[0] = SOCKS_VERSION
    request[1] = command
    request[2] = 0x00 // Reserved
    request[3] = addressType
    addressBuffer.copy(request, 4)
    request.writeUInt16BE(port, 4 + addressBuffer.length)

    return request
  }

  /**
   * Handle CONNECT response
   */
  handleConnectResponse () {
    if (this.buffer.length < 4) {
      return // Not enough data for header
    }

    const version = this.buffer[0]
    const reply = this.buffer[1]
    const addressType = this.buffer[3]

    if (version !== SOCKS_VERSION) {
      throw new Socks5ProxyError(`Invalid SOCKS version in reply: ${version}`, 'UND_ERR_SOCKS5_REPLY_VERSION')
    }

    // Calculate the expected response length
    let responseLength = 4 // VER + REP + RSV + ATYP
    if (addressType === ADDRESS_TYPES.IPV4) {
      responseLength += 4 + 2 // IPv4 + port
    } else if (addressType === ADDRESS_TYPES.DOMAIN) {
      if (this.buffer.length < 5) {
        return // Need domain length byte
      }
      responseLength += 1 + this.buffer[4] + 2 // length byte + domain + port
    } else if (addressType === ADDRESS_TYPES.IPV6) {
      responseLength += 16 + 2 // IPv6 + port
    } else {
      throw new Socks5ProxyError(`Invalid address type in reply: ${addressType}`, 'UND_ERR_SOCKS5_ADDR_TYPE')
    }

    if (this.buffer.length < responseLength) {
      return // Not enough data for full response
    }

    if (reply !== REPLY_CODES.SUCCEEDED) {
      const errorMessage = this.getReplyErrorMessage(reply)
      throw new Socks5ProxyError(`SOCKS5 connection failed: ${errorMessage}`, `UND_ERR_SOCKS5_REPLY_${reply}`)
    }

    // Parse bound address and port
    let boundAddress
    let offset = 4

    if (addressType === ADDRESS_TYPES.IPV4) {
      boundAddress = Array.from(this.buffer.subarray(offset, offset + 4)).join('.')
      offset += 4
    } else if (addressType === ADDRESS_TYPES.DOMAIN) {
      const domainLength = this.buffer[offset]
      offset += 1
      boundAddress = this.buffer.subarray(offset, offset + domainLength).toString()
      offset += domainLength
    } else if (addressType === ADDRESS_TYPES.IPV6) {
      // Parse IPv6 address from 16-byte buffer
      const parts = []
      for (let i = 0; i < 8; i++) {
        const value = this.buffer.readUInt16BE(offset + i * 2)
        parts.push(value.toString(16))
      }
      boundAddress = parts.join(':')
      offset += 16
    }

    const boundPort = this.buffer.readUInt16BE(offset)

    this.buffer = EMPTY_BUFFER
    this.state = STATES.CONNECTED
    this.socket.removeListener('data', this.onSocketData)

    debug('connected, bound address:', boundAddress, 'port:', boundPort)
    this.emit('connected', { address: boundAddress, port: boundPort })
  }

  /**
   * Get human-readable error message for reply code
   */
  getReplyErrorMessage (reply) {
    switch (reply) {
      case REPLY_CODES.GENERAL_FAILURE:
        return 'General SOCKS server failure'
      case REPLY_CODES.CONNECTION_NOT_ALLOWED:
        return 'Connection not allowed by ruleset'
      case REPLY_CODES.NETWORK_UNREACHABLE:
        return 'Network unreachable'
      case REPLY_CODES.HOST_UNREACHABLE:
        return 'Host unreachable'
      case REPLY_CODES.CONNECTION_REFUSED:
        return 'Connection refused'
      case REPLY_CODES.TTL_EXPIRED:
        return 'TTL expired'
      case REPLY_CODES.COMMAND_NOT_SUPPORTED:
        return 'Command not supported'
      case REPLY_CODES.ADDRESS_TYPE_NOT_SUPPORTED:
        return 'Address type not supported'
      default:
        return `Unknown error code: ${reply}`
    }
  }
}

module.exports = {
  Socks5Client,
  AUTH_METHODS,
  COMMANDS,
  ADDRESS_TYPES,
  REPLY_CODES,
  STATES
}