Repository navigation
Expand file tree
/
Copy pathproxy_core.ps1
More file actions
360 lines (324 loc) · 14 KB
/
Copy pathproxy_core.ps1
File metadata and controls
360 lines (324 loc) · 14 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# 获取参数:PowerShell 端通过 $__WinProxyCliArgs 传入
if ($env:_PROXY_SHELL -eq "pwsh" -and (Test-Path Variable:__WinProxyCliArgs)) {
$cliArgs = @($__WinProxyCliArgs)
} else {
$cliArgs = @($args)
}
$shellType = if ($env:_PROXY_SHELL) { $env:_PROXY_SHELL.ToLower() } else { "cmd" }
$outFile = $env:_PROXY_OUT
$configPath = Join-Path $HOME ".proxy_config.json"
$envNames = @("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY")
$actions = New-Object System.Collections.Generic.List[string]
function Add-SetProxyEnv([string]$Key, [string]$Val) {
if ($shellType -eq "pwsh") {
Set-Item -Path "Env:$Key" -Value $Val
} elseif ($shellType -eq "bash") {
$esc = $Val -replace "'", "'\''"
$actions.Add("export $Key='$esc'")
$actions.Add("export $($Key.ToLower())='$esc'")
} else {
$esc = ($Val -replace '"', '') -replace '%', '%%'
$actions.Add("set `"$Key=$esc`"")
}
}
function Add-UnsetProxyEnv([string]$Key) {
if ($shellType -eq "pwsh") {
Remove-Item -Path "Env:$Key" -ErrorAction SilentlyContinue
} elseif ($shellType -eq "bash") {
$actions.Add("unset $Key $($Key.ToLower())")
} else {
$actions.Add("set `"$Key=`"")
}
}
function Get-SessionCache {
if ($shellType -eq "pwsh") {
if ($global:_ProxySessionConfig) { return $global:_ProxySessionConfig }
return [ordered]@{ http = ""; https = ""; all = "" }
}
return [ordered]@{
http = if ($env:_PROXY_TMP_HTTP) { [string]$env:_PROXY_TMP_HTTP } else { "" }
https = if ($env:_PROXY_TMP_HTTPS) { [string]$env:_PROXY_TMP_HTTPS } else { "" }
all = if ($env:_PROXY_TMP_ALL) { [string]$env:_PROXY_TMP_ALL } else { "" }
}
}
function Add-SetTmpCache([string]$ProtoKey, [string]$Val) {
if ($shellType -eq "pwsh") {
if (-not $global:_ProxySessionConfig) {
$global:_ProxySessionConfig = [ordered]@{ http = ""; https = ""; all = "" }
}
$global:_ProxySessionConfig[$ProtoKey] = $Val
} else {
$tmpName = "_PROXY_TMP_$($ProtoKey.ToUpper())"
if ($shellType -eq "bash") {
$esc = $Val -replace "'", "'\''"
$actions.Add("export $tmpName='$esc'")
} else {
$esc = ($Val -replace '"', '') -replace '%', '%%'
$actions.Add("set `"$tmpName=$esc`"")
}
}
}
function Clear-AllTmpCache {
if ($shellType -eq "pwsh") {
$global:_ProxySessionConfig = $null
} else {
foreach ($tKey in @("_PROXY_TMP_HTTP", "_PROXY_TMP_HTTPS", "_PROXY_TMP_ALL")) {
if ($shellType -eq "bash") {
$actions.Add("unset $tKey")
} else {
$actions.Add("set `"$tKey=`"")
}
}
}
}
function Flush-Actions {
if ($shellType -ne "pwsh" -and $outFile -and $actions.Count -gt 0) {
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
if ($shellType -eq "bash") {
$content = ($actions -join "`n") + "`n"
} else {
$content = "@echo off`r`n" + ($actions -join "`r`n") + "`r`n"
}
[System.IO.File]::WriteAllText($outFile, $content, $utf8NoBom)
}
}
function Show-Help {
Write-Host @"
用法:
proxy on 检测代理连通性并在当前终端开启
proxy off 关闭当前终端的代理
proxy set [--all|-a|--http|--https] <URL> [--global]
设置代理地址 (默认等效于 --all)
加 --global 会同时写入预设及 Windows 全局环境变量
proxy show 查看当前终端、临时缓存、预设及全局代理配置
proxy clear | remove 清空所有代理配置并关闭当前代理
示例:
proxy set -a http://127.0.0.1:7890
proxy set --http http://127.0.0.1:7890 --https http://127.0.0.1:7891 --global
proxy on
"@ -ForegroundColor Cyan
}
function Get-ProxyConfig([string]$Path) {
$cfg = [ordered]@{ http = ""; https = ""; all = "" }
if (Test-Path $Path) {
try {
$raw = Get-Content -Path $Path -Raw -Encoding UTF8 | ConvertFrom-Json
if ($raw.http) { $cfg.http = [string]$raw.http }
if ($raw.https) { $cfg.https = [string]$raw.https }
if ($raw.all) { $cfg.all = [string]$raw.all }
} catch {}
}
return $cfg
}
function Save-ProxyConfig([string]$Path, $Cfg) {
$Cfg | ConvertTo-Json | Set-Content -Path $Path -Encoding UTF8
}
function Normalize-ProxyUrl([string]$Url) {
$Url = $Url.Trim()
if ([string]::IsNullOrWhiteSpace($Url)) { return $null }
if ($Url -notmatch '^[a-zA-Z][a-zA-Z0-9+.-]*://') {
$Url = "http://$Url"
}
try {
$uri = [System.Uri]$Url
if (-not $uri.Host -or $uri.Port -le 0) { return $null }
return $Url
} catch {
return $null
}
}
function Test-ProxyConnection([string]$ProxyUrl, [int]$TimeoutMs = 1500) {
$client = $null
try {
$uri = [System.Uri]$ProxyUrl
$client = New-Object System.Net.Sockets.TcpClient
$async = $client.BeginConnect($uri.Host, $uri.Port, $null, $null)
$wait = $async.AsyncWaitHandle.WaitOne($TimeoutMs, $false)
if ($wait -and $client.Connected) {
$client.EndConnect($async)
return $true
}
return $false
} catch {
return $false
} finally {
if ($client) { $client.Close() }
}
}
try {
if ($cliArgs.Count -eq 0) {
Show-Help
return
}
$cmd = $cliArgs[0].ToLower()
switch ($cmd) {
"on" {
$targets = [ordered]@{}
# 1. 读当前终端临时缓存
$sc = Get-SessionCache
if ($sc.http) { $targets["HTTP_PROXY"] = $sc.http }
if ($sc.https) { $targets["HTTPS_PROXY"] = $sc.https }
if ($sc.all) { $targets["ALL_PROXY"] = $sc.all }
# 2. 读全局预设文件 (.proxy_config.json)
if ($targets.Count -eq 0) {
$cfg = Get-ProxyConfig $configPath
if ($cfg.http) { $targets["HTTP_PROXY"] = $cfg.http }
if ($cfg.https) { $targets["HTTPS_PROXY"] = $cfg.https }
if ($cfg.all) { $targets["ALL_PROXY"] = $cfg.all }
}
# 3. 读当前终端环境变量
if ($targets.Count -eq 0) {
foreach ($name in $envNames) {
$v = (Get-Item -Path "Env:$name" -ErrorAction SilentlyContinue).Value
if ($v) { $targets[$name] = $v }
}
}
if ($targets.Count -eq 0) {
Write-Host "[提示] 尚未配置任何代理链接,请先设置代理地址。输入 proxy help 以查看帮助和示例。" -ForegroundColor Yellow
return
}
$checked = @{}
foreach ($entry in $targets.GetEnumerator()) {
$url = $entry.Value
if (-not $checked.ContainsKey($url)) {
Write-Host "正在检测代理连通性: $url ... " -NoNewline
$ok = Test-ProxyConnection $url
$checked[$url] = $ok
if ($ok) { Write-Host "通畅" -ForegroundColor Green }
else { Write-Host "连接失败" -ForegroundColor Red }
}
if (-not $checked[$url]) {
Write-Host "[失败] 代理 $url 无法连接,已中止开启(请确认代理软件已启动)。" -ForegroundColor Red
return
}
}
foreach ($entry in $targets.GetEnumerator()) {
Add-SetProxyEnv $entry.Key $entry.Value
}
Write-Host "[成功] 已在当前终端连接代理。" -ForegroundColor Green
}
"off" {
foreach ($name in $envNames) {
Add-UnsetProxyEnv $name
}
Write-Host "[成功] 已断开当前终端的代理。" -ForegroundColor Green
}
"set" {
$isGlobal = $false
$updates = [ordered]@{}
$positionalUrl = $null
$i = 1
while ($i -lt $cliArgs.Count) {
$arg = $cliArgs[$i]
switch ($arg.ToLower()) {
"--global" { $isGlobal = $true }
"-g" { $isGlobal = $true }
{ $_ -in @("--all", "-a", "--http", "--https") } {
if ($i + 1 -ge $cliArgs.Count -or $cliArgs[$i + 1].StartsWith("-")) {
Write-Host "[错误] 参数 $arg 后面缺少代理地址。" -ForegroundColor Red
return
}
$val = Normalize-ProxyUrl $cliArgs[$i + 1]
if (-not $val) {
Write-Host "[错误] 无效的代理地址: $($cliArgs[$i + 1])" -ForegroundColor Red
return
}
if ($_ -in @("--all", "-a")) {
$updates["http"] = $val
$updates["https"] = $val
$updates["all"] = $val
} elseif ($_ -eq "--http") {
$updates["http"] = $val
} elseif ($_ -eq "--https") {
$updates["https"] = $val
}
$i++
}
default {
if ($arg.StartsWith("-")) {
Write-Host "[错误] 未知参数: $arg" -ForegroundColor Red
return
}
$val = Normalize-ProxyUrl $arg
if (-not $val) {
Write-Host "[错误] 无效的代理地址: $arg" -ForegroundColor Red
return
}
$positionalUrl = $val
}
}
$i++
}
if ($updates.Count -eq 0 -and $positionalUrl) {
$updates["http"] = $positionalUrl
$updates["https"] = $positionalUrl
$updates["all"] = $positionalUrl
}
if ($updates.Count -eq 0) {
Write-Host "[错误] 请提供代理地址,例如: proxy set -a http://127.0.0.1:7890" -ForegroundColor Red
return
}
$envMap = @{ http = "HTTP_PROXY"; https = "HTTPS_PROXY"; all = "ALL_PROXY" }
if ($isGlobal) {
$cfg = Get-ProxyConfig $configPath
foreach ($entry in $updates.GetEnumerator()) {
$cfg[$entry.Key] = $entry.Value
}
Save-ProxyConfig $configPath $cfg
Clear-AllTmpCache
foreach ($entry in $updates.GetEnumerator()) {
$envKey = $envMap[$entry.Key]
Add-SetProxyEnv $envKey $entry.Value
[Environment]::SetEnvironmentVariable($envKey, $entry.Value, "User")
}
Write-Host "[成功] 已设置代理(当前终端 + 预设配置 + Windows 全局用户环境变量)。" -ForegroundColor Green
} else {
foreach ($entry in $updates.GetEnumerator()) {
Add-SetProxyEnv $envMap[$entry.Key] $entry.Value
Add-SetTmpCache $entry.Key $entry.Value
}
Write-Host "[成功] 已在当前终端临时设置代理(仅当前窗口有效,未写入全局配置)。" -ForegroundColor Green
}
}
"show" {
Write-Host "=== 当前终端代理 (Session) ===" -ForegroundColor Cyan
foreach ($name in $envNames) {
$val = (Get-Item -Path "Env:$name" -ErrorAction SilentlyContinue).Value
Write-Host (" {0,-12}: {1}" -f $name, $(if ($val) { $val } else { "(未设置)" }))
}
$sc = Get-SessionCache
Write-Host "`n=== 当前终端临时配置 (Session Cache) ===" -ForegroundColor Cyan
Write-Host (" {0,-12}: {1}" -f "http", $(if ($sc.http) { $sc.http } else { "(未设置)" }))
Write-Host (" {0,-12}: {1}" -f "https", $(if ($sc.https) { $sc.https } else { "(未设置)" }))
Write-Host (" {0,-12}: {1}" -f "all", $(if ($sc.all) { $sc.all } else { "(未设置)" }))
$cfg = Get-ProxyConfig $configPath
Write-Host "`n=== 已保存预设配置 ($configPath) ===" -ForegroundColor Cyan
Write-Host (" {0,-12}: {1}" -f "http", $(if ($cfg.http) { $cfg.http } else { "(未设置)" }))
Write-Host (" {0,-12}: {1}" -f "https", $(if ($cfg.https) { $cfg.https } else { "(未设置)" }))
Write-Host (" {0,-12}: {1}" -f "all", $(if ($cfg.all) { $cfg.all } else { "(未设置)" }))
Write-Host "`n=== Windows 全局用户环境变量 (Global/User) ===" -ForegroundColor Cyan
foreach ($name in $envNames) {
$gVal = [Environment]::GetEnvironmentVariable($name, "User")
Write-Host (" {0,-12}: {1}" -f $name, $(if ($gVal) { $gVal } else { "(未设置)" }))
}
}
{ $_ -in @("clear", "remove") } {
foreach ($name in $envNames) {
Add-UnsetProxyEnv $name
if ([Environment]::GetEnvironmentVariable($name, "User")) {
[Environment]::SetEnvironmentVariable($name, $null, "User")
}
}
Clear-AllTmpCache
if (Test-Path $configPath) {
Remove-Item -Path $configPath -Force
}
Write-Host "[成功] 已清空当前终端、临时缓存、预设配置以及 Windows 全局用户环境变量中的所有代理设置。" -ForegroundColor Green
}
default {
Show-Help
}
}
} finally {
Flush-Actions
}