Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NPM Imports
- let net = require(`net`);
- let exec = require(`child_process`).exec;
- class Connection {
- constructor(app) {
- this.app = app;
- this.app.collections.connections = new this.app.utils.Collection();
- }
- connect(id, cmd, connection = false) {
- return new Promise(async (resolve, reject) => {
- let res = (await this.app.mysql.query({ daemon: true, query: `SELECT * FROM daemon WHERE id = "${id}";` }));
- let authToken = res[0].token.slice(2, 14);
- this.client = new net.Socket();
- let con = this.client.connect(res[0].port, res[0].ip);
- this.app.collections.connections.set(id, con);
- con.write(`auth ${authToken}\n`);
- con.on(`data`, (data) => {
- data = data.toString('utf8');
- this.app.utils.Logger.debug(`DAEMON: (ID: ${res[0].id}, NAME: ${res[0].name}) => ${data.split(`\n`)[0]}`);
- let token = data.split(`:`)[1];
- if (data.includes(`token :`)) exec(`php ./src/handlers/php/genCodeword.php ${token}`, function (err, stdout, stderr) { con.write(`codeword: ${stdout}\n`); });
- });
- con.on(`error`, (err) => {
- this.app.utils.Logger.error(err);
- return reject(err);
- });
- return resolve(con);
- });
- }
- command(id, cmd) {
- return new Promise(async (resolve, reject) => {
- let con = this.client.connections.get(id);
- if (!con) con = (await this.connect(1));
- con.write(`${cmd}\n`);
- con.on(`data`, (data) => { console.log(`2`, data) });
- });
- }
- };
- module.exports = Connection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement