本文整理匯總了TypeScript中smart-buffer.SmartBuffer.readUInt16BE方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript SmartBuffer.readUInt16BE方法的具體用法?TypeScript SmartBuffer.readUInt16BE怎麽用?TypeScript SmartBuffer.readUInt16BE使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類smart-buffer.SmartBuffer
的用法示例。
在下文中一共展示了SmartBuffer.readUInt16BE方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: handleSocks5IncomingConnectionResponse
/**
* Handles Socks v5 incoming connection request (BIND).
*/
private handleSocks5IncomingConnectionResponse() {
// Peek at available data (we need at least 5 bytes to get the hostname length)
const header = this._receiveBuffer.peek(5);
if (header[0] !== 0x05 || header[1] !== Socks5Response.Granted) {
this._closeSocket(
`${ERRORS.Socks5ProxyRejectedIncomingBoundConnection} - ${
Socks5Response[header[1]]
}`
);
} else {
// 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];
const dataNeeded = SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(
hostLength
); // header + host length + 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;
}
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 });
}
}
示例2: handleSocks5FinalHandshakeResponse
/**
* Handles Socks v5 final handshake response.
* @param data
*/
private handleSocks5FinalHandshakeResponse() {
// Peek at available data (we need at least 5 bytes to get the hostname length)
const header = this._receiveBuffer.peek(5);
if (header[0] !== 0x05 || header[1] !== Socks5Response.Granted) {
this._closeSocket(
`${ERRORS.InvalidSocks5FinalHandshakeRejected} - ${
Socks5Response[header[1]]
}`
);
} else {
// 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];
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;
}
buff = SmartBuffer.fromBuffer(
this._receiveBuffer.get(dataNeeded).slice(4)
);
remoteHost = {
host: ip.toString(buff.readBuffer(16)),
port: buff.readUInt16BE()
};
}
// We have everything we need
this.state = SocksClientState.ReceivedFinalResponse;
// If using CONNECT, the client is now in the established state.
if (SocksCommand[this._options.command] === SocksCommand.connect) {
this.state = SocksClientState.Established;
this.removeInternalSocketHandlers();
this.emit('established', { socket: this._socket });
} else if (SocksCommand[this._options.command] === SocksCommand.bind) {
/* If using BIND, the Socks client is now in BoundWaitingForConnection state.
This means that the remote proxy server is waiting for a remote connection to the bound port. */
this.state = SocksClientState.BoundWaitingForConnection;
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
this.emit('bound', { socket: this._socket, remoteHost });
/*
//.........這裏部分代碼省略.........