Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Switch Proxy On or Off


  • Please log in to reply
4 replies to this topic
RaveWolf
  • Members
  • 11 posts
  • Last active: Nov 29 2016 10:12 PM
  • Joined: 10 Feb 2014

Hi all,

 

This is my first shared script, so please bear with me. Tips and advice are always welcome.  :D

 

I have seen many attempts/theories on how to switch the proxy on and off.

Many have ridiculously large scripts, registry edits and reloads or reboots, etc...

None of which worked for me.

 

Here is my Solution (a bit crude, but it works for me 100%):

 

The Theory and what it does -

1) Toggle the Status (On|Off), (via *.ahk file)

  1. Opening the Internet Setting directly (inetcpl.cpl),
  2. Click on the Connections Tab,
  3. Click on Lan Settings,
  4. Click on Proxy Server check box,
  5. Click Ok to close child window,
  6. Click Ok to close parent window.

2) Display Status. (Msgbox)

  1. Run the *.bat File, (If someone knows how to convert this to AHK Script please let me know)
  2. Get result and save to windows clipboard,
  3. Get current Status from clipboard.
  4. Convert to a single digit (0|1),
  5. Compare it for the "if" statements
  6. Check the current status,
  7. Display Updated Status,
  8. Sleep for 3 seconds and close.

Possible Issues / Troubleshooting:

My System - Win 7 (64bit), Screen Resolution (1366 x 768)

 

Please note that the process used, opens up the Internet Settings Control Panel and uses a automated mouse Click process to activate the settings. (This is not ideal, but it works)

 

I found that when clicking on the "Proxy Server" check box, the mouse would move to a different location within that window on each run and would miss the check box. I found the same issue when using the Tab key option.

I can only assume that the Window changes sizes or is populated Dynamically and therefore changes the location of each control while loading. What I did to resolve this, was to Move the mouse to 0,0 and then move it again to 145, 210. This solved the issue and now works 100%

 

NB: Before running the full script,... I suggest commenting out the "Click" actions to confirm that the mouse is indeed moving to the correct position (the "Proxy Server" check box) before running the "Click" action.

 

*************** How it's done! ***************

 

Process:

1) Create a Batch (*.bat) file and insert the Script below. (If someone knows how to convert this to AHK Script please let me know)

2) Save this *bat file in the same folder as your .ahk script or change "Run, ProxyState.bat" below to your desired location.

3) Create your *ahk file and insert the Script below.

 

*************** Scripts Start ***************

 

Filename: ProxyState.bat

Contents:

@echo off

setlocal ENABLEEXTENSIONS
set KEY_NAME=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings
set VALUE_NAME=ProxyEnable
for /F "usebackq tokens=3" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
  echo %%A | clip
)
 
Filename: Network.ahk
AHK File Contents:
; Network Settings
*#i::
{
Run inetcpl.cpl
SetTitleMatchMode, 2
WinWait, Internet Properties, , 15, , 
WinActivate, Internet Properties, , 15, , 
Click 250, 45
Click 330, 400
WinWait, Local Area Network (LAN) Settings, , 15, 
WinActivate, Local Area Network (LAN) Settings, , 15, , 
MouseMove, 0, 0
MouseMove, 145, 210
Click
Send {Enter}
Send {Tab}
Send {Enter}
}
Goto, GetProxyState
return
 
GetProxyState:
;*#o::
{
clipboard = 
Run, ProxyState.bat
ClipWait
clipboard = %clipboard%
StringReplace, clipboard, clipboard, `r`n, , All
StringTrimLeft, ProxyState, clipboard, 2
ProxyOn = 1
ProxyOff = 0
If (ProxyState = ProxyOff)
{
MsgBox, 0, Proxy Status, Proxy is now OFF., 3
}
else If (ProxyState = ProxyOn)
{
MsgBox, 0, Proxy Status, Proxy is now ON., 3
}
else MsgBox, 0, Proxy Status, ProxyState = %ProxyState%; ProxyOn = %ProxyOn%; ProxyOff = %ProxyOff%; Clipboard = %clipboard%, 
}
return

 

*************** Scripts End ***************

 

PS: The last MsgBox is just there for a visual error check to ensure that proxy value is pulling through correctly.

 

Enjoy.



V1P3R
  • Members
  • 28 posts
  • Last active: Jun 19 2018 12:13 PM
  • Joined: 26 Sep 2006

Hello,

 

I'm doing one myself now also.

This one basically switches from using an automatic config script (PAC file) to using a Proxy server. Shouldn't be too hard for you to modify to suit your own needs. Works flawlessly for me.

2 LOREM IPSUM placeholders for the stuff I'm using.

Autohotkey 2.0 syntax, but shouldn't be hard to translate the few incompatible stuff back to 1.0. (RegRead HKCU\* should be RegRead HKCU,* eg.)

It doesn't need a batch file or tedious WinActive, etc. stuff.

The value of the 9th byte of the DefaultConnectionSettings value is explained in the comments.

setproxy()
;newbyte values
;01	None
;03	Proxy Server
;05	PAC script
;07     03 and 05
;09	Auto detect
;0B	03 and 09
;0D	05 and 09
;0F	03 and 05 and 09
{
	IESET:="Software\Microsoft\Windows\CurrentVersion\Internet Settings"
	RegRead,pac,HKCU\%IESET%,AutoConfigURL
	If pac=""
	{
		ieproxy:="LOREM IPSUM 1"
		RegDelete,HKCU\%IESET%,ProxyServer
		RegWrite,REG_SZ,HKCU\%IESET%,AutoConfigURL,%ieproxy%
		RegWrite,REG_DWORD,HKCU\%IESET%,Proxyenable,0
		RegDelete,HKCU\%IESET%,ProxyOverride
		newByte:="0D"
	}
	else
	{
		ieproxy:="LOREM IPSUM 2"
		RegWrite,REG_SZ,HKCU\%IESET%,ProxyServer,%ieproxy%
		RegDelete,HKCU\%IESET%,AutoConfigURL
		RegWrite,REG_DWORD,HKCU\%IESET%,Proxyenable,1
		RegWrite,REG_SZ,HKCU\%IESET%,ProxyOverride,<local>
		newByte:="03"
	}
	RegRead,oldSet1,HKCU\%IESET%,DefaultConnectionSettings
	RegRead,oldSet2,HKCU\%IESET%\Connections,DefaultConnectionSettings
	newSet1:=RegExReplace(oldSet1,".{2}",newbyte,,1,17)
	newSet2:=RegExReplace(oldSet2,".{2}",newbyte,,1,17)
	RegWrite,REG_BINARY,HKCU\%IESET%,DefaultConnectionSettings,%newSet1%
	RegWrite,REG_BINARY,HKCU\%IESET%\Connections,DefaultConnectionSettings,%newSet2%
	dllcall("wininet\InternetSetOptionW","int","0","int","39","int","0","int","0")
	dllcall("wininet\InternetSetOptionW","int","0","int","37","int","0","int","0")
	Return
}



AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012

On ALL (nearly 15) Windows Vista-8 PCs (some x64 some x86) I have in my company here, this code below works.

 

You could use something simple like this and actually automate it.

SetProxy(AddressPort:="",State:=""){
IfNotEqual,AddressPort,,RegWrite,REG_SZ,HKCU,Software\Microsoft\Windows\CurrentVersion\Internet Settings,ProxyServer,%AddressPort%
RegWrite,REG_DWORD,HKCU,Software\Microsoft\Windows\CurrentVersion\Internet Settings,ProxyEnable,% ((State=""||State=1)?1:0)
DllCall("wininet\InternetSetOptionW",int,0,int,39,int,0,int,0),DllCall("wininet\InternetSetOptionW",int,0,int,37,int,0,int,0)
return
}

Then to grab the values:

RegRead(RootKey:="HKCU",SubKey:="Software\Microsoft\Windows\CurrentVersion\Internet Settings",ValueName:=""){
RegRead,O,%RootKey%,%SubKey%,%ValueName%
return O
}

As an example:

SetProxy("0.0.0.0:0",0)
,HostProxy=RegRead(,,"ProxyServer")
,ProxyStatus=RegRead(,,"ProxyEnable")
If ProxyStatus
   ProxyStatus:="ON"
else ProxyStatus:="OFF"

Msgbox,Your proxy IP address is: %HostProxy%`nThis proxy is %ProxyStatus%


RaveWolf
  • Members
  • 11 posts
  • Last active: Nov 29 2016 10:12 PM
  • Joined: 10 Feb 2014

Wow, Thanks guys.

 

These are truly awesome and work perfectly. No more old *.bat files.

It all makes perfect sense, just need to do my homework on the "DllCall".



RickC
  • Members
  • 123 posts
  • Last active: Nov 08 2016 05:19 AM
  • Joined: 13 Nov 2005

The proxy toggler we use:

Gui, Add, Button, x26 y10 w100 h30 Gon , I'm at work
Gui, Add, Button, x26 y50 w100 h30 Goff , I'm at home
Gui, Show, x129 y99 h86 w154, Proxy Toggler
Return


on:
setproxy("ON")
ExitApp
return


off:
setproxy("OFF")
ExitApp
return


setproxy(state="")
{ 
if(state = "")
state = "ON"
if (state ="ON")
RegWrite,REG_SZ,HKCU,Software\Microsoft\Windows\CurrentVersion\Internet Settings,AutoConfigURL,http://shield.bcc.lan/proxyconfiguration/ldap.pac
   else if (state="OFF")
RegDelete,HKCU,Software\Microsoft\Windows\CurrentVersion\Internet Settings,AutoConfigURL


   dllcall("wininet\InternetSetOptionW","int","0","int","39","int","0","int","0")
   dllcall("wininet\InternetSetOptionW","int","0","int","37","int","0","int","0")
   Return
}
;----------------------------------------------------------
; Function RegRead
;----------------------------------------------------------
RegRead(RootKey, SubKey, ValueName = "") {
   RegRead, v, %RootKey%, %SubKey%, %ValueName%
   Return, v
}


GuiClose:
ExitApp

Hope this helps...