<commit_before>/*    Copyright 2019-2021 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);
const Sensor = require('./Sensor.js').Sensor;
const sclient = require('../util/redis_manager.js').getSubscriptionClient();
const sem = require('../sensor/SensorEventManager.js').getInstance();
const ip = require('ip');

const platformLoader = require('../platform/PlatformLoader.js');
const platform = platformLoader.getPlatform();
const f = require('../net2/Firewalla.js');
const dhcphookForGoldFile = `${f.getFireRouterConfigFolder()}/dhcp/conf/dhcpScript.conf`;
const fs = require('fs');
const Promise = require('bluebird');
Promise.promisifyAll(fs);
const DNSMASQ = require('../extension/dnsmasq/dnsmasq.js');
const dnsmasq = new DNSMASQ();

class DnsmasqDhcpSensor extends Sensor {
    async run() {
        if (platform.getName() == 'gold') {
            await fs.writeFileAsync(dhcphookForGoldFile, `dhcp-script=/home/pi/firewalla/extension/dnsmasq/dhcp_hook.sh`);
            await dnsmasq.scheduleRestartDHCPService();
        }
        sclient.on("message", (channel, message) => {
            if (channel == 'dnsmasq.dhcp.lease') {
                if (message) {
                    try {
                        const host = JSON.parse(message);
                        if (host && host.mac) {
                            this.processHost(host);
                        }
                    } catch (e) {
                        log.warn("Parse dnsmasq.dhcp.lease messge error", e, message)
                    }
                }
            }
        });
        sclient.subscribe("dnsmasq.dhcp.lease");
    }
    processHost(host) {
        const action = host.action;
        if (action == 'del') return;
        let hostInfo = {
            mac: host.mac,
            bname: host.hostname,
            from: "dnsmasq.dhcp.lease"
        };
        if (ip.isV4Format(host.ip)) {
            hostInfo.ipv4 = host.ip;
            hostInfo.ipv4Addr = host.ip;
        } else if (ip.isV6Format(host.ip)) {
            hostInfo.ipv6Addr = host.ip
        } else {
            return;
        }
        sem.emitEvent({
            type: "DeviceUpdate",
            message: `Found a device via dnsmasq dhcp lease ${hostInfo.ipv4} ${hostInfo.mac}`,
            host: hostInfo
        });
    }
}

module.exports = DnsmasqDhcpSensor;
<commit_msg>remove gold hard code for dnsmasq<commit_after>/*    Copyright 2019-2021 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);
const Sensor = require('./Sensor.js').Sensor;
const sclient = require('../util/redis_manager.js').getSubscriptionClient();
const sem = require('../sensor/SensorEventManager.js').getInstance();
const ip = require('ip');

const platformLoader = require('../platform/PlatformLoader.js');
const platform = platformLoader.getPlatform();
const f = require('../net2/Firewalla.js');
const dhcphook = `${f.getFireRouterConfigFolder()}/dhcp/conf/dhcpScript.conf`;
const fs = require('fs');
const Promise = require('bluebird');
Promise.promisifyAll(fs);
const DNSMASQ = require('../extension/dnsmasq/dnsmasq.js');
const dnsmasq = new DNSMASQ();

class DnsmasqDhcpSensor extends Sensor {
    async run() {

      if(platform.isFireRouterManaged()) {
        await fs.writeFileAsync(dhcphook, `dhcp-script=${f.getFirewallaHome()}/extension/dnsmasq/dhcp_hook.sh`);
        await dnsmasq.scheduleRestartDHCPService();
      }

        sclient.on("message", (channel, message) => {
            if (channel == 'dnsmasq.dhcp.lease') {
                if (message) {
                    try {
                        const host = JSON.parse(message);
                        if (host && host.mac) {
                            this.processHost(host);
                        }
                    } catch (e) {
                        log.warn("Parse dnsmasq.dhcp.lease messge error", e, message)
                    }
                }
            }
        });
        sclient.subscribe("dnsmasq.dhcp.lease");
    }
    processHost(host) {
        const action = host.action;
        if (action == 'del') return;
        let hostInfo = {
            mac: host.mac,
            bname: host.hostname,
            from: "dnsmasq.dhcp.lease"
        };
        if (ip.isV4Format(host.ip)) {
            hostInfo.ipv4 = host.ip;
            hostInfo.ipv4Addr = host.ip;
        } else if (ip.isV6Format(host.ip)) {
            hostInfo.ipv6Addr = host.ip
        } else {
            return;
        }
        sem.emitEvent({
            type: "DeviceUpdate",
            message: `Found a device via dnsmasq dhcp lease ${hostInfo.ipv4} ${hostInfo.mac}`,
            host: hostInfo
        });
    }
}

module.exports = DnsmasqDhcpSensor;

{'fraction_non_alphanumeric': 0.12056384742951907, 'fraction_numerical': 0.005638474295190713, 'mean_word_length': 2.1493472584856397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5420440', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1764, 'n_words': 451}