本文整理匯總了TypeScript中smart-buffer.SmartBuffer類的典型用法代碼示例。如果您正苦於以下問題:TypeScript SmartBuffer類的具體用法?TypeScript SmartBuffer怎麽用?TypeScript SmartBuffer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了SmartBuffer類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: handleSocks4FinalHandshakeResponse
/**
* Handles Socks v4 handshake response.
* @param data
*/
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 {
this.state = SocksClientState.Established;
this.removeInternalSocketHandlers();
this.emit('established', { socket: this._socket });
}
}
}
示例2: parseUDPFrame
/**
* Parses a SOCKS UDP frame.
* @param data
*/
static parseUDPFrame(data: Buffer): SocksUDPFrameDetails {
const buff = SmartBuffer.fromBuffer(data);
buff.readOffset = 2;
const frameNumber = buff.readUInt8();
const hostType: Socks5HostType = buff.readUInt8();
let remoteHost;
if (hostType === Socks5HostType.IPv4) {
remoteHost = ip.fromLong(buff.readUInt32BE());
} else if (hostType === Socks5HostType.IPv6) {
remoteHost = ip.toString(buff.readBuffer(16));
} else {
remoteHost = buff.readString(buff.readUInt8());
}
const remotePort = buff.readUInt16BE();
return {
frameNumber,
remoteHost: {
host: remoteHost,
port: remotePort
},
data: buff.readBuffer()
};
}
示例3: sendSocks5UserPassAuthentication
/**
* Sends Socks v5 user & password auth handshake.
*
* Note: No auth and user/pass are currently supported.
*/
private sendSocks5UserPassAuthentication() {
const userId = this._options.proxy.userId || '';
const password = this._options.proxy.password || '';
const buff = new SmartBuffer();
buff.writeUInt8(0x01);
buff.writeUInt8(Buffer.byteLength(userId));
buff.writeString(userId);
buff.writeUInt8(Buffer.byteLength(password));
buff.writeString(password);
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5UserPassAuthenticationResponse;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentAuthentication;
}
示例4: sendSocks5InitialHandshake
/**
* Sends initial Socks v5 handshake request.
*/
private sendSocks5InitialHandshake() {
const buff = new 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(Socks5Auth.NoAuth);
buff.writeUInt8(Socks5Auth.UserPass);
} else {
buff.writeUInt8(1);
buff.writeUInt8(Socks5Auth.NoAuth);
}
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5InitialHandshakeResponse;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentInitialHandshake;
}
示例5: handleSocks4IncomingConnectionResponse
/**
* Handles Socks v4 incoming connection request (BIND)
* @param data
*/
private handleSocks4IncomingConnectionResponse() {
const data = this._receiveBuffer.get(8);
if (data[1] !== Socks4Response.Granted) {
this._closeSocket(
`${ERRORS.Socks4ProxyRejectedIncomingBoundConnection} - (${
Socks4Response[data[1]]
})`
);
} else {
const buff = SmartBuffer.fromBuffer(data);
buff.readOffset = 2;
const remoteHost: SocksRemoteHost = {
port: buff.readUInt16BE(),
host: ip.fromLong(buff.readUInt32BE())
};
this.state = SocksClientState.Established;
this.removeInternalSocketHandlers();
this.emit('established', { socket: this._socket, remoteHost });
}
}
示例6: 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 });
}
}
示例7: 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 });
/*
//.........這裏部分代碼省略.........
示例8: sendSocks5CommandRequest
/**
* Sends Socks v5 final handshake request.
*/
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);
}
buff.writeUInt16BE(this._options.destination.port);
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
this._socket.write(buff.toBuffer());
this.state = SocksClientState.SentFinalHandshake;
}
示例9: sendSocks4InitialHandshake
/**
* Sends initial Socks v4 handshake request.
*/
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);
}
this._nextRequiredPacketBufferSize =
SOCKS_INCOMING_PACKET_SIZES.Socks4Response;
this._socket.write(buff.toBuffer());
}
示例10: createUDPFrame
/**
* Creates a SOCKS UDP Frame.
* @param options
*/
static createUDPFrame(options: SocksUDPFrameDetails): Buffer {
const buff = new SmartBuffer();
buff.writeUInt16BE(0);
buff.writeUInt8(options.frameNumber || 0);
// IPv4/IPv6/Hostname
if (net.isIPv4(options.remoteHost.host)) {
buff.writeUInt8(Socks5HostType.IPv4);
buff.writeUInt32BE(ip.toLong(options.remoteHost.host));
} else if (net.isIPv6(options.remoteHost.host)) {
buff.writeUInt8(Socks5HostType.IPv6);
buff.writeBuffer(ip.toBuffer(options.remoteHost.host));
} else {
buff.writeUInt8(Socks5HostType.Hostname);
buff.writeUInt8(Buffer.byteLength(options.remoteHost.host));
buff.writeString(options.remoteHost.host);
}
// Port
buff.writeUInt16BE(options.remoteHost.port);
// Data
buff.writeBuffer(options.data);
return buff.toBuffer();
}