libhyperproxy/dist/socket.js

108 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-01-12 17:50:38 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const streamx_1 = require("streamx");
const IPV4 = "IPv4";
const IPV6 = "IPv6";
class Socket extends streamx_1.Duplex {
_allowHalfOpen;
remoteAddress;
remotePort;
remoteFamily;
bufferSize;
2023-02-25 01:04:45 +00:00
remotePublicKey;
2023-02-26 03:10:17 +00:00
_emulateWebsocket;
constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket = false, } = {}) {
2023-01-12 17:50:38 +00:00
super({ write });
this._allowHalfOpen = allowHalfOpen;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
2023-02-25 01:04:45 +00:00
this.remotePublicKey = remotePublicKey;
2023-02-26 03:10:17 +00:00
this._emulateWebsocket = emulateWebsocket;
2023-01-12 17:50:38 +00:00
if (remoteAddress) {
const type = Socket.isIP(remoteAddress);
if (!type) {
throw Error("invalid remoteAddress");
}
2023-02-25 04:51:59 +00:00
this.remoteFamily = type === 6 ? IPV6 : IPV4;
2023-01-12 17:50:38 +00:00
}
2023-02-26 03:10:17 +00:00
if (this._emulateWebsocket) {
2023-02-26 03:47:19 +00:00
this.addEventListener = this.addListener;
this.removeEventListener = this.removeListener;
this.send = this.write;
2023-02-26 03:49:50 +00:00
this.close = this.end;
2023-02-26 03:12:36 +00:00
this.addEventListener("data", (data) =>
2023-02-26 03:10:17 +00:00
// @ts-ignore
2023-02-26 03:12:36 +00:00
this.emit("message", new MessageEvent("data", { data })));
2023-02-26 03:10:17 +00:00
}
2023-01-12 17:50:38 +00:00
}
_connecting;
get connecting() {
return this._connecting;
}
get readyState() {
2023-02-26 03:38:46 +00:00
if (this._emulateWebsocket) {
if (this._connecting) {
return 0;
}
else if (this.readable && this.writable) {
return 1;
}
else {
return 3;
}
}
2023-01-12 17:50:38 +00:00
if (this._connecting) {
return "opening";
}
else if (this.readable && this.writable) {
return "open";
}
else if (this.readable && !this.writable) {
return "readOnly";
}
else if (!this.readable && this.writable) {
return "writeOnly";
}
else {
return "closed";
}
}
listen() {
throw new Error("Not supported");
}
setTimeout(msecs, callback) {
throw new Error("Not implemented");
}
_onTimeout() {
// @ts-ignore
this.emit("timeout");
}
setNoDelay(enable) { }
setKeepAlive(setting, msecs) { }
address() {
return {
address: this.remoteAddress,
port: this.remotePort,
family: this.remoteFamily,
};
}
static isIP(input) {
if (Socket.isIPv4(input)) {
return 4;
}
else if (Socket.isIPv6(input)) {
return 6;
}
else {
return 0;
}
}
static isIPv4(input) {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(input);
}
static isIPv6(input) {
return /^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d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