wifi-scanner.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2018-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var MemoryStream = require('MemoryStream');
  14. var WindowsChildScript = 'var parent = require("ScriptContainer");var Wireless = require("wifi-scanner-windows");Wireless.on("Scan", function (ap) { parent.send(ap); });Wireless.Scan();';
  15. function AccessPoint(_ssid, _bssid, _lq)
  16. {
  17. this.ssid = _ssid;
  18. this.bssid = _bssid;
  19. this.lq = _lq;
  20. }
  21. AccessPoint.prototype.toString = function ()
  22. {
  23. return ("[" + this.bssid + "]: " + this.ssid + " (" + this.lq + ")");
  24. //return (this.ssid + " [" + this.bssid + "]: " + this.lq);
  25. }
  26. function WiFiScanner()
  27. {
  28. var emitterUtils = require('events').inherits(this);
  29. emitterUtils.createEvent('accessPoint');
  30. this.hasWireless = function ()
  31. {
  32. var retVal = false;
  33. var interfaces = require('os').networkInterfaces();
  34. for (var name in interfaces)
  35. {
  36. if (interfaces[name][0].type == 'wireless') { retVal = true; break; }
  37. }
  38. return (retVal);
  39. };
  40. this.Scan = function ()
  41. {
  42. if (process.platform == 'win32')
  43. {
  44. this.main = require('ScriptContainer').Create(15, ContainerPermissions.DEFAULT);
  45. this.main.parent = this;
  46. this.main.on('data', function (j) { this.parent.emit('accessPoint', new AccessPoint(j.ssid, j.bssid, j.lq)); });
  47. this.main.addModule('wifi-scanner-windows', getJSModule('wifi-scanner-windows'));
  48. this.main.ExecuteString(WindowsChildScript);
  49. }
  50. else if (process.platform == 'linux')
  51. {
  52. // Need to get the wireless interface name
  53. var interfaces = require('os').networkInterfaces();
  54. var wlan = null;
  55. for (var i in interfaces)
  56. {
  57. if (interfaces[i][0].type == 'wireless')
  58. {
  59. wlan = i;
  60. break;
  61. }
  62. }
  63. if (wlan != null)
  64. {
  65. this.child = require('child_process').execFile('/sbin/iwlist', ['iwlist', wlan, 'scan']);
  66. this.child.parent = this;
  67. this.child.ms = new MemoryStream();
  68. this.child.ms.parent = this.child;
  69. this.child.stdout.on('data', function (buffer) { this.parent.ms.write(buffer); });
  70. this.child.on('exit', function () { this.ms.end(); });
  71. this.child.ms.on('end', function ()
  72. {
  73. var str = this.buffer.toString();
  74. tokens = str.split(' - Address: ');
  75. for (var block in tokens)
  76. {
  77. if (block == 0) continue;
  78. var ln = tokens[block].split('\n');
  79. var _bssid = ln[0];
  80. var _lq;
  81. var _ssid;
  82. for (var lnblock in ln)
  83. {
  84. lnblock = ln[lnblock].trim();
  85. lnblock = lnblock.trim();
  86. if (lnblock.startsWith('ESSID:'))
  87. {
  88. _ssid = lnblock.slice(7, lnblock.length - 1);
  89. if (_ssid == '<hidden>') { _ssid = ''; }
  90. }
  91. if (lnblock.startsWith('Signal level='))
  92. {
  93. _lq = lnblock.slice(13,lnblock.length-4);
  94. }
  95. else if (lnblock.startsWith('Quality='))
  96. {
  97. _lq = lnblock.slice(8, 10);
  98. var scale = lnblock.slice(11, 13);
  99. }
  100. }
  101. this.parent.parent.emit('accessPoint', new AccessPoint(_ssid, _bssid, _lq));
  102. }
  103. });
  104. }
  105. }
  106. }
  107. }
  108. module.exports = WiFiScanner;