Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // NPM Imports
  2. let net = require(`net`);
  3. let exec = require(`child_process`).exec;
  4.  
  5. class Connection {
  6. constructor(app) {
  7. this.app = app;
  8. this.app.collections.connections = new this.app.utils.Collection();
  9. }
  10.  
  11. connect(id, cmd, connection = false) {
  12. return new Promise(async (resolve, reject) => {
  13. let res = (await this.app.mysql.query({ daemon: true, query: `SELECT * FROM daemon WHERE id = "${id}";` }));
  14. let authToken = res[0].token.slice(2, 14);
  15. this.client = new net.Socket();
  16. let con = this.client.connect(res[0].port, res[0].ip);
  17. this.app.collections.connections.set(id, con);
  18. con.write(`auth ${authToken}\n`);
  19. con.on(`data`, (data) => {
  20. data = data.toString('utf8');
  21. this.app.utils.Logger.debug(`DAEMON: (ID: ${res[0].id}, NAME: ${res[0].name}) => ${data.split(`\n`)[0]}`);
  22. let token = data.split(`:`)[1];
  23. if (data.includes(`token :`)) exec(`php ./src/handlers/php/genCodeword.php ${token}`, function (err, stdout, stderr) { con.write(`codeword: ${stdout}\n`); });
  24. });
  25. con.on(`error`, (err) => {
  26. this.app.utils.Logger.error(err);
  27. return reject(err);
  28. });
  29. return resolve(con);
  30. });
  31. }
  32.  
  33. command(id, cmd) {
  34. return new Promise(async (resolve, reject) => {
  35. let con = this.client.connections.get(id);
  36. if (!con) con = (await this.connect(1));
  37. con.write(`${cmd}\n`);
  38. con.on(`data`, (data) => { console.log(`2`, data) });
  39. });
  40. }
  41. };
  42.  
  43. module.exports = Connection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement