refactor: bug fixes, added type asserts and use ? chaining where needed
This commit is contained in:
parent
8797249ae5
commit
1ad6c1d692
19
src/peer.ts
19
src/peer.ts
|
@ -1,11 +1,10 @@
|
|||
import Proxy from "./proxy.js";
|
||||
import Socket from "./socket.js";
|
||||
import { Buffer } from "buffer";
|
||||
import { maybeGetAsyncProperty } from "./util.js";
|
||||
|
||||
export type OnOpen = (
|
||||
peer: Peer,
|
||||
socket: Socket,
|
||||
data: any
|
||||
data: any,
|
||||
) =>
|
||||
| { connect: boolean }
|
||||
| Promise<{ connect: boolean }>
|
||||
|
@ -19,7 +18,7 @@ export type OnChannel = (peer: Peer, channel: any) => void;
|
|||
|
||||
export type OnOpenBound = (
|
||||
socket: Socket,
|
||||
data: any
|
||||
data: any,
|
||||
) =>
|
||||
| { connect: boolean }
|
||||
| Promise<{ connect: boolean }>
|
||||
|
@ -75,18 +74,18 @@ export default abstract class Peer {
|
|||
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._onopen = onopen?.bind(undefined, this) as OnOpenBound;
|
||||
this._onreceive = onreceive?.bind(undefined, this) as OnReceiveBound;
|
||||
this._onsend = onsend?.bind(undefined, this) as OnSendBound;
|
||||
this._onclose = onclose?.bind(undefined, this) as OnCloseBound;
|
||||
this._onchannel = onchannel?.bind(undefined, this) as OnChannelBound;
|
||||
this._emulateWebsocket = emulateWebsocket;
|
||||
}
|
||||
|
||||
protected _socket?: Socket;
|
||||
|
||||
get socket(): Socket {
|
||||
return this._socket;
|
||||
return this._socket as Socket;
|
||||
}
|
||||
|
||||
protected _channel?: any;
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import BasePeer from "../../peer.js";
|
||||
import { maybeGetAsyncProperty } from "../../util.js";
|
||||
import Socket from "../../socket.js";
|
||||
import BasePeer from "#peer.js";
|
||||
import { maybeGetAsyncProperty } from "#util.js";
|
||||
import Socket from "#socket.js";
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
export default class Peer extends BasePeer {
|
||||
private _pipe?: any;
|
||||
|
||||
protected async initSocket() {
|
||||
const self = this;
|
||||
|
||||
|
@ -32,7 +33,7 @@ export default class Peer extends BasePeer {
|
|||
}
|
||||
|
||||
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)) {
|
||||
// @ts-ignore
|
||||
self._socket?.emit("connect");
|
||||
|
@ -52,7 +53,7 @@ export default class Peer extends BasePeer {
|
|||
if (m instanceof Uint8Array) {
|
||||
m = Buffer.from(m);
|
||||
}
|
||||
self._socket.emit("data", m);
|
||||
self._socket?.emit("data", m);
|
||||
await self._onreceive?.(m);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -19,6 +19,7 @@ import {
|
|||
} from "./multiSocket/types.js";
|
||||
import DummySocket from "./multiSocket/dummySocket.js";
|
||||
import Peer from "./multiSocket/peer.js";
|
||||
import { uint8ArrayToHexString } from "binconv";
|
||||
|
||||
export interface MultiSocketProxyOptions extends ProxyOptions {
|
||||
socketClass?: any;
|
||||
|
@ -102,7 +103,7 @@ export default class MultiSocketProxy extends Proxy {
|
|||
private _peers: Map<string, PeerEntity> = new Map<string, PeerEntity>();
|
||||
private _nextPeer;
|
||||
private _server = false;
|
||||
private _allowedPorts = [];
|
||||
private _allowedPorts: number[] = [];
|
||||
|
||||
constructor(options: MultiSocketProxyOptions) {
|
||||
super(options);
|
||||
|
@ -209,12 +210,14 @@ export default class MultiSocketProxy extends Proxy {
|
|||
self._allowedPorts.length &&
|
||||
!self._allowedPorts.includes((m as TcpSocketConnectOpts).port)
|
||||
) {
|
||||
self.get(await self._getPublicKey(peer)).messages.errorSocket.send({
|
||||
id: (m as SocketRequest).id,
|
||||
err: new Error(
|
||||
`port ${(m as TcpSocketConnectOpts).port} not allowed`
|
||||
),
|
||||
});
|
||||
self
|
||||
.get(await self._getPublicKey(peer))
|
||||
?.messages.errorSocket?.send({
|
||||
id: (m as SocketRequest).id,
|
||||
err: new Error(
|
||||
`port ${(m as TcpSocketConnectOpts).port} not allowed`,
|
||||
),
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -227,7 +230,7 @@ export default class MultiSocketProxy extends Proxy {
|
|||
m.id,
|
||||
self,
|
||||
self.get(await self._getPublicKey(peer)) as PeerEntity,
|
||||
m
|
||||
m,
|
||||
).connect();
|
||||
return;
|
||||
}
|
||||
|
@ -290,7 +293,6 @@ export default class MultiSocketProxy extends Proxy {
|
|||
const message = await peer.channel.addMessage({
|
||||
encoding: errorSocketEncoding,
|
||||
onmessage(m: ErrorSocketRequest) {
|
||||
// @ts-ignore
|
||||
self._sockets.get(m.id)?.emit("error", m.err);
|
||||
},
|
||||
});
|
||||
|
@ -300,7 +302,7 @@ export default class MultiSocketProxy extends Proxy {
|
|||
}
|
||||
|
||||
private _toString(pubkey: Uint8Array) {
|
||||
return b4a.from(pubkey).toString("hex");
|
||||
return uint8ArrayToHexString(pubkey);
|
||||
}
|
||||
|
||||
private async _getPublicKey(peer: Peer) {
|
||||
|
|
|
@ -3,8 +3,8 @@ import { TcpSocketConnectOpts } from "net";
|
|||
import { clearTimeout } from "timers";
|
||||
import MultiSocketProxy from "../multiSocket.js";
|
||||
import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js";
|
||||
import { maybeGetAsyncProperty } from "../../util.js";
|
||||
import Socket, { SocketOptions } from "../../socket.js";
|
||||
import { maybeGetAsyncProperty } from "#util.js";
|
||||
import Socket, { SocketOptions } from "#socket.js";
|
||||
|
||||
export default class DummySocket extends Socket {
|
||||
private _options: TcpSocketConnectOpts;
|
||||
|
@ -18,7 +18,7 @@ export default class DummySocket extends Socket {
|
|||
manager: MultiSocketProxy,
|
||||
peer: PeerEntity,
|
||||
connectOptions: TcpSocketConnectOpts,
|
||||
socketOptions: SocketOptions
|
||||
socketOptions: SocketOptions,
|
||||
) {
|
||||
super(socketOptions);
|
||||
this._id = id;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import BasePeer from "../../peer.js";
|
||||
import Socket from "../../socket.js";
|
||||
import BasePeer from "#peer.js";
|
||||
import Socket from "#socket.js";
|
||||
import MultiSocketProxy from "../multiSocket.js";
|
||||
|
||||
export default class Peer extends BasePeer {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { Callback, Duplex } from "streamx";
|
||||
import { Callback } from "streamx";
|
||||
import * as net from "net";
|
||||
import { Socket, TcpSocketConnectOpts } from "net";
|
||||
import MultiSocketProxy from "../multiSocket.js";
|
||||
import { PeerEntity, SocketRequest, WriteSocketRequest } from "./types.js";
|
||||
import * as net from "net";
|
||||
import BaseSocket from "../../socket.js";
|
||||
|
||||
export default class TcpSocket extends BaseSocket {
|
||||
|
@ -18,7 +18,7 @@ export default class TcpSocket extends BaseSocket {
|
|||
remoteId: number,
|
||||
manager: MultiSocketProxy,
|
||||
peer: PeerEntity,
|
||||
options: TcpSocketConnectOpts
|
||||
options: TcpSocketConnectOpts,
|
||||
) {
|
||||
super();
|
||||
this._remoteId = remoteId;
|
||||
|
@ -72,9 +72,9 @@ export default class TcpSocket extends BaseSocket {
|
|||
["timeout", "error", "connect", "end", "destroy", "close"].forEach(
|
||||
(event) => {
|
||||
this._socket?.on(event, (...args: any) =>
|
||||
this.emit(event as any, ...args)
|
||||
this.emit(event as any, ...args),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
this._socket.pipe(this as any);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { ProxyOptions } from "../../proxy.js";
|
||||
import Peer from "../../peer.js";
|
||||
import Peer from "#peer.js";
|
||||
|
||||
export interface SocketRequest {
|
||||
remoteId: number;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { Duplex, DuplexEvents, Callback } from "streamx";
|
||||
import { write } from "fs";
|
||||
import { Callback, Duplex, DuplexEvents } from "streamx";
|
||||
|
||||
const IPV4 = "IPv4";
|
||||
const IPV6 = "IPv6";
|
||||
|
@ -14,7 +13,7 @@ export interface SocketOptions {
|
|||
write?: (
|
||||
this: Duplex<any, any, any, any, true, true, DuplexEvents<any, any>>,
|
||||
data: any,
|
||||
cb: Callback
|
||||
cb: Callback,
|
||||
) => void;
|
||||
emulateWebsocket?: boolean;
|
||||
}
|
||||
|
@ -23,9 +22,7 @@ export default class Socket extends Duplex {
|
|||
private _allowHalfOpen: boolean;
|
||||
public remoteAddress: any;
|
||||
public remotePort: any;
|
||||
public remoteFamily: AddressFamily;
|
||||
|
||||
public bufferSize;
|
||||
public remoteFamily?: AddressFamily;
|
||||
|
||||
declare readable: true;
|
||||
declare writable: true;
|
||||
|
@ -49,7 +46,7 @@ export default class Socket extends Duplex {
|
|||
this._allowHalfOpen = allowHalfOpen;
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.remotePort = remotePort;
|
||||
this.remotePublicKey = remotePublicKey;
|
||||
this.remotePublicKey = remotePublicKey as Uint8Array;
|
||||
this._emulateWebsocket = emulateWebsocket;
|
||||
|
||||
if (remoteAddress) {
|
||||
|
@ -68,15 +65,15 @@ export default class Socket extends Duplex {
|
|||
this.close = this.end;
|
||||
this.addEventListener("data", (data: any) =>
|
||||
// @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 {
|
||||
return this._connecting;
|
||||
return this._connecting as boolean;
|
||||
}
|
||||
|
||||
get readyState(): string | number {
|
||||
|
@ -140,13 +137,13 @@ export default class Socket extends Duplex {
|
|||
|
||||
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(
|
||||
input
|
||||
input,
|
||||
);
|
||||
}
|
||||
|
||||
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(
|
||||
input
|
||||
input,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue