Created
March 21, 2020 13:20
-
-
Save nanto/ec323bb7cee4220b7f42ff874c8419b9 to your computer and use it in GitHub Desktop.
Serve proxy auto-configuration file on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Serve proxy auto-configuration file | |
Param( | |
[int]$PacPort = 2694, | |
[int]$SocksPort = 1080 | |
) | |
# Write your pac file content here: | |
$pac = [System.Text.Encoding]::UTF8.GetBytes(@" | |
function FindProxyForURL(url, host) { | |
var socks = 'SOCKS5 127.0.0.1:$SocksPort'; | |
if ( | |
dnsDomainIs(host, '.internal.exapmle.com') || | |
dnsDomainIs(host, '.internal.example.org') | |
) { | |
return socks; | |
} | |
var hostIP = dnsResolve(host); | |
if ( | |
isInNet(hostIP, '10.0.0.0', '255.0.0.0') || | |
isInNet(hostIP, '192.168.0.0', '255.255.0.0') | |
) { | |
return socks; | |
} | |
return 'DIRECT'; | |
} | |
"@) | |
$url = "http://127.0.0.1:$PacPort/" | |
$registryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' | |
$registryName = 'AutoConfigURL' | |
# Refresh proxy settings. | |
# ref. https://web.archive.org/web/20170602213824/http://www.hsyntt.com/changing-proxy-server-settings-for-internet-explorer-using-powershell/ | |
$WinInet = Add-Type -MemberDefinition @' | |
[DllImport("wininet.dll")] | |
public static extern bool InternetSetOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, uint dwBufferLength); | |
'@ -Name Win32InternetSettings -PassThru | |
$INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95 | |
$INTERNET_OPTION_REFRESH = 37 | |
function Refresh-Proxy-Settings { | |
$WinInet::InternetSetOption([System.IntPtr]::Zero, $INTERNET_OPTION_PROXY_SETTINGS_CHANGED, [System.IntPtr]::Zero, 0) | Out-Null | |
$WinInet::InternetSetOption([System.IntPtr]::Zero, $INTERNET_OPTION_REFRESH, [System.IntPtr]::Zero, 0) | Out-Null | |
} | |
$listener = New-Object System.Net.HttpListener | |
$listener.Prefixes.Add($url) | |
try { | |
$listener.Start() | |
Set-ItemProperty -Path $registryPath -Name $registryName -Value $url | |
Refresh-Proxy-Settings | |
Write-Host "Serving Proxy Auto-Configuration File on $url" | |
Write-Host "Please execute 'ssh -D $SocksPort <gateway-server>'" | |
Write-Host '' | |
Write-Host 'Press Ctrl+C to exit.' | |
while ($True) { | |
# Use async method to accept Ctrl+C while waiting a request. | |
# ref. https://stackoverflow.com/questions/51218257/await-async-c-sharp-method-from-powershell | |
$task = $listener.GetContextAsync() | |
while (-not $task.AsyncWaitHandle.WaitOne(200)) { } | |
$context = $task.GetAwaiter().GetResult() | |
$response = $context.Response | |
$response.ContentType = 'application/x-ns-proxy-autoconfig' | |
$response.ContentLength64 = $pac.Length | |
$response.Close($pac, $True) | |
} | |
} finally { | |
Remove-ItemProperty -Path $registryPath -Name $registryName | |
Refresh-Proxy-Settings | |
$listener.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm... I can't reproduce the issue. This script still works for me (enables proxy without restarting browsers) on Windows 11 Pro 23H2 and Firefox nightly 127.0a1, Chrome canary 126.0.6443.0, and Edge 124.0.2478.51.