master
IoTcat 5 years ago
parent 7501895874
commit 2ab9c12b48
  1. 1
      node/node_modules/macfromip/.npmignore
  2. 30
      node/node_modules/macfromip/README.md
  3. 10
      node/node_modules/macfromip/example.js
  4. 160
      node/node_modules/macfromip/macfromip.js
  5. 62
      node/node_modules/macfromip/package.json
  6. 22
      node/node_modules/macfromip/test/test-macfromip.js
  7. 5
      node/package-lock.json
  8. 4
      node/package.json
  9. 89
      node/wiot.js

@ -0,0 +1 @@
npm-debug.log

@ -0,0 +1,30 @@
macfromip
=========
## Synopsis
* Nodejs script;
* Gets a MAC address from a LAN IP address;
* Only works on linux, OSX and win32 platforms;
## Code Example
```
var macfromip = require('macfromip');
macfromip.getMac('192.168.2.169', function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
```
## Installation
```
npm install macfromip
```
## TODO List:
Complete list on [Trello](https://trello.com/b/B1WM4gbZ/macfromip)

@ -0,0 +1,10 @@
var macfromip = require('./macfromip.js');
macfromip.getMac('192.168.2.57', function(err, data){
if(err){
console.log(err);
}
else{
console.log(data);
}
});

@ -0,0 +1,160 @@
/* jshint node: true */
'use strict';
var macfromip = exports;
var cp = require('child_process');
var os = require('os');
var MACADDRESS_LENGTH = 17;
macfromip.isEmpty = function(value){
return ((value === null) || (typeof value === 'undefined') || 0 === value.length);
};
macfromip.isString = function(value){
if(macfromip.isEmpty(value)){
throw new Error('Expected a not null value');
}
if (typeof value === 'string') {
return true;
}
return false;
};
macfromip.isIpAddress = function(ipaddress){
if(!macfromip.isString(ipaddress)){
throw new Error('Expected a string');
}
/* Thanks to http://www.w3resource.com/javascript/form/ip-address-validation.php#sthash.kBJql3HS.dpuf */
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress.trim())){
return (true);
}
return (false);
};
macfromip.ipIsSelf = function(ipaddress){
var ifaces = os.networkInterfaces();
var selfIps = new Array();
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
selfIps.push(iface.address);
});
});
var index;
for (index = 0; index < selfIps.length; ++index) {
if(selfIps[index] === ipaddress){
return true;
}
}
return false;
};
macfromip.getMacInLinux = function(ipAddress, callback){
// OSX requires -c switch first
var ls = cp.exec('ping -c 1 ' + ipAddress,
function(error, stdout, stderr) {
if (error !== null) {
callback('IP address unreachable', 'exec error: ' + error);
return;
}
if (stderr !== null && stderr !== '') {
callback('IP address unreachable', 'stderr: ' + stderr);
return;
}
var ls2 = cp.exec('arp -a',
function(error2, stdout2, stderr2) {
if (error2 !== null) {
callback('IP address unreachable', 'exec error: ' + error2);
return;
}
if (stderr2 !== null && stderr2 !== '') {
callback('IP address unreachable', 'stderr: ' + stderr2);
return;
}
stdout2 = (stdout2.substring(stdout2.indexOf(ipAddress) + (ipAddress.length + 5))).substring(MACADDRESS_LENGTH, 0);
callback(false, stdout2);
return;
});
});
};
macfromip.getMacInWin32 = function(ipAddress, callback){
var ls = cp.exec('ping ' + ipAddress + ' -n 1',
function(error, stdout, stderr) {
if (error !== null) {
callback('IP address unreachable', 'exec error: ' + error);
return;
}
if (stderr !== null && stderr !== '') {
callback('IP address unreachable', 'stderr: ' + stderr);
return;
}
var ls2 = cp.exec('arp -a',
function(error2, stdout2, stderr2) {
if (error2 !== null) {
callback('IP address unreachable', 'exec error: ' + error2);
return;
}
if (stderr2 !== null && stderr2 !== '') {
callback('IP address unreachable', 'stderr: ' + stderr2);
return;
}
var offset = 22 - ipAddress.length;
stdout2 = (stdout2.substring(stdout2.indexOf(ipAddress) + (ipAddress.length + offset))).substring(MACADDRESS_LENGTH, 0);
callback(false, stdout2);
return;
});
});
};
macfromip.getMac = function(ipAddress, callback) {
if(!macfromip.isIpAddress(ipAddress)) {
throw new Error("The value you entered is not a valid IP address");
}
if(macfromip.ipIsSelf(ipAddress)){
throw new Error("The IP address cannot be self");
}
switch(os.platform()){
case 'linux':
macfromip.getMacInLinux(ipAddress, function(err, mac){
callback(err, mac);
});
break;
case 'win32':
macfromip.getMacInWin32(ipAddress, function(err, mac){
callback(err, mac);
});
break;
// OSX
case 'darwin':
macfromip.getMacInLinux(ipAddress, function(err, mac){
callback(err, mac);
});
break;
default:
callback('Unsupported platform: ' + os.platform(), null);
break;
}
};

@ -0,0 +1,62 @@
{
"_from": "macfromip",
"_id": "macfromip@1.1.1",
"_inBundle": false,
"_integrity": "sha1-uy9GiBDMOm0ZFxabj4InHuMOTd0=",
"_location": "/macfromip",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "macfromip",
"name": "macfromip",
"escapedName": "macfromip",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/macfromip/-/macfromip-1.1.1.tgz",
"_shasum": "bb2f468810cc3a6d1917169b8f82271ee30e4ddd",
"_spec": "macfromip",
"_where": "E:\\Arduino_project\\wIoT\\node",
"author": {
"name": "bcamarneiro",
"email": "camarneirobruno@gmail.com"
},
"bugs": {
"url": "https://github.com/bcamarneiro/macfromip/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Paul Cook"
},
{
"name": "Sean Nessworthy",
"email": "sean@nessworthy.me"
}
],
"deprecated": false,
"description": "On given an IP Address, will attempt to identify that IP's MAC address.",
"directories": {
"test": "test"
},
"homepage": "https://github.com/bcamarneiro/macfromip",
"keywords": [
"MAC",
"IP",
"Address"
],
"license": "MIT",
"main": "macfromip.js",
"name": "macfromip",
"repository": {
"type": "git",
"url": "git+https://github.com/bcamarneiro/macfromip.git"
},
"version": "1.1.1"
}

@ -0,0 +1,22 @@
var macfromip = require('../macfromip.js');
exports['isIpAddress'] = function (test) {
test.equal(macfromip.isEmpty('0'), false);
test.equal(macfromip.isEmpty(' '), false);
test.equal(macfromip.isEmpty(''), true);
test.equal(macfromip.isEmpty(null), true);
test.equal(macfromip.isEmpty(), true);
test.equal(macfromip.isString(false), false);
test.equal(macfromip.isString(1233536), false);
test.equal(macfromip.isString('1233536'), true);
test.equal(macfromip.isString('#$%&asda3445'), true);
test.equal(macfromip.isIpAddress('127.0.0.1'), true);
test.equal(macfromip.isIpAddress('255.255.255.255'), true);
test.equal(macfromip.isIpAddress('0.0.0.0'), true);
test.equal(macfromip.isIpAddress('asdasdasdghdsgsdg'), false);
test.equal(macfromip.isIpAddress('192.168.2.5a'), false);
test.done();
};

@ -7,6 +7,11 @@
"@network-utils/arp-lookup": {
"version": "https://registry.npm.taobao.org/@network-utils/arp-lookup/download/@network-utils/arp-lookup-1.0.3.tgz"
},
"macfromip": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/macfromip/-/macfromip-1.1.1.tgz",
"integrity": "sha1-uy9GiBDMOm0ZFxabj4InHuMOTd0="
},
"node-arp": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/node-arp/-/node-arp-1.0.6.tgz",

@ -1,6 +1,6 @@
{
"name": "wiot",
"version": "0.0.4",
"version": "0.0.5",
"description": "An awesome iot system for web developers~",
"main": "wiot.js",
"scripts": {
@ -25,7 +25,7 @@
"homepage": "https://github.com/IoTcat/wIoT-nodeJS#readme",
"dependencies": {
"@network-utils/arp-lookup": "^1.0.3",
"node-arp": "^1.0.6",
"macfromip": "^1.1.1",
"ping": "^0.2.2"
}
}

@ -2,9 +2,10 @@
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-05-04 18:59:49
* @Last Modified by:
* @Last Modified time: 2019-05-05 02:31:52
* @Last Modified time: 2019-05-05 03:28:11
*/
var wiot = function (o_params) {
var wiot = function (o_params) {
var o = {
MAC: "",
pin: {
@ -27,7 +28,8 @@
LOW: 0,
data: {},
ip: "default",
ip_range: "192.168.4",
ip_range: "192.168.0",
localIP: "127.0.0.1",
version: "",
errDelayTime: 2000,
okDelayTime: 30,
@ -36,7 +38,7 @@
IntervalTime: 2000,
MaxToReScanTime: 180000,
MinResearchTime: 5000,
IpScanTimeOut: 300,
IpScanTimeOut: 1,
pingTimeout: 2,
LastConnectTime: Date.parse(new Date()),
isConnected: false,
@ -62,10 +64,10 @@
/* require packages */
var http = require('http');
var net = require('net');
var arp = require('@network-utils/arp-lookup');
var ping = require('ping');
var nodeArp = require('node-arp');
var os = require('os');
var nodeArp = require('macfromip');
/* tmp global var */
var ip_point = 0;
@ -81,32 +83,48 @@
};
var getLocalIp = () => {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach((details) => {
if ((details.family == 'IPv4') && (details.internal == false)) {
o.localIP = details.address;
var ipParts = [];
ipParts = o.localIP.split(".");
o.ip_range = ipParts[0] + '.' + ipParts[1] + '.' + ipParts[2];
}
});
}
};
var ip_scan = function () {
var socket = new net.Socket();
socket.setTimeout(o.IpScanTimeOut);
socket.on('connect', function () {
socket.end();
ping.promise.probe(generate_IP(), {
timeout: o.IpScanTimeOut
}).then(function (res) {
if (res.alive == true) {
check_MAC();
});
socket.on('timeout', function () {
socket.destroy();
next_IP();
});
socket.on('error', function (err) {
return;
}
next_IP();
});
socket.on('close', function (err) {});
socket.connect(80, generate_IP());
};
var check_MAC = () => {
o.LastConnectTime = Date.parse(new Date());
nodeArp.getMAC(generate_IP(), (err, mac) => {
if (generate_IP() == o.localIP) {
next_IP();
return;
}
nodeArp.getMac(generate_IP(), (err, mac) => {
mac = mac.replace(/-/g, ":");
if (!err) {
if (o.debug) console.log('Searched IP: ' + generate_IP());
if (mac.toUpperCase() == o.MAC.toUpperCase()) {
if (o.hint) console.log('wiot - ' + o.MAC + ': Found MAC from ' + o.ip + ' :: Method: node-arp');
setTimeout(reCheck_MAC, o.IpScanTimeOut*2);
if (o.hint) console.log('wiot - ' + o.MAC + ': Found MAC from ' + o.ip + ' :: Method: macfromip');
setTimeout(reCheck_MAC, o.IpScanTimeOut * 2);
return;
}
next_IP();
@ -117,12 +135,14 @@
};
var reCheck_MAC = () => {
nodeArp.getMAC(generate_IP(), (err, mac) => {
if(!err){
if(o.debug) console.log('Checking IP: '+generate_IP());
if(mac.toUpperCase() == o.MAC.toUpperCase()){
nodeArp.getMac(generate_IP(), (err, mac) => {
mac = mac.replace(/-/g, ":");
if (!err) {
if (o.debug) console.log('Checking IP: ' + generate_IP());
if (mac.toUpperCase() == o.MAC.toUpperCase()) {
o.ip = generate_IP();
if (o.hint) console.log('wiot - ' + o.MAC + ': Confirm MAC from ' + o.ip + ' :: Method: node-arp');
if (o.hint) console.log('wiot - ' + o.MAC + ': Confirm MAC from ' + o.ip + ' :: Method: macfromip');
lstn();
setup();
return;
@ -132,7 +152,7 @@
}
next_IP();
});
}
};
var generate_IP = () => {
return o.ip_range + '.' + ip_point;
@ -146,7 +166,7 @@
}
ip_point++;
ip_scan();
}
};
var get_sync_url = () => {
var s = 'http://' + o.ip + '/sync?D1=';
@ -190,10 +210,10 @@
o.ip = val;
} else if (ip == "default") {
ip_scan();
return;
}
o.ip = val;
if (o.debug) console.log('Found MAC: ' + o.MAC + ' from IP: ' + val + ' :: Method: arp-lookup');
if (o.debug) console.log('Found MAC: ' + o.MAC + ' from IP: ' + o.ip + ' :: Method: arp-lookup');
lstn();
setup();
}, (err) => {
@ -371,8 +391,9 @@
/* exc functions */
var ini = function () {
if (o.hint) console.log('wiot - ' + o.MAC + ': init...');
//getMAC();
ip_scan();
getLocalIp();
getMAC();
//ip_scan();
};
async function lstn() {
@ -439,6 +460,6 @@
return o;
};
};
exports.client = wiot;
exports.client = wiot;

Loading…
Cancel
Save