libhyperproxy/src/peer.ts

134 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-01-12 17:39:23 +00:00
import Proxy from "./proxy.js";
import Socket from "./socket.js";
import { Buffer } from "buffer";
2023-04-16 02:17:30 +00:00
import { maybeGetAsyncProperty } from "./util.js";
2023-01-12 17:39:23 +00:00
export type OnOpen = (
2023-03-02 10:30:51 +00:00
peer: Peer,
2023-01-12 17:39:23 +00:00
socket: Socket,
data: any
) =>
| { connect: boolean }
| Promise<{ connect: boolean }>
| Promise<void>
| void;
export type OnData = (peer: Peer, data: any) => void;
2023-01-12 17:39:23 +00:00
export type OnReceive = OnData;
export type OnClose = OnData;
export type OnSend = OnData;
export type OnChannel = (peer: Peer, channel: any) => void;
2023-01-12 17:39:23 +00:00
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;
2023-01-12 17:39:23 +00:00
export interface DataSocketOptions {
onopen?: OnOpen;
onreceive?: OnReceive;
onsend?: OnSend;
onclose?: OnClose;
onchannel?: OnChannel;
2023-02-26 03:16:40 +00:00
emulateWebsocket?: boolean;
2023-01-12 17:39:23 +00:00
}
export interface PeerOptions {
peer: any;
muxer: any;
}
export interface PeerOptionsWithProxy extends PeerOptions {
proxy: Proxy;
}
2023-04-16 02:17:30 +00:00
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;
2023-01-12 17:39:23 +00:00
constructor({
proxy,
peer,
muxer,
onopen,
onreceive,
onsend,
onclose,
onchannel,
2023-02-26 03:16:40 +00:00
emulateWebsocket = false,
2023-01-12 17:39:23 +00:00
}: PeerOptionsWithProxy & DataSocketOptions) {
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);
2023-02-26 03:16:40 +00:00
this._emulateWebsocket = emulateWebsocket;
2023-01-12 17:39:23 +00:00
}
2023-04-16 02:17:30 +00:00
protected _socket?: Socket;
2023-03-05 08:00:04 +00:00
get socket(): Socket {
return this._socket;
}
2023-04-16 02:17:30 +00:00
protected _channel?: any;
2023-03-02 10:41:13 +00:00
get channel(): any {
return this._channel;
}
2023-04-16 02:17:30 +00:00
protected abstract initSocket();
protected abstract handleChannelOnOpen(m: any): Promise<void>;
protected abstract handleChannelOnClose(socket: Socket): Promise<void>;
protected async initChannel() {
2023-01-12 17:39:23 +00:00
const self = this;
this._channel = await this._muxer.createChannel({
2023-01-12 17:39:23 +00:00
protocol: this._proxy.protocol,
onopen: async (m: any) => {
await this.handleChannelOnOpen(m);
// @ts-ignore
await this._onopen?.(this._channel);
},
onclose: async (socket: Socket) => {
await this.handleChannelOnClose(socket);
// @ts-ignore
await this._onclose?.();
},
2023-01-12 17:39:23 +00:00
});
2023-04-16 02:17:30 +00:00
await this.initMessages();
2023-01-12 17:39:23 +00:00
await this._onchannel?.(this._channel);
await this._channel.open();
this._proxy.emit("peerChannelOpen", this);
2023-01-12 17:39:23 +00:00
}
2023-04-09 16:15:23 +00:00
2023-04-16 02:17:30 +00:00
async init() {
await this.initSocket();
await this.initChannel();
2023-04-09 16:15:23 +00:00
}
2023-04-16 02:17:30 +00:00
protected async initMessages() {}
2023-04-09 16:15:23 +00:00
}