feat: 增强时间同步 - 配置可靠NTP源+持久化chrony+管理菜单
This commit is contained in:
75
ss-rust.sh
75
ss-rust.sh
@@ -70,14 +70,76 @@ get_ip() {
|
|||||||
# ============ 时间同步 ============
|
# ============ 时间同步 ============
|
||||||
sync_time() {
|
sync_time() {
|
||||||
info "同步系统时间..."
|
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
|
if command -v timedatectl &>/dev/null; then
|
||||||
timedatectl set-ntp true 2>/dev/null || true
|
timedatectl set-ntp true 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
if command -v chronyd &>/dev/null; then
|
|
||||||
systemctl enable --now chronyd 2>/dev/null || true
|
# 4. 验证
|
||||||
chronyc makestep 2>/dev/null || true
|
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
|
fi
|
||||||
info "当前时间: $(date '+%Y-%m-%d %H:%M:%S %Z')"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# ============ 安装 ss-rust ============
|
# ============ 安装 ss-rust ============
|
||||||
@@ -569,10 +631,11 @@ show_menu() {
|
|||||||
echo -e " ${GREEN}7.${NC} 重启服务"
|
echo -e " ${GREEN}7.${NC} 重启服务"
|
||||||
echo -e " ${GREEN}8.${NC} 查看日志"
|
echo -e " ${GREEN}8.${NC} 查看日志"
|
||||||
echo -e " ${GREEN}10.${NC} ⚡ BBR 加速优化"
|
echo -e " ${GREEN}10.${NC} ⚡ BBR 加速优化"
|
||||||
|
echo -e " ${GREEN}11.${NC} 🕐 时间同步 (SS2022必需)"
|
||||||
echo -e " ${RED}9.${NC} 卸载"
|
echo -e " ${RED}9.${NC} 卸载"
|
||||||
echo -e " ${YELLOW}0.${NC} 退出"
|
echo -e " ${YELLOW}0.${NC} 退出"
|
||||||
echo ""
|
echo ""
|
||||||
read -rp "请选择 [0-10]: " choice
|
read -rp "请选择 [0-11]: " choice
|
||||||
|
|
||||||
case "$choice" in
|
case "$choice" in
|
||||||
1) do_install ;;
|
1) do_install ;;
|
||||||
@@ -585,6 +648,7 @@ show_menu() {
|
|||||||
8) journalctl -u ss-rust --no-pager -n 30 ;;
|
8) journalctl -u ss-rust --no-pager -n 30 ;;
|
||||||
9) uninstall ;;
|
9) uninstall ;;
|
||||||
10) setup_bbr ;;
|
10) setup_bbr ;;
|
||||||
|
11) get_pkg_manager; install_deps; sync_time ;;
|
||||||
0) exit 0 ;;
|
0) exit 0 ;;
|
||||||
*) warn "无效选择" ;;
|
*) warn "无效选择" ;;
|
||||||
esac
|
esac
|
||||||
@@ -603,6 +667,7 @@ main() {
|
|||||||
log|logs) journalctl -u ss-rust --no-pager -n 30 ;;
|
log|logs) journalctl -u ss-rust --no-pager -n 30 ;;
|
||||||
reset) reset_keys ;;
|
reset) reset_keys ;;
|
||||||
bbr) setup_bbr ;;
|
bbr) setup_bbr ;;
|
||||||
|
sync|time) get_pkg_manager; install_deps; sync_time ;;
|
||||||
*)
|
*)
|
||||||
if [[ -f /etc/shadowsocks-rust/config.json ]]; then
|
if [[ -f /etc/shadowsocks-rust/config.json ]]; then
|
||||||
show_menu
|
show_menu
|
||||||
|
|||||||
Reference in New Issue
Block a user