From 0250e5efe8d7dd19334cc32f1f6aa3eab55cf051 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 17 Apr 2026 12:21:47 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BA=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=20-=20=E9=85=8D=E7=BD=AE=E5=8F=AF=E9=9D=A0NT?= =?UTF-8?q?P=E6=BA=90+=E6=8C=81=E4=B9=85=E5=8C=96chrony+=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ss-rust.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/ss-rust.sh b/ss-rust.sh index b4f4ea5..e156cc3 100644 --- a/ss-rust.sh +++ b/ss-rust.sh @@ -70,14 +70,76 @@ get_ip() { # ============ 时间同步 ============ sync_time() { info "同步系统时间..." + + # 1. 配置 chrony NTP 源 (国际 + 阿里云) + if command -v chronyd &>/dev/null; then + local chrony_conf + if [[ -f /etc/chrony/chrony.conf ]]; then + chrony_conf="/etc/chrony/chrony.conf" + elif [[ -f /etc/chrony.conf ]]; then + chrony_conf="/etc/chrony.conf" + fi + + if [[ -n "${chrony_conf:-}" ]]; then + # 备份原配置 + cp "$chrony_conf" "${chrony_conf}.bak.$(date +%s)" 2>/dev/null || true + + # 检查是否已配置过 + if ! grep -q '# SS-Rust NTP' "$chrony_conf" 2>/dev/null; then + # 注释掉原有 server/pool 行 + sed -i 's/^\(server \|pool \)/#&/' "$chrony_conf" 2>/dev/null || true + # 追加可靠 NTP 源 + cat >> "$chrony_conf" <<'NTP' + +# SS-Rust NTP — 多源冗余 +server ntp.aliyun.com iburst +server ntp1.aliyun.com iburst +server time.cloudflare.com iburst +pool pool.ntp.org iburst maxsources 4 + +# 允许大幅时钟跳变 (SS2022 对时间敏感) +makestep 1 -1 +NTP + fi + fi + + # 确保 chrony 服务启动并持久化 + systemctl enable --now chronyd 2>/dev/null || \ + systemctl enable --now chrony 2>/dev/null || true + # 等一下让 chrony 连上源 + sleep 1 + chronyc makestep 2>/dev/null || true + + # 2. 没有 chrony 就用 ntpd + elif command -v ntpd &>/dev/null; then + systemctl enable --now ntpd 2>/dev/null || \ + systemctl enable --now ntp 2>/dev/null || true + ntpdate -u pool.ntp.org 2>/dev/null || true + fi + + # 3. timedatectl 兜底 if command -v timedatectl &>/dev/null; then timedatectl set-ntp true 2>/dev/null || true fi - if command -v chronyd &>/dev/null; then - systemctl enable --now chronyd 2>/dev/null || true - chronyc makestep 2>/dev/null || true + + # 4. 验证 + local offset="" + if command -v chronyc &>/dev/null; then + offset=$(chronyc tracking 2>/dev/null | awk '/System time/{print $4, $5}') + fi + info "当前时间: $(date '+%Y-%m-%d %H:%M:%S %Z')${offset:+ | 偏移: $offset}" + + # 5. 检查偏移是否过大 (>30s 告警) + if command -v chronyc &>/dev/null; then + local drift_s + drift_s=$(chronyc tracking 2>/dev/null | awk '/System time/{gsub(/[^0-9.]/,"",$4); printf "%d", $4}') + if [[ "${drift_s:-0}" -gt 30 ]]; then + warn "时钟偏移 ${drift_s}s 过大! SS2022 可能连接失败, 正在强制同步..." + chronyc makestep 2>/dev/null || true + sleep 2 + info "同步后时间: $(date '+%Y-%m-%d %H:%M:%S %Z')" + fi fi - info "当前时间: $(date '+%Y-%m-%d %H:%M:%S %Z')" } # ============ 安装 ss-rust ============ @@ -569,10 +631,11 @@ show_menu() { echo -e " ${GREEN}7.${NC} 重启服务" echo -e " ${GREEN}8.${NC} 查看日志" echo -e " ${GREEN}10.${NC} ⚡ BBR 加速优化" + echo -e " ${GREEN}11.${NC} 🕐 时间同步 (SS2022必需)" echo -e " ${RED}9.${NC} 卸载" echo -e " ${YELLOW}0.${NC} 退出" echo "" - read -rp "请选择 [0-10]: " choice + read -rp "请选择 [0-11]: " choice case "$choice" in 1) do_install ;; @@ -585,6 +648,7 @@ show_menu() { 8) journalctl -u ss-rust --no-pager -n 30 ;; 9) uninstall ;; 10) setup_bbr ;; + 11) get_pkg_manager; install_deps; sync_time ;; 0) exit 0 ;; *) warn "无效选择" ;; esac @@ -603,6 +667,7 @@ main() { log|logs) journalctl -u ss-rust --no-pager -n 30 ;; reset) reset_keys ;; bbr) setup_bbr ;; + sync|time) get_pkg_manager; install_deps; sync_time ;; *) if [[ -f /etc/shadowsocks-rust/config.json ]]; then show_menu