Help Window developer set proxy with script

张鑫 - Sep 18 - - Dev Community

Who should read Proxy101?

  • Developers based in mainland China.
  • Anyone using a local proxy for various reasons.

We mainly focus on your local development environment. This guide is for you if:

  • You often use a proxy client during development.
  • You use a VPN and a proxy client together.
  • You’re not sure which proxy configuration to use for different endpoints.

Quick Setup

If you don't want to read more, just use the script I provided, but make sure your application is included. Here:

Script using http://127.0.0.1:1080 as default proxy settings.

What guide we don’t provided

  • We do not teach how to cross walls
  • We do not talk about some protocols like shadowsocks\vmess etc
  • We do not talk about buy/setup server side proxy/VPN server # How we organized guide For different operating systems and applications, we will recommend different settings methods.
  • [x] System proxy scripts
  • [x] Git
  • [x] IntelliJ IDEA
  • [x] Powershell
  • [x] Docker Desktop
  • [x] Maven
  • [x] Npm
  • [x] Powershell
  • [ ] Chrome
  • [ ] Macos(TODO)

Assume what you have now
A proxy client start on localhost listen on port 1080, protocol is http or socks5
Best Practices For Windows

System scripts

Set system proxy properly Or you can set through UI which I will skip

Set your proxyServer if you’re not 1080

Let’s see you are in a company named bilibili, your company’s internal domain is *.bilibili.co and for sure you don’t want the traffic to bilibili.co through your proxy server. You can add it bypass list.

# Set the proxy server address and port
$proxyServer = "http://127.0.0.1:1080"


# Set the bypass list
$bypassList = "*.bilibili.co;<local>"


# Enable the proxy and set the bypass list
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyServer -Value $proxyServer
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyEnable -Value 1
Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -Name ProxyOverride -Value $bypassList


# Notify the system of the proxy change
$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
$INTERNET_OPTION_SETTINGS_CHANGED = 39
$INTERNET_OPTION_REFRESH = 37
$type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) | Out-Null
$type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0) | Out-Null


Write-Host "Proxy has been configured and enabled with the specified bypass list."


Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' | Select-Object ProxyServer, ProxyEnable, ProxyOverride

Enter fullscreen mode Exit fullscreen mode

Git

Problems, you can find different methods config proxy for git clone using https. If you cannot clone using ssh protocol, configure the following steps.

@"
Host github.com
    Hostname ssh.github.com
    Port 443
    User git
"@ | Out-File -Append -FilePath "$env:USERPROFILE\.ssh\config" -Encoding utf8
Enter fullscreen mode Exit fullscreen mode

This script tells git clone to github using 443, which is a https protocol.

Import:You still have to set up a proxy for http. I will show you later

IntelliJ IDEA

Official guides

For my use experience, I will use it when I need to download a plugin from the plugin market.

IntelliJ IDEA for Java developers

Maven

Make sure you override the settings.xml,because all the best pracetice on Maven will depend on it.

Here’s how to setting properly

Image description

Code

If you need your java program’s connection using proxy, you have two options.

  1. Set proxy for your http client. Configuration methods for different libs. I will list some common use cases later.
  2. Use “proxifier" to proxy the traffic. Here's the instructions.

Terminal

Will be the same for Powershell.

Docker

Officials Guide

Set up as following, if you need pull image in private network, add private network subnet or domains in bypass list


function Update-DockerProxy {
    param (
        [string]$HttpProxy,
        [string]$HttpsProxy,
        [string]$NoProxy
    )

    $dockerConfigPath = "$env:USERPROFILE\.docker\config.json"

    if (Test-Path $dockerConfigPath) {
        $json = Get-Content $dockerConfigPath | ConvertFrom-Json
    } else {
        $json = @{}
    }

    $json.proxies.default = @{
        httpProxy  = $HttpProxy
        httpsProxy = $HttpsProxy
        noProxy    = $NoProxy
    }

    $json | ConvertTo-Json -Depth 10 | Set-Content $dockerConfigPath

    Write-Host "Docker proxy settings updated"
}
Enter fullscreen mode Exit fullscreen mode

Maven

To accelerate maven download speed, we have two option

  1. Use a different mirror
  2. Set proxy

I will only show proxy parts

Using Powershell to config maven proxy


function Update-MavenProxy {
    param (
        [string]$MavenSettingsPath = "$env:USERPROFILE\.m2\settings.xml",
        [string]$HttpProxy,
        [string]$HttpsProxy,
        [string]$NoProxy,
        [string]$ProxyUsername = "",
        [string]$ProxyPassword = ""
    )

    # Check if settings.xml exists, if not, create it with the default structure
    if (-Not (Test-Path $MavenSettingsPath)) {
        if (-Not (Test-Path "$env:USERPROFILE\.m2")) {
            New-Item -Path "$env:USERPROFILE\.m2" -ItemType Directory
        }

        @"
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              http://maven.apache.org/xsd/settings-1.0.0.xsd">
</settings>
"@ | Out-File -FilePath $MavenSettingsPath -Encoding utf8
    }

    [xml]$xml = Get-Content $MavenSettingsPath

    # Remove existing proxy elements
    $existingProxies = $xml.settings.proxies.proxy
    foreach ($proxy in $existingProxies) {
        $xml.settings.proxies.RemoveChild($proxy) | Out-Null
    }

    if (-Not $xml.settings.proxies) {
        $proxiesNode = $xml.CreateElement("proxies")
        $xml.settings.AppendChild($proxiesNode) | Out-Null
    }

    $proxy = $xml.CreateElement("proxy")
    $proxy.AppendChild($xml.CreateElement("active")).InnerText = "true"
    $proxy.AppendChild($xml.CreateElement("protocol")).InnerText = "http"
    $proxy.AppendChild($xml.CreateElement("host")).InnerText = ($HttpProxy -split ':')[1].Trim('/')
    $proxy.AppendChild($xml.CreateElement("port")).InnerText = ($HttpProxy -split ':')[-1]

    if ($ProxyUsername -and $ProxyPassword) {
        $proxy.AppendChild($xml.CreateElement("username")).InnerText = $ProxyUsername
        $proxy.AppendChild($xml.CreateElement("password")).InnerText = $ProxyPassword
    }

    $proxy.AppendChild($xml.CreateElement("nonProxyHosts")).InnerText = $NoProxy.Replace(",", "|")

    $xml.settings.proxies.AppendChild($proxy) | Out-Null
    $xml.Save($MavenSettingsPath)

    Write-Host "Maven proxy settings updated in $MavenSettingsPath"

}
Enter fullscreen mode Exit fullscreen mode

NPM

Also npm can accelerate through proxy or change mirror, Change proxy using following command

npm config set proxy http://127.0.0.1:1080  
npm config set https-proxy http://127.0.0.1:1080
Enter fullscreen mode Exit fullscreen mode

Chrome

  • using SwitchyOmega plugin
  • using system proxy default SwitchyOmega
.
Terabox Video Player