mirror of
https://gitee.com/xmz7891/ykxmz.git
synced 2026-10-18 12:30:44 +00:00
2.1.1 修掉了 nettune 死锁之后,公网上到 200,但**过一阵又 530**。
这次抓到的是隧道自己:
cloudflared 的 precheck 输出(tunnel.log):
| UDP Connectivity region1.v2.argotunnel.com FAIL QUIC connection failed |
| TCP Connectivity region1.v2.argotunnel.com FAIL HTTP/2 connection is blocked or unreachable |
| Cloudflare API api.cloudflare.com:443 PASS API is reachable |
| ERROR: Allow outbound QUIC traffic on port 7844 or use HTTP2. |
也就是 **443 通、7844 不通**,而且是**间歇性**的(过几分钟自己又好了,
然后再次断)。Cloudflare 隧道的边缘连接走 7844(QUIC 走 UDP 7844、
HTTP/2 走 TCP 7844)。这条线路对它是间歇性阻断 —— 属于运营商那一侧,
设备改不了,但可以让 cloudflared 自己去适应。
== 两个改动 ==
1. **协议从写死改成 auto**
lanremote-tunnel --protocol quic -> --protocol auto
lanremote-tunnel-h2 --protocol http2 -> --protocol auto
原来两条实例各赌一个协议:那一刻 7844 的 UDP 和 TCP 都不通就一起死,
没有退路(实测连接数掉到 1/0、公网 530)。改成 auto 之后
cloudflared 自己在 QUIC / HTTP/2 之间挑当前能通的那条,
10 条连接全都有自适应能力。
2. **--retries 5 -> 1000000**
原来失败 5 次就退出、靠 systemd Restart=always 拉起来,
每次退出到重启之间网站都是断的。而这条线路的抖动是分钟级的 ——
实测隧道每 3~10 分钟就被重启一次(19:37 / 19:40 / 19:43 / 19:50 / 19:52),
公网跟着一起断。拉大之后抖动期间连接自己恢复,整条隧道不退出。
3. 顺手加日志轮转(tunnel.log 已经 2.98 MB,这台机器硬盘不大)。
== 实测 ==
改前:连接数 1/0,公网 530
改后:连接数 7+2(接近满配 10),公网连测 5 次全部 200
== 同步 ==
relay/deploy.sh 里的 MAIN_PROTO / ALT_PROTO 也改成 auto、
TUNNEL_BASE 的 --retries 也拉到 1000000,新机器装出来就是加固过的。
全量回归 46 个阶段全部通过。
134 lines
5.2 KiB
PowerShell
134 lines
5.2 KiB
PowerShell
# 用 aapt2 + javac + d8 + zipalign + apksigner 手工构建 APK(不需要 Gradle)
|
||
# 用法: powershell -File android\build_apk.ps1
|
||
#
|
||
# 注意:刻意不用 $ErrorActionPreference="Stop"。
|
||
# 原生工具的告警(javac 的 deprecation note 等)会写 stderr,
|
||
# 在 Stop 模式下会被 PowerShell 当成致命错误中断构建。
|
||
# 改为每一步显式检查 $LASTEXITCODE。
|
||
$ErrorActionPreference = "Continue"
|
||
|
||
$android = $PSScriptRoot # LanRemote/android(本脚本所在目录)
|
||
$root = Split-Path -Parent $android # LanRemote
|
||
$tc = Join-Path $root "..\starklink\toolchain"
|
||
$jdk = Join-Path $tc "jdk17"
|
||
$sdk = Join-Path $tc "android-sdk"
|
||
$bt = Join-Path $sdk "build-tools\34.0.0"
|
||
$androidJar = Join-Path $sdk "platforms\android-34\android.jar"
|
||
$keystore = Join-Path $tc "debug.keystore"
|
||
|
||
$javac = Join-Path $jdk "bin\javac.exe"
|
||
$keytool = Join-Path $jdk "bin\keytool.exe"
|
||
$jar = Join-Path $jdk "bin\jar.exe"
|
||
|
||
$aapt2 = Join-Path $bt "aapt2.exe"
|
||
$zipalign = Join-Path $bt "zipalign.exe"
|
||
$apksigner= Join-Path $bt "apksigner.bat"
|
||
$d8 = Join-Path $bt "d8.bat"
|
||
|
||
$work = Join-Path $android "build"
|
||
$outDir = Join-Path $root "dist"
|
||
$apkName = "ykxmz-watch-2.1.2.apk"
|
||
|
||
function Step($text) { Write-Host "`n=== $text ===" -ForegroundColor Cyan }
|
||
|
||
# 前置检查
|
||
foreach ($tool in @($javac, $aapt2, $d8, $zipalign, $apksigner)) {
|
||
if (-not (Test-Path $tool)) { throw "缺少工具: $tool" }
|
||
}
|
||
if (-not (Test-Path $androidJar)) { throw "缺少 android.jar: $androidJar" }
|
||
if (-not (Test-Path $keystore)) { throw "缺少签名用 keystore: $keystore" }
|
||
|
||
$env:JAVA_HOME = $jdk
|
||
$env:PATH = "$(Join-Path $jdk 'bin');$env:PATH"
|
||
|
||
if (Test-Path $work) { Remove-Item -Recurse -Force $work }
|
||
New-Item -ItemType Directory -Force -Path $work | Out-Null
|
||
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
|
||
|
||
# ---------------- 1. 编译资源 ----------------
|
||
Step "1/6 aapt2 compile 资源"
|
||
$compiled = Join-Path $work "res.zip"
|
||
& $aapt2 compile --dir (Join-Path $android "res") -o $compiled
|
||
if ($LASTEXITCODE -ne 0) { throw "aapt2 compile 失败" }
|
||
|
||
# ---------------- 2. 链接资源 + 生成 R.java ----------------
|
||
Step "2/6 aapt2 link 生成资源表与 R.java"
|
||
$genDir = Join-Path $work "gen"
|
||
New-Item -ItemType Directory -Force -Path $genDir | Out-Null
|
||
$unsigned = Join-Path $work "app-unsigned.apk"
|
||
& $aapt2 link `
|
||
-o $unsigned `
|
||
-I $androidJar `
|
||
--manifest (Join-Path $android "AndroidManifest.xml") `
|
||
-R $compiled `
|
||
--java $genDir `
|
||
--min-sdk-version 24 `
|
||
--target-sdk-version 34 `
|
||
--version-code 5 `
|
||
--version-name "2.1.2" `
|
||
--auto-add-overlay
|
||
if ($LASTEXITCODE -ne 0) { throw "aapt2 link 失败" }
|
||
|
||
# ---------------- 3. 编译 Java ----------------
|
||
Step "3/6 javac 编译 Java 源码"
|
||
$classesDir = Join-Path $work "classes"
|
||
New-Item -ItemType Directory -Force -Path $classesDir | Out-Null
|
||
|
||
$javaSources = @()
|
||
$javaSources += Get-ChildItem -Recurse -Filter *.java -Path (Join-Path $android "src") | ForEach-Object { $_.FullName }
|
||
$javaSources += Get-ChildItem -Recurse -Filter R.java -Path $genDir -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName }
|
||
if ($javaSources.Count -eq 0) { throw "没有找到 Java 源文件" }
|
||
Write-Host "源文件 $($javaSources.Count) 个"
|
||
|
||
& $javac -source 8 -target 8 -nowarn -Xlint:none -encoding UTF-8 `
|
||
-bootclasspath $androidJar `
|
||
-d $classesDir `
|
||
$javaSources 2>&1 | Out-String | Write-Host
|
||
if ($LASTEXITCODE -ne 0) { throw "javac 编译失败" }
|
||
|
||
# ---------------- 4. d8 转 dex ----------------
|
||
Step "4/6 d8 转换为 classes.dex"
|
||
$classesJar = Join-Path $work "classes.jar"
|
||
& $jar cf $classesJar -C $classesDir .
|
||
if ($LASTEXITCODE -ne 0) { throw "打包 classes.jar 失败" }
|
||
|
||
$dexDir = Join-Path $work "dex"
|
||
New-Item -ItemType Directory -Force -Path $dexDir | Out-Null
|
||
& $d8 --lib $androidJar --min-api 24 --output $dexDir $classesJar 2>&1 | Out-String | Write-Host
|
||
if ($LASTEXITCODE -ne 0) { throw "d8 失败" }
|
||
$dex = Join-Path $dexDir "classes.dex"
|
||
if (-not (Test-Path $dex)) { throw "未生成 classes.dex" }
|
||
Write-Host ("classes.dex 大小 {0:N1} KB" -f ((Get-Item $dex).Length / 1KB))
|
||
|
||
# ---------------- 5. 组装 APK ----------------
|
||
Step "5/6 组装 APK"
|
||
& python (Join-Path $root "_build\pack_apk.py") $unsigned $dex
|
||
if ($LASTEXITCODE -ne 0) { throw "写入 classes.dex 失败" }
|
||
|
||
$aligned = Join-Path $work "app-aligned.apk"
|
||
& $zipalign -f 4 $unsigned $aligned
|
||
if ($LASTEXITCODE -ne 0) { throw "zipalign 失败" }
|
||
|
||
# ---------------- 6. 签名 ----------------
|
||
Step "6/6 apksigner 签名"
|
||
$final = Join-Path $outDir $apkName
|
||
if (Test-Path $final) { Remove-Item $final -Force }
|
||
& $apksigner sign `
|
||
--ks $keystore `
|
||
--ks-pass pass:android `
|
||
--key-pass pass:android `
|
||
--ks-key-alias androiddebugkey `
|
||
--v1-signing-enabled true `
|
||
--v2-signing-enabled true `
|
||
--out $final `
|
||
$aligned
|
||
if ($LASTEXITCODE -ne 0) { throw "签名失败" }
|
||
|
||
Step "验证签名"
|
||
& $apksigner verify --print-certs $final
|
||
if ($LASTEXITCODE -ne 0) { throw "签名校验失败" }
|
||
|
||
Step "构建完成"
|
||
Write-Host ("APK: {0}" -f $final) -ForegroundColor Green
|
||
Write-Host ("大小: {0:N2} MB" -f ((Get-Item $final).Length / 1MB)) -ForegroundColor Green
|