This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVPNs Manager.ahk
More file actions
1132 lines (842 loc) · 42.1 KB
/
Copy pathVPNs Manager.ahk
File metadata and controls
1132 lines (842 loc) · 42.1 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
VPNs Manager - v0.2.3
Created by BLBC (github.com/hjk789)
Copyright (c) 2020+ BLBC
*/
SetBatchLines 0 ; For each line of code of the script, a predefined delay of 10 ms happens. This sets the delay to the smallest value needed for each line.
;************** SETTINGS **************
global isUsingComodoFirewall := false
global isUsingPeerBlock := false
global isUsingWindowsFirewall := false
global peerblockVPNsFileFullPath := "<FULL PATH TO WHERE IS LOCATED YOUR PEERBLOCK BLOCKLISTS>\VPNs.txt"
global comodoFirewallVPNsRegistryPath := "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\CmdAgent\CisConfigs\<CONFIG ID>\Firewall\Network Aliases\<THE NETWORK ZONE ID WHICH WILL CONTAIN THE VPNs IPs>"
global windowsFirewallVPNsRegistryPath := "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules"
global windowsFirewallRuleGUID := "{THE-GUID-OF-THE-RULE-THAT-ALLOWS-THE-VPN-SERVERS-IPs}"
global vpnAdapterName := "VPN Client Adapter - VPN" ; This is the default NIC name that SoftEther suggests for the network adapter created to be used by the VPNs. If you've set a different name, change it to to the name you've set.
global SoftEtherDirectoryPath := "E:\Program Files\SoftEther VPN Client"
global vpncmd := """" SoftEtherDirectoryPath "\vpncmd.exe"" 127.0.0.1 /client /cmd"
;**************************************
#SingleInstance force
SetWorkingDir %A_ScriptDir%
ListLines off
global iniFileName := "vpns-settings.ini"
global whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
global chosenRow := ""
global highlightedRow := ""
global currentConName := ""
global currentConStatus := ""
global eligibleServers := []
global stopFetching := false
if (isUsingComodoFirewall)
{
RegRead, activeConfig, HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\CmdAgent\CisConfigs, Active ; This gets Comodo Firewall's currently active configuration ...
comodoFirewallVPNsRegistryPath := strReplace(comodoFirewallVPNsRegistryPath, "<CONFIG ID>", activeConfig) ; ... and adds it to the registry path.
}
;/* Create the main screen */
;{
gui, mainUI:new, LastFound
;/* Set the taskbar icon as the ringed planet icon */
;{
shell32Handle := DllCall("GetModuleHandle", str, "shell32.dll", ptr)
iconHandle := DllCall("LoadImage", ptr, shell32Handle, str, "#14", uint, 1, int, 32, int, 32, uint, 0)
SendMessage, WM_SETICON:=0x80, 0, iconHandle ; Set as the LastFound window's small icon version.
SendMessage, WM_SETICON:=0x80, 1, iconHandle ; Set as the LastFound window's big icon version.
;}
;/* Create the ListView */
;{
lvWidth := 560
gui, add, listview, Sort ReadOnly Grid h300 w%lvWidth% glistviewEvent, Name|Server|Good|Status|Last online|Chkd|Max Speed|Max Avg. ;|Trend speed|Trend speed 2
global cols := {"Name":1, "Server":2, "Good":3, "Status":4, "LastOnline":5, "Checked":6, "MaxSpeed":7, "MaxAverage":8} ; Store the columns indexes in an object for easy use.
LV_ModifyCol(cols.server, "Integer Left")
;}
;/* Create the ImageList with the icons that will be used by the ListView */
;{
ImageListID := IL_Create()
LV_SetImageList(ImageListID)
IL_Add(ImageListID, "shell32.dll", 78) ; Add the yellow triangle icon.
IL_Add(ImageListID, "shell32.dll", 300) ; Add the green arrow icon.
IL_Add(ImageListID, "shell32.dll", 132) ; Add the red X icon.
IL_Add(ImageListID, "shell32.dll", 136) ; Add the globe with arrow icon.
global icons := {"triangle":"Icon1", "greenArrow":"Icon2", "X":"Icon3", "globeWithArrow":"Icon4"} ; Store the icons indexes in an object for easy use.
;}
rebuildListView()
;/* Create the buttons */
;{
Gui, Font, s22
Gui, add, button, y15 w35 h35 gopenAddServerScreen, +
Gui, Font, s30
Gui, add, button, xp y+85 w34 h40 gchooseRandomServer, ↷
Gui, add, button, xp y+85 w34 h40 grebuildListView, ⟲
Gui, Font, s12
Gui, add, button, x110 h40 gfetchServers, Fetch servers
Gui, add, button, x+155 h40 gpingAllServers, Ping servers
;}
;/* Create the status bar */
;{
Gui, Font, s11
Gui, Add, StatusBar
statusbarWidth := lvWidth + 67
SB_SetParts(statusbarWidth - 100) ; Create a separation at the end of the status bar ...
SB_SetText(LV_GetCount() " servers", 2) ; ... that displays the number of servers listed.
;}
;/* Create the context menu items */
;{
Menu, MainContextMenu, Add, Ping, contextMenuHandler
Menu, MainContextMenu, Add ; Separator
Menu, GoodSubContextMenu, Add, Route, contextMenuHandler
Menu, GoodSubContextMenu, Add, Speed, contextMenuHandler
Menu, GoodSubContextMenu, Add, Both, contextMenuHandler
Menu, MainContextMenu, Add, Good, :GoodSubContextMenu
Menu, MainContextMenu, Add
Menu, MainContextMenu, Add, Copy, contextMenuHandler
Menu, MainContextMenu, Add, Copy IP, contextMenuHandler
Menu, MainContextMenu, Add
Menu, MainContextMenu, Add, Disconnect, contextMenuHandler
Menu, MainContextMenu, Add
Menu, MainContextMenu, Add, Delete, contextMenuHandler
;}
;/* Setup the tray options */
;{
Menu, Tray, NoStandard ; Remove all the pre-included items (Reload, Pause script, etc.).
Menu, Tray, Add, Show VPNs Manager, ShowGui
Menu, Tray, Add
Menu, Tray, Add, Exit, mainUIGuiClose
Menu, Tray, Default, Show VPNs Manager ; Make it so that when double-clicking the tray icon, the main screen is shown, as the "Show VPNs Manager" option is set as default.
Menu, Tray, Click, 1 ; Require only one click to trigger the default option.
Menu, Tray, Icon, shell32.dll, 14 ; Set the tray icon to the ringed globe icon.
Menu, Tray, Tip, VPNs Manager ; Set the tray icon's tooltip.
;}
OnMessage(0x112, "WM_SYSCOMMAND") ; When minimized, run the WM_SYSCOMMAND function below, to hide the main GUI.
;/* UI functions, subroutines and event handlers */
;{
;/*Minimize to tray and restore functions*/
;{
WM_SYSCOMMAND(wParam) ; This function is called whenever the Gui receives a command from the window's native controls, such as minimizing the window.
{
If (wParam == 61472) ; Minimize command.
SetTimer, HideGui, -1 ; Hide the gui only after the window finishes being minimized, otherwise the minimize command makes it visible again. 1ms is enough time.
} ; And it needs to be a timer so that the thread isn't interrupted in the meantime, otherwise the same thing happens.
HideGui() {
Gui, mainUI:Hide
}
ShowGui() {
Gui, mainUI:Show
}
;}
mainUIGuiContextMenu() ; This function is called whenever the user right-clicks inside the mainUI GUI.
{
highlightedRow := getRowData(A_EventInfo) ; When right-clicking a listview, the A_EventInfo variable value is the row index where the right-click occurred.
Menu, MainContextMenu, Show
}
contextMenuHandler() ; This function is called, in a separate thread, whenever the user chooses an item of the context menu.
{
gui, mainUI:default ; LV_* functions use the default GUI of the thread it was called from. Each thread considers the most recently created GUI in it as the default one.
; Because the contextMenuHandler is called from another thread, this command sets the mainUI GUI created in the main thread as the respective thread's default GUI.
if (A_ThisMenu == "MainContextMenu")
{
if (A_ThisMenuItem == "Ping")
{
numSelectedRows := LV_GetCount("S")
Loop %numSelectedRows%
{
RowNumber := LV_GetNext(RowNumber)
if (!RowNumber) ; if 0
break
row := getRowData(RowNumber)
try
success := pingServer(row.ip, RowNumber)
catch
success := false
if (!success)
{
msgbox Couldn't connect to the server.
return
}
if (numSelectedRows > 1)
SB_SetText("Pinged " A_Index " of " numSelectedRows " servers", 1) ; Show the progress in the status bar
}
sleep 2000
SB_SetText("", 1) ; Clear the status bar after 2 seconds.
}
else if (A_ThisMenuItem == "Copy")
clipboard := highlightedRow.name " " highlightedRow.server
else if (A_ThisMenuItem == "Copy IP")
clipboard := highlightedRow.ip
else if (A_ThisMenuItem == "Disconnect")
{
disconnectFromServer()
LV_Modify(highlightedRow.index, "Icon99")
}
else if (A_ThisMenuItem == "Delete")
{
chosenRow := highlightedRow
deleteSelectedServers()
}
}
else if (A_ThisMenu == "GoodSubContextMenu")
{
IniWrite, %A_ThisMenuItem%, %iniFileName%, % highlightedRow.ip, Good
LV_Modify(highlightedRow.index, "Col" cols.good, A_ThisMenuItem)
}
}
listviewEvent() ; This function is called whenever the listview receives an action, such as double-clicking a row.
{
if (A_GuiEvent == "DoubleClick") ; The A_GuiEvent variable value is the action that the listview received.
connectToServer(A_EventInfo) ; Here A_EventInfo value is the row index where the action was performed.
}
;}
gui show,, VPNs Manager
;}
;/* Startup parameters */
;{
if (A_Args.MaxIndex() >= 1)
{
Critical ; Prevent the startup procedures from being interrupted.
if (A_Args[1] == "--fetchServers")
fetchServers()
else if (A_Args[1] == "--changeSoftEtherConfig")
changeSoftEtherConfig()
else if (A_Args[1] == "--connectOnStartup")
{
chooseRandomServer()
;/* When the startup argument connectOnStartup is used, update the servers status once after the first 3 minutes, which is enough time for everything to be up and running before that. */
tempFunc := Func("pingAllServers").bind() ; Because there can be only one timer for each function/label, this creates a function object and binds an empty parameter to make it seem different to the interpreter.
setTimer, %tempFunc%, -120000 ; 2 minutes.
}
else if (A_Args[1] == "--openAddServerScreen")
openAddServerScreen()
else if (inStr(A_Args[1], "--delete"))
{
deleteSelectedServers(A_Args[2], A_Args[3])
if (A_Args[1] == "--deleteBadServer")
chooseRandomServer()
}
Critical off
}
;}
setTimer, pingAllServers, 3600000 ; Ping all servers each hour.
setTimer, waitForConnectError, -1 ; Wait, in a separated thread, for a connection error, otherwise it would halt the whole program.
global avgarr := []
global avgsamples := 50
global maxSpeed := 0
global maxAvg := 0
global iniMaxSpeed := 0
global iniMaxAvg := 0
setupNetworkSpeedMeter()
return
rebuildListView()
{
gui, mainUI:default
LV_Delete()
IniRead, ips, %iniFileName% ; Get a list with the name of all sections in the ini file, which are all IP addresses, plus the Settings section.
getCurrentConnectionFromCmd()
Loop, Parse, ips, `n ; Loop line by line.
{
icon := "Icon0" ; Default icon (none).
if (A_LoopField == "Settings")
continue ; Ignore the Settings section.
;/* Get the servers data from the ini file */
;{
IniRead, name, %iniFileName%, %A_LoopField%, Name
IniRead, port, %iniFileName%, %A_LoopField%, Port
IniRead, good, %iniFileName%, %A_LoopField%, Good, %A_Space%
IniRead, status, %iniFileName%, %A_LoopField%, Status
IniRead, lastSeenOnline, %iniFileName%, %A_LoopField%, LastSeenOnline, %A_Space%
IniRead, checked, %iniFileName%, %A_LoopField%, Checked
IniRead, imaxspeed, %iniFileName%, %A_LoopField%, MaxSpeed, %A_Space%
IniRead, imaxavg, %iniFileName%, %A_LoopField%, MaxAverage, %A_Space%
if (imaxspeed)
imaxspeed .= " KB"
if (imaxavg)
imaxavg .= " KB/s"
;}
;/* Determine the icons to be shown in the listview */
;{
if (name == currentConName)
{
if (inStr(currentConStatus, "|Connection Completed"))
icon := icons.greenArrow
else if (inStr(currentConStatus, "not connected"))
icon := icons.triangle
else
icon := icons.globeWithArrow
}
if (status == "Off")
icon := icons.X
;}
LV_Add(icon, name, A_LoopField ":" port, good, status, lastSeenOnline, checked, imaxspeed, imaxavg)
}
LV_ModifyCol()
LV_ModifyCol(cols.name, "120")
LV_ModifyCol(cols.good, "SortDesc AutoHdr")
LV_ModifyCol(cols.status, "SortDesc")
LV_ModifyCol(cols.checked, "37")
LV_ModifyCol(cols.maxspeed, "51")
LV_ModifyCol(cols.maxaverage, "51")
buildListEligibleServers()
SB_SetText(LV_GetCount() " servers", 2)
}
getCurrentConnectionFromCmd()
{
FileRead, currentConStatus, %iniFileName%:currentConStatus ; Here, and in other parts, NTFS' alternate data streams are used as a disposable, persistent storage of vpncmd's outputs, instead of creating files just for that.
runwait, cmd /c (%vpncmd% accountlist) > %iniFileName%:serversList,, hide
FileRead, serversList, %iniFileName%:serversList
RegExMatch(serversList, "Name \|([^\n]+?)\nStatus +?\|(?:Connected|Connecting).+?(\d+\.\d+\.\d+\.\d+)", currentConMatch) ; Parse vpncmd's output to get the name of the connected server. The parsed output looks like this: VPN Connection Setting Name |USA, New York
; Status |Connected
currentConName := currentConMatch1 ; RegExMatch creates a pseudo array containing the matches. In this case, the currentConMatch variable contains the
; whole matched string, and currentConMatch1 contains the first capturing group in the regex, which is the server's name.
if (currentConName != "")
{
if (chosenRow == "")
{
chosenRow := {}
chosenRow.name := currentConName
chosenRow.ip := currentConMatch2
}
checkConnection()
}
}
buildListEligibleServers()
{
Critical
gui, mainUI:default
eligibleServers := []
loop % LV_GetCount()
{
row := getRowData(A_Index)
if (row.status == "Online" && (row.checked == "no" || row.good != ""))
eligibleServers.push(A_Index)
if (currentConName == row.name)
chosenRow := row
}
Critical off
}
getRowData(index)
{
gui, mainUI:default
LV_GetText(name, index, cols.name)
LV_GetText(server, index, cols.server)
ip := (strSplit(server, ":"))[1]
LV_GetText(good, index, cols.good)
LV_GetText(status, index, cols.status)
LV_GetText(checked, index, cols.checked)
return {"index": index, "name": name, "server": server, "ip": ip, "good": good, "status": status, "checked": checked}
}
waitForConnectError()
{
loop
{
winwaitactive Connect Error ; Wait for the connect error dialog when the "Hide Status Window" option is disabled. The alternative to this would be to keep
winhide ; polling for the connection status, but it would need to have a short interval like 1 second, and that may not be a good solution.
chooseRandomServer()
}
}
chooseRandomServer()
{
random, rand, 1, eligibleServers.MaxIndex()
connectToServer(eligibleServers[rand])
eligibleServers.removeAt(rand) ; Delete it from the array so that it's not chosen again until the next time the eligible servers list is built.
if (!eligibleServers.MaxIndex()) ; When all eligible servers were already chosen,
buildListEligibleServers() ; rebuild the list.
}
connectToServer(index)
{
gui, mainUI:default
disconnectFromServer() ; SoftEther requires the current connection to be disconnected before connecting to another server.
if (chosenRow == "")
rebuildListView()
chosenRow := getRowData(index)
name := chosenRow.name
run %vpncmd% accountconnect "%name%",, hide
winwait Connecting,, 2 ; Wait for the connecting dialog when the "Hide Status Window" option is disabled.
ifwinexist Connecting ; In case that option is enabled, the 2 seconds timeout prevents the script from getting stuck infinitely.
winhide
replaceFileContent(iniFileName ":currentConStatus", "")
currentConStatus := ""
currentConName = %name%
LV_Modify(index, icons.globeWithArrow)
setTimer, checkConnection, 1000
}
disconnectFromServer()
{
setTimer, updateNetworkMeter, off
runwait %vpncmd% accountdisconnect "%currentConName%",, hide
if (chosenRow)
LV_Modify(chosenRow.index, "Icon99") ; Invalid icon index to remove the current icon.
}
replaceFileContent(filePath, fileContent)
{
file := FileOpen(filePath, "w") ; Create an empty file. If the file already exists, overwrite it.
file.Write(fileContent)
file.Close()
}
checkConnection()
{
gui, mainUI:default
name := chosenRow.name
ip := chosenRow.ip
runwait, cmd /c (%vpncmd% accountStatusGet "%name%") > "%iniFileName%:currentConStatus",, hide
FileRead, currentConStatus, %iniFileName%:currentConStatus
IniRead, checked, %iniFileName%, %ip%, Checked
if (inStr(currentConStatus, "not connected") || inStr(currentConStatus, "Retrying") || (inStr(currentConStatus, "|Connection Completed") && inStr(currentConStatus, "MD5")))
{
if (inStr(currentConStatus, "MD5"))
msgbox This server uses RC4-MD5 encryption and will be deleted.
else if (checked != "yes")
msgbox, 49,, Couldn't connect to the server. Do you want to delete this server?
else
msgbox, 53,, Couldn't connect to the server.
setTimer, checkConnection, delete
replaceFileContent(iniFileName ":currentConStatus", "")
currentConStatus := ""
ifmsgbox Cancel
return
ifmsgbox Retry
{
connectToServer(chosenRow.index)
return
}
if (checked != "yes")
{
if (!A_IsAdmin && (isUsingWindowsFirewall || isUsingComodoFirewall))
Run, *RunAs "%A_ScriptFullPath%" --deleteBadServer %name% %ip% ; Start the VPNs Manager again as admin, and because the SingleInstance directive is set to "force", the unelevated instance is automatically killed.
chooseRandomServer() ; SoftEther can't delete a server that is currently connected. This switches to another server.
deleteSelectedServers(name, ip)
}
}
else if (inStr(currentConStatus, "|Connection Completed"))
{
setTimer, checkConnection, delete
if (checked != "yes")
IniWrite, yes, %iniFileName%, %ip%, Checked
if (chosenRow != "")
LV_Modify(chosenRow.index, icons.greenArrow " Col" cols.checked, "yes")
IniRead, iniMaxSpeed, %iniFileName%, %ip%, MaxSpeed, 0
IniRead, iniMaxAvg, %iniFileName%, %ip%, MaxAverage, 0
setTimer, updateNetworkMeter, on
}
}
deleteSelectedServers(pName := "", pIP := "")
{
gui, mainUI:default
if (!A_IsAdmin && (isUsingWindowsFirewall || isUsingComodoFirewall))
{
msgbox, 49,, Are you sure you want to delete the selected server(s)?
ifmsgbox Cancel
return
name := chosenRow.name
ip := chosenRow.ip
Run, *RunAs "%A_ScriptFullPath%" --delete "%name%" %ip%
}
else if (pName != "")
{
msgbox, 49,, Are you sure you want to delete the server "%pName%"?
ifmsgbox Cancel
return
ip := pIP
name := pName
}
RowNumber = 0
IniRead, deletedServers, %iniFileName%, Settings, DeletedServers, %A_Space%
Critical ; Prevent the user from deselecting the rows before it finished deleting.
numSelectedRows := LV_GetCount("S")
if (numSelectedRows == 0)
numSelectedRows = 1
Loop %numSelectedRows%
{
if (pName == "")
{
RowNumber := LV_GetNext(RowNumber - 1)
if (!RowNumber)
break
row := getRowData(RowNumber)
ip := row.ip
name := row.name
SB_SetText(LV_GetCount() " servers", 2)
}
;/* Remove from SoftEther VPNs list*/
runwait, %vpncmd% accountdelete "%name%",, hide
deletedServers .= ip ","
;/* Remove the server from the ini file */
IniDelete, %iniFileName%, %ip%
;/* Remove from the PeerBlock list */
if (isUsingPeerBlock)
{
FileRead, peerblockVPNsFileContent, %peerblockVPNsFileFullPath%
peerblockVPNsFileContent := strReplace(peerblockVPNsFileContent, name ":" ip "-" ip, "")
replaceFileContent(peerblockVPNsFileFullPath, peerblockVPNsFileContent)
}
;/* Remove from Comodo Firewall's exceptions */
if (isUsingComodoFirewall)
{
loop, reg, %comodoFirewallVPNsRegistryPath%, k
{
RegRead, regIP, %comodoFirewallVPNsRegistryPath%\%A_LoopRegName%\IPV4, AddrStart
if (regIP == ip)
{
RegDelete %comodoFirewallVPNsRegistryPath%\%A_LoopRegName%
break
}
}
}
;/* Remove from Windows Firewall's exceptions */
if (isUsingWindowsFirewall)
{
RegRead, rule, %windowsFirewallVPNsRegistryPath%, %windowsFirewallRuleGUID%
rule := strReplace(rule, "|RA4=" ip, "")
RegWrite, REG_SZ, %windowsFirewallVPNsRegistryPath%, %windowsFirewallRuleGUID%, %rule%
}
if (RowNumber > 0)
LV_Delete(RowNumber)
else break
}
Critical off
IniWrite, %deletedServers%, %iniFileName%, Settings, DeletedServers
if (RowNumber == 0)
rebuildListView()
}
pingAllServers()
{
gui, mainUI:default
numRows := LV_GetCount()
Loop, %numRows%
{
row := getRowData(A_index)
try
success := pingServer(row.ip, A_Index)
catch
success := false
if (!success)
{
msgbox, 53,, Couldn't connect to the server.
ifmsgbox Retry
{
pingAllServers()
return
}
ifmsgbox Cancel
return
}
SB_SetText("Pinged " A_Index " of " numRows " servers", 1)
}
rebuildListView()
sleep 2000
SB_SetText("", 1)
}
pingServer(serverIp, rowIndex)
{
gui, mainUI:default
ip := serverIp
IniRead, port, %iniFileName%, %ip%, Port
Critical ; Enable the critical mode for the request, otherwise the request fails when another thread interrupts it.
whr.open("POST", "https://ports.yougetsignal.com/check-port.php") ; The port check feature from YouGetSignal is used to check if the servers port used for the VPN is open. The alternatives to this would be using a third-party commandline
whr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") ; program that enables checking ports or using the PowerShell cmdlet Test-Connection. Using a third-party server is the most compatible and standalone method.
whr.send("remoteAddress=" ip "&portNumber=" port)
response := whr.responseText
success := inStr(response, ip) ; The server's response always include the pinged IP, either when open or closed. This checks whether the request went OK and whether the response includes the expected info.
Critical off
if (success)
{
formatTime, now,, dd/MM HH:mm ; Get the current time.
isOpen := inStr(response, "open")
if (isOpen)
{
status := "Online"
IniWrite, %now%, %iniFileName%, %ip%, LastSeenOnline
LV_Modify(rowIndex, "Col" cols.lastonline, now)
}
else status := "Off"
IniWrite, %status%, %iniFileName%, %ip%, Status
LV_Modify(rowIndex, "Col" cols.status, status)
}
else if (inStr(response, "check limit reached")) ; YouGetSignal has a daily limit of port checks. But you only reach this limit if you repeatedly ping several servers several times.
{ ; Pinging 20 servers 10 times a day isn't enough. Also, the limit is per IP, so you just need to switch to another VPN server to use it again.
msgbox % (strSplit(response, ". "))[1] ; When the limit is reached, YouGetSignal responds with an error message. This shows in a msgbox the most relevant part of the error.
}
return success
}
addNewServer(serverName, serverIP, port)
{
gui, mainUI:default
;/* Add the server to SoftEther VPNs list */
runwait %vpncmd% accountcreate "%serverName%" /server:%serverIP%:%port% /hub:"VPNGATE" /username:"vpn" /nic:"%VpnAdapterName%",, hide
;/* Add to the ini file */
IniWrite, %port%, %iniFileName%, %serverIP%, Port
IniWrite, %serverName%, %iniFileName%, %serverIP%, Name
IniWrite, Online, %iniFileName%, %serverIP%, Status
IniWrite, no, %iniFileName%, %serverIP%, Checked
;/* Add to the PeerBlock list */
if (isUsingPeerBlock)
FileAppend, %serverName%:%serverIP%-%serverIP%`n, %peerblockVPNsFileFullPath%
;/* Add to Comodo Firewall's exceptions */
if (isUsingComodoFirewall)
{
Random, rand, 50, 999
RegWrite, REG_DWORD, %comodoFirewallVPNsRegistryPath%, Num, 999
RegWrite, REG_DWORD, %comodoFirewallVPNsRegistryPath%\%rand%, Source, 2
RegWrite, REG_DWORD, %comodoFirewallVPNsRegistryPath%\%rand%, Type, 1
RegWrite, REG_SZ, %comodoFirewallVPNsRegistryPath%\%rand%\IPV4, AddrStart, %serverIP%
RegWrite, REG_SZ, %comodoFirewallVPNsRegistryPath%\%rand%\IPV4, AddrEnd, %serverIP%
RegWrite, REG_DWORD, %comodoFirewallVPNsRegistryPath%\%rand%\IPV4, AddrType, 1
}
;/* Add to Windows Firewall's exceptions */
if (isUsingWindowsFirewall)
{
RegRead, rule, %windowsFirewallVPNsRegistryPath%, %windowsFirewallRuleGUID%
rule .= "|RA4=" serverIP
RegWrite, REG_SZ, %windowsFirewallVPNsRegistryPath%, %windowsFirewallRuleGUID%, %rule%
}
}
changeSoftEtherConfig()
{
if (!A_IsAdmin)
Run *RunAs "%A_ScriptFullPath%" --changeSoftEtherConfig
gui, mainUI:default
FileRead, softetherConfigFileContent, %SoftEtherDirectoryPath%\vpn_client.config
softetherConfigFileContent := strReplace(softetherConfigFileContent, "DisableQoS false" , "DisableQoS true")
softetherConfigFileContent := strReplace(softetherConfigFileContent, "NoUdpAcceleration false", "NoUdpAcceleration true")
softetherConfigFileContent := strReplace(softetherConfigFileContent, "UseCompress false" , "UseCompress true")
softetherConfigFileContent := strReplace(softetherConfigFileContent, "HideNicInfoWindow false", "HideNicInfoWindow true")
softetherConfigFileContent := strReplace(softetherConfigFileContent, "NumRetry 4294967295" , "NumRetry 1")
softetherConfigFileContent := strReplace(softetherConfigFileContent, "RetryInterval 15" , "RetryInterval 1")
runwait, net stop sevpnclient,, hide ; SoftEther's service need to be stopped to be able to change the config file. And stopping a service requires administrator priviledges.
replaceFileContent(SoftEtherDirectoryPath "\vpn_client.config", softetherConfigFileContent)
runwait, net start sevpnclient,, hide
run, %SoftEtherDirectoryPath%\vpncmgr.exe
if (currentConName != "")
run %vpncmd% accountconnect "%currentConName%",, hide
}
fetchServers()
{
if (!A_IsAdmin && (isUsingComodoFirewall || isUsingWindowsFirewall))
Run *RunAs "%A_ScriptFullPath%" --fetchServers
;/* Create the "Fetching servers..." dialog */
;{
gui fetchingUI:new, -Caption Border
gui, font, s12
gui add, text,, Fetching servers...
gui add, button, xp+37 y+10 gSetStopFetching, Stop
gui fetchingUI:show
stopFetching := false
;}
gui, mainUI:default
addedServers := 0
acceptableServerRegex := "s)<h3 class=["" -\w""]+?>(\d+\.\d+\.\d+\.\d+)<\/h3>\s+<p class=[ -\w""]+?>\w+ TCP\((\d+)\)[ \w()]+?<br>ping (80|[1-7][0-9])ms\(US\)" ; Regex to search for a server with a latency of 10-80ms to US, capturing its IP and TCP port.
loop ; Fetch infinitely ...
{
if (addedServers >= 15 || stopFetching) ; ... until 15 servers are added or until the user clicks the "Stop" button.
{
gui fetchingUI:destroy
break
}
try
{
whr.open("GET", "https://freevpn.gg?p=" A_Index) ; VPNs Manager fetches the servers from freevpn.gg, which holds a database of VPN servers, mostly, if not all, from VPN Gate. The
whr.send() ; advantage over taking directly from VPN Gate is that the latency tests are made from a US server instead of from a Japanese one,
pageResponse := whr.responseText ; which lets you have a more precise idea of the server's latency. Also, because it stores the full list of servers, you can even
; filter by country if you want, just add "/s/two_letters_country_code" before the question mark, e.g. "https://freevpn.gg/s/US?p=".
}
catch
{
msgbox, 48,, Couldn't connect to the server.
if (addedServers > 0)
break
else
return
}
currentPage := A_Index
i := 1 ; Position at the page's HTML code
while (i := RegExMatch(pageResponse, acceptableServerRegex, match, i+StrLen(match))) ; RegExMatch returns the character index where the occurrence begins. If it's not found, it returns 0, which is interpreted as false and the
{ ; while loop ends. The last parameter determines where the search must start from, which in this case is from where the previous occurrence ends.
FileRead, vpnsIniFileContent, %iniFileName%
ip := match1 ; match1 contains the first capturing group in the regex, which is the IP,
port := match2 ; and match2 contains the second group, which is the port number.
country := city := ""
if (ip != "" && !inStr(vpnsIniFileContent, ip)) ; Prevent an already added or deleted server from being added again. Only proceed if this IP doesn't appear anywhere in the file.
{
whr.open("GET", "https://freevpn.gg/c/" ip) ; Open the fetched server's details page ...
whr.send()
locationResponse := whr.responseText
RegExMatch(locationResponse, "<br><br>(.+?)<br>", location) ; ... to parse the server's location, which generally is "City, Country". Some servers have only the country name.
if (inStr(location1, ",")) ; If it's "City, Country" ...
{
locationsplit := StrSplit(location1, ", ") ; ... separate the two to do the processings.
country := locationsplit[2]
city := locationsplit[1]
}
else ; else, if it's only the country name.
country := location1
;/* Replace the countries name with their short versions to save space in the listview and make it easier to read */
country := strReplace(country, "United States of America", "USA")
country := strReplace(country, "Russian Federation", "Russia")
country := strReplace(country, "United Kingdom of Great Britain and Northern Ireland", "UK")
country := strReplace(country, "Venezuela (Bolivarian Republic of)", "Venezuela")
country := strReplace(country, "Republic of ", "")
StrReplace(vpnsIniFileContent, country, country, count) ; Get how many servers of this country are already added. The fourth parameter outputs the number of occurrences
; replaced. As all occurrences of the country name were replaced by itself, the result is basically a counter.
location := country
if (city != "")
location .= ", " city ; Here the location name is reversed, in which the most relevant info, the country
; name, comes before the city name. The ".=" operator is shorthand for concatenation.
if (count < 5) ; Prevent adding more than 5 servers of the same country, for variety reasons. Otherwise it would fetch only
{ ; servers from Korea and Japan, as the vast majority of VPN Gate servers are from these two countries.
if (inStr(vpnsIniFileContent, "Name=" location)) ; If there's already a server with this name ...
{
random, rand, 1, 50
location .= " " rand ; ... append a random number to it to differentiate.
}
addNewServer(location, ip, port)
addedServers++
LV_Add("Icon0", location, ip)
SB_SetText("Page " currentPage ", " addedServers " added - " location, 1)
}
}
}
SB_SetText("Page " currentPage ", " addedServers " added - " location, 1)
}
finishAddServer()
sleep 2000
SB_SetText("", 1)
}
setStopFetching() {
Critical
stopFetching := true
}
finishAddServer()
{
changeSoftEtherConfig()
if (isUsingComodoFirewall)
{
run "C:\Program Files\COMODO\COMODO Internet Security\cis.exe" --configUI=CeFirewallSettingsPage.html ; Open Comodo Firewall's settings screen ...
winwaitactive ahk_class CisMainWizard
sleep 500
send {tab}{enter} ; ... and automatically confirm it. This is necessary for the changes in the registry to take effect.
}
if (isUsingPeerBlock)
msgbox Don't forget to update PeerBlock settings.
gui addServerUI:destroy
rebuildListView()
}
openAddServerScreen()
{
if (!A_IsAdmin)
Run, *RunAs "%A_ScriptFullPath%" --openAddServerScreen
gui, addServerUI:new
gui, font, s10
;/* Name field */
gui, add, text, x18 y8, Name
gui, add, edit, vnameInput x18 y30 w208 limit60
;/* IP field */
gui, add, text, x18 y60 , Server IP
gui, add, edit, vipInput x18 y80 limit15
gui, font, s15
gui, add, text, x171 y78, :
;/* Port field */
gui, font, s10
gui, add, text, x180 y60, Port
gui, add, edit, vportInput w45 x180 y80 limit5 number
;/* "Add" button */
Gui, Font, s12
gui, add, button, x23 y+15 w80 h30 default gSubmitAddServerScreen, Add
;/* "Finish" button */
gui, add, button, xp+120 yp w80 h30 gFinishAddServer, Finish
gui show, w245 h160, Add Server
}
submitAddServerScreen()
{
global nameInput, ipInput, portInput ; The input box variables are required to be global.