wifi-scanner-windows.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. function _Scan()
  14. {
  15. var wlanInterfaces = this.Marshal.CreatePointer();
  16. this.Native.WlanEnumInterfaces(this.Handle, 0, wlanInterfaces);
  17. var count = wlanInterfaces.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
  18. var info = wlanInterfaces.Deref().Deref(8, 532);
  19. var iname = info.Deref(16, 512).AnsiString;
  20. var istate;
  21. switch (info.Deref(528, 4).toBuffer().readUInt32LE(0))
  22. {
  23. case 0:
  24. istate = "NOT READY";
  25. break;
  26. case 1:
  27. istate = "CONNECTED";
  28. break;
  29. case 2:
  30. istate = "AD-HOC";
  31. break;
  32. case 3:
  33. istate = "DISCONNECTING";
  34. break;
  35. case 4:
  36. istate = "DISCONNECTED";
  37. break;
  38. case 5:
  39. istate = "ASSOCIATING";
  40. break;
  41. case 6:
  42. istate = "DISCOVERING";
  43. break;
  44. case 7:
  45. istate = "AUTHENTICATING";
  46. break;
  47. default:
  48. istate = "UNKNOWN";
  49. break;
  50. }
  51. var iguid = info.Deref(0, 16);
  52. if (this.Native.WlanScan(this.Handle, iguid, 0, 0, 0).Val == 0)
  53. {
  54. return (true);
  55. }
  56. else
  57. {
  58. return (false);
  59. }
  60. }
  61. function AccessPoint(_ssid, _bssid, _rssi, _lq)
  62. {
  63. this.ssid = _ssid;
  64. this.bssid = _bssid;
  65. this.rssi = _rssi;
  66. this.lq = _lq;
  67. }
  68. AccessPoint.prototype.toString = function()
  69. {
  70. return (this.ssid + " [" + this.bssid + "]: " + this.lq);
  71. }
  72. function OnNotify(NotificationData)
  73. {
  74. var NotificationSource = NotificationData.Deref(0, 4).toBuffer().readUInt32LE(0);
  75. var NotificationCode = NotificationData.Deref(4, 4).toBuffer().readUInt32LE(0);
  76. var dataGuid = NotificationData.Deref(8, 16);
  77. if ((NotificationSource & 0X00000008) && (NotificationCode == 7))
  78. {
  79. var bss = this.Parent.Marshal.CreatePointer();
  80. var result = this.Parent.Native.GetBSSList(this.Parent.Handle, dataGuid, 0, 3, 0, 0, bss).Val;
  81. if (result == 0)
  82. {
  83. var totalSize = bss.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
  84. var numItems = bss.Deref().Deref(4, 4).toBuffer().readUInt32LE(0);
  85. for (i = 0; i < numItems; ++i)
  86. {
  87. var item = bss.Deref().Deref(8 + (360 * i), 360);
  88. var ssid = item.Deref(4, 32).String.trim();
  89. var bssid = item.Deref(40, 6).HexString2;
  90. var rssi = item.Deref(56, 4).toBuffer().readUInt32LE(0);
  91. var lq = item.Deref(60, 4).toBuffer().readUInt32LE(0);
  92. this.Parent.emit('Scan', new AccessPoint(ssid, bssid, rssi, lq));
  93. }
  94. }
  95. }
  96. }
  97. function Wireless()
  98. {
  99. var emitterUtils = require('events').inherits(this);
  100. this.Marshal = require('_GenericMarshal');
  101. this.Native = this.Marshal.CreateNativeProxy("wlanapi.dll");
  102. this.Native.CreateMethod("WlanOpenHandle");
  103. this.Native.CreateMethod("WlanGetNetworkBssList", "GetBSSList");
  104. this.Native.CreateMethod("WlanRegisterNotification");
  105. this.Native.CreateMethod("WlanEnumInterfaces");
  106. this.Native.CreateMethod("WlanScan");
  107. this.Native.CreateMethod("WlanQueryInterface");
  108. var negotiated = this.Marshal.CreatePointer();
  109. var h = this.Marshal.CreatePointer();
  110. this.Native.WlanOpenHandle(2, 0, negotiated, h);
  111. this.Handle = h.Deref();
  112. this._NOTIFY_PROXY_OBJECT = this.Marshal.CreateCallbackProxy(OnNotify, 2);
  113. this._NOTIFY_PROXY_OBJECT.Parent = this;
  114. var PrevSource = this.Marshal.CreatePointer();
  115. var result = this.Native.WlanRegisterNotification(this.Handle, 0X0000FFFF, 0, this._NOTIFY_PROXY_OBJECT.Callback, this._NOTIFY_PROXY_OBJECT.State, 0, PrevSource);
  116. emitterUtils.createEvent('Scan');
  117. emitterUtils.addMethod('Scan', _Scan);
  118. this.GetConnectedNetwork = function ()
  119. {
  120. var interfaces = this.Marshal.CreatePointer();
  121. console.log('Success = ' + this.Native.WlanEnumInterfaces(this.Handle, 0, interfaces).Val);
  122. var count = interfaces.Deref().Deref(0, 4).toBuffer().readUInt32LE(0);
  123. var info = interfaces.Deref().Deref(8, 532);
  124. var iname = info.Deref(16, 512).AnsiString;
  125. var istate = info.Deref(528, 4).toBuffer().readUInt32LE(0);
  126. if(info.Deref(528, 4).toBuffer().readUInt32LE(0) == 1) // CONNECTED
  127. {
  128. var dataSize = this.Marshal.CreatePointer();
  129. var pData = this.Marshal.CreatePointer();
  130. var valueType = this.Marshal.CreatePointer();
  131. var iguid = info.Deref(0, 16);
  132. var retVal = this.Native.WlanQueryInterface(this.Handle, iguid, 7, 0, dataSize, pData, valueType).Val;
  133. if (retVal == 0)
  134. {
  135. var associatedSSID = pData.Deref().Deref(524, 32).String;
  136. var bssid = pData.Deref().Deref(560, 6).HexString;
  137. var lq = pData.Deref().Deref(576, 4).toBuffer().readUInt32LE(0);
  138. return (new AccessPoint(associatedSSID, bssid, 0, lq));
  139. }
  140. }
  141. throw ("GetConnectedNetworks: FAILED (not associated to a network)");
  142. };
  143. return (this);
  144. }
  145. module.exports = new Wireless();