Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This may be a feature or a tips: Instantly refresh Windows proxy settings #4

Open
Unknown78 opened this issue Aug 5, 2022 · 1 comment

Comments

@Unknown78
Copy link

After changing the proxy settings, whether it's enabling, disabling, or changing the address/port, it does not automatically applies to the whole system.

This PowerShell script from stackoverflow helps it a lot.
https://stackoverflow.com/a/39079005/6458107https://stackoverflow.com/questions/21568502/ie-enable-disable-proxy-settings-via-registry

function Refresh-System
{
  $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

$INTERNET_OPTION_SETTINGS_CHANGED   = 39
$INTERNET_OPTION_REFRESH            = 37
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
return $a -and $b
}
Refresh-System

Save that script into something like RefreshPROXY.ps1 and run it whenever you need to.


I usually turn this script into one line that you could run on command prompt, windows terminal, or inside a batch script however you want to.

powershell -command "$signature = '[DllImport("^""wininet.dll"^"", SetLastError = true, CharSet=CharSet.Auto)] public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);'; $type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru; $type::InternetSetOption(0,39,0,0); $type::InternetSetOption(0,37,0,0);" > nul


This could be a feature and be run as dispatch proxy-refresh if it were implemented. With it's own Rust code, of course, good luck devs... 👍

@alexkirsz
Copy link
Owner

If this is a genuinely useful command for all Windows programs, that feature would make sense in its own crate and exposed as its own CLI.

For this rewrite, I also looked at automatically applying proxy settings to the whole system as a dispatch option. There are crates that allow you to do this, but I'm wary of compatibility between OSes.

For Windows: https://crates.io/crates/proxyconf
For Mac: https://github.com/wyhaya/networksetup
For Linux, I expect there's a plethora of options depending on the distribution.

I am not likely to implement this myself as part of dispatch-proxy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants