forked from play175/wifiNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifiNotifer_windows.go
374 lines (301 loc) · 9.59 KB
/
wifiNotifer_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// +build windows
package wifiNotifier
/*
#cgo LDFLAGS: -lwlanapi -lole32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <wlanapi.h>
#include <windot11.h> // for DOT11_SSID struct
#include <objbase.h>
#include <wtypes.h>
//#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
static HANDLE hClient = NULL;
static inline void openHandle()
{
if(hClient != NULL)return;
DWORD dwMaxClient = 2;
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
}
}
static inline void wchar2char(const wchar_t* wchar , char * m_char)
{
int len= WideCharToMultiByte( CP_ACP ,0,wchar ,wcslen( wchar ), NULL,0, NULL ,NULL );
WideCharToMultiByte( CP_ACP ,0,wchar ,wcslen( wchar ),m_char,len, NULL ,NULL );
m_char[len]= '\0';
}
static inline char* concatenate(char* baseString, const char* toAdd, char delimiter) {
if (toAdd == NULL)
return baseString;
int addingLen = strlen(toAdd);
if (addingLen == 0)
return baseString;
if (baseString == NULL) {
baseString = (char*)malloc(addingLen +1);
//sprintf_s(baseString, addingLen + 1, "%s%s", baseString);
memset(baseString, 0, addingLen + 1);
strcpy_s(baseString, addingLen + 1, toAdd);
return baseString;
}
int newSize = strlen(baseString) + ((delimiter != 0) ? 1 : 0) + addingLen + 1;
char* newString = (char*)malloc(newSize);
if (delimiter != 0)
sprintf_s(newString, newSize, "%s%c%s", baseString, delimiter, toAdd);
else
sprintf_s(newString, newSize, "%s%s", baseString, toAdd);
free(baseString);
return newString;
}
static inline char * getCurrentSSID(void) {
openHandle();
char *ssid = malloc(256);
memset(ssid,0,256);
DWORD dwResult = 0;
unsigned int i;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
} else {
for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO *) & pIfList->InterfaceInfo[i];
if (pIfInfo->isState == wlan_interface_state_connected) {
dwResult = WlanQueryInterface(hClient,
&pIfInfo->InterfaceGuid,
wlan_intf_opcode_current_connection,
NULL,
&connectInfoSize,
(PVOID *) &pConnectInfo,
&opCode);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanQueryInterface failed with error: %u\n", dwResult);
} else {
// wprintf(L" Profile name used:\t %ws\n", pConnectInfo->strProfileName);
wchar2char(pConnectInfo->strProfileName,ssid);
break;
}
}
}
}
if (pConnectInfo != NULL) {
WlanFreeMemory(pConnectInfo);
pConnectInfo = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return ssid;
}
static inline int getCurrentNetworkSecurity() {
openHandle();
int retSecurity = 0xFFFFFFFF;
char *ssid = malloc(256);
memset(ssid,0,256);
DWORD dwResult = 0;
unsigned int i;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
} else {
for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO *) & pIfList->InterfaceInfo[i];
if (pIfInfo->isState == wlan_interface_state_connected) {
dwResult = WlanQueryInterface(hClient,
&pIfInfo->InterfaceGuid,
wlan_intf_opcode_current_connection,
NULL,
&connectInfoSize,
(PVOID *) &pConnectInfo,
&opCode);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanQueryInterface failed with error: %u\n", dwResult);
} else {
// wprintf(L" Profile name used:\t %ws\n", pConnectInfo->strProfileName);
//wchar2char(pConnectInfo->strProfileName,ssid);
retSecurity = pConnectInfo->wlanSecurityAttributes.dot11CipherAlgorithm;
break;
}
}
}
}
if (pConnectInfo != NULL) {
WlanFreeMemory(pConnectInfo);
pConnectInfo = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return retSecurity;
}
static inline char* getAvailableSSIDs(void) {
HANDLE hClient = NULL;
DWORD dwMaxClient = 2;
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
unsigned int i;
char* retList = NULL;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL;
PWLAN_BSS_LIST ppWlanBssList = NULL;
dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanOpenHandle failed with error: %u\n", dwResult);
return NULL;
}
dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);
if (dwResult != ERROR_SUCCESS) {
wprintf(L"WlanEnumInterfaces failed with error: %u\n", dwResult);
return NULL;
}
for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) {
pIfInfo = (WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i];
dwResult = WlanGetAvailableNetworkList(hClient,
&pIfInfo->InterfaceGuid,
0,
NULL,
&pBssList);
if (dwResult != ERROR_SUCCESS)
wprintf(L"WlanGetAvailableNetworkList failed with error: %u\n", dwResult);
else
{
dwResult = WlanScan(hClient, &pIfInfo->InterfaceGuid, 0, 0, 0);
if (dwResult != ERROR_SUCCESS)
wprintf(L"WlanScan failed with error: %u\n", dwResult);
else
{
Sleep(3000);
dwResult = WlanGetNetworkBssList(
hClient,
&pIfInfo->InterfaceGuid,
NULL,
dot11_BSS_type_any,
0,
NULL,
&ppWlanBssList);
if (ERROR_SUCCESS != dwResult)
wprintf(L"WlanGetNetworkBssList failed with error: %u\n", dwResult);
else
{
for (DWORD i = 0; i < ppWlanBssList->dwNumberOfItems; i++)
retList = concatenate(retList, (char*)ppWlanBssList->wlanBssEntries[i].dot11Ssid.ucSSID, '\n');
}
}
}
}
if (ppWlanBssList != NULL) {
WlanFreeMemory(ppWlanBssList);
ppWlanBssList = NULL;
}
if (pBssList != NULL) {
WlanFreeMemory(pBssList);
pBssList = NULL;
}
if (pIfList != NULL) {
WlanFreeMemory(pIfList);
pIfList = NULL;
}
return retList;
}
static inline void onWifiChanged(PWLAN_NOTIFICATION_DATA data,PVOID context)
{
extern void __onWifiChanged(char *);
__onWifiChanged(getCurrentSSID());
}
static inline void setWifiNotifier()
{
openHandle();
DWORD hResult = ERROR_SUCCESS;
DWORD pdwPrevNotifSource = 0;
hResult=WlanRegisterNotification(hClient,
WLAN_NOTIFICATION_SOURCE_ACM,
TRUE,
(WLAN_NOTIFICATION_CALLBACK)onWifiChanged,
NULL,
NULL,
&pdwPrevNotifSource);
if(hResult!=ERROR_SUCCESS)
printf("failed WlanRegisterNotification=%d \n",hResult);
while(TRUE){
Sleep(10);
}
WlanCloseHandle(hClient,NULL);
printf("WlanCloseHandle success \n");
}
*/
import "C"
import (
"strings"
"unsafe"
)
var internalOnWifiChangedCb func(string)
//export __onWifiChanged
func __onWifiChanged(ssid *C.char) {
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
if internalOnWifiChangedCb != nil {
internalOnWifiChangedCb(goSsid)
}
}
// GetAvailableSSIDs returns the list of the names of available Wi-Fi networks
func GetAvailableSSIDs() []string {
ssidList := C.getAvailableSSIDs()
goSsidList := C.GoString(ssidList)
C.free(unsafe.Pointer(ssidList))
return strings.Split(goSsidList, "\n")
}
// GetCurrentSSID returns current WiFi SSID
func GetCurrentSSID() string {
ssid := C.getCurrentSSID()
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
return goSsid
}
// GetCurrentNetworkIsInsecure returns current security mode
func GetCurrentNetworkIsInsecure() bool {
const (
DOT11_CIPHER_ALGO_NONE = 0x00
DOT11_CIPHER_ALGO_WEP40 = 0x01
DOT11_CIPHER_ALGO_TKIP = 0x02
DOT11_CIPHER_ALGO_CCMP = 0x04
DOT11_CIPHER_ALGO_WEP104 = 0x05
DOT11_CIPHER_ALGO_WPA_USE_GROUP = 0x100
DOT11_CIPHER_ALGO_RSN_USE_GROUP = 0x100
DOT11_CIPHER_ALGO_WEP = 0x101
DOT11_CIPHER_ALGO_IHV_START = 0x80000000
DOT11_CIPHER_ALGO_IHV_END = 0xffffffff
)
security := C.getCurrentNetworkSecurity()
switch security {
case DOT11_CIPHER_ALGO_NONE,
DOT11_CIPHER_ALGO_WEP40,
DOT11_CIPHER_ALGO_WEP104,
DOT11_CIPHER_ALGO_WEP:
return true
}
return false
}
// SetWifiNotifier initializes a handler method 'OnWifiChanged'
func SetWifiNotifier(cb func(string)) error {
internalOnWifiChangedCb = cb
go C.setWifiNotifier()
return nil
}