Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
remoteHost = {
host: buff.readString(hostLength),
port: buff.readUInt16BE()
};
// IPv6
}
else if (addressType === constants_1.Socks5HostType.IPv6) {
// Check if data is available.
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
if (this._receiveBuffer.length < dataNeeded) {
this._nextRequiredPacketBufferSize = dataNeeded;
return;
}
buff = smart_buffer_1.SmartBuffer.fromBuffer(this._receiveBuffer.get(dataNeeded).slice(4));
remoteHost = {
host: ip.toString(buff.readBuffer(16)),
port: buff.readUInt16BE()
};
}
this.state = constants_1.SocksClientState.Established;
this.removeInternalSocketHandlers();
this.emit('established', { socket: this._socket, remoteHost });
}
}
get socksClientOptions() {
onMessage (buffer, src) {
this.countRcv++
this.debug('< message %d bytes from %s', buffer.length, src)
if (buffer.length < 36) {
return
} // Less than ip header + sctp header
const headerLength = (buffer[0] & 0x0F) << 2
// Const protocol = buffer[9]
const dst = ip.toString(buffer, 16, 4)
const packetLength = readLength(buffer)
if (!checkLength(buffer, headerLength, packetLength)) {
return
}
this.debug('< ip packet ok %s <- %s', dst, src)
const payload = buffer.slice(headerLength)
const packet = Packet.fromBuffer(payload)
this.receivePacket(packet, src, dst)
}
function decode_attr_xor_mapped_addr(buffer, start, end) {
var family = (buffer.readUInt16BE(start) === 0x02) ? 6 : 4;
// xor the port against the magic key
var port = buffer.readUInt16BE(start + 2) ^
buffer.readUInt16BE(stun.XOR_KEY_OFFSET);
// xor the address against magic key and tid
var addr_buf = buffer.slice(start + 4, end);
var xor_buf = Buffer.allocUnsafe(addr_buf.length);
var k = stun.XOR_KEY_OFFSET;
for (var i = 0; i < xor_buf.length; ++i) {
xor_buf[i] = addr_buf[i] ^ buffer[k];
k += 1;
}
var address = ip_module.toString(xor_buf, 0, family);
return {
family: 'IPv' + family,
port: port,
address: address
};
}
function convert(str) {
str = ip.toString(ip.toBuffer(str)); //TODO: validate better.
return str; // already ip address
}
decodeHeader({ buffer, fail }) {
if (buffer.length < 7) {
return fail(`invalid length: ${buffer.length}`);
}
const atyp = buffer[0];
let addr; // string
let port; // number
let offset = 3;
switch (atyp) {
case ATYP_V4:
addr = ip.toString(buffer.slice(1, 5));
port = buffer.slice(5, 7).readUInt16BE(0);
offset += 4;
break;
case ATYP_V6:
if (buffer.length < 19) {
return fail(`invalid length: ${buffer.length}`);
}
addr = ip.toString(buffer.slice(1, 17));
port = buffer.slice(17, 19).readUInt16BE(0);
offset += 16;
break;
case ATYP_DOMAIN: {
const domainLen = buffer[1];
if (buffer.length < domainLen + 4) {
return fail(`invalid length: ${buffer.length}`);
}
option.code = buf.readUInt16BE(offset)
option.type = optioncodes.toString(option.code)
offset += 2
const len = buf.readUInt16BE(offset)
offset += 2
option.data = buf.slice(offset, offset + len)
switch (option.code) {
// case 3: NSID. No decode makes sense.
case 8: // ECS
option.family = buf.readUInt16BE(offset)
offset += 2
option.sourcePrefixLength = buf.readUInt8(offset++)
option.scopePrefixLength = buf.readUInt8(offset++)
const padded = Buffer.alloc((option.family === 1) ? 4 : 16)
buf.copy(padded, 0, offset, offset + len - 4)
option.ip = ip.toString(padded)
break
// case 12: Padding. No decode makes sense.
case 11: // KEEP-ALIVE
if (len > 0) {
option.timeout = buf.readUInt16BE(offset)
offset += 2
}
break
case 14:
option.tags = []
for (let i = 0; i < len; i += 2) {
option.tags.push(buf.readUInt16BE(offset))
offset += 2
}
// don't worry about default. caller will use data if desired
}
static from(type, message, owner) {
const packet = StunAddressAttribute.decode(message);
const port = xorPort(packet.port);
const address = xorIP(ip.toString(packet.address), owner);
const attribute = new StunXorAddressAttribute(type, address, port);
attribute.setOwner(owner);
return attribute;
}
onHeartbeatAck (chunk) {
this.debugger.trace(
'< HEARTBEAT ACK',
chunk.heartbeat_info.length,
chunk.heartbeat_info
)
/*
Upon receipt of the HEARTBEAT ACK, a verification is made that the
nonce included in the HEARTBEAT parameter is the one sent to the
address indicated inside the HEARTBEAT parameter. When this match
occurs, the address that the original HEARTBEAT was sent to is now
considered CONFIRMED and available for normal data transfer.
*/
const nonce = chunk.heartbeat_info.readUInt32BE(0)
if (this.nonces[nonce]) {
const address = ip.toString(chunk.heartbeat_info, 8, 4)
this.debugger.trace('address confirmed alive', address)
}
delete this.nonces[nonce]
}
function xorIP(address, owner) {
let xored = null;
if (net.isIPv4(address)) {
xored = xorIPv4(pton4(address));
} else if (net.isIPv6(address)) {
xored = xorIPv6(pton6(address), owner.transactionId);
} else {
throw new Error(`Invalid ip address: ${address}`);
}
return ip.toString(xored);
}
ra.decode = function (buf, offset) {
if (!offset) offset = 0
offset += 2
const host = ip.toString(buf, offset, 4)
ra.decode.bytes = 6
return host
}