<commit_before>/*    Copyright 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';

const log = require('../net2/logger.js')(__filename, 'info');
const { Sensor } = require('./Sensor.js');
const sem = require('../sensor/SensorEventManager.js').getInstance();
const MessageBus = require('../net2/MessageBus.js');
const fc = require('../net2/config.js');

const HostManager = require('../net2/HostManager.js');
const hostManager = new HostManager();
const sysManager = require('../net2/SysManager.js')
const networkProfileManager = require('../net2/NetworkProfileManager.js')

const { Address6 } = require('ip-address')

// const PM2 = require('../alarm/PolicyManager2.js');
// const pm2 = new PM2();

const FEATURE_KEY = 'new_device_tag'

function copyPolicy(policy) {
  try {
    if (!policy) return {}
    return JSON.parse(JSON.stringify(policy))
  } catch(err) {
    log.error('Failed to parse policy', policy, err)
    return {}
  }
}

class NewDeviceTagSensor extends Sensor {

  constructor() {
    super()

    // use dedicated instance as messageBus can't deal with multiple subscribers well
    this.messageBus = new MessageBus('info', 'newDeviceTag')
    this.queue = []
    this.macIndex = {}
  }

  async checkAndExecutePolicy(channel, type, mac, host) {
    try {
      if (this.macIndex[mac]) {
        // ip address is required to find the corresponding policy
        if (!host.ipv4Addr && !host.ipv6Addr) return

        if (!host.ipv4Addr && host.ipv6Addr) {
          const v6Addresses = host.ipv6Addr
            .map(str => new Address6(str))
            .filter(add6 => add6.isValid() && !add6.isLinkLocal())

          log.debug(v6Addresses)
          // only link local v6 address, ignore this event
          if (!v6Addresses.length) return

          host.realV6Address = v6Addresses
        }
        this.messageBus.unsubscribe("DiscoveryEvent", "Device:Updated", mac)
        delete this.macIndex[mac]
      }
      else
        return // already checked

      log.info('Checking new device', host)

      await hostManager.loadPolicyAsync()
      const systemPolicy = copyPolicy(hostManager.policy.newDeviceTag)<commit_msg>ignore Firewalla's mac<commit_after>/*    Copyright 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';

const log = require('../net2/logger.js')(__filename, 'info');
const { Sensor } = require('./Sensor.js');
const sem = require('../sensor/SensorEventManager.js').getInstance();
const MessageBus = require('../net2/MessageBus.js');
const fc = require('../net2/config.js');

const HostManager = require('../net2/HostManager.js');
const hostManager = new HostManager();
const sysManager = require('../net2/SysManager.js')
const networkProfileManager = require('../net2/NetworkProfileManager.js')

const { Address6 } = require('ip-address')

// const PM2 = require('../alarm/PolicyManager2.js');
// const pm2 = new PM2();

const FEATURE_KEY = 'new_device_tag'

function copyPolicy(policy) {
  try {
    if (!policy) return {}
    return JSON.parse(JSON.stringify(policy))
  } catch(err) {
    log.error('Failed to parse policy', policy, err)
    return {}
  }
}

class NewDeviceTagSensor extends Sensor {

  constructor() {
    super()

    // use dedicated instance as messageBus can't deal with multiple subscribers well
    this.messageBus = new MessageBus('info', 'newDeviceTag')
    this.queue = []
    this.macIndex = {}
  }

  async checkAndExecutePolicy(channel, type, mac, host) {
    try {
      if (this.macIndex[mac]) {
        // ip address is required to find the corresponding policy
        if (!host.ipv4Addr && !host.ipv6Addr) return

        if (!host.ipv4Addr && host.ipv6Addr) {
          const v6Addresses = host.ipv6Addr
            .map(str => new Address6(str))
            .filter(add6 => add6.isValid() && !add6.isLinkLocal())

          log.debug(v6Addresses)
          // only link local v6 address, ignore this event
          if (!v6Addresses.length) return

          host.realV6Address = v6Addresses
        }
        this.messageBus.unsubscribe("DiscoveryEvent", "Device:Updated", mac)
        delete this.macIndex[mac]
      }
      else
        return // already checked

      if (sysManager.isMyMac(mac)) {
        log.debug('Skipping firewalla mac', host)
        return
      }

      log.info('Checking new device', host)

      await hostManager.loadPolicyAsync()
      const systemPolicy = copyPolicy(hostManager.policy.newDeviceTag)
{'fraction_non_alphanumeric': 0.11750176429075512, 'fraction_numerical': 0.010938602681721948, 'mean_word_length': 3.0120311394196744, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '7561512', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1619, 'n_words': 496}