-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-proxy-config.ps1
More file actions
79 lines (65 loc) · 3.66 KB
/
Copy pathapply-proxy-config.ps1
File metadata and controls
79 lines (65 loc) · 3.66 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
# apply-proxy-config.ps1 — 拿到隧道地址后,一键回填扩展配置
#
# 作用:把 Cloudflare Tunnel 地址(和可选的访问令牌)自动写入:
# 1. background.js -> const PROXY_ENDPOINT / const PROXY_ACCESS_TOKEN
# 2. manifest.json -> host_permissions[0]
#
# 用法(在本目录下的 PowerShell 执行):
# .\apply-proxy-config.ps1 -TunnelUrl https://xxx.trycloudflare.com # 只换地址,令牌保持不变
#
# 回填完成后到 edge://extensions 点「重新加载」即可生效。
param(
[Parameter(Mandatory = $true)]
[string]$TunnelUrl,
[Parameter(Mandatory = $false)]
[string]$Token = ""
)
$ErrorActionPreference = "Stop"
$root = $PSScriptRoot
if (-not $root) { $root = (Get-Location).Path }
$bgPath = Join-Path $root "background.js"
$mfPath = Join-Path $root "manifest.json"
# ---------- 0. 参数校验 ----------
$TunnelUrl = $TunnelUrl.TrimEnd("/")
if ($TunnelUrl -notmatch '^https://[a-zA-Z0-9.-]+$') {
throw "TunnelUrl 必须形如 https://xxx.trycloudflare.com 或 https://trans.你的域名(不带路径),当前:$TunnelUrl"
}
if (-not (Test-Path $bgPath)) { throw "找不到 $bgPath" }
if (-not (Test-Path $mfPath)) { throw "找不到 $mfPath" }
# 统一用「UTF-8 无 BOM」读写,避免中文注释乱码 / 浏览器解析问题
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
# ---------- 1. background.js ----------
$bg = [System.IO.File]::ReadAllText($bgPath, [System.Text.Encoding]::UTF8)
$patternEndpoint = "const PROXY_ENDPOINT = '[^']*';"
if ($bg -notmatch $patternEndpoint) { throw "background.js 中未找到 const PROXY_ENDPOINT = '...' 行" }
$bg = [regex]::Replace($bg, $patternEndpoint, "const PROXY_ENDPOINT = '$TunnelUrl';")
Write-Host "[ok] background.js PROXY_ENDPOINT -> $TunnelUrl" -ForegroundColor Green
if ($Token -ne "") {
$patternToken = "const PROXY_ACCESS_TOKEN = '[^']*';"
if ($bg -notmatch $patternToken) { throw "background.js 中未找到 const PROXY_ACCESS_TOKEN = '...' 行" }
$bg = [regex]::Replace($bg, $patternToken, "const PROXY_ACCESS_TOKEN = '$Token';")
Write-Host "[ok] background.js PROXY_ACCESS_TOKEN 已更新" -ForegroundColor Green
} else {
Write-Host "[skip] 未提供 -Token,PROXY_ACCESS_TOKEN 保持原值" -ForegroundColor Yellow
}
[System.IO.File]::WriteAllText($bgPath, $bg, $utf8NoBom)
# ---------- 2. manifest.json ----------
$mf = [System.IO.File]::ReadAllText($mfPath, [System.Text.Encoding]::UTF8)
# 只替换 host_permissions 数组里的第一个 https://.../* 条目,保留原有缩进与格式
$patternHost = '("host_permissions"\s*:\s*\[\s*")https://[^"]+(")'
if ($mf -notmatch $patternHost) { throw 'manifest.json 中未找到 host_permissions 的 https 条目' }
$mf = [regex]::Replace($mf, $patternHost, ('${1}' + $TunnelUrl + '/*${2}'))
[System.IO.File]::WriteAllText($mfPath, $mf, $utf8NoBom)
Write-Host "[ok] manifest.json host_permissions -> $TunnelUrl/*" -ForegroundColor Green
# 回读校验 JSON 合法性
try {
[System.IO.File]::ReadAllText($mfPath, [System.Text.Encoding]::UTF8) | ConvertFrom-Json | Out-Null
Write-Host "[ok] manifest.json JSON 校验通过" -ForegroundColor Green
} catch {
throw "manifest.json 回填后 JSON 校验失败:$_"
}
# ---------- 3. 收尾提示 ----------
Write-Host ""
Write-Host "全部回填完成。下一步:" -ForegroundColor Cyan
Write-Host " 1. 打开 edge://extensions,找到「中英互译助手 EN2ZH」,点击「重新加载」"
Write-Host " 2. 验证:curl.exe -H `"x-access-token: <令牌>`" -H `"Content-Type: application/json`" -d '{\`"text\`":\`"hello\`"}' $TunnelUrl/api/translate"