Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created August 29, 2019 07:05
Show Gist options
  • Save mark05e/955d06a92a2e9e9dcd40b7efd03063f0 to your computer and use it in GitHub Desktop.
Save mark05e/955d06a92a2e9e9dcd40b7efd03063f0 to your computer and use it in GitHub Desktop.
AHK Script to enable/disable proxy. Works on Win10 too!
; set_ie_proxy.ahk
;#################################################################################
; Enable/Disable IE Proxy
;#################################################################################
; ref: https://autohotkey.com/board/topic/101854-switch-proxy-on-or-off/?p=633874
SetProxy("proxy.myproxyprovider.com:1234",1)
,HostProxy=RegRead(,,"ProxyServer")
,ProxyStatus=RegRead(,,"ProxyEnable")
If ProxyStatus
ProxyStatus:="ON"
else ProxyStatus:="OFF"
Msgbox,Your proxy IP address is: %HostProxy%`nThis proxy is %ProxyStatus%
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
}
RegRead(RootKey:="HKCU",SubKey:="Software\Microsoft\Windows\CurrentVersion\Internet Settings",ValueName:=""){
RegRead,O,%RootKey%,%SubKey%,%ValueName%
return O
}
;#################################################################################
;EXAMPLES
;#################################################################################
;SetProxy("0.0.0.0:0",0) ; set 0.0.0.0, port 0 as proxy address and disable proxy usage
;SetProxy("0.0.0.0:0",1) ; set 0.0.0.0, port 0 as proxy address and ENABLE proxy usage
;SetProxy("",0) ; clear proxy address and disable proxy usage
; Shortcut to access IE proxy settings
; on the run prompt type: control inetcpl.cpl,,4
; ref: http://www.renjusblog.com/2013/09/create-shortcut-key-lan-settings-window.html
@erbanku
Copy link

erbanku commented May 15, 2024

This works fine.
(Written by ChatGPT)

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; Toggle Proxy
^!p::  ; Ctrl+Alt+P as the hotkey.
ToggleProxy()
return

ToggleProxy() {
    ; Access the registry key where proxy settings are stored.
    RegRead, ProxyEnable, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Internet Settings, ProxyEnable
    
    ; Check if the proxy is currently enabled (ProxyEnable = 1).
    if (ProxyEnable = 1) {
        ; Proxy is enabled, so disable it.
        RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Internet Settings, ProxyEnable, 0
        Tooltip, Proxy disabled.
    } else {
        ; Proxy is disabled, so enable it.
        RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Internet Settings, ProxyEnable, 1
        Tooltip, Proxy enabled.
    }

    ; Refresh Internet settings to apply changes.
    DllCall("Wininet.dll\InternetSetOptionW", UInt, 0, UInt, 39, UInt, 0, UInt, 0)  ; INTERNET_OPTION_SETTINGS_CHANGED
    DllCall("Wininet.dll\InternetSetOptionW", UInt, 0, UInt, 37, UInt, 0, UInt, 0)  ; INTERNET_OPTION_REFRESH

    ; Show a notification for 1 second.
    SetTimer, RemoveTooltip, 1000  ; Remove the tooltip after 1 second.
    return
}

RemoveTooltip:
    SetTimer, RemoveTooltip, Off
    Tooltip
return

@mark05e
Copy link
Author

mark05e commented May 16, 2024

Thank you for sharing @erbanku

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment