'use strict';

const io2 = require('socket.io-client');
const url = "https://api.firewalla.com";
const path = "/socket";

const fs = require('fs');
const Promise = require('bluebird');
Promise.promisifyAll(fs);


const cp = require('child_process');
const mac =  getSignatureMac();
const memory = getTotalMemory()

function getSignatureMac() {
  try {
    const mac = cp.execSync("cat /sys/class/net/eth0/address", { encoding: 'utf8' });
    return mac && mac.trim().toUpperCase();
  } catch(err) {
    return "";
  }
}

const socket = io2(url, { path: path, transports: ['websocket'], 'upgrade': false });

function log(message) {
  console.log(new Date(), message);
}

function isBooted() {
  const fw_hb_file = '/dev/shm/fw_heartbeat';
  try{
    if (fs.existsSync(fw_hb_file)) {
      return false;
    } else {
      log("System was booted.");
      cp.execSync(`touch ${fw_hb_file}`)
      return true;
    }
  } catch (err) {
    return false;
  }
}

function getTotalMemory() {
  const result = exec("free -m | awk '/Mem:/ {print $2}'");
  return result && result.stdout && result.stdout.replace(/\n$/, '')
}

function getSysinfo(status) {
  const booted = isBooted();
  const uptime = require('os').uptime()
  return {booted, mac, memory, status, uptime};
}

function update(status) {
  const info = getSysinfo(status);
  socket.emit('update', info);
}

const job = setTimeout(() => {
  update("schedule");
}, 30 * 3600 * 1000);

socket.on('connect', () => {
  log("Connected to heartbeat server.");
  update('connect');
});

socket.on('disconnect', () => {
  log("Disconnected from heartbeat server.");
});

socket.on("update", (data) => {
  update("cloud");
});

socket.on('reconnect', () => {
  log("Reconnected to heartbeat server.");
  update('reconnect');
});
@@ -1,8 +1,1185 @@
+/*    Copyright 2020 Firewalla INC%0A *%0A *    This program is free software: you can redistribute it and/or  modify%0A *    it under the terms of the GNU Affero General Public License, version 3,%0A *    as published by the Free Software Foundation.%0A *%0A *    This program is distributed in the hope that it will be useful,%0A *    but WITHOUT ANY WARRANTY; without even the implied warranty of%0A *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A *    GNU Affero General Public License for more details.%0A *%0A *    You should have received a copy of the GNU Affero General Public License%0A *    along with this program.  If not, see %3Chttp://www.gnu.org/licenses/%3E.%0A */%0A%0A /*%0A  * Box (collect the following info)%0A  *   box states: just rebooted, this process crashed and connect, reconnected the socket io, scheduled update%0A  *   memory size%0A  *   mac addresses of ethX%0A  *   speed of each ethX%0A  *   Bluetooth mac address%0A  *   uptime%0A  *   uname -m%0A  *   box eid (if have)%0A  *   box license (8 char prefix only)%0A  *   gateway mac address (the first three bytes)%0A  *   cpu temp%0A  *   current timestamp%0A  *   hash of firerouter (if applicable)%0A  *   hash of firewalla%0A  */%0A%0A
 'use str
@@ -1483,16 +1483,52 @@
 Memory()
+%0Aconst gatewayMac = getGatewayMac();
 %0A%0Afuncti
@@ -1723,24 +1723,409 @@
  %22%22;%0A  %7D%0A%7D%0A%0A
+function getGatewayMac() %7B%0A  const sysManager = require('../net2/SysManager.js');%0A  const inter = sysManager.getDefaultWanInterface()%0A  const gateway = inter.gateway_ip;%0A  const arpCmd = %60arp -a -n %7C grep $%7BgatewayIP%7D -w %7C awk '%7Bprint $4%7D'%60;%0A  const result = cp.exec(arpCmd);%0A  const mac = result.stdout;%0A  if (mac) %7B%0A    return mac.substring(0,11);%0A  %7D else %7B%0A    return null;%0A  %7D%0A%7D%0A%0A
 const socket
@@ -2594,16 +2594,19 @@
 esult = 
+cp.
 exec(%22fr
@@ -2811,16 +2811,16 @@
 ptime()%0A
-
   return
@@ -2828,16 +2828,28 @@
 %7Bbooted,
+ gatewayMac,
  mac, me

add gatewayMac