Skip to content

Commit f20ae7f

Browse files
committed
Fix SOCKS IPv6 address parsing
1 parent cfe1c3b commit f20ae7f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/server/socks-server.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,28 @@ export function buildSocksServer(options: SocksServerOptions): SocksServer {
222222

223223
let address: SocksTcpAddress;
224224

225-
if (addressType === 0x1) {
225+
if (addressType === 0x1) { // IPv4
226226
const addressData = await readBytes(socket, 6);
227227
const ip = addressData.subarray(0, 4).join('.');
228228
const port = addressData.readUInt16BE(4);
229229
address = { type: 'ipv4', ip, port };
230-
} else if (addressType === 0x3) {
230+
} else if (addressType === 0x3) { // DNS
231231
const nameLength = await readBytes(socket, 1);
232232
const nameAndPortData = await readBytes(socket, nameLength[0] + 2);
233233
const name = nameAndPortData.subarray(0, nameLength[0]).toString('utf8');
234234
const port = nameAndPortData.readUInt16BE(nameLength[0]);
235235
address = { type: 'hostname', hostname: name, port };
236-
} else if (addressType === 0x4) {
236+
} else if (addressType === 0x4) { // IPv6
237237
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+
239247
const port = addressData.readUInt16BE(16);
240248
address = { type: 'ipv6', ip, port };
241249
} else {

0 commit comments

Comments
 (0)