Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function encode_attr_mapped_addr(addr, buffer, offset, end) {
buffer.writeUInt16BE(addr.family === 'IPv6' ? 0x02 : 0x01, offset);
// xor the port against the magic key
buffer.writeUInt16BE(addr.port, offset + 2);
ip_module.toBuffer(addr.address, buffer, offset + 4);
}
sendSocks5CommandRequest() {
const buff = new smart_buffer_1.SmartBuffer();
buff.writeUInt8(0x05);
buff.writeUInt8(constants_1.SocksCommand[this._options.command]);
buff.writeUInt8(0x00);
// ipv4, ipv6, domain?
if (net.isIPv4(this._options.destination.host)) {
buff.writeUInt8(constants_1.Socks5HostType.IPv4);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
}
else if (net.isIPv6(this._options.destination.host)) {
buff.writeUInt8(constants_1.Socks5HostType.IPv6);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
}
else {
buff.writeUInt8(constants_1.Socks5HostType.Hostname);
buff.writeUInt8(this._options.destination.host.length);
buff.writeString(this._options.destination.host);
}
buff.writeUInt16BE(this._options.destination.port);
this._nextRequiredPacketBufferSize =
constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
this._socket.write(buff.toBuffer());
this.state = constants_1.SocksClientState.SentFinalHandshake;
}
/**
function encode_attr_xor_mapped_addr(addr, buffer, offset, end) {
buffer.writeUInt16BE(addr.family === 'IPv6' ? 0x02 : 0x01, offset);
// xor the port against the magic key
buffer.writeUInt16BE(addr.port ^ buffer.readUInt16BE(stun.XOR_KEY_OFFSET), offset + 2);
ip_module.toBuffer(addr.address, buffer, offset + 4);
var k = stun.XOR_KEY_OFFSET;
for (var i = offset + 4; i < end; ++i) {
buffer[i] ^= buffer[k];
k += 1;
}
}
sendSocks4InitialHandshake() {
const userId = this._options.proxy.userId || '';
const buff = new smart_buffer_1.SmartBuffer();
buff.writeUInt8(0x04);
buff.writeUInt8(constants_1.SocksCommand[this._options.command]);
buff.writeUInt16BE(this._options.destination.port);
// Socks 4 (IPv4)
if (net.isIPv4(this._options.destination.host)) {
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
buff.writeStringNT(userId);
// Socks 4a (hostname)
}
else {
buff.writeUInt8(0x00);
buff.writeUInt8(0x00);
buff.writeUInt8(0x00);
buff.writeUInt8(0x01);
buff.writeStringNT(userId);
buff.writeStringNT(this._options.destination.host);
}
this._nextRequiredPacketBufferSize =
constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks4Response;
this._socket.write(buff.toBuffer());
}
/**
onInitTargetAddress({ host, port }) {
const type = getAddrType(host);
this._atyp = type;
this._port = ntb(port);
this._host = (type === ATYP_DOMAIN) ? Buffer.from(host) : ip.toBuffer(host);
}
this._heartbeatInterval = setInterval(() => {
/*
To probe an address for verification, an endpoint will send
HEARTBEATs including a 64-bit random nonce and a path indicator (to
identify the address that the HEARTBEAT is sent to) within the
HEARTBEAT parameter.
*/
for (const address in this.destinations) {
const destination = this.destinations[address]
const heartbeatInfo = crypto.randomBytes(12)
const nonce = heartbeatInfo.readUInt32BE(0)
this.nonces[nonce] = true
ip.toBuffer(address, heartbeatInfo, 8)
this.debugger.trace(
'> heartbeat to %s, %d bytes',
address,
heartbeatInfo.length,
heartbeatInfo,
destination
)
this._sendChunk('heartbeat', { heartbeat_info: heartbeatInfo }, address)
/*
The endpoint should increment the respective error counter of the
destination transport address each time a HEARTBEAT is sent to that
address and not acknowledged within one RTO.
When the value of this counter reaches the protocol parameter
'Path.Max.Retrans', the endpoint should mark the corresponding
destination address as inactive if it is not so marked, and may also
function createHeader (packet) {
const buffer = Buffer.from(IP_HEADER_TEMPLATE)
writeLength(buffer, buffer.length + packet.payload.length)
if (packet.ttl > 0 && packet.ttl < 0xFF) {
buffer.writeUInt8(packet.ttl, 8)
}
ip.toBuffer(packet.src, buffer, 12)
ip.toBuffer(packet.dst, buffer, 16)
return buffer
}
function ipHmac(key, uid, addr) {
if (ip.isV4Format(addr)) {
addr = '::' + addr
}
addr = ip.toBuffer(addr)
var hmac = crypto.createHmac('sha256', key)
hmac.update(uid)
hmac.update(addr)
return hmac.digest()
}
function encode (value, buffer, offset) {
if (!buffer) buffer = Buffer.alloc(16)
if (!offset) offset = 0
if (offset + 16 > buffer.length) throw new RangeError('destination buffer is too small')
if (ip.isV4Format(value)) {
IPV4_PREFIX.copy(buffer, offset)
ip.toBuffer(value, buffer, offset + 12)
} else if (ip.isV6Format(value)) {
ip.toBuffer(value, buffer, offset)
} else {
throw Error('Invalid IP address value')
}
return buffer
}
ra.encode = function (host, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(ra.encodingLength(host))
if (!offset) offset = 0
buf.writeUInt16BE(4, offset)
offset += 2
ip.toBuffer(host, buf, offset)
ra.encode.bytes = 6
return buf
}