Skip to content

Instantly share code, notes, and snippets.

Created Mar 21, 2020
What would you like to do?
Serve proxy auto-configuration file on Windows
# Serve proxy auto-configuration file
[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$SocksPort';
if (
dnsDomainIs(host, '') ||
dnsDomainIs(host, '')
) {
return socks;
var hostIP = dnsResolve(host);
if (
isInNet(hostIP, '', '') ||
isInNet(hostIP, '', '')
) {
return socks;
return 'DIRECT';
$url = "$PacPort/"
$registryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
$registryName = 'AutoConfigURL'
# Refresh proxy settings.
# ref.
$WinInet = Add-Type -MemberDefinition @'
public static extern bool InternetSetOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, uint dwBufferLength);
'@ -Name Win32InternetSettings -PassThru
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
try {
Set-ItemProperty -Path $registryPath -Name $registryName -Value $url
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.
$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

This comment has been minimized.

Copy link

@jay-99 jay-99 commented Jun 19, 2020

Hi. I have noticed that the Refresh-Proxy-Settings function has stopped working on windows 10 version 2004. I've test this on 3 computers running Windows 1909 and it's working on all of them. I've also tested on 3 computers running Windows 10 2004 and none of them work, so I think an update has broken this. Are you able to confirm on your side?


This comment has been minimized.

Copy link
Owner Author

@nanto nanto commented Jun 19, 2020

Thank you for your report! I'm afraid I don't have Windows 10 2004 computers.

Maybe we have to change proxy settings by calling the InternetSetOption function with the INTERNET_OPTION_PER_CONNECTION_OPTION flag as written in , or we have to use some other API...


This comment has been minimized.

Copy link

@jay-99 jay-99 commented Jun 23, 2020

Thanks, I will look into this and let you know if I find anything useful.

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