Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'smart-buffer' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
);
remoteHost = {
host: buff.readString(hostLength),
port: buff.readUInt16BE()
};
// IPv6
} else if (addressType === Socks5HostType.IPv6) {
// Check if data is available.
const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
if (this._receiveBuffer.length < dataNeeded) {
this._nextRequiredPacketBufferSize = dataNeeded;
return;
}
buff = SmartBuffer.fromBuffer(
this._receiveBuffer.get(dataNeeded).slice(4)
);
remoteHost = {
host: ip.toString(buff.readBuffer(16)),
port: buff.readUInt16BE()
};
}
this.state = SocksClientState.Established;
this.removeInternalSocketHandlers();
this.emit('established', { socket: this._socket, remoteHost });
}
}
private handleSocks4FinalHandshakeResponse() {
const data = this._receiveBuffer.get(8);
if (data[1] !== Socks4Response.Granted) {
this._closeSocket(
`${ERRORS.Socks4ProxyRejectedConnection} - (${Socks4Response[data[1]]})`
);
} else {
// Bind response
if (SocksCommand[this._options.command] === SocksCommand.bind) {
const buff = SmartBuffer.fromBuffer(data);
buff.readOffset = 2;
const remoteHost: SocksRemoteHost = {
port: buff.readUInt16BE(),
host: ip.fromLong(buff.readUInt32BE())
};
// If host is 0.0.0.0, set to proxy host.
if (remoteHost.host === '0.0.0.0') {
remoteHost.host = this._options.proxy.ipaddress;
}
this.state = SocksClientState.BoundWaitingForConnection;
this.emit('bound', { socket: this._socket, remoteHost });
// Connect response
} else {
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);
sendSocks5InitialHandshake() {
const buff = new smart_buffer_1.SmartBuffer();
buff.writeUInt8(0x05);
// We should only tell the proxy we support user/pass auth if auth info is actually provided.
// Note: As of Tor v0.3.5.7+, if user/pass auth is an option from the client, by default it will always take priority.
if (this._options.proxy.userId || this._options.proxy.password) {
buff.writeUInt8(2);
buff.writeUInt8(constants_1.Socks5Auth.NoAuth);
buff.writeUInt8(constants_1.Socks5Auth.UserPass);
}
else {
buff.writeUInt8(1);
buff.writeUInt8(constants_1.Socks5Auth.NoAuth);
}
this._nextRequiredPacketBufferSize =
constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5InitialHandshakeResponse;
this._socket.write(buff.toBuffer());
this.state = constants_1.SocksClientState.SentInitialHandshake;
let xferBuffer: Buffer;
let remainingBuffer: SmartBuffer;
if (plainWriteBuffer.length <= this._getAvailableRemoteBufferSize()) {
// Send the whole buffer in one go.
xferBuffer = plainWriteBuffer;
remainingBuffer = new SmartBuffer();
} else {
// Cut the buffer into two.
xferBuffer = Buffer.alloc(this._getAvailableRemoteBufferSize());
plainWriteBuffer.copy(xferBuffer, 0, 0, this._getAvailableRemoteBufferSize());
const secondPartBuffer = Buffer.alloc(plainWriteBuffer.length - this._getAvailableRemoteBufferSize());
plainWriteBuffer.copy(secondPartBuffer, 0, this._getAvailableRemoteBufferSize());
remainingBuffer = new SmartBuffer();
remainingBuffer.writeBuffer(secondPartBuffer);
}
WebIpc.writeBulkFile(this._fileIdentifier, xferBuffer);
this._writeBuffer = remainingBuffer;
this._removeBufferDelta -= xferBuffer.length;
this._availableSize += xferBuffer.length;
this._onAvailableSizeChangeEventEmitter.fire(this._availableSize);
}
if (this._closePending && this._writeBuffer.length === 0) {
WebIpc.closeBulkFile(this._fileIdentifier, this._success);
this._isOpen = false;
this._closePending = false;
private sendSocks5CommandRequest() {
const buff = new SmartBuffer();
buff.writeUInt8(0x05);
buff.writeUInt8(SocksCommand[this._options.command]);
buff.writeUInt8(0x00);
// ipv4, ipv6, domain?
if (net.isIPv4(this._options.destination.host)) {
buff.writeUInt8(Socks5HostType.IPv4);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
} else if (net.isIPv6(this._options.destination.host)) {
buff.writeUInt8(Socks5HostType.IPv6);
buff.writeBuffer(ip.toBuffer(this._options.destination.host));
} else {
buff.writeUInt8(Socks5HostType.Hostname);
buff.writeUInt8(this._options.destination.host.length);
buff.writeString(this._options.destination.host);
private sendSocks4InitialHandshake() {
const userId = this._options.proxy.userId || '';
const buff = new SmartBuffer();
buff.writeUInt8(0x04);
buff.writeUInt8(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);
static createUDPFrame(options) {
const buff = new smart_buffer_1.SmartBuffer();
buff.writeUInt16BE(0);
buff.writeUInt8(options.frameNumber || 0);
// IPv4/IPv6/Hostname
if (net.isIPv4(options.remoteHost.host)) {
buff.writeUInt8(constants_1.Socks5HostType.IPv4);
buff.writeUInt32BE(ip.toLong(options.remoteHost.host));
}
else if (net.isIPv6(options.remoteHost.host)) {
buff.writeUInt8(constants_1.Socks5HostType.IPv6);
buff.writeBuffer(ip.toBuffer(options.remoteHost.host));
}
else {
buff.writeUInt8(constants_1.Socks5HostType.Hostname);
buff.writeUInt8(Buffer.byteLength(options.remoteHost.host));
buff.writeString(options.remoteHost.host);
}
// Read address type
const addressType = header[3];
let remoteHost: SocksRemoteHost;
let buff: SmartBuffer;
// IPv4
if (addressType === Socks5HostType.IPv4) {
// Check if data is available.
const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
if (this._receiveBuffer.length < dataNeeded) {
this._nextRequiredPacketBufferSize = dataNeeded;
return;
}
buff = SmartBuffer.fromBuffer(
this._receiveBuffer.get(dataNeeded).slice(4)
);
remoteHost = {
host: ip.fromLong(buff.readUInt32BE()),
port: buff.readUInt16BE()
};
// If given host is 0.0.0.0, assume remote proxy ip instead.
if (remoteHost.host === '0.0.0.0') {
remoteHost.host = this._options.proxy.ipaddress;
}
// Hostname
} else if (addressType === Socks5HostType.Hostname) {
const hostLength = header[4];
}
// Hostname
} else if (addressType === Socks5HostType.Hostname) {
const hostLength = header[4];
const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(
hostLength
); // header + host length + host + port
// Check if data is available.
if (this._receiveBuffer.length < dataNeeded) {
this._nextRequiredPacketBufferSize = dataNeeded;
return;
}
buff = SmartBuffer.fromBuffer(
this._receiveBuffer.get(dataNeeded).slice(5) // Slice at 5 to skip host length
);
remoteHost = {
host: buff.readString(hostLength),
port: buff.readUInt16BE()
};
// IPv6
} else if (addressType === Socks5HostType.IPv6) {
// Check if data is available.
const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
if (this._receiveBuffer.length < dataNeeded) {
this._nextRequiredPacketBufferSize = dataNeeded;
return;
}