Browse Source

fix

master
peter 5 years ago
parent
commit
741c253202

+ 1
- 1
lib/RPCaller.min.js
File diff suppressed because it is too large
View File


+ 5
- 5
lib/src/backend/RPCSocketServer.d.ts View File

1
 import { Socket } from "./RPCSocketServer";
1
 import { Socket } from "./RPCSocketServer";
2
 declare type rpcType = 'hook' | 'unhook' | 'call';
2
 declare type rpcType = 'hook' | 'unhook' | 'call';
3
-declare type visibility = 'public' | 'private';
4
 export declare type Outcome = "Success" | "Error";
3
 export declare type Outcome = "Success" | "Error";
5
 export declare type Visibility = "127.0.0.1" | "0.0.0.0";
4
 export declare type Visibility = "127.0.0.1" | "0.0.0.0";
6
 export declare class Response {
5
 export declare class Response {
25
 export interface RPCExporter {
24
 export interface RPCExporter {
26
     name: string;
25
     name: string;
27
     exportRPCs(): socketioRPC[];
26
     exportRPCs(): socketioRPC[];
27
+    exportPublicRPCs(): socketioRPC[];
28
 }
28
 }
29
 declare type baseRPC = {
29
 declare type baseRPC = {
30
     type: rpcType;
30
     type: rpcType;
31
     name: string;
31
     name: string;
32
-    visibility: visibility;
33
 };
32
 };
34
 declare type hookRPC = baseRPC & {
33
 declare type hookRPC = baseRPC & {
35
     type: 'hook';
34
     type: 'hook';
69
 export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
68
 export declare const rpcToRpcinfo: (rpc: socketioRPC, owner: string) => RpcInfo;
70
 export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
69
 export declare const rpcHooker: (socket: Socket, owner: string, RPCs: socketioRPC[], makeUnique?: boolean) => ExtendedRpcInfo[];
71
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
70
 declare type OnFunction = (type: 'error' | 'close', f: (e?: any) => void) => Socket;
72
-export declare type Socket = {
71
+export interface Socket {
73
     port: number;
72
     port: number;
74
     hook: (rpcname: string, ...args: any[]) => Socket;
73
     hook: (rpcname: string, ...args: any[]) => Socket;
75
     unhook: (rpcname: string) => Socket;
74
     unhook: (rpcname: string) => Socket;
78
     on: OnFunction;
77
     on: OnFunction;
79
     destroy: () => void;
78
     destroy: () => void;
80
     close: () => void;
79
     close: () => void;
81
-};
80
+}
82
 export declare type RPCSocketConf = {
81
 export declare type RPCSocketConf = {
83
     connectionHandler: (socket: Socket) => void;
82
     connectionHandler: (socket: Socket) => void;
84
     errorHandler: (socket: Socket) => (error: any) => void;
83
     errorHandler: (socket: Socket) => (error: any) => void;
93
     private wsServer;
92
     private wsServer;
94
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
93
     constructor(port: number, rpcExporters?: RPCExporter[], visibility?: Visibility, conf?: RPCSocketConf);
95
     private startWebsocket;
94
     private startWebsocket;
96
-    protected initApis(socket: any): void;
95
+    protected initApis(socket: Socket): void;
96
+    protected initPublicApis(socket: Socket): void;
97
 }
97
 }
98
 export {};
98
 export {};

+ 18
- 6
lib/src/backend/RPCSocketServer.js View File

38
                 owner: owner,
38
                 owner: owner,
39
                 argNames: extractArgs(rpc.func),
39
                 argNames: extractArgs(rpc.func),
40
                 type: rpc.type,
40
                 type: rpc.type,
41
-                visibility: rpc.visibility,
42
                 name: rpc.name,
41
                 name: rpc.name,
43
                 func: rpc.func,
42
                 func: rpc.func,
44
             };
43
             };
47
                 owner: owner,
46
                 owner: owner,
48
                 argNames: extractArgs(rpc.func),
47
                 argNames: extractArgs(rpc.func),
49
                 type: rpc.type,
48
                 type: rpc.type,
50
-                visibility: rpc.visibility,
51
                 name: rpc.name,
49
                 name: rpc.name,
52
                 func: rpc.func,
50
                 func: rpc.func,
53
             };
51
             };
57
                 owner: owner,
55
                 owner: owner,
58
                 argNames: extractArgs(generator(undefined)),
56
                 argNames: extractArgs(generator(undefined)),
59
                 type: rpc.type,
57
                 type: rpc.type,
60
-                visibility: rpc.visibility,
61
                 name: rpc.name,
58
                 name: rpc.name,
62
                 unhook: rpc.unhook,
59
                 unhook: rpc.unhook,
63
                 generator: generator,
60
                 generator: generator,
126
             this.io.on('socket', (socket) => {
123
             this.io.on('socket', (socket) => {
127
                 socket.on('error', this.conf.errorHandler(socket));
124
                 socket.on('error', this.conf.errorHandler(socket));
128
                 socket.on('close', this.conf.closeHandler(socket));
125
                 socket.on('close', this.conf.closeHandler(socket));
129
-                this.initApis(socket);
126
+                if (this.visibility === "127.0.0.1")
127
+                    this.initApis(socket);
128
+                else
129
+                    this.initPublicApis(socket);
130
             });
130
             });
131
             this.wsServer.listen(this.port, this.visibility);
131
             this.wsServer.listen(this.port, this.visibility);
132
         }
132
         }
140
             {
140
             {
141
                 name: 'info',
141
                 name: 'info',
142
                 type: 'call',
142
                 type: 'call',
143
-                visibility: 'private',
144
                 func: async () => rpcInfos
143
                 func: async () => rpcInfos
145
             }
144
             }
146
         ];
145
         ];
147
         const rpcInfos = [
146
         const rpcInfos = [
148
             ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
147
             ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
149
-            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportRPCs()))
148
+            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
149
+        ];
150
+    }
151
+    initPublicApis(socket) {
152
+        const adminRPCs = [
153
+            {
154
+                name: 'info',
155
+                type: 'call',
156
+                func: async () => rpcInfos
157
+            }
158
+        ];
159
+        const rpcInfos = [
160
+            ...exports.rpcHooker(socket, "Admin", adminRPCs, false),
161
+            ...this.rpcExporters.flatMap(exporter => exports.rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
150
         ];
162
         ];
151
     }
163
     }
152
 }
164
 }

lib/src/frontend/RPCaller.d.ts → lib/src/frontend/RPCSocket.d.ts View File

1
-declare type RPCReceiver = {
2
-    [RPCGroup in string]: any;
3
-};
1
+import { Socket } from "../backend/RPCSocketServer";
4
 /**
2
 /**
5
  * Dynamic library to communicate with FrontblockService remotely
3
  * Dynamic library to communicate with FrontblockService remotely
6
  *
4
  *
8
  * Will ask it's service for available RPCs and parse them into methods of this object
6
  * Will ask it's service for available RPCs and parse them into methods of this object
9
  * for convenient access.
7
  * for convenient access.
10
  */
8
  */
11
-export declare class RPCaller implements RPCReceiver {
9
+export declare class RPCSocket implements Socket {
10
+    port: number;
11
+    private server;
12
+    private tls;
12
     private socket;
13
     private socket;
13
     constructor(port: number, server: string, tls?: boolean);
14
     constructor(port: number, server: string, tls?: boolean);
15
+    hook(name: any, args: any): Socket;
16
+    unhook(name: any): Socket;
17
+    on(type: "error" | "close", f: (e?: any) => void): Socket;
18
+    destroy(): void;
19
+    close(): void;
20
+    call(rpcname: string, ...args: any[]): Promise<any>;
21
+    fire(rpcname: string, ...args: any[]): Promise<any>;
14
     connect(): Promise<void>;
22
     connect(): Promise<void>;
15
     info(): Promise<any>;
23
     info(): Promise<any>;
16
     private callGenerator;
24
     private callGenerator;
17
     private hookGenerator;
25
     private hookGenerator;
18
     private unhookGenerator;
26
     private unhookGenerator;
19
 }
27
 }
20
-export {};

lib/src/frontend/RPCaller.js → lib/src/frontend/RPCSocket.js View File

12
  * Will ask it's service for available RPCs and parse them into methods of this object
12
  * Will ask it's service for available RPCs and parse them into methods of this object
13
  * for convenient access.
13
  * for convenient access.
14
  */
14
  */
15
-class RPCaller {
15
+class RPCSocket {
16
     constructor(port, server, tls = false) {
16
     constructor(port, server, tls = false) {
17
-        this.socket = bsock.connect(port, server, tls);
17
+        this.port = port;
18
+        this.server = server;
19
+        this.tls = tls;
20
+    }
21
+    hook(name, args) {
22
+        return this.socket.hook(name, args);
23
+    }
24
+    unhook(name) {
25
+        return this.socket.unhook(name);
26
+    }
27
+    on(type, f) {
28
+        return this.socket.on(type, name);
29
+    }
30
+    destroy() {
31
+        return this.socket.destroy();
32
+    }
33
+    close() {
34
+        return this.socket.close();
35
+    }
36
+    async call(rpcname, ...args) {
37
+        return await this.socket.call.apply(this.socket, [rpcname, ...args]);
38
+    }
39
+    async fire(rpcname, ...args) {
40
+        return await this.socket.fire.apply(this.socket, [rpcname, ...args]);
18
     }
41
     }
19
     async connect() {
42
     async connect() {
43
+        this.socket = await bsock.connect(this.port, this.server, this.tls);
20
         const info = await this.info();
44
         const info = await this.info();
21
         info.forEach(i => {
45
         info.forEach(i => {
22
             let f;
46
             let f;
68
                         } )()`);
92
                         } )()`);
69
     }
93
     }
70
 }
94
 }
71
-exports.RPCaller = RPCaller;
95
+exports.RPCSocket = RPCSocket;

+ 5
- 5
lib/test/test.js View File

2
 Object.defineProperty(exports, "__esModule", { value: true });
2
 Object.defineProperty(exports, "__esModule", { value: true });
3
 const RPCSocketServer_1 = require("../src/backend/RPCSocketServer");
3
 const RPCSocketServer_1 = require("../src/backend/RPCSocketServer");
4
 //@ts-ignore
4
 //@ts-ignore
5
-const RPCaller_1 = require("../src/frontend/RPCaller");
5
+const RPCSocket_1 = require("../src/frontend/RPCSocket");
6
 new RPCSocketServer_1.RPCSocketServer(20000, [{
6
 new RPCSocketServer_1.RPCSocketServer(20000, [{
7
         name: "HelloWorldRPCGroup",
7
         name: "HelloWorldRPCGroup",
8
+        exportPublicRPCs: () => [],
8
         exportRPCs: () => [{
9
         exportRPCs: () => [{
9
                 type: 'call',
10
                 type: 'call',
10
                 name: 'echo',
11
                 name: 'echo',
11
                 func: async (s) => s,
12
                 func: async (s) => s,
12
-                visibility: 'private'
13
-            }]
14
-    }]);
15
-const caller = new RPCaller_1.RPCaller(20000, 'localhost');
13
+            }],
14
+    }], "0.0.0.0");
15
+const caller = new RPCSocket_1.RPCSocket(20000, 'localhost');
16
 caller.connect().then(_ => {
16
 caller.connect().then(_ => {
17
     caller.info().then(console.log);
17
     caller.info().then(console.log);
18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log);
18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log);

+ 23
- 8
src/backend/RPCSocketServer.ts View File

50
 export interface RPCExporter{
50
 export interface RPCExporter{
51
     name: string
51
     name: string
52
     exportRPCs() : socketioRPC[]
52
     exportRPCs() : socketioRPC[]
53
+    exportPublicRPCs() : socketioRPC[]
53
 }
54
 }
54
 
55
 
55
 type baseRPC = { 
56
 type baseRPC = { 
107
                 owner: owner,
108
                 owner: owner,
108
                 argNames: extractArgs(rpc.func),
109
                 argNames: extractArgs(rpc.func),
109
                 type: rpc.type,
110
                 type: rpc.type,
110
-                visibility: rpc.visibility,
111
                 name: rpc.name,
111
                 name: rpc.name,
112
                 func: rpc.func,
112
                 func: rpc.func,
113
             }
113
             }
116
                 owner: owner,
116
                 owner: owner,
117
                 argNames: extractArgs(rpc.func),
117
                 argNames: extractArgs(rpc.func),
118
                 type: rpc.type,
118
                 type: rpc.type,
119
-                visibility: rpc.visibility,
120
                 name: rpc.name,
119
                 name: rpc.name,
121
                 func: rpc.func,
120
                 func: rpc.func,
122
             }
121
             }
126
                 owner: owner,
125
                 owner: owner,
127
                 argNames: extractArgs(generator(undefined)),
126
                 argNames: extractArgs(generator(undefined)),
128
                 type: rpc.type,
127
                 type: rpc.type,
129
-                visibility: rpc.visibility,
130
                 name: rpc.name,
128
                 name: rpc.name,
131
                 unhook: rpc.unhook,
129
                 unhook: rpc.unhook,
132
                 generator: generator,
130
                 generator: generator,
184
 
182
 
185
 type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => Socket
183
 type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => Socket
186
 
184
 
187
-export type Socket = {
185
+export interface Socket {
188
     port: number
186
     port: number
189
     hook: (rpcname: string, ...args: any[]) => Socket
187
     hook: (rpcname: string, ...args: any[]) => Socket
190
     unhook: (rpcname:string) => Socket
188
     unhook: (rpcname:string) => Socket
225
             this.io.on('socket', (socket:Socket) => {
223
             this.io.on('socket', (socket:Socket) => {
226
                 socket.on('error', this.conf.errorHandler(socket))
224
                 socket.on('error', this.conf.errorHandler(socket))
227
                 socket.on('close', this.conf.closeHandler(socket))
225
                 socket.on('close', this.conf.closeHandler(socket))
228
-                this.initApis(socket)
226
+                if(this.visibility === "127.0.0.1")
227
+                    this.initApis(socket)
228
+                else
229
+                    this.initPublicApis(socket)
229
             })
230
             })
230
             this.wsServer.listen(this.port, this.visibility)
231
             this.wsServer.listen(this.port, this.visibility)
231
         }catch(e){
232
         }catch(e){
234
         }
235
         }
235
     }
236
     }
236
 
237
 
237
-    protected initApis(socket){
238
+    protected initApis(socket:Socket){
238
         const adminRPCs:socketioRPC[] = [
239
         const adminRPCs:socketioRPC[] = [
239
             {
240
             {
240
                 name: 'info',
241
                 name: 'info',
241
                 type: 'call',
242
                 type: 'call',
242
-                visibility: 'private',
243
                 func: async () => rpcInfos
243
                 func: async () => rpcInfos
244
             }
244
             }
245
         ]
245
         ]
246
 
246
 
247
         const rpcInfos:ExtendedRpcInfo[] = [
247
         const rpcInfos:ExtendedRpcInfo[] = [
248
             ...rpcHooker(socket, "Admin", adminRPCs, false),
248
             ...rpcHooker(socket, "Admin", adminRPCs, false),
249
-            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportRPCs()))
249
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, [...exporter.exportPublicRPCs(), ...exporter.exportRPCs()]))
250
+        ]
251
+    }
252
+
253
+    protected initPublicApis(socket:Socket){
254
+        const adminRPCs:socketioRPC[] = [
255
+            {
256
+                name: 'info',
257
+                type: 'call',
258
+                func: async () => rpcInfos
259
+            }
260
+        ]
261
+
262
+        const rpcInfos:ExtendedRpcInfo[] = [
263
+            ...rpcHooker(socket, "Admin", adminRPCs, false),
264
+            ...this.rpcExporters.flatMap(exporter => rpcHooker(socket, exporter.name, exporter.exportPublicRPCs()))
250
         ]
265
         ]
251
     }
266
     }
252
 }
267
 }

src/frontend/RPCaller.ts → src/frontend/RPCSocket.ts View File

1
-import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction } from "../backend/RPCSocketServer";
1
+import { ExtendedRpcInfo, UnhookFunction, callbackFunction, AsyncFunction, Socket } from "../backend/RPCSocketServer";
2
 var bsock = require('bsock')
2
 var bsock = require('bsock')
3
 
3
 
4
 //fix args with defaults like "force = true" -> "force"
4
 //fix args with defaults like "force = true" -> "force"
6
     return str.split("=")[0]
6
     return str.split("=")[0]
7
 }
7
 }
8
 
8
 
9
-type RPCReceiver = {
10
-    [RPCGroup in string]: any
11
-}
12
 
9
 
13
 /**
10
 /**
14
  * Dynamic library to communicate with FrontblockService remotely
11
  * Dynamic library to communicate with FrontblockService remotely
17
  * Will ask it's service for available RPCs and parse them into methods of this object
14
  * Will ask it's service for available RPCs and parse them into methods of this object
18
  * for convenient access.
15
  * for convenient access.
19
  */
16
  */
20
-export class RPCaller implements RPCReceiver{
21
-    private socket
22
-    
23
-    constructor(port:number, server: string, tls: boolean = false){
24
-        this.socket = bsock.connect(port, server, tls)
17
+export class RPCSocket implements Socket{
18
+    private socket: Socket
19
+    constructor(public port:number, private server: string, private tls: boolean = false){
25
     }
20
     }
26
 
21
 
22
+    hook(name, args){
23
+        return this.socket.hook(name, args)
24
+    }
25
+
26
+    unhook(name){
27
+        return this.socket.unhook(name)
28
+    }
29
+
30
+    on(type: "error" | "close", f: (e?: any) => void){
31
+        return this.socket.on(type, name)
32
+    }
33
+
34
+    destroy(){
35
+        return this.socket.destroy()
36
+    }
37
+
38
+    close(){
39
+        return this.socket.close()
40
+    }
41
+
42
+    async call (rpcname: string, ...args: any[]) : Promise<any>{
43
+        return await this.socket.call.apply(this.socket, [rpcname, ...args])
44
+    }
45
+
46
+    async fire(rpcname: string, ...args: any[]) : Promise<any>{
47
+        return await this.socket.fire.apply(this.socket, [rpcname, ...args])
48
+    }
49
+    
27
     async connect(){
50
     async connect(){
51
+        this.socket = await bsock.connect(this.port, this.server, this.tls)
52
+
28
         const info:ExtendedRpcInfo[] = await this.info()
53
         const info:ExtendedRpcInfo[] = await this.info()
29
         info.forEach(i => {
54
         info.forEach(i => {
30
             let f: any
55
             let f: any

+ 1
- 1
src/frontend/webpack.prod.js View File

4
 module.exports = {
4
 module.exports = {
5
   mode: 'production',
5
   mode: 'production',
6
   target: "web",
6
   target: "web",
7
-  entry: path.resolve(__dirname, 'RPCaller.ts'),
7
+  entry: path.resolve(__dirname, 'RPCSocket.ts'),
8
   output: {
8
   output: {
9
       path: path.resolve(__dirname, '../../lib'),
9
       path: path.resolve(__dirname, '../../lib'),
10
       filename: 'RPCaller.min.js',
10
       filename: 'RPCaller.min.js',

+ 5
- 5
test/test.ts View File

1
 import { RPCSocketServer } from '../src/backend/RPCSocketServer'
1
 import { RPCSocketServer } from '../src/backend/RPCSocketServer'
2
 //@ts-ignore
2
 //@ts-ignore
3
-import {RPCaller} from '../src/frontend/RPCaller'
3
+import {RPCSocket} from '../src/frontend/RPCSocket'
4
 
4
 
5
 new RPCSocketServer(20000, [{
5
 new RPCSocketServer(20000, [{
6
     name: "HelloWorldRPCGroup",
6
     name: "HelloWorldRPCGroup",
7
+    exportPublicRPCs: () => [],
7
     exportRPCs: () => [{
8
     exportRPCs: () => [{
8
         type: 'call',
9
         type: 'call',
9
         name: 'echo',
10
         name: 'echo',
10
         func: async (s:string) => s,
11
         func: async (s:string) => s,
11
-        visibility: 'private'
12
-    }]
13
-}] )
12
+    }],
13
+}])
14
 
14
 
15
-const caller = new RPCaller(20000, 'localhost')
15
+const caller = new RPCSocket(20000, 'localhost')
16
 caller.connect().then(_ => {
16
 caller.connect().then(_ => {
17
     caller.info().then(console.log)
17
     caller.info().then(console.log)
18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log)
18
     caller["HelloWorldRPCGroup"].echo("x").then(console.log)

Loading…
Cancel
Save