Files
SimpleRemoter/android/build_openssl_android.sh
T
yuanyuanxiangandQoder 51747dc0ea Feature: Add on-demand TLS transport with hot-switch to the Android client
Reuse the Linux OpenSSL TlsChannel implementation (platform guard widened
to __linux__); the trust anchor lives in the app-private filesDir, injected
via TlsSetAnchorDir in nativeInit before the connection thread starts.
COMMAND_TLS_ENABLE/DISABLE save/remove the anchor, hot-reload and reconnect
only when the transport actually changes, so the master menu takes effect
within seconds without restarting the app. Login reports RES_TLS, refreshed
before each re-login. The reconnect flag is consumed before the drop check
and cleared before reconnecting so a switch racing a disconnect cannot
cause an extra reconnect/login cycle.

Cross-compile OpenSSL 3.5.0 per ABI with build_openssl_android.sh (NDK
clang + MSYS2 perl/make), integrated into build_android_libs.ps1
(-OpenSslOnly). Static libs and per-ABI headers are tracked like the
existing libzstd/libsign prebuilts; --exclude-libs keeps OpenSSL symbols
private to libyama.so, isolated from the Java-layer Conscrypt.

Co-Authored-By: Qoder <noreply@qoder.com>
2026-11-11 11:56:32 +01:00

185 lines
7.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# build_openssl_android.sh - 为 Android 交叉编译 OpenSSL 静态库(libssl.a / libcrypto.a)
#
# 用途:Android 客户端单向 TLS(client/TlsChannelLinux.cpp)需要 OpenSSL。Android 的
# libsign.a 用纯 C 便携 SHA256,不含 OpenSSL,故这里新引入 libssl + libcrypto。
# 与 Linux 客户端共用同一套 TLS 语义(TLS1.2 钉扎信任锚、fail-closed、热重载)。
#
# 产出(按 ABI,opensslconf.h 各架构不同,头文件也按 ABI 存放):
# app/src/main/cpp/lib/<ABI>/libssl.a
# app/src/main/cpp/lib/<ABI>/libcrypto.a
# app/src/main/cpp/lib/<ABI>/include/openssl/*.h
#
# 用法:
# ./build_openssl_android.sh # 全部 4 个 ABI
# ./build_openssl_android.sh arm64-v8a # 只构建指定 ABI
# ANDROID_NDK=/path/to/ndk ./build_openssl_android.sh
# FORCE=1 ./build_openssl_android.sh # 已存在也重建
#
# 依赖:
# - Android NDK(r22+,含 clang / llvm-ar)
# - make(Windows 上用 MSYS2 的 make:pacman -S make;NDK 原生 make 不认 MSYS 路径)
# - 完整的 perl(OpenSSL Configure 需要 IPC::Cmd / Locale::Maketext::Simple 等核心模块)。
# Git for Windows 自带的精简 perl 缺这些模块,会失败;Windows 上请用 MSYS2
# (bash + perl + make,见上),或在 Linux/macOS 上运行。
# - 下载 OpenSSL 源码(curl/wget + tar)。
#
# 说明:OpenSSL 的 Android 配置要求设置 ANDROID_NDK_ROOT 并把 NDK 的 llvm bin 加进 PATH,
# 它会自动选用 <triple><api>-clang 与 llvm-ar,不要传 --cross-compile-prefix。
# 见 openssl-<ver>/NOTES-ANDROID.md。
set -euo pipefail
cd "$(dirname "$0")"
# Windows 上 PATH 里可能先命中 Git for Windows 的 usr/bin(bash/env/perl)。
# 不同 MSYS 运行时之间传子进程会丢环境变量(Configure 拿不到 ANDROID_NDK_ROOT),
# 故把本运行时自己的 /usr/bin 置顶,保证 perl/env 等都用 MSYS2 自身的。
case "$(uname -s 2>/dev/null || echo Windows)" in
MINGW*|MSYS*|CYGWIN*) export PATH="/usr/bin:$PATH" ;;
esac
OPENSSL_VERSION="${OPENSSL_VERSION:-3.5.0}"
OPENSSL_TAR="openssl-${OPENSSL_VERSION}.tar.gz"
OPENSSL_URL="https://github.com/openssl/openssl/releases/download/openssl-${OPENSSL_VERSION}/${OPENSSL_TAR}"
OPENSSL_DIR="openssl-${OPENSSL_VERSION}"
API_LEVEL="${API_LEVEL:-21}"
ALL_ABIS=(arm64-v8a armeabi-v7a x86 x86_64)
if [[ $# -gt 0 ]]; then
ABIS=("$@")
else
ABIS=("${ALL_ABIS[@]}")
fi
LIB_BASE="app/src/main/cpp/lib"
# --- 定位 NDK ---
if [[ -z "${ANDROID_NDK:-}" ]]; then
ANDROID_NDK="${ANDROID_NDK_ROOT:-}"
fi
if [[ -z "${ANDROID_NDK:-}" ]]; then
sdk="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
if [[ -z "$sdk" ]]; then
case "$(uname -s 2>/dev/null || echo Windows)" in
Darwin*) sdk="$HOME/Library/Android/sdk" ;;
Linux*) sdk="$HOME/Android/Sdk" ;;
*) sdk="${LOCALAPPDATA:-}/Android/Sdk" ;;
esac
fi
if [[ -d "$sdk/ndk" ]]; then
ANDROID_NDK="$(ls -d "$sdk/ndk/"* 2>/dev/null | sort -V | tail -1)"
fi
fi
[[ -z "${ANDROID_NDK:-}" ]] && { echo "Error: ANDROID_NDK not set and not found"; exit 1; }
[[ -f "$ANDROID_NDK/source.properties" ]] || { echo "Error: invalid NDK: $ANDROID_NDK"; exit 1; }
echo "NDK: $ANDROID_NDK"
# --- 宿主平台 tag ---
case "$(uname -s 2>/dev/null || echo Windows)" in
Linux*) HOST_TAG="linux-x86_64" ;;
Darwin*) HOST_TAG="darwin-x86_64" ;;
*) HOST_TAG="windows-x86_64" ;;
esac
LLVM_BIN="$ANDROID_NDK/toolchains/llvm/prebuilt/$HOST_TAG/bin"
PREBUILT_BIN="$ANDROID_NDK/prebuilt/$HOST_TAG/bin"
[[ -d "$LLVM_BIN" ]] || { echo "Error: NDK llvm bin not found: $LLVM_BIN"; exit 1; }
# --- perl 完整性检查(OpenSSL Configure 依赖 IPC::Cmd)---
PERL="${PERL:-perl}"
if ! "$PERL" -MIPC::Cmd -e1 >/dev/null 2>&1; then
echo "Error: '$PERL' 缺少 OpenSSL Configure 所需的核心模块(IPC::Cmd 等)。"
echo " Git for Windows 自带精简 perl 不可用;Windows 上请在 MSYS2 里运行本脚本,例如:"
echo " /c/msys64/usr/bin/bash.exe -c 'cd /c/github/YAMA/android && ./build_openssl_android.sh'"
exit 1
fi
echo "perl: $("$PERL" -e 'print $^V')"
# --- make ---
# Windows 上必须用 MSYS2 的 make:Configure 生成的 Makefile 里是 /c/... 路径,
# NDK 自带的原生 make.exe 不认识(报 No rule to make target '/c/...')。
# MSYS2 make 调原生 clang 时会自动转换路径参数。系统无 make 时才退回 NDK prebuilt。
if ! command -v make >/dev/null 2>&1; then
if [[ -x "$PREBUILT_BIN/make" || -x "$PREBUILT_BIN/make.exe" ]]; then
export PATH="$PREBUILT_BIN:$PATH"
fi
fi
command -v make >/dev/null 2>&1 || { echo "Error: make not found (MSYS2: pacman -S make)"; exit 1; }
export ANDROID_NDK_ROOT="$ANDROID_NDK"
export PATH="$LLVM_BIN:$PATH"
# ABI → OpenSSL target
target_for() {
case "$1" in
arm64-v8a) echo "android-arm64" ;;
armeabi-v7a) echo "android-arm" ;;
x86) echo "android-x86" ;;
x86_64) echo "android-x86_64" ;;
*) echo "Error: unsupported ABI $1" >&2; exit 1 ;;
esac
}
# --- 下载并解压源码 ---
if [[ ! -d "$OPENSSL_DIR" ]]; then
if [[ ! -f "$OPENSSL_TAR" ]]; then
echo "Downloading OpenSSL $OPENSSL_VERSION ..."
if command -v curl >/dev/null 2>&1; then
curl -fSL -o "$OPENSSL_TAR" "$OPENSSL_URL"
else
wget -O "$OPENSSL_TAR" "$OPENSSL_URL"
fi
fi
echo "Extracting ..."
tar -xf "$OPENSSL_TAR"
fi
[[ -f "$OPENSSL_DIR/Configure" ]] || { echo "Error: $OPENSSL_DIR/Configure missing"; exit 1; }
SRC="$PWD/$OPENSSL_DIR"
# 裁剪掉用不到的部分以减小体积/加快构建;保留 TLS1.2/1.3(客户端钉扎 TLS1.2)。
CONFIG_OPTS=(
"-D__ANDROID_API__=$API_LEVEL"
-fPIC
--libdir=lib
no-shared no-apps no-docs no-tests
no-engine no-dso no-legacy no-comp no-weak-ssl-ciphers
)
JOBS="${JOBS:-$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 4)}"
for ABI in "${ABIS[@]}"; do
out_ssl="$LIB_BASE/$ABI/libssl.a"
out_crypto="$LIB_BASE/$ABI/libcrypto.a"
if [[ -f "$out_ssl" && -f "$out_crypto" && "${FORCE:-0}" != "1" ]]; then
echo "[skip] openssl $ABI -- already exists (FORCE=1 to rebuild)"
continue
fi
target="$(target_for "$ABI")"
build="openssl_build/$ABI"
install="$PWD/openssl_install/$ABI"
echo "===== openssl $ABI ($target) ====="
# Windows 上空目录可能被别的进程当作 CWD 占用而删不掉;空则直接复用,非空才报错。
if ! rm -rf "$build" "$install" 2>/dev/null; then
if [[ -d "$build" && -n "$(ls -A "$build" 2>/dev/null)" ]]; then
echo "Error: $build 被占用且非空,请关闭占用它的进程后重试" >&2
exit 1
fi
fi
mkdir -p "$build" "$LIB_BASE/$ABI"
(
cd "$build"
"$PERL" "$SRC/Configure" "$target" \
"--prefix=$install" "--openssldir=$install/ssl" "${CONFIG_OPTS[@]}"
make -j"$JOBS" build_sw
make install_dev
)
cp -f "$install/lib/libssl.a" "$out_ssl"
cp -f "$install/lib/libcrypto.a" "$out_crypto"
rm -rf "$LIB_BASE/$ABI/include"
cp -r "$install/include" "$LIB_BASE/$ABI/include"
echo " --> $out_ssl ($(du -k "$out_ssl" | cut -f1) KB)"
echo " --> $out_crypto ($(du -k "$out_crypto" | cut -f1) KB)"
done
echo "===== Done ====="