本文整理汇总了TypeScript中smart-buffer.SmartBuffer.readString方法的典型用法代码示例。如果您正苦于以下问题:TypeScript SmartBuffer.readString方法的具体用法?TypeScript SmartBuffer.readString怎么用?TypeScript SmartBuffer.readString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smart-buffer.SmartBuffer
的用法示例。
在下文中一共展示了SmartBuffer.readString方法的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 });
/*
//.........这里部分代码省略.........