<commit_before>/*    Copyright 2016-2020 Firewalla Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';

process.title = "FireMain";

const log = require("./logger.js")(__filename);

log.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
log.info("Main Starting ");
log.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

require('events').EventEmitter.prototype._maxListeners = 100;

const sem = require('../sensor/SensorEventManager.js').getInstance();

const fs = require('fs');


const platform = require('../platform/PlatformLoader.js').getPlatform();

function updateTouchFile() {
  const mainTouchFile = "/dev/shm/main.touch";

  fs.open(mainTouchFile, 'w', (err, fd) => {
    if(!err) {
      fs.close(fd, () => { })
    }
  })
}

const bone = require("../lib/Bone.js");

const firewalla = require("./Firewalla.js");

const ModeManager = require('./ModeManager.js')
const mode = require('./Mode.js')
const WifiInterface = require('./WifiInterface.js');

const fireRouter = require('./FireRouter.js')

// api/main/monitor all depends on sysManager configuration
const sysManager = require('./SysManager.js');

const sensorLoader = require('../sensor/SensorLoader.js');

const fc = require('./config.js')
const cp = require('child_process');

let interfaceDetected = false;

if(!bone.isAppConnected()) {
  log.info("Waiting for cloud token created by kickstart job...");
}

detectInterface()
resetModeInInitStage()
run0()

async function detectInterface() {
  await fireRouter.waitTillReady()
  interfaceDetected = true;
}

// Start recording network status whether cloud is ready or not
fireRouter.waitTillReady().then(() => {
  const NetworkStatsSensor = sensorLoader.initSingleSensor('NetworkStatsSensor');
  NetworkStatsSensor.run()
})

const boneSensor = sensorLoader.initSingleSensor('BoneSensor');

async function run0() {
  const isModeConfigured = await mode.isModeConfigured();

  if (interfaceDetected && bone.cloudready()==true &&
      bone.isAppConnected() &&
      sysManager.isConfigInitialized()) {
    await boneSensor.checkIn().catch((err) => {
      log.error("Got error when checkin, err", err);
      // running firewalla in non-license mode if checkin failed, do not return, continue run()
    })<commit_msg>bug fix<commit_after>/*    Copyright 2016-2020 Firewalla Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';

process.title = "FireMain";

const log = require("./logger.js")(__filename);

log.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
log.info("Main Starting ");
log.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

require('events').EventEmitter.prototype._maxListeners = 100;

const sem = require('../sensor/SensorEventManager.js').getInstance();

const fs = require('fs');


const platform = require('../platform/PlatformLoader.js').getPlatform();

function updateTouchFile() {
  const mainTouchFile = "/dev/shm/main.touch";

  fs.open(mainTouchFile, 'w', (err, fd) => {
    if(!err) {
      fs.close(fd, () => { })
    }
  })
}

const bone = require("../lib/Bone.js");

const firewalla = require("./Firewalla.js");

const ModeManager = require('./ModeManager.js')
const mode = require('./Mode.js')
const WifiInterface = require('./WifiInterface.js');

const fireRouter = require('./FireRouter.js')

// api/main/monitor all depends on sysManager configuration
const sysManager = require('./SysManager.js');

const sensorLoader = require('../sensor/SensorLoader.js');

const fc = require('./config.js')
const cp = require('child_process');

let interfaceDetected = false;

if(!bone.isAppConnected()) {
  log.info("Waiting for cloud token created by kickstart job...");
}

detectInterface()
resetModeInInitStage()
run0()

async function detectInterface() {
  await fireRouter.waitTillReady()
  interfaceDetected = true;
}

// Start recording network status whether cloud is ready or not
fireRouter.waitTillReady().then(() => {
  const NetworkStatsSensor = sensorLoader.initSingleSensor('NetworkStatsSensor');
  NetworkStatsSensor.run()
})

const boneSensor = sensorLoader.initSingleSensor('BoneSensor');

async function run0() {
  const isModeConfigured = await mode.isModeConfigured();

  if (interfaceDetected && bone.cloudready()==true &&
      bone.isAppConnected() &&
      isModeConfigured &&
      sysManager.isConfigInitialized()) {
    await boneSensor.checkIn().catch((err) => {
      log.error("Got error when checkin, err", err);
      // running firewalla in non-license mode if checkin failed, do not return, continue run()
    })
{'fraction_non_alphanumeric': 0.1797945205479452, 'fraction_numerical': 0.004794520547945206, 'mean_word_length': 4.3489010989010985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2299235', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1638, 'n_words': 478}