All files / web_socket/agents/client agent.js

0% Statements 0/50
0% Branches 0/14
0% Functions 0/12
0% Lines 0/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                                                                                                                                                                                             
/* eslint-disable import/extensions */
/* eslint-disable no-console */
 
const io = require('socket.io-client');
const os = require('os');
const fs = require('fs');
const path = require('path');
const agentPrinter = require('./agentPrinter');
 
const currentDir = __dirname;
const tempFolderDir = path.resolve(currentDir, 'temp');
 
require('log-timestamp');
 
const makePaths = (pathsFolder) =>
  pathsFolder.forEach(
    (pathFolder) =>
      !fs.existsSync(pathFolder) &&
      fs.mkdirSync(pathFolder, { recursive: true })
  );
 
// Initial folders create
makePaths([tempFolderDir]);
 
// Initialize Agents Modules
agentPrinter.initialize(currentDir, tempFolderDir);
 
// ############## EXEMPLO ################
// npm run -- run-agent-client -code teste -url "http://localhost:3333/agents" -token "123"
// #######################################
 
// Params input
const getParameterValue = (parameterName) => {
  const index = process.argv.indexOf(parameterName);
  if (index !== -1 && index + 1 < process.argv.length) {
    return process.argv[index + 1].trim();
  }
  return null;
};
 
// Parametros de entrada
 
const code = getParameterValue('-code');
const token = getParameterValue('-token');
const url = getParameterValue('-url'); // 'http://localhost:3333/agents'
 
if (!code) throw new Error('Code required');
if (!token) throw new Error('Token required');
if (!url) throw new Error('Url required');
 
// DADOS DA MAQUINA
const getOsInformation = () => {
  // Nome da maquina
  const hostname = os.hostname();
 
  // Endereços IP de rede
  const interfaces = os.networkInterfaces();
  const ips = Object.keys(interfaces)
    .map((key) => interfaces[key])
    .flat()
    .filter((info) => info.family === 'IPv4' && !info.internal)
    .map((info) => info.address);
 
  // Sistema operacional
  const osType = os.type();
  const osRelease = os.release();
 
  const dados = [
    `ID: ${hostname}`,
    `IP: ${ips}`,
    `SO: ${osType} - ${osRelease}`,
  ];
 
  return dados.join('\n');
};
 
// CONNECT TO HOST
const socketConnect = () => {
  const socket = io(url, {
    transportOptions: {
      polling: {
        extraHeaders: {
          Authorization: `Bearer ${token}`,
          AppCode: code,
        },
      },
    },
  });
 
  // Socket events
  agentPrinter.addSocketEvents(socket);
 
  socket.on('connect', () => {
    console.log('Connected');
  });
 
  socket.on('initialized', () =>
    socket.emit('initialized', getOsInformation())
  );
 
  socket.on('disconnect', () => {
    console.log('Exited');
  });
 
  socket.on('connect_error', (err) => {
    console.log('Error: ', err.message);
  });
};
 
socketConnect();