forked from ElectricRCAircraftGuy/Windows_Proxy_Toggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle_proxy_on_off.vbs
executable file
·77 lines (70 loc) · 3.5 KB
/
toggle_proxy_on_off.vbs
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
' This file is part of Windows_Proxy_Toggler: https://github.com/ElectricRCAircraftGuy/Windows_Proxy_Toggler
'
' Toggle your Proxy on and off via a clickable desktop shortcut/icon
' By Gabriel Staples, June 2017
' www.ElectricRCAircraftGuy.com
' See the README at the link above.
Option Explicit
'Variables & Constants:
Dim ProxySettings_path, VbsScript_filename, Desktop_path
VbsScript_filename = "toggle_proxy_on_off.vbs"
'sec; change this value to set how long the message box displays when you toggle the proxy setting
Const MESSAGE_BOX_TIMEOUT = 1
Const PROXY_OFF = 0
Dim WSHShell, proxyEnableVal, username
Set WSHShell = WScript.CreateObject("WScript.Shell")
'get the username string for use in path names, since trying to use the "%USERNAME%" variable
'directly in path names throws an error
username = WSHShell.ExpandEnvironmentStrings("%USERNAME%")
ProxySettings_path = createobject("Scripting.FileSystemObject").GetFolder(".").Path
Desktop_path = WSHShell.SpecialFolders("Desktop")
'Determine current proxy setting and toggle to opposite setting
proxyEnableVal = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If proxyEnableVal = PROXY_OFF Then
TurnProxyOn
Else
TurnProxyOff
End If
'Subroutine to Toggle Proxy Setting to ON
Sub TurnProxyOn
'turn proxy on via a registry entry
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
'create/update desktop shortcut
CreateOrUpdateDesktopShortcut("on")
'notify user via an auto-timed popup box
WSHShell.Popup "Internet proxy is now ON", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub
'Subroutine to Toggle Proxy Setting to OFF
Sub TurnProxyOff
'turn proxy off via a registry entry
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
'create/update desktop shortcut
CreateOrUpdateDesktopShortcut("off")
'notify user via an auto-timed popup box
WSHShell.Popup "Internet proxy is now OFF", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub
'Subroutine to create or update a shortcut on the desktop
Sub CreateOrUpdateDesktopShortcut(onOrOff)
'create a shortcut
Dim shortcut, iconStr
Set shortcut = WSHShell.CreateShortcut(Desktop_path + "\Proxy On-Off.lnk")
'Set the target path (target file) to run when the shortcut is clicked
shortcut.TargetPath = ProxySettings_path + "\" + VbsScript_filename
'Set the working directory. This is necessary in case you ever make this shortcut call a batch
'(.bat) file, for instance, which in turn calls a .vbs script. In order to know where the .vbs
'script file/command is located, the shortcut must be operating in the working directory where
'the .vbs scripts are located. Otherwise, calls to the .vbs scripts from a .bat file this
'shortcut points to, for instance, won't work since their directories are not in the Windows
'%PATH% variable, and you'll get an error which states: "'name_of_vbs_script_file' is not
'recognized as an internal or external command, operable program or batch file."
shortcut.WorkingDirectory = ProxySettings_path
'Set the icon to associate with this shortcut
If onOrOff = "on" Then
iconStr = "on.ico"
ElseIf onOrOff = "off" Then
iconStr = "off.ico"
End If
shortcut.IconLocation = ProxySettings_path + "\icons\" + iconStr
'Save the shortcut
shortcut.Save
End Sub