Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RPCaller.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var bsock = require('bsock');
  4. //fix args with defaults like "force = true" -> "force"
  5. function stripAfterEquals(str) {
  6. return str.split("=")[0];
  7. }
  8. /**
  9. * Dynamic library to communicate with FrontblockService remotely
  10. *
  11. * This will be automatically injected into the webpages served by FrontblockService
  12. * Will ask it's service for available RPCs and parse them into methods of this object
  13. * for convenient access.
  14. */
  15. class RPCaller {
  16. constructor(port, server, tls = false) {
  17. this.socket = bsock.connect(port, server, tls);
  18. }
  19. async connect() {
  20. const info = await this.info();
  21. info.forEach(i => {
  22. let f;
  23. switch (i.type) {
  24. case 'call':
  25. f = this.callGenerator(i.uniqueName, i.argNames);
  26. break;
  27. case 'hook':
  28. f = this.hookGenerator(i.uniqueName, i.argNames);
  29. break;
  30. case 'unhook':
  31. f = this.unhookGenerator(i.uniqueName, i.argNames);
  32. break;
  33. }
  34. if (this[i.owner] == null)
  35. this[i.owner] = {};
  36. this[i.owner][i.name] = f;
  37. this[i.owner][i.name].bind(this);
  38. });
  39. }
  40. async info() {
  41. return await this.socket.call('info');
  42. }
  43. callGenerator(fnName, fnArgs) {
  44. const headerArgs = fnArgs.join(",");
  45. const argParams = fnArgs.map(stripAfterEquals).join(",");
  46. return eval('( () => async (' + headerArgs + ') => { return await this.socket.call("' + fnName + '", ' + argParams + ')} )()');
  47. }
  48. hookGenerator(fnName, fnArgs) {
  49. const headerArgs = fnArgs.join(",");
  50. const argParams = fnArgs.map(stripAfterEquals).join(",");
  51. return eval(`( () => async (` + headerArgs + (headerArgs.length !== 0 ? "," : "") + ` callback) => {
  52. const r = await this.socket.call("` + fnName + `", ` + argParams + `)
  53. if(r.uid != null){
  54. this.socket.hook(res.uid, callback)
  55. }
  56. return res
  57. } )()`);
  58. }
  59. unhookGenerator(fnName, fnArgs) {
  60. const headerArgs = fnArgs.join(",");
  61. const argParams = fnArgs.map(stripAfterEquals).join(",");
  62. if (fnArgs.length != 1)
  63. console.error("UnhookFunction", fnName, "specified more than one argument: (" + headerArgs + ")");
  64. return eval(`( () => async (` + headerArgs + `) => {
  65. const r = await this.socket.call("` + fnName + `", ` + argParams + `)
  66. this.socket.unhook(` + argParams + `)
  67. return res
  68. } )()`);
  69. }
  70. }
  71. exports.RPCaller = RPCaller;