-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershell-windows-autoproxy.psm1
90 lines (81 loc) · 2.9 KB
/
powershell-windows-autoproxy.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
$proxyInfo = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
# Base
$proxyEnable = $proxyInfo.ProxyEnable
$proxyServer = $proxyInfo.ProxyServer
$proxyPort = $proxyServer.Split(':')[1]
$proxyOverride = $proxyInfo.ProxyOverride
$no_proxy = ($proxyOverride -split ";") -join ","
$no_proxy = $no_proxy -replace "(,|^)<local>(,|$)", "$1$2"
$checkProxy = [Environment]::GetEnvironmentVariable('http_proxy')
# WSA
$WsaService = Get-Service -Name WsaService -ErrorAction SilentlyContinue
if ($WsaService) {
$WsaEnabled = $WsaService.Status -eq 'Running'
$adbCommand = Get-Command -Name adb -ErrorAction SilentlyContinue
if ( $proxyServer -like "*127.0.0.1*" ){
$WsaIP = $(Get-NetIPAddress -InterfaceAlias 'vEthernet (WSLCore)' -AddressFamily IPV4).IPAddress
} else {
$WsaIP = $proxyServer.Split(':')[0]
}
}
function proxy {
if ($proxyServer) {
# PowerShell
[Environment]::SetEnvironmentVariable("http_proxy", "http://$proxyServer")
[Environment]::SetEnvironmentVariable("https_proxy", "http://$proxyServer")
# [Environment]::SetEnvironmentVariable("all_proxy", "socks5://$proxyServer")
[Environment]::SetEnvironmentVariable("no_proxy", $no_proxy)
try {
Get-Command git -ErrorAction Stop >$null
git config --global http.proxy "http://$proxyServer"
git config --global https.proxy "http://$proxyServer"
} catch {
# Write-Output "Git command does not exist, skipping proxy configuration"
}
# WSA
if ($WsaEnabled) {
if (-not $adbCommand) {
Write-Host "adb command not found. Installing adb..."
winget install Google.PlatformTools
} else {
adb connect 127.0.0.1:58526
if (adb devices | Select-String -Pattern "device$") {
adb shell settings put global http_proxy ${WsaIP}:${proxyPort}
}
}
}
}
}
function noproxy {
# PowerShell
[Environment]::SetEnvironmentVariable("http_proxy", $null)
[Environment]::SetEnvironmentVariable("https_proxy", $null)
# [Environment]::SetEnvironmentVariable("all_proxy", $null)
[Environment]::SetEnvironmentVariable("no_proxy", $null)
try {
Get-Command git -ErrorAction Stop >$null
git config --global --unset http.proxy
git config --global --unset https.proxy
} catch {
# Write-Output "Git command does not exist, skipping proxy configuration"
}
# WSA
if ($WsaEnabled) {
if (adb devices | Select-String -Pattern "device$") {
adb shell settings put global http_proxy :0
}
}
}
if ($proxyEnable) {
proxy
}
elseif ($checkProxy) {
noproxy
}
$exportModuleMemberParams = @{
Function = @(
'proxy',
'noproxy'
)
}
Export-ModuleMember @exportModuleMemberParams