UNPKG

domotz-remote-pawn

Version:

Domotz Agent

113 lines (94 loc) 3.95 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Tommaso Latini <tommaso@domotz.com> on 27/08/15. */ const DOMOTZ_NMAP = 'domotz_nmap --host-timeout 30s -oX - '; const IPV4_BITS = 32; const DOMOTZ_NETMASK = 24; const DEFAULT_MAX_SUPPORTED_NETMASK = 22; const SCANNED_IP_PER_NMAP = Math.pow(2, IPV4_BITS - DOMOTZ_NETMASK); const DEFAULT_PORTS = [ 80, 8000, 8001, 8080, 8888, 5000, 3000, // HTTP 3389, // RDP 22, // SSH 23, // TelNet 5900 // VNC ]; function getPortString(discovery) { return (discovery ? discovery.ports : DEFAULT_PORTS).join(','); } function getMaxSupportedNetmask(discovery) { return discovery && discovery.max_ip_netmask ? discovery.max_ip_netmask : DEFAULT_MAX_SUPPORTED_NETMASK; } var factory = function (ip, configuration, nmapBlackList) { var discovery = configuration.discovery; var portString = getPortString(discovery); var maxNetmask = getMaxSupportedNetmask(discovery); function getStandardNmapCmd() { var ipList = nmapBlackList.getDeviceToExclude(); return (process.getuid() === 0 ? '' : 'sudo ') + DOMOTZ_NMAP + (ipList.length ? ('--exclude ' + ipList.join(',')) + ' ' : ' '); } function getPingNmapCmd(networkList, arpDisabled) { return getStandardNmapCmd() + '-sP' + (arpDisabled ? ' --disable-arp-ping ' : ' ') + networkList; } function getFastNmapCmd(addr, netmask, arpDisabled) { if (netmask < maxNetmask) { console.warn('Netmask Out of Range [%d-%d]: %d', maxNetmask, DOMOTZ_NETMASK, netmask); netmask = maxNetmask; } var commands = [], baseIP = ip.cidr(addr + '/' + netmask), parallel = Math.pow(2, DOMOTZ_NETMASK - netmask); for (var i = 0; i < parallel; i++) { var network = ip.fromLong(ip.toLong(baseIP) + i * SCANNED_IP_PER_NMAP); var cidr = ((netmask > DOMOTZ_NETMASK) ? netmask : DOMOTZ_NETMASK); var cmd = getPingNmapCmd(network + '/' + cidr, arpDisabled); commands.push(cmd); } return commands; } function getRetryCmd(ipList, arpDisabled) { return getPingNmapCmd(ipList.join(' '), arpDisabled); } function getRetryArpDisabledCmd(ipList) { var targets = ''; for (var i = 0; i < ipList.length; i++) { targets += ipList[i] + ' '; } return getPingNmapCmd(targets, true); } function getPortNmapCmd(ip) { // using TCP SYN scanning (https://nmap.org/book/man-port-scanning-techniques.html) return getStandardNmapCmd() + '-sS -p' + portString + ' ' + ip; } function getExplicitPortNmapCmd(ip, explicitPortString) { // using TCP SYN scanning (https://nmap.org/book/man-port-scanning-techniques.html) return getStandardNmapCmd() + '-n -sS -Pn --disable-arp-ping -p' + explicitPortString + ' ' + ip; } return { getPingNmapCmd: getPingNmapCmd, getRetryCmd: getRetryCmd, getRetryArpDisabledCmd: getRetryArpDisabledCmd, getFastNmapCmd: getFastNmapCmd, getPortNmapCmd: getPortNmapCmd, getExplicitPortNmapCmd: getExplicitPortNmapCmd }; }; module.exports.factory = factory;