feat: 提交
这个提交包含在:
@@ -64,15 +64,23 @@ bash <(curl -fsSL https://git.suhang.me/suhang/scripts/raw/branch/release/realm/
|
|||||||
|
|
||||||
## GitHub 代理
|
## GitHub 代理
|
||||||
|
|
||||||
安装时会提示输入 GitHub 代理域名(默认 `ghproxy.com`,输入 `none` 表示直连)。脚本会把代理拼接到 GitHub API 与 release 下载地址前,例如:
|
安装时会提示输入 GitHub 代理域名(**默认直连,不使用代理**;如需走代理推荐填 `ghfast.top`,输入 `none` 也表示直连)。脚本会把代理拼接到 GitHub API 与 release 下载地址前,例如:
|
||||||
|
|
||||||
```
|
```
|
||||||
https://ghproxy.com/https://github.com/zhboner/realm/releases/download/...
|
https://ghfast.top/https://github.com/zhboner/realm/releases/download/...
|
||||||
https://ghproxy.com/https://api.github.com/repos/zhboner/realm/releases/latest
|
https://ghfast.top/https://api.github.com/repos/zhboner/realm/releases/latest
|
||||||
```
|
```
|
||||||
|
|
||||||
选择会保存到 `<INSTALL_DIR>/.proxy`,更新内核(菜单 `10`)时会沿用。如需更换代理,可直接编辑该文件后再执行更新。
|
选择会保存到 `<INSTALL_DIR>/.proxy`,更新内核(菜单 `10`)时会沿用。如需更换代理,可直接编辑该文件后再执行更新。
|
||||||
|
|
||||||
|
## 调试
|
||||||
|
|
||||||
|
启动脚本时加 `--debug`(或 `-d`)可打印更多日志,包括架构识别、代理拼接结果、curl 详细输出、写入配置等:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bash <(curl -fsSL https://git.suhang.me/suhang/scripts/raw/branch/release/realm/realm.sh) --debug
|
||||||
|
```
|
||||||
|
|
||||||
## 配置示例
|
## 配置示例
|
||||||
|
|
||||||
脚本生成的 `config.toml` 大致如下,每条规则对应一个 `[[endpoints]]` 块:
|
脚本生成的 `config.toml` 大致如下,每条规则对应一个 `[[endpoints]]` 块:
|
||||||
|
|||||||
@@ -14,8 +14,14 @@ REALM_LOG=""
|
|||||||
REALM_SERVICE="/etc/systemd/system/realm.service"
|
REALM_SERVICE="/etc/systemd/system/realm.service"
|
||||||
SCRIPT_RAW_URL="https://git.suhang.me/suhang/scripts/raw/branch/release/realm/realm.sh"
|
SCRIPT_RAW_URL="https://git.suhang.me/suhang/scripts/raw/branch/release/realm/realm.sh"
|
||||||
GITHUB_API="https://api.github.com/repos/zhboner/realm/releases/latest"
|
GITHUB_API="https://api.github.com/repos/zhboner/realm/releases/latest"
|
||||||
DEFAULT_PROXY="ghproxy.com"
|
DEFAULT_PROXY=""
|
||||||
GITHUB_PROXY=""
|
GITHUB_PROXY=""
|
||||||
|
DEBUG=false
|
||||||
|
|
||||||
|
function dbg() {
|
||||||
|
[[ "$DEBUG" == "true" ]] || return 0
|
||||||
|
printf "${YELLOW}[DEBUG %s]${NC} %s\n" "$(date '+%H:%M:%S')" "$*" >&2
|
||||||
|
}
|
||||||
|
|
||||||
function set_paths() {
|
function set_paths() {
|
||||||
INSTALL_DIR="$1"
|
INSTALL_DIR="$1"
|
||||||
@@ -31,29 +37,39 @@ function detect_install_dir() {
|
|||||||
fi
|
fi
|
||||||
[[ -z "$dir" ]] && dir="$DEFAULT_INSTALL_DIR"
|
[[ -z "$dir" ]] && dir="$DEFAULT_INSTALL_DIR"
|
||||||
set_paths "$dir"
|
set_paths "$dir"
|
||||||
|
dbg "detect_install_dir: INSTALL_DIR=${INSTALL_DIR} (service=${REALM_SERVICE})"
|
||||||
load_proxy
|
load_proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
function gh_url() {
|
function gh_url() {
|
||||||
local url="$1"
|
local url="$1"
|
||||||
|
local out
|
||||||
if [[ -n "$GITHUB_PROXY" ]]; then
|
if [[ -n "$GITHUB_PROXY" ]]; then
|
||||||
echo "https://${GITHUB_PROXY}/${url}"
|
out="https://${GITHUB_PROXY}/${url}"
|
||||||
else
|
else
|
||||||
echo "$url"
|
out="$url"
|
||||||
fi
|
fi
|
||||||
|
dbg "gh_url: ${url} -> ${out}"
|
||||||
|
echo "$out"
|
||||||
}
|
}
|
||||||
|
|
||||||
function load_proxy() {
|
function load_proxy() {
|
||||||
if [[ -n "$INSTALL_DIR" && -f "${INSTALL_DIR}/.proxy" ]]; then
|
if [[ -n "$INSTALL_DIR" && -f "${INSTALL_DIR}/.proxy" ]]; then
|
||||||
GITHUB_PROXY=$(head -n1 "${INSTALL_DIR}/.proxy" 2>/dev/null | tr -d '[:space:]')
|
GITHUB_PROXY=$(head -n1 "${INSTALL_DIR}/.proxy" 2>/dev/null | tr -d '[:space:]')
|
||||||
|
dbg "load_proxy: ${GITHUB_PROXY:-<none>} (from ${INSTALL_DIR}/.proxy)"
|
||||||
|
else
|
||||||
|
dbg "load_proxy: no proxy file at ${INSTALL_DIR}/.proxy"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function prompt_proxy() {
|
function prompt_proxy() {
|
||||||
local p
|
local p
|
||||||
read -p "请输入 GitHub 代理域名 [默认 ${DEFAULT_PROXY},输入 none 不使用代理]: " p
|
local hint="默认不使用代理"
|
||||||
|
[[ -n "$DEFAULT_PROXY" ]] && hint="默认 ${DEFAULT_PROXY}"
|
||||||
|
echo -e "${CYAN}提示${NC}: 当前可用的 GitHub 代理推荐 ${YELLOW}ghfast.top${NC}"
|
||||||
|
read -p "请输入 GitHub 代理域名(${hint},输入 none 强制不使用代理): " p
|
||||||
p="${p:-$DEFAULT_PROXY}"
|
p="${p:-$DEFAULT_PROXY}"
|
||||||
if [[ "$p" == "none" || "$p" == "NONE" ]]; then
|
if [[ -z "$p" || "$p" == "none" || "$p" == "NONE" ]]; then
|
||||||
GITHUB_PROXY=""
|
GITHUB_PROXY=""
|
||||||
echo "GitHub proxy: disabled"
|
echo "GitHub proxy: disabled"
|
||||||
else
|
else
|
||||||
@@ -103,27 +119,36 @@ function check_dependencies() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function detect_arch() {
|
function detect_arch() {
|
||||||
local m
|
local m a
|
||||||
m=$(uname -m)
|
m=$(uname -m)
|
||||||
case "$m" in
|
case "$m" in
|
||||||
x86_64|amd64) echo "x86_64-unknown-linux-gnu" ;;
|
x86_64|amd64) a="x86_64-unknown-linux-gnu" ;;
|
||||||
aarch64|arm64) echo "aarch64-unknown-linux-gnu" ;;
|
aarch64|arm64) a="aarch64-unknown-linux-gnu" ;;
|
||||||
armv7l|armv7) echo "armv7-unknown-linux-gnueabihf" ;;
|
armv7l|armv7) a="armv7-unknown-linux-gnueabihf" ;;
|
||||||
*)
|
*)
|
||||||
echo -e "${RED}Unsupported architecture: $m${NC}" >&2
|
echo -e "${RED}Unsupported architecture: $m${NC}" >&2
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
dbg "detect_arch: uname=${m} -> ${a}"
|
||||||
|
echo "$a"
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_latest_version() {
|
function get_latest_version() {
|
||||||
local api_url v
|
local api_url v
|
||||||
api_url=$(gh_url "$GITHUB_API")
|
api_url=$(gh_url "$GITHUB_API")
|
||||||
|
dbg "get_latest_version: GET ${api_url}"
|
||||||
|
local stderr_redirect="2>/dev/null"
|
||||||
|
if [[ "$DEBUG" == "true" ]]; then
|
||||||
|
v=$(curl -fSL "$api_url" | grep -oE '"tag_name":\s*"[^"]+"' | head -n1 | sed -E 's/.*"([^"]+)"$/\1/')
|
||||||
|
else
|
||||||
v=$(curl -fsSL "$api_url" 2>/dev/null | grep -oE '"tag_name":\s*"[^"]+"' | head -n1 | sed -E 's/.*"([^"]+)"$/\1/')
|
v=$(curl -fsSL "$api_url" 2>/dev/null | grep -oE '"tag_name":\s*"[^"]+"' | head -n1 | sed -E 's/.*"([^"]+)"$/\1/')
|
||||||
|
fi
|
||||||
if [[ -z "$v" ]]; then
|
if [[ -z "$v" ]]; then
|
||||||
echo -e "${RED}Failed to fetch latest realm version from GitHub (proxy: ${GITHUB_PROXY:-none}).${NC}" >&2
|
echo -e "${RED}Failed to fetch latest realm version from GitHub (proxy: ${GITHUB_PROXY:-none}).${NC}" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
dbg "get_latest_version: ${v}"
|
||||||
echo "$v"
|
echo "$v"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +163,10 @@ function download_realm() {
|
|||||||
tmp=$(mktemp -d)
|
tmp=$(mktemp -d)
|
||||||
|
|
||||||
echo "Downloading realm ${version} (${arch}) via ${GITHUB_PROXY:-direct}..."
|
echo "Downloading realm ${version} (${arch}) via ${GITHUB_PROXY:-direct}..."
|
||||||
if ! curl -fsSL "$url" -o "${tmp}/realm.tar.gz"; then
|
dbg "download_realm: url=${url} tmp=${tmp}"
|
||||||
|
local curl_flags=(-fSL)
|
||||||
|
[[ "$DEBUG" == "true" ]] && curl_flags=(-fSL -v)
|
||||||
|
if ! curl "${curl_flags[@]}" "$url" -o "${tmp}/realm.tar.gz"; then
|
||||||
echo -e "${RED}Download failed: $url${NC}"
|
echo -e "${RED}Download failed: $url${NC}"
|
||||||
rm -rf "$tmp"
|
rm -rf "$tmp"
|
||||||
return 1
|
return 1
|
||||||
@@ -356,6 +384,7 @@ listen = "${listen}"
|
|||||||
remote = "${remote}"
|
remote = "${remote}"
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
dbg "add_rule: appended endpoint listen=${listen} remote=${remote} to ${REALM_CONF}"
|
||||||
|
|
||||||
open_firewall_port "$listen_port"
|
open_firewall_port "$listen_port"
|
||||||
systemctl restart realm
|
systemctl restart realm
|
||||||
@@ -520,6 +549,28 @@ function main_menu() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parse_args() {
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
--debug|-d)
|
||||||
|
DEBUG=true
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
echo "Usage: $0 [--debug]"
|
||||||
|
echo " --debug, -d 打印更多日志"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo -e "${RED}未知参数: $1${NC}" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
[[ "$DEBUG" == "true" ]] && dbg "DEBUG mode enabled"
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_args "$@"
|
||||||
check_root
|
check_root
|
||||||
detect_install_dir
|
detect_install_dir
|
||||||
main_menu
|
main_menu
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户