Compare commits

...

15 Commits

Author SHA1 Message Date
semantic-release-bot 3a0fc7f1e2 chore(release): 0.0.2-develop.4 [skip ci]
## [0.0.2-develop.4](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.3...v0.0.2-develop.4) (2023-08-02)

### Bug Fixes

* pass remoteAddress and remotePort as part of socket options ([608aace](608aace286))
2023-08-02 20:38:33 +00:00
Derrick Hammer 3e646eb473
Merge remote-tracking branch 'origin/develop' into develop 2023-08-02 16:37:30 -04:00
Derrick Hammer 608aace286
fix: pass remoteAddress and remotePort as part of socket options 2023-08-02 16:37:21 -04:00
semantic-release-bot 894c9d9e36 chore(release): 0.0.2-develop.3 [skip ci]
## [0.0.2-develop.3](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.2...v0.0.2-develop.3) (2023-08-02)

### Bug Fixes

* add stub unref method for UDP ([3ed168a](3ed168abd7))
2023-08-02 19:19:48 +00:00
Derrick Hammer 9c7f240056
Merge remote-tracking branch 'origin/develop' into develop 2023-08-02 15:19:04 -04:00
Derrick Hammer 3ed168abd7
fix: add stub unref method for UDP 2023-08-02 15:18:59 -04:00
semantic-release-bot fa4209c869 chore(release): 0.0.2-develop.2 [skip ci]
## [0.0.2-develop.2](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.1...v0.0.2-develop.2) (2023-07-09)

### Bug Fixes

* import setTimeout so we are using the same instance in browser ([bb26fb3](bb26fb3955))
2023-07-09 14:59:57 +00:00
Derrick Hammer bb26fb3955
fix: import setTimeout so we are using the same instance in browser 2023-07-09 10:59:14 -04:00
Derrick Hammer 62464627bc
Merge remote-tracking branch 'origin/develop' into develop 2023-07-08 01:18:06 -04:00
Derrick Hammer a8811d9fae
chore: unneeded import 2023-07-08 01:03:49 -04:00
semantic-release-bot 03a8db88d8 chore(release): 0.0.2-develop.1 [skip ci]
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.1...v0.0.2-develop.1) (2023-07-05)
2023-07-05 10:26:20 +00:00
Derrick Hammer 2c931edb85
ci: add missing config 2023-07-05 06:25:15 -04:00
Derrick Hammer abd3d1d136
ci: setup 2023-07-05 06:23:26 -04:00
Derrick Hammer 1ad6c1d692
refactor: bug fixes, added type asserts and use ? chaining where needed 2023-07-05 06:19:41 -04:00
Derrick Hammer 8797249ae5
refactor: switch to new devops 2023-07-05 06:17:57 -04:00
40 changed files with 19540 additions and 1216 deletions

13
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

5
.presetterrc.json Normal file
View File

@ -0,0 +1,5 @@
{
"preset": [
"@lumeweb/node-library-preset"
]
}

22
CHANGELOG.md Normal file
View File

@ -0,0 +1,22 @@
## [0.0.2-develop.4](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.3...v0.0.2-develop.4) (2023-08-02)
### Bug Fixes
* pass remoteAddress and remotePort as part of socket options ([608aace](https://git.lumeweb.com/LumeWeb/libhyperproxy/commit/608aace286bc62b327954d369023412c40e8e83d))
## [0.0.2-develop.3](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.2...v0.0.2-develop.3) (2023-08-02)
### Bug Fixes
* add stub unref method for UDP ([3ed168a](https://git.lumeweb.com/LumeWeb/libhyperproxy/commit/3ed168abd70f37145e46dbe1c98363f9dd8afb0d))
## [0.0.2-develop.2](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.2-develop.1...v0.0.2-develop.2) (2023-07-09)
### Bug Fixes
* import setTimeout so we are using the same instance in browser ([bb26fb3](https://git.lumeweb.com/LumeWeb/libhyperproxy/commit/bb26fb3955c9e37f91266e32b9e4ec91684e4f77))
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/libhyperproxy/compare/v0.0.1...v0.0.2-develop.1) (2023-07-05)

11
dist/index.d.ts vendored
View File

@ -1,11 +0,0 @@
import Proxy from "./proxy.js";
import Socket from "./socket.js";
import Peer, { DataSocketOptions, PeerOptions, PeerOptionsWithProxy, OnOpen, OnSend, OnReceive, OnClose } from "./peer.js";
import Server from "./server.js";
import DummySocket from "./proxies/multiSocket/dummySocket.js";
import TcpSocket from "./proxies/multiSocket/tcpSocket.js";
import BasicProxy from "./proxies/basic.js";
import MultiSocketProxy from "./proxies/multiSocket.js";
export { Proxy, Socket, Server, Peer, DataSocketOptions, PeerOptions, PeerOptionsWithProxy, OnOpen, OnSend, OnReceive, OnClose, DummySocket, TcpSocket, BasicProxy, MultiSocketProxy, };
export declare function createSocket(port: number, host: string): Socket;
export declare function createServer(): Server;

33
dist/index.js vendored
View File

@ -1,33 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createServer = exports.createSocket = exports.MultiSocketProxy = exports.BasicProxy = exports.TcpSocket = exports.DummySocket = exports.Peer = exports.Server = exports.Socket = exports.Proxy = void 0;
const proxy_js_1 = __importDefault(require("./proxy.js"));
exports.Proxy = proxy_js_1.default;
const socket_js_1 = __importDefault(require("./socket.js"));
exports.Socket = socket_js_1.default;
const peer_js_1 = __importDefault(require("./peer.js"));
exports.Peer = peer_js_1.default;
const server_js_1 = __importDefault(require("./server.js"));
exports.Server = server_js_1.default;
const dummySocket_js_1 = __importDefault(require("./proxies/multiSocket/dummySocket.js"));
exports.DummySocket = dummySocket_js_1.default;
const tcpSocket_js_1 = __importDefault(require("./proxies/multiSocket/tcpSocket.js"));
exports.TcpSocket = tcpSocket_js_1.default;
const basic_js_1 = __importDefault(require("./proxies/basic.js"));
exports.BasicProxy = basic_js_1.default;
const multiSocket_js_1 = __importDefault(require("./proxies/multiSocket.js"));
exports.MultiSocketProxy = multiSocket_js_1.default;
function createSocket(port, host) {
return new socket_js_1.default({
remotePort: port,
remoteAddress: host,
});
}
exports.createSocket = createSocket;
function createServer() {
return new server_js_1.default();
}
exports.createServer = createServer;

59
dist/peer.d.ts vendored
View File

@ -1,59 +0,0 @@
import Proxy from "./proxy.js";
import Socket from "./socket.js";
export type OnOpen = (peer: Peer, socket: Socket, data: any) => {
connect: boolean;
} | Promise<{
connect: boolean;
}> | Promise<void> | void;
export type OnData = (peer: Peer, data: any) => void;
export type OnReceive = OnData;
export type OnClose = OnData;
export type OnSend = OnData;
export type OnChannel = (peer: Peer, channel: any) => void;
export type OnOpenBound = (socket: Socket, data: any) => {
connect: boolean;
} | Promise<{
connect: boolean;
}> | Promise<void> | void;
export type OnDataBound = (data: any) => void;
export type OnReceiveBound = OnDataBound;
export type OnCloseBound = OnDataBound;
export type OnSendBound = OnDataBound;
export type OnChannelBound = (channel: any) => void;
export interface DataSocketOptions {
onopen?: OnOpen;
onreceive?: OnReceive;
onsend?: OnSend;
onclose?: OnClose;
onchannel?: OnChannel;
emulateWebsocket?: boolean;
}
export interface PeerOptions {
peer: any;
muxer: any;
}
export interface PeerOptionsWithProxy extends PeerOptions {
proxy: Proxy;
}
export default abstract class Peer {
protected _proxy: Proxy;
protected _peer: any;
protected _muxer: any;
protected _onopen: OnOpenBound;
protected _onreceive: OnReceiveBound;
protected _onsend: OnSendBound;
protected _onclose: OnCloseBound;
protected _onchannel: OnChannelBound;
protected _emulateWebsocket: boolean;
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket, }: PeerOptionsWithProxy & DataSocketOptions);
protected _socket?: Socket;
get socket(): Socket;
protected _channel?: any;
get channel(): any;
protected abstract initSocket(): any;
protected abstract handleChannelOnOpen(m: any): Promise<void>;
protected abstract handleChannelOnClose(socket: Socket): Promise<void>;
protected initChannel(): Promise<void>;
init(): Promise<void>;
protected initMessages(): Promise<void>;
}

58
dist/peer.js vendored
View File

@ -1,58 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Peer {
_proxy;
_peer;
_muxer;
_onopen;
_onreceive;
_onsend;
_onclose;
_onchannel;
_emulateWebsocket;
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket = false, }) {
this._proxy = proxy;
this._peer = peer;
this._muxer = muxer;
this._onopen = onopen?.bind(undefined, this);
this._onreceive = onreceive?.bind(undefined, this);
this._onsend = onsend?.bind(undefined, this);
this._onclose = onclose?.bind(undefined, this);
this._onchannel = onchannel?.bind(undefined, this);
this._emulateWebsocket = emulateWebsocket;
}
_socket;
get socket() {
return this._socket;
}
_channel;
get channel() {
return this._channel;
}
async initChannel() {
const self = this;
this._channel = await this._muxer.createChannel({
protocol: this._proxy.protocol,
onopen: async (m) => {
await this.handleChannelOnOpen(m);
// @ts-ignore
await this._onopen?.(this._channel);
},
onclose: async (socket) => {
await this.handleChannelOnClose(socket);
// @ts-ignore
await this._onclose?.();
},
});
await this.initMessages();
await this._onchannel?.(this._channel);
await this._channel.open();
this._proxy.emit("peerChannelOpen", this);
}
async init() {
await this.initSocket();
await this.initChannel();
}
async initMessages() { }
}
exports.default = Peer;

View File

@ -1,5 +0,0 @@
import Proxy from "../proxy.js";
import { DataSocketOptions, PeerOptions } from "../peer.js";
export default class BasicProxy extends Proxy {
protected handlePeer({ peer, muxer, ...options }: DataSocketOptions & PeerOptions): void;
}

14
dist/proxies/basic.js vendored
View File

@ -1,14 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const proxy_js_1 = __importDefault(require("../proxy.js"));
const peer_js_1 = __importDefault(require("./basic/peer.js"));
class BasicProxy extends proxy_js_1.default {
handlePeer({ peer, muxer, ...options }) {
const conn = new peer_js_1.default({ proxy: this, peer, muxer, ...options });
conn.init();
}
}
exports.default = BasicProxy;

View File

@ -1,9 +0,0 @@
import BasePeer from "../../peer.js";
import Socket from "../../socket.js";
export default class Peer extends BasePeer {
private _pipe?;
protected initSocket(): Promise<void>;
protected handleChannelOnOpen(m: any): Promise<void>;
protected handleChannelOnClose(socket: Socket): Promise<void>;
protected initMessages(): Promise<void>;
}

View File

@ -1,58 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const peer_js_1 = __importDefault(require("../../peer.js"));
const util_js_1 = require("../../util.js");
const socket_js_1 = __importDefault(require("../../socket.js"));
const buffer_1 = require("buffer");
class Peer extends peer_js_1.default {
_pipe;
async initSocket() {
const self = this;
const raw = await (0, util_js_1.maybeGetAsyncProperty)(self._peer.rawStream);
this._socket = new socket_js_1.default({
remoteAddress: raw.remoteHost,
remotePort: raw.remotePort,
remotePublicKey: await (0, util_js_1.maybeGetAsyncProperty)(self._peer.remotePublicKey),
async write(data, cb) {
self._pipe?.send(data);
await self._onsend?.(data);
cb();
},
emulateWebsocket: self._emulateWebsocket,
});
}
async handleChannelOnOpen(m) {
if (!m) {
m = buffer_1.Buffer.from([]);
}
if (m instanceof Uint8Array) {
m = buffer_1.Buffer.from(m);
}
this._socket?.on("end", () => this._channel.close());
let ret = await this._onopen?.(this._socket, m);
if (!ret || (ret && ret.connect === false)) {
// @ts-ignore
self._socket?.emit("connect");
}
this._socket?.emit("data", m);
}
async handleChannelOnClose(socket) {
this._socket?.destroy();
}
async initMessages() {
const self = this;
this._pipe = await this._channel.addMessage({
async onmessage(m) {
if (m instanceof Uint8Array) {
m = buffer_1.Buffer.from(m);
}
self._socket.emit("data", m);
await self._onreceive?.(m);
},
});
}
}
exports.default = Peer;

View File

@ -1,36 +0,0 @@
/// <reference types="node" />
import Proxy, { ProxyOptions } from "../proxy.js";
import type { TcpSocketConnectOpts } from "net";
import { DataSocketOptions, PeerOptions } from "../peer.js";
import { PeerEntity } from "./multiSocket/types.js";
import Peer from "./multiSocket/peer.js";
export interface MultiSocketProxyOptions extends ProxyOptions {
socketClass?: any;
server: boolean;
allowedPorts?: number[];
}
export default class MultiSocketProxy extends Proxy {
handlePeer({ peer, muxer, ...options }: DataSocketOptions & PeerOptions): Promise<void>;
private socketClass;
private _peers;
private _nextPeer;
private _server;
private _allowedPorts;
constructor(options: MultiSocketProxyOptions);
private _socketMap;
get socketMap(): Map<number, number>;
private _sockets;
get sockets(): Map<number, any>;
handleNewPeerChannel(peer: Peer): Promise<void>;
handleClosePeer(peer: Peer): Promise<void>;
get(pubkey: Uint8Array): PeerEntity | undefined;
update(pubkey: Uint8Array, data: Partial<PeerEntity>): void;
createSocket(options: TcpSocketConnectOpts): typeof this.socketClass;
private _registerOpenSocketMessage;
private _registerWriteSocketMessage;
private _registerCloseSocketMessage;
private _registerTimeoutSocketMessage;
private _registerErrorSocketMessage;
private _toString;
private _getPublicKey;
}

View File

@ -1,251 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const proxy_js_1 = __importDefault(require("../proxy.js"));
const tcpSocket_js_1 = __importDefault(require("./multiSocket/tcpSocket.js"));
const compact_encoding_1 = require("compact-encoding");
const serialize_error_1 = require("serialize-error");
const b4a_1 = __importDefault(require("b4a"));
const util_js_1 = require("../util.js");
const dummySocket_js_1 = __importDefault(require("./multiSocket/dummySocket.js"));
const peer_js_1 = __importDefault(require("./multiSocket/peer.js"));
const socketEncoding = {
preencode(state, m) {
compact_encoding_1.uint.preencode(state, m.id);
compact_encoding_1.uint.preencode(state, m.remoteId);
},
encode(state, m) {
compact_encoding_1.uint.encode(state, m.id);
compact_encoding_1.uint.encode(state, m.remoteId);
},
decode(state, m) {
return {
remoteId: compact_encoding_1.uint.decode(state, m),
id: compact_encoding_1.uint.decode(state, m),
};
},
};
const writeSocketEncoding = {
preencode(state, m) {
socketEncoding.preencode(state, m);
compact_encoding_1.raw.preencode(state, m.data);
},
encode(state, m) {
socketEncoding.encode(state, m);
compact_encoding_1.raw.encode(state, m.data);
},
decode(state, m) {
const socket = socketEncoding.decode(state, m);
return {
...socket,
data: compact_encoding_1.raw.decode(state, m),
};
},
};
const errorSocketEncoding = {
preencode(state, m) {
socketEncoding.preencode(state, m);
compact_encoding_1.json.preencode(state, (0, serialize_error_1.serializeError)(m.err));
},
encode(state, m) {
socketEncoding.encode(state, m);
compact_encoding_1.json.encode(state, (0, serialize_error_1.serializeError)(m.err));
},
decode(state, m) {
const socket = socketEncoding.decode(state, m);
return {
...socket,
err: (0, serialize_error_1.deserializeError)(compact_encoding_1.json.decode(state, m)),
};
},
};
const nextSocketId = (0, util_js_1.idFactory)(1);
class MultiSocketProxy extends proxy_js_1.default {
async handlePeer({ peer, muxer, ...options }) {
const conn = new peer_js_1.default({
...this.socketOptions,
proxy: this,
peer,
muxer,
...options,
});
await conn.init();
this.emit("peer", conn);
}
socketClass;
_peers = new Map();
_nextPeer;
_server = false;
_allowedPorts = [];
constructor(options) {
super(options);
if (options.socketClass) {
this.socketClass = options.socketClass;
}
else {
if (options.server) {
this.socketClass = tcpSocket_js_1.default;
}
else {
this.socketClass = dummySocket_js_1.default;
}
}
if (options.server) {
this._server = true;
}
this._nextPeer = (0, util_js_1.roundRobinFactory)(this._peers);
}
_socketMap = new Map();
get socketMap() {
return this._socketMap;
}
_sockets = new Map();
get sockets() {
return this._sockets;
}
async handleNewPeerChannel(peer) {
this.update(await this._getPublicKey(peer), {
peer,
});
await this._registerOpenSocketMessage(peer);
await this._registerWriteSocketMessage(peer);
await this._registerCloseSocketMessage(peer);
await this._registerTimeoutSocketMessage(peer);
await this._registerErrorSocketMessage(peer);
}
async handleClosePeer(peer) {
for (const item of this._sockets) {
if (item[1].peer.peer === peer) {
item[1].end();
}
}
const pubkey = this._toString(await this._getPublicKey(peer));
if (this._peers.has(pubkey)) {
this._peers.delete(pubkey);
}
}
get(pubkey) {
if (this._peers.has(this._toString(pubkey))) {
return this._peers.get(this._toString(pubkey));
}
return undefined;
}
update(pubkey, data) {
const peer = this.get(pubkey) ?? {};
this._peers.set(this._toString(pubkey), {
...peer,
...data,
...{
messages: {
...peer?.messages,
...data?.messages,
},
},
});
}
createSocket(options) {
if (!this._peers.size) {
throw new Error("no peers found");
}
const peer = this._nextPeer();
const socketId = nextSocketId();
const socket = new this.socketClass(socketId, this, peer, options);
this._sockets.set(socketId, socket);
return socket;
}
async _registerOpenSocketMessage(peer) {
const self = this;
const message = await peer.channel.addMessage({
encoding: {
preencode: this._server ? socketEncoding.preencode : compact_encoding_1.json.preencode,
encode: this._server ? socketEncoding.encode : compact_encoding_1.json.encode,
decode: this._server ? compact_encoding_1.json.decode : socketEncoding.decode,
},
async onmessage(m) {
if (self._server) {
if (self._allowedPorts.length &&
!self._allowedPorts.includes(m.port)) {
self.get(await self._getPublicKey(peer)).messages.errorSocket.send({
id: m.id,
err: new Error(`port ${m.port} not allowed`),
});
return;
}
}
m = m;
if (self._server) {
new self.socketClass(nextSocketId(), m.id, self, self.get(await self._getPublicKey(peer)), m).connect();
return;
}
const socket = self._sockets.get(m.id);
if (socket) {
socket.remoteId = m.remoteId;
// @ts-ignore
socket.emit("connect");
}
},
});
this.update(await this._getPublicKey(peer), {
messages: { openSocket: message },
});
}
async _registerWriteSocketMessage(peer) {
const self = this;
const message = await peer.channel.addMessage({
encoding: writeSocketEncoding,
onmessage(m) {
self._sockets.get(m.id)?.push(b4a_1.default.from(m.data));
},
});
this.update(await this._getPublicKey(peer), {
messages: { writeSocket: message },
});
}
async _registerCloseSocketMessage(peer) {
const self = this;
const message = await peer.channel.addMessage({
encoding: socketEncoding,
onmessage(m) {
self._sockets.get(m.id)?.end();
},
});
this.update(await this._getPublicKey(peer), {
messages: { closeSocket: message },
});
}
async _registerTimeoutSocketMessage(peer) {
const self = this;
const message = await peer.channel.addMessage({
encoding: socketEncoding,
onmessage(m) {
// @ts-ignore
self._sockets.get(m.id)?.emit("timeout");
},
});
this.update(await this._getPublicKey(peer), {
messages: { timeoutSocket: message },
});
}
async _registerErrorSocketMessage(peer) {
const self = this;
const message = await peer.channel.addMessage({
encoding: errorSocketEncoding,
onmessage(m) {
// @ts-ignore
self._sockets.get(m.id)?.emit("error", m.err);
},
});
this.update(await this._getPublicKey(peer), {
messages: { errorSocket: message },
});
}
_toString(pubkey) {
return b4a_1.default.from(pubkey).toString("hex");
}
async _getPublicKey(peer) {
return (0, util_js_1.maybeGetAsyncProperty)(peer.stream.remotePublicKey);
}
}
exports.default = MultiSocketProxy;

View File

@ -1,21 +0,0 @@
/// <reference types="node" />
import { Callback } from "streamx";
import { TcpSocketConnectOpts } from "net";
import MultiSocketProxy from "../multiSocket.js";
import { PeerEntity } from "./types.js";
import Socket, { SocketOptions } from "../../socket.js";
export default class DummySocket extends Socket {
private _options;
private _id;
private _proxy;
private _connectTimeout?;
constructor(id: number, manager: MultiSocketProxy, peer: PeerEntity, connectOptions: TcpSocketConnectOpts, socketOptions: SocketOptions);
private _remoteId;
set remoteId(value: number);
private _peer;
get peer(): any;
_write(data: any, cb: any): Promise<void>;
_destroy(cb: Callback): Promise<void>;
connect(): Promise<void>;
setTimeout(ms: number, cb: Function): void;
}

View File

@ -1,67 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const timers_1 = require("timers");
const util_js_1 = require("../../util.js");
const socket_js_1 = __importDefault(require("../../socket.js"));
class DummySocket extends socket_js_1.default {
_options;
_id;
_proxy;
_connectTimeout;
constructor(id, manager, peer, connectOptions, socketOptions) {
super(socketOptions);
this._id = id;
this._proxy = manager;
this._peer = peer;
this._options = connectOptions;
// @ts-ignore
this.on("timeout", () => {
if (this._connectTimeout) {
(0, timers_1.clearTimeout)(this._connectTimeout);
}
});
}
_remoteId = 0;
set remoteId(value) {
this._remoteId = value;
this._proxy.socketMap.set(this._id, value);
}
_peer;
get peer() {
return this._peer;
}
async _write(data, cb) {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.writeSocket))?.send({
id: this._id,
remoteId: this._remoteId,
data,
});
cb();
}
async _destroy(cb) {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.closeSocket))?.send({
id: this._id,
remoteId: this._remoteId,
});
this._proxy.socketMap.delete(this._id);
this._proxy.sockets.delete(this._id);
}
async connect() {
(await (0, util_js_1.maybeGetAsyncProperty)(this._peer.messages.openSocket))?.send({
...this._options,
id: this._id,
});
}
setTimeout(ms, cb) {
if (this._connectTimeout) {
(0, timers_1.clearTimeout)(this._connectTimeout);
}
this._connectTimeout = setTimeout(() => {
cb && cb();
}, ms);
}
}
exports.default = DummySocket;

View File

@ -1,11 +0,0 @@
import BasePeer from "../../peer.js";
import Socket from "../../socket.js";
import MultiSocketProxy from "../multiSocket.js";
export default class Peer extends BasePeer {
protected initMessages(): Promise<void>;
protected _proxy: MultiSocketProxy;
protected initSocket(): Promise<void>;
get stream(): any;
protected handleChannelOnClose(socket: Socket): Promise<void>;
protected handleChannelOnOpen(m: any): Promise<void>;
}

View File

@ -1,20 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const peer_js_1 = __importDefault(require("../../peer.js"));
class Peer extends peer_js_1.default {
async initMessages() {
await this._proxy.handleNewPeerChannel(this);
}
async initSocket() { }
get stream() {
return this._muxer.stream;
}
async handleChannelOnClose(socket) {
return this._proxy.handleClosePeer(this);
}
async handleChannelOnOpen(m) { }
}
exports.default = Peer;

View File

@ -1,20 +0,0 @@
/// <reference types="node" />
import { Callback } from "streamx";
import { TcpSocketConnectOpts } from "net";
import MultiSocketProxy from "../multiSocket.js";
import { PeerEntity } from "./types.js";
import BaseSocket from "../../socket.js";
export default class TcpSocket extends BaseSocket {
private _options;
private _id;
private _remoteId;
private _proxy;
private _socket?;
constructor(id: number, remoteId: number, manager: MultiSocketProxy, peer: PeerEntity, options: TcpSocketConnectOpts);
private _peer;
get peer(): any;
_write(data: any, cb: any): void;
_destroy(cb: Callback): void;
connect(): void;
private _getSocketRequest;
}

View File

@ -1,92 +0,0 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const net = __importStar(require("net"));
const socket_js_1 = __importDefault(require("../../socket.js"));
class TcpSocket extends socket_js_1.default {
_options;
_id;
_remoteId;
_proxy;
_socket;
constructor(id, remoteId, manager, peer, options) {
super();
this._remoteId = remoteId;
this._proxy = manager;
this._id = id;
this._peer = peer;
this._options = options;
this._proxy.sockets.set(this._id, this);
this._proxy.socketMap.set(this._id, this._remoteId);
}
_peer;
get peer() {
return this._peer;
}
_write(data, cb) {
this._peer.messages.writeSocket?.send({
...this._getSocketRequest(),
data,
});
cb();
}
_destroy(cb) {
this._proxy.sockets.delete(this._id);
this._proxy.socketMap.delete(this._id);
this._peer.messages.closeSocket?.send(this._getSocketRequest());
}
connect() {
this.on("error", (err) => {
this._peer.messages.errorSocket?.send({
...this._getSocketRequest(),
err,
});
});
// @ts-ignore
this.on("timeout", () => {
this._peer.messages.timeoutSocket?.send(this._getSocketRequest());
});
// @ts-ignore
this.on("connect", () => {
this._peer.messages.openSocket?.send(this._getSocketRequest());
});
this._socket = net.connect(this._options);
["timeout", "error", "connect", "end", "destroy", "close"].forEach((event) => {
this._socket?.on(event, (...args) => this.emit(event, ...args));
});
this._socket.pipe(this);
this.pipe(this._socket);
}
_getSocketRequest() {
return {
id: this._id,
remoteId: this._remoteId,
};
}
}
exports.default = TcpSocket;

View File

@ -1,28 +0,0 @@
import Peer from "../../peer.js";
export interface SocketRequest {
remoteId: number;
id: number;
}
export type CloseSocketRequest = SocketRequest;
export interface WriteSocketRequest extends SocketRequest {
data: Uint8Array;
}
export interface ErrorSocketRequest extends SocketRequest {
err: Error;
}
type Message = {
send: (pubkey: Uint8Array | any) => void;
};
export interface PeerEntityMessages {
keyExchange: Message;
openSocket: Message;
writeSocket: Message;
closeSocket: Message;
timeoutSocket: Message;
errorSocket: Message;
}
export interface PeerEntity {
messages: PeerEntityMessages | Partial<PeerEntityMessages>;
peer: Peer;
}
export {};

View File

@ -1,2 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

24
dist/proxy.d.ts vendored
View File

@ -1,24 +0,0 @@
/// <reference types="node" />
import { DataSocketOptions, PeerOptions } from "./peer.js";
import EventEmitter from "events";
export interface ProxyOptions extends DataSocketOptions {
swarm: any;
protocol: string;
listen?: boolean;
autostart?: boolean;
}
export default abstract class Proxy extends EventEmitter {
protected _listen: any;
protected _autostart: boolean;
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen, autostart, emulateWebsocket, }: ProxyOptions);
protected _socketOptions: DataSocketOptions;
get socketOptions(): DataSocketOptions;
private _swarm;
get swarm(): any;
private _protocol;
get protocol(): string;
protected abstract handlePeer({ peer, muxer, ...options }: DataSocketOptions & PeerOptions): any;
protected _init(): void;
private init;
private _handleConnection;
}

62
dist/proxy.js vendored
View File

@ -1,62 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const protomux_1 = __importDefault(require("protomux"));
const events_1 = __importDefault(require("events"));
class Proxy extends events_1.default {
_listen;
_autostart;
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen = false, autostart = false, emulateWebsocket = false, }) {
super();
this._swarm = swarm;
this._protocol = protocol;
this._listen = listen;
this._autostart = autostart;
this._socketOptions = {
onopen,
onreceive,
onsend,
onclose,
onchannel,
emulateWebsocket,
};
this.init();
}
_socketOptions;
get socketOptions() {
return this._socketOptions;
}
_swarm;
get swarm() {
return this._swarm;
}
_protocol;
get protocol() {
return this._protocol;
}
_init() {
// Implement in subclasses
}
async init() {
if (this._listen) {
this._swarm.on("connection", this._handleConnection.bind(this));
}
await this._init();
}
_handleConnection(peer) {
const muxer = protomux_1.default.from(peer);
const handlePeer = this.handlePeer.bind(this, {
peer,
muxer,
...this._socketOptions,
});
if (this._autostart) {
handlePeer();
return;
}
muxer.pair(this._protocol, handlePeer);
}
}
exports.default = Proxy;

22
dist/server.d.ts vendored
View File

@ -1,22 +0,0 @@
/// <reference types="node" />
import EventEmitter from "events";
export default class Server extends EventEmitter {
address(): {
address: string;
family: string;
port: number;
};
close(): Promise<void>;
getConnections(): Promise<number>;
listen(...args: any[]): Promise<{
address: string;
family: string;
port: number;
}>;
get listening(): boolean;
set listening(value: boolean);
get maxConnections(): any;
set maxConnections(value: any);
ref(): this;
unref(): this;
}

41
dist/server.js vendored
View File

@ -1,41 +0,0 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = __importDefault(require("events"));
class Server extends events_1.default {
address() {
return {
address: "127.0.0.1",
family: "IPv4",
port: 0,
};
}
async close() {
return;
}
async getConnections() {
return 0;
}
async listen(...args) {
const address = this.address();
this.emit("listening", address);
return address;
}
get listening() {
return false;
}
set listening(value) { }
get maxConnections() {
return undefined;
}
set maxConnections(value) { }
ref() {
return this;
}
unref() {
return this;
}
}
exports.default = Server;

43
dist/socket.d.ts vendored
View File

@ -1,43 +0,0 @@
import { Duplex, DuplexEvents, Callback } from "streamx";
type AddressFamily = "IPv6" | "IPv4";
export interface SocketOptions {
allowHalfOpen?: boolean;
remoteAddress?: string;
remotePort?: number;
remotePublicKey?: Uint8Array;
write?: (this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>, data: any, cb: Callback) => void;
emulateWebsocket?: boolean;
}
export default class Socket extends Duplex {
private _allowHalfOpen;
remoteAddress: any;
remotePort: any;
remoteFamily: AddressFamily;
bufferSize: any;
readable: true;
writable: true;
remotePublicKey: Uint8Array;
private _emulateWebsocket;
addEventListener?: typeof this.addListener;
removeEventListener?: typeof this.removeListener;
send?: typeof this.write;
close?: typeof this.end;
constructor({ allowHalfOpen, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket, }?: SocketOptions);
private _connecting;
get connecting(): boolean;
get readyState(): string | number;
listen(): void;
setTimeout(msecs: any, callback: any): void;
_onTimeout(): void;
setNoDelay(enable: boolean): void;
setKeepAlive(setting: any, msecs: number): void;
address(): {
address: any;
port: any;
family: AddressFamily;
};
static isIP(input: string): number;
static isIPv4(input: string): boolean;
static isIPv6(input: string): boolean;
}
export {};

107
dist/socket.js vendored
View File

@ -1,107 +0,0 @@
"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;
remotePublicKey;
_emulateWebsocket;
constructor({ allowHalfOpen = false, remoteAddress, remotePort, remotePublicKey, write, emulateWebsocket = false, } = {}) {
super({ write });
this._allowHalfOpen = allowHalfOpen;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
this.remotePublicKey = remotePublicKey;
this._emulateWebsocket = emulateWebsocket;
if (remoteAddress) {
const type = Socket.isIP(remoteAddress);
if (!type) {
throw Error("invalid remoteAddress");
}
this.remoteFamily = type === 6 ? IPV6 : IPV4;
}
if (this._emulateWebsocket) {
this.addEventListener = this.addListener;
this.removeEventListener = this.removeListener;
this.send = this.write;
this.close = this.end;
this.addEventListener("data", (data) =>
// @ts-ignore
this.emit("message", new MessageEvent("data", { data })));
}
}
_connecting;
get connecting() {
return this._connecting;
}
get readyState() {
if (this._emulateWebsocket) {
if (this._connecting) {
return 0;
}
else if (this.readable && this.writable) {
return 1;
}
else {
return 3;
}
}
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|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,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}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((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}))|:))$/.test(input);
}
}
exports.default = Socket;

4
dist/util.d.ts vendored
View File

@ -1,4 +0,0 @@
export declare function idFactory(start: number, step?: number, limit?: number): () => number;
export declare function roundRobinFactory<T>(list: Map<string, any>): () => T;
export declare function maybeGetAsyncProperty(object: any): Promise<any>;
export declare function isPromise(obj: Promise<any>): boolean;

41
dist/util.js vendored
View File

@ -1,41 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPromise = exports.maybeGetAsyncProperty = exports.roundRobinFactory = exports.idFactory = void 0;
function idFactory(start, step = 1, limit = 2 ** 32) {
let id = start;
return function nextId() {
const nextId = id;
id += step;
if (id >= limit)
id = start;
return nextId;
};
}
exports.idFactory = idFactory;
function roundRobinFactory(list) {
let index = 0;
return () => {
const keys = [...list.keys()].sort();
if (index >= keys.length) {
index = 0;
}
return list.get(keys[index++]);
};
}
exports.roundRobinFactory = roundRobinFactory;
async function maybeGetAsyncProperty(object) {
if (typeof object === "function") {
object = object();
}
if (isPromise(object)) {
object = await object;
}
return object;
}
exports.maybeGetAsyncProperty = maybeGetAsyncProperty;
function isPromise(obj) {
return (!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function");
}
exports.isPromise = isPromise;

19419
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,35 @@
{ {
"name": "@lumeweb/libhyperproxy", "name": "@lumeweb/libhyperproxy",
"version": "0.1.0", "version": "0.0.2-develop.4",
"main": "dist/index.js", "main": "lib/index.js",
"type": "module",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/libhyperproxy.git"
},
"devDependencies": { "devDependencies": {
"@types/b4a": "^1.6.0", "@lumeweb/node-library-preset": "^0.2.7",
"@types/node": "^18.11.18", "@types/serialize-error": "^4.0.1",
"@types/streamx": "^2.9.1", "@types/streamx": "^2.9.1",
"prettier": "^2.8.2", "presetter": "*"
"typescript": "^4.9.4" },
"readme": "ERROR: No README data found!",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
}, },
"dependencies": { "dependencies": {
"b4a": "^1.6.3", "binconv": "^0.2.0",
"buffer": "^6.0.3", "compact-encoding": "^2.12.0",
"compact-encoding": "^2.11.0", "protomux": "^3.5.0",
"protomux": "^3.4.0",
"serialize-error": "^11.0.0", "serialize-error": "^11.0.0",
"streamx": "^2.13.0" "streamx": "^2.15.0"
},
"files": [
"lib"
],
"publishConfig": {
"access": "public"
} }
} }

View File

@ -1,11 +1,10 @@
import Proxy from "./proxy.js"; import Proxy from "./proxy.js";
import Socket from "./socket.js"; import Socket from "./socket.js";
import { Buffer } from "buffer";
import { maybeGetAsyncProperty } from "./util.js";
export type OnOpen = ( export type OnOpen = (
peer: Peer, peer: Peer,
socket: Socket, socket: Socket,
data: any data: any,
) => ) =>
| { connect: boolean } | { connect: boolean }
| Promise<{ connect: boolean }> | Promise<{ connect: boolean }>
@ -19,7 +18,7 @@ export type OnChannel = (peer: Peer, channel: any) => void;
export type OnOpenBound = ( export type OnOpenBound = (
socket: Socket, socket: Socket,
data: any data: any,
) => ) =>
| { connect: boolean } | { connect: boolean }
| Promise<{ connect: boolean }> | Promise<{ connect: boolean }>
@ -75,18 +74,18 @@ export default abstract class Peer {
this._proxy = proxy; this._proxy = proxy;
this._peer = peer; this._peer = peer;
this._muxer = muxer; this._muxer = muxer;
this._onopen = onopen?.bind(undefined, this); this._onopen = onopen?.bind(undefined, this) as OnOpenBound;
this._onreceive = onreceive?.bind(undefined, this); this._onreceive = onreceive?.bind(undefined, this) as OnReceiveBound;
this._onsend = onsend?.bind(undefined, this); this._onsend = onsend?.bind(undefined, this) as OnSendBound;
this._onclose = onclose?.bind(undefined, this); this._onclose = onclose?.bind(undefined, this) as OnCloseBound;
this._onchannel = onchannel?.bind(undefined, this); this._onchannel = onchannel?.bind(undefined, this) as OnChannelBound;
this._emulateWebsocket = emulateWebsocket; this._emulateWebsocket = emulateWebsocket;
} }
protected _socket?: Socket; protected _socket?: Socket;
get socket(): Socket { get socket(): Socket {
return this._socket; return this._socket as Socket;
} }
protected _channel?: any; protected _channel?: any;

View File

@ -1,10 +1,11 @@
import BasePeer from "../../peer.js"; import BasePeer from "#peer.js";
import { maybeGetAsyncProperty } from "../../util.js"; import { maybeGetAsyncProperty } from "#util.js";
import Socket from "../../socket.js"; import Socket from "#socket.js";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
export default class Peer extends BasePeer { export default class Peer extends BasePeer {
private _pipe?: any; private _pipe?: any;
protected async initSocket() { protected async initSocket() {
const self = this; const self = this;
@ -32,7 +33,7 @@ export default class Peer extends BasePeer {
} }
this._socket?.on("end", () => this._channel.close()); this._socket?.on("end", () => this._channel.close());
let ret = await this._onopen?.(this._socket, m); let ret = await this._onopen?.(this._socket as Socket, m);
if (!ret || (ret && ret.connect === false)) { if (!ret || (ret && ret.connect === false)) {
// @ts-ignore // @ts-ignore
self._socket?.emit("connect"); self._socket?.emit("connect");
@ -52,7 +53,7 @@ export default class Peer extends BasePeer {
if (m instanceof Uint8Array) { if (m instanceof Uint8Array) {
m = Buffer.from(m); m = Buffer.from(m);
} }
self._socket.emit("data", m); self._socket?.emit("data", m);
await self._onreceive?.(m); await self._onreceive?.(m);
}, },
}); });

View File

@ -19,6 +19,7 @@ import {
} from "./multiSocket/types.js"; } from "./multiSocket/types.js";
import DummySocket from "./multiSocket/dummySocket.js"; import DummySocket from "./multiSocket/dummySocket.js";
import Peer from "./multiSocket/peer.js"; import Peer from "./multiSocket/peer.js";
import { uint8ArrayToHexString } from "binconv";
export interface MultiSocketProxyOptions extends ProxyOptions { export interface MultiSocketProxyOptions extends ProxyOptions {
socketClass?: any; socketClass?: any;
@ -102,7 +103,7 @@ export default class MultiSocketProxy extends Proxy {
private _peers: Map<string, PeerEntity> = new Map<string, PeerEntity>(); private _peers: Map<string, PeerEntity> = new Map<string, PeerEntity>();
private _nextPeer; private _nextPeer;
private _server = false; private _server = false;
private _allowedPorts = []; private _allowedPorts: number[] = [];
constructor(options: MultiSocketProxyOptions) { constructor(options: MultiSocketProxyOptions) {
super(options); super(options);
@ -189,7 +190,10 @@ export default class MultiSocketProxy extends Proxy {
const peer = this._nextPeer(); const peer = this._nextPeer();
const socketId = nextSocketId(); const socketId = nextSocketId();
const socket = new this.socketClass(socketId, this, peer, options); const socket = new this.socketClass(socketId, this, peer, options, {
remoteAddress: options.host,
remotePort: options.port,
});
this._sockets.set(socketId, socket); this._sockets.set(socketId, socket);
return socket; return socket;
@ -209,12 +213,14 @@ export default class MultiSocketProxy extends Proxy {
self._allowedPorts.length && self._allowedPorts.length &&
!self._allowedPorts.includes((m as TcpSocketConnectOpts).port) !self._allowedPorts.includes((m as TcpSocketConnectOpts).port)
) { ) {
self.get(await self._getPublicKey(peer)).messages.errorSocket.send({ self
id: (m as SocketRequest).id, .get(await self._getPublicKey(peer))
err: new Error( ?.messages.errorSocket?.send({
`port ${(m as TcpSocketConnectOpts).port} not allowed` id: (m as SocketRequest).id,
), err: new Error(
}); `port ${(m as TcpSocketConnectOpts).port} not allowed`,
),
});
return; return;
} }
} }
@ -227,7 +233,7 @@ export default class MultiSocketProxy extends Proxy {
m.id, m.id,
self, self,
self.get(await self._getPublicKey(peer)) as PeerEntity, self.get(await self._getPublicKey(peer)) as PeerEntity,
m m,
).connect(); ).connect();
return; return;
} }
@ -290,7 +296,6 @@ export default class MultiSocketProxy extends Proxy {
const message = await peer.channel.addMessage({ const message = await peer.channel.addMessage({
encoding: errorSocketEncoding, encoding: errorSocketEncoding,
onmessage(m: ErrorSocketRequest) { onmessage(m: ErrorSocketRequest) {
// @ts-ignore
self._sockets.get(m.id)?.emit("error", m.err); self._sockets.get(m.id)?.emit("error", m.err);
}, },
}); });
@ -300,7 +305,7 @@ export default class MultiSocketProxy extends Proxy {
} }
private _toString(pubkey: Uint8Array) { private _toString(pubkey: Uint8Array) {
return b4a.from(pubkey).toString("hex"); return uint8ArrayToHexString(pubkey);
} }
private async _getPublicKey(peer: Peer) { private async _getPublicKey(peer: Peer) {

View File

@ -1,10 +1,10 @@
import { Callback, Duplex } from "streamx"; import { Callback } from "streamx";
import { TcpSocketConnectOpts } from "net"; import { TcpSocketConnectOpts } from "net";
import { clearTimeout } from "timers"; import { clearTimeout, setTimeout } from "timers";
import MultiSocketProxy from "../multiSocket.js"; import MultiSocketProxy from "../multiSocket.js";
import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js"; import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js";
import { maybeGetAsyncProperty } from "../../util.js"; import { maybeGetAsyncProperty } from "#util.js";
import Socket, { SocketOptions } from "../../socket.js"; import Socket, { SocketOptions } from "#socket.js";
export default class DummySocket extends Socket { export default class DummySocket extends Socket {
private _options: TcpSocketConnectOpts; private _options: TcpSocketConnectOpts;
@ -18,7 +18,7 @@ export default class DummySocket extends Socket {
manager: MultiSocketProxy, manager: MultiSocketProxy,
peer: PeerEntity, peer: PeerEntity,
connectOptions: TcpSocketConnectOpts, connectOptions: TcpSocketConnectOpts,
socketOptions: SocketOptions socketOptions: SocketOptions,
) { ) {
super(socketOptions); super(socketOptions);
this._id = id; this._id = id;
@ -81,4 +81,6 @@ export default class DummySocket extends Socket {
cb && cb(); cb && cb();
}, ms) as any; }, ms) as any;
} }
public unref() {}
} }

View File

@ -1,5 +1,5 @@
import BasePeer from "../../peer.js"; import BasePeer from "#peer.js";
import Socket from "../../socket.js"; import Socket from "#socket.js";
import MultiSocketProxy from "../multiSocket.js"; import MultiSocketProxy from "../multiSocket.js";
export default class Peer extends BasePeer { export default class Peer extends BasePeer {

View File

@ -1,8 +1,8 @@
import { Callback, Duplex } from "streamx"; import { Callback } from "streamx";
import * as net from "net";
import { Socket, TcpSocketConnectOpts } from "net"; import { Socket, TcpSocketConnectOpts } from "net";
import MultiSocketProxy from "../multiSocket.js"; import MultiSocketProxy from "../multiSocket.js";
import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js"; import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js";
import * as net from "net";
import BaseSocket from "../../socket.js"; import BaseSocket from "../../socket.js";
export default class TcpSocket extends BaseSocket { export default class TcpSocket extends BaseSocket {
@ -18,7 +18,7 @@ export default class TcpSocket extends BaseSocket {
remoteId: number, remoteId: number,
manager: MultiSocketProxy, manager: MultiSocketProxy,
peer: PeerEntity, peer: PeerEntity,
options: TcpSocketConnectOpts options: TcpSocketConnectOpts,
) { ) {
super(); super();
this._remoteId = remoteId; this._remoteId = remoteId;
@ -72,9 +72,9 @@ export default class TcpSocket extends BaseSocket {
["timeout", "error", "connect", "end", "destroy", "close"].forEach( ["timeout", "error", "connect", "end", "destroy", "close"].forEach(
(event) => { (event) => {
this._socket?.on(event, (...args: any) => this._socket?.on(event, (...args: any) =>
this.emit(event as any, ...args) this.emit(event as any, ...args),
); );
} },
); );
this._socket.pipe(this as any); this._socket.pipe(this as any);

View File

@ -1,5 +1,4 @@
import { ProxyOptions } from "../../proxy.js"; import Peer from "#peer.js";
import Peer from "../../peer.js";
export interface SocketRequest { export interface SocketRequest {
remoteId: number; remoteId: number;

View File

@ -1,5 +1,4 @@
import { Duplex, DuplexEvents, Callback } from "streamx"; import { Callback, Duplex, DuplexEvents } from "streamx";
import { write } from "fs";
const IPV4 = "IPv4"; const IPV4 = "IPv4";
const IPV6 = "IPv6"; const IPV6 = "IPv6";
@ -14,7 +13,7 @@ export interface SocketOptions {
write?: ( write?: (
this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>, this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>,
data: any, data: any,
cb: Callback cb: Callback,
) => void; ) => void;
emulateWebsocket?: boolean; emulateWebsocket?: boolean;
} }
@ -23,9 +22,7 @@ export default class Socket extends Duplex {
private _allowHalfOpen: boolean; private _allowHalfOpen: boolean;
public remoteAddress: any; public remoteAddress: any;
public remotePort: any; public remotePort: any;
public remoteFamily: AddressFamily; public remoteFamily?: AddressFamily;
public bufferSize;
declare readable: true; declare readable: true;
declare writable: true; declare writable: true;
@ -49,7 +46,7 @@ export default class Socket extends Duplex {
this._allowHalfOpen = allowHalfOpen; this._allowHalfOpen = allowHalfOpen;
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
this.remotePort = remotePort; this.remotePort = remotePort;
this.remotePublicKey = remotePublicKey; this.remotePublicKey = remotePublicKey as Uint8Array;
this._emulateWebsocket = emulateWebsocket; this._emulateWebsocket = emulateWebsocket;
if (remoteAddress) { if (remoteAddress) {
@ -68,15 +65,15 @@ export default class Socket extends Duplex {
this.close = this.end; this.close = this.end;
this.addEventListener("data", (data: any) => this.addEventListener("data", (data: any) =>
// @ts-ignore // @ts-ignore
this.emit("message", new MessageEvent("data", { data })) this.emit("message", new MessageEvent("data", { data })),
); );
} }
} }
private _connecting: boolean; private _connecting?: boolean;
get connecting(): boolean { get connecting(): boolean {
return this._connecting; return this._connecting as boolean;
} }
get readyState(): string | number { get readyState(): string | number {
@ -140,13 +137,13 @@ export default class Socket extends Duplex {
static isIPv4(input: string) { static isIPv4(input: string) {
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( 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 input,
); );
} }
static isIPv6(input: string) { static isIPv6(input: string) {
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|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,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}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((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}))|:))$/.test( 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|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,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}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((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}))|:))$/.test(
input input,
); );
} }
} }

View File

@ -1,14 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"esModuleInterop": true,
"outDir": "dist",
"moduleResolution": "node",
"declaration": true,
},
"include": ["src"],
"exclude": [
"node_modules"
]
}