This commit is contained in:
2019-09-18 01:43:58 +02:00
parent 34ec2d0244
commit 741c253202
10 changed files with 131 additions and 48 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+5 -5
View File
@@ -1,6 +1,5 @@
import { Socket } from "./RPCSocketServer";
declare type rpcType = 'hook' | 'unhook' | 'call';
declare type visibility = 'public' | 'private';
export declare type Outcome = "Success" | "Error";
export declare type Visibility = "127.0.0.1" | "0.0.0.0";
export declare class Response {
@@ -25,11 +24,11 @@ export declare type AsyncFunction = (...args: any[]) => Promise<any>;
export interface RPCExporter {
name: string;
exportRPCs(): socketioRPC[];
exportPublicRPCs(): socketioRPC[];
}
declare type baseRPC = {
type: rpcType;
name: string;
visibility: visibility;
};
declare type hookRPC = baseRPC & {
type: 'hook';
@@ -69,7 +68,7 @@ export declare type ExtendedRpcInfo = RpcInfo & {
export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
export declare type Socket = {
export interface Socket {
port: number;
hook: (rpcname: string, ...args: any[]) => Socket;
unhook: (rpcname: string) => Socket;
@@ -78,7 +77,7 @@ export declare type Socket = {
on: OnFunction;
destroy: () => void;
close: () => void;
};
}
export declare type RPCSocketConf = {
connectionHandler: (socket: Socket) => void;
errorHandler: (socket: Socket) => (error: any) => void;
@@ -93,6 +92,7 @@ export declare class RPCSocketServer {
private wsServer;
constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
private startWebsocket;
protected initApis(socket: any): void;
protected initApis(socket: Socket): void;
protected initPublicApis(socket: Socket): void;
}
export {};
+18 -6
View File
@@ -38,7 +38,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
owner: owner,
argNames: extractArgs(rpc.func),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
func: rpc.func,
};
@@ -47,7 +46,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
owner: owner,
argNames: extractArgs(rpc.func),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
func: rpc.func,
};
@@ -57,7 +55,6 @@ exports.rpcToRpcinfo = (rpc, owner) => {
owner: owner,
argNames: extractArgs(generator(undefined)),
type: rpc.type,
visibility: rpc.visibility,
name: rpc.name,
unhook: rpc.unhook,
generator: generator,
@@ -126,7 +123,10 @@ class RPCSocketServer {
this.io.on('socket', (socket) => {
socket.on('error', this.conf.errorHandler(socket));
socket.on('close', this.conf.closeHandler(socket));
this.initApis(socket);
if (this.visibility === "127.0.0.1")
this.initApis(socket);
else
this.initPublicApis(socket);
});
this.wsServer.listen(this.port, this.visibility);
}
@@ -140,13 +140,25 @@ class RPCSocketServer {
{
name: 'info',
type: 'call',
visibility: 'private',
func: async () => rpcInfos
}
];
const rpcInfos = [
...exports.rpcHooker(socket, "Admin", adminRPCs, false),
...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportRPCs()))
...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
];
}
initPublicApis(socket) {
const adminRPCs = [
{
name: 'info',
type: 'call',
func: async () => rpcInfos
}
];
const rpcInfos = [
...exports.rpcHooker(socket, "Admin", adminRPCs, false),
...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
];
}
}
@@ -1,6 +1,4 @@
declare type RPCReceiver = {
[RPCGroup in string]: any;
};
import { Socket } from "../backend/RPCSocketServer";
/**
* Dynamic library to communicate with FrontblockService remotely
*
@@ -8,13 +6,22 @@ declare type RPCReceiver = {
* Will ask it's service for available RPCs and parse them into methods of this object
* for convenient access.
*/
export declare class RPCaller implements RPCReceiver {
export declare class RPCSocket implements Socket {
port: number;
private server;
private tls;
private socket;
constructor(port: number, server: string, tls?: boolean);
hook(name: any, args: any): Socket;
unhook(name: any): Socket;
on(type: "error" | "close", f: (e?: any) => void): Socket;
destroy(): void;
close(): void;
call(rpcname: string, ...args: any[]): Promise<any>;
fire(rpcname: string, ...args: any[]): Promise<any>;
connect(): Promise<void>;
info(): Promise<any>;
private callGenerator;
private hookGenerator;
private unhookGenerator;
}
export {};
@@ -12,11 +12,35 @@ function stripAfterEquals(str) {
* Will ask it's service for available RPCs and parse them into methods of this object
* for convenient access.
*/
class RPCaller {
class RPCSocket {
constructor(port, server, tls = false) {
this.socket = bsock.connect(port, server, tls);
this.port = port;
this.server = server;
this.tls = tls;
}
hook(name, args) {
return this.socket.hook(name, args);
}
unhook(name) {
return this.socket.unhook(name);
}
on(type, f) {
return this.socket.on(type, name);
}
destroy() {
return this.socket.destroy();
}
close() {
return this.socket.close();
}
async call(rpcname, ...args) {
return await this.socket.call.apply(this.socket, [rpcname, ...args]);
}
async fire(rpcname, ...args) {
return await this.socket.fire.apply(this.socket, [rpcname, ...args]);
}
async connect() {
this.socket = await bsock.connect(this.port, this.server, this.tls);
const info = await this.info();
info.forEach(i => {
let f;
@@ -68,4 +92,4 @@ class RPCaller {
} )()`);
}
}
exports.RPCaller = RPCaller;
exports.RPCSocket = RPCSocket;
+5 -5
View File
@@ -2,17 +2,17 @@
Object.defineProperty(exports, "__esModule", { value: true });
const RPCSocketServer_1 = require("../src/backend/RPCSocketServer");
//@ts-ignore
const RPCaller_1 = require("../src/frontend/RPCaller");
const RPCSocket_1 = require("../src/frontend/RPCSocket");
new RPCSocketServer_1.RPCSocketServer(20000, [{
name: "HelloWorldRPCGroup",
exportPublicRPCs: () => [],
exportRPCs: () => [{
type: 'call',
name: 'echo',
func: async (s) => s,
visibility: 'private'
}]
}]);
const caller = new RPCaller_1.RPCaller(20000, 'localhost');
}],
}], "0.0.0.0");
const caller = new RPCSocket_1.RPCSocket(20000, 'localhost');
caller.connect().then(_ => {
caller.info().then(console.log);
caller["HelloWorldRPCGroup"].echo("x").then(console.log);