@@ -222,20 +222,28 @@ export function buildSocksServer(options: SocksServerOptions): SocksServer {
222
222
223
223
let address : SocksTcpAddress ;
224
224
225
- if ( addressType === 0x1 ) {
225
+ if ( addressType === 0x1 ) { // IPv4
226
226
const addressData = await readBytes ( socket , 6 ) ;
227
227
const ip = addressData . subarray ( 0 , 4 ) . join ( '.' ) ;
228
228
const port = addressData . readUInt16BE ( 4 ) ;
229
229
address = { type : 'ipv4' , ip, port } ;
230
- } else if ( addressType === 0x3 ) {
230
+ } else if ( addressType === 0x3 ) { // DNS
231
231
const nameLength = await readBytes ( socket , 1 ) ;
232
232
const nameAndPortData = await readBytes ( socket , nameLength [ 0 ] + 2 ) ;
233
233
const name = nameAndPortData . subarray ( 0 , nameLength [ 0 ] ) . toString ( 'utf8' ) ;
234
234
const port = nameAndPortData . readUInt16BE ( nameLength [ 0 ] ) ;
235
235
address = { type : 'hostname' , hostname : name , port } ;
236
- } else if ( addressType === 0x4 ) {
236
+ } else if ( addressType === 0x4 ) { // IPv6
237
237
const addressData = await readBytes ( socket , 18 ) ;
238
- const ip = addressData . subarray ( 0 , 16 ) . join ( ':' ) ;
238
+
239
+ const ipv6Bytes = addressData . subarray ( 0 , 16 ) ;
240
+ const hextets = [ ] ;
241
+ for ( let i = 0 ; i < ipv6Bytes . length ; i += 2 ) {
242
+ const hextet = ( ( ipv6Bytes [ i ] << 8 ) | ipv6Bytes [ i + 1 ] ) . toString ( 16 ) ;
243
+ hextets . push ( hextet ) ;
244
+ }
245
+ const ip = hextets . join ( ':' ) ;
246
+
239
247
const port = addressData . readUInt16BE ( 16 ) ;
240
248
address = { type : 'ipv6' , ip, port } ;
241
249
} else {
0 commit comments