feat: 交互菜单 + 查看/修改端口/重置密钥/启停服务

This commit is contained in:
mango
2026-02-14 20:58:07 +08:00
parent ecfa84c754
commit e44d172e24

View File

@@ -302,15 +302,135 @@ uninstall() {
exit 0
}
# ============ 主流程 ============
main() {
case "${1:-}" in
uninstall|remove)
check_root
uninstall
;;
*)
check_root
# ============ 查看配置 ============
show_config() {
if [[ ! -f /etc/shadowsocks-rust/config.json ]]; then
error "未安装 shadowsocks-rust"
fi
echo ""
echo -e "${CYAN}════════════════════════════════════════${NC}"
echo -e "${CYAN} 📋 当前配置${NC}"
echo -e "${CYAN}════════════════════════════════════════${NC}"
echo ""
cat /etc/shadowsocks-rust/config.json
echo ""
echo -e "${CYAN}────────────────────────────────────────${NC}"
echo -e "${GREEN}服务状态:${NC}"
systemctl status ss-rust --no-pager 2>/dev/null | head -5
echo ""
if [[ -f /etc/shadowsocks-rust/subscribe/info.txt ]]; then
cat /etc/shadowsocks-rust/subscribe/info.txt
fi
}
# ============ 修改端口 ============
change_port() {
if [[ ! -f /etc/shadowsocks-rust/config.json ]]; then
error "未安装 shadowsocks-rust"
fi
echo ""
echo -e "${GREEN}当前配置:${NC}"
python3 -c "
import json
with open('/etc/shadowsocks-rust/config.json') as f:
c = json.load(f)
for i,s in enumerate(c['servers']):
print(f\" 节点{i+1}: {s['method']} | 端口 {s['server_port']}\")
" 2>/dev/null || cat /etc/shadowsocks-rust/config.json
echo ""
read -p "输入节点编号 (1/2): " node_num
read -p "输入新端口: " new_port
python3 -c "
import json,sys
with open('/etc/shadowsocks-rust/config.json') as f:
c = json.load(f)
idx = int('${node_num}') - 1
if 0 <= idx < len(c['servers']):
c['servers'][idx]['server_port'] = int('${new_port}')
with open('/etc/shadowsocks-rust/config.json','w') as f:
json.dump(c, f, indent=4)
print('端口已修改')
else:
print('无效节点编号')
sys.exit(1)
" 2>/dev/null
systemctl restart ss-rust
info "服务已重启,新端口: ${new_port}"
# 重新生成订阅
SERVER_IP=$(get_ip)
source_config
gen_subscribe
info "订阅配置已更新"
}
# ============ 读取现有配置 ============
source_config() {
if [[ -f /etc/shadowsocks-rust/config.json ]]; then
eval $(python3 -c "
import json
with open('/etc/shadowsocks-rust/config.json') as f:
c = json.load(f)
s = c['servers']
print(f'PORT_2022={s[0][\"server_port\"]}')
print(f'KEY_2022={s[0][\"password\"]}')
print(f'METHOD_2022={s[0][\"method\"]}')
print(f'PORT_RAW={s[1][\"server_port\"]}')
print(f'KEY_RAW={s[1][\"password\"]}')
print(f'METHOD_RAW={s[1][\"method\"]}')
" 2>/dev/null)
fi
}
# ============ 重置密钥 ============
reset_keys() {
if [[ ! -f /etc/shadowsocks-rust/config.json ]]; then
error "未安装 shadowsocks-rust"
fi
SERVER_IP=$(get_ip)
KEY_2022=$(gen_key_128)
KEY_RAW=$(openssl rand -base64 16)
python3 -c "
import json
with open('/etc/shadowsocks-rust/config.json') as f:
c = json.load(f)
c['servers'][0]['password'] = '${KEY_2022}'
c['servers'][1]['password'] = '${KEY_RAW}'
with open('/etc/shadowsocks-rust/config.json','w') as f:
json.dump(c, f, indent=4)
" 2>/dev/null
systemctl restart ss-rust
source_config
gen_subscribe
info "密钥已重置,服务已重启"
show_result
}
# ============ 交互菜单 ============
show_menu() {
echo ""
echo -e "${CYAN}════════════════════════════════════════${NC}"
echo -e "${CYAN} 🚀 SS-Rust 管理面板${NC}"
echo -e "${CYAN}════════════════════════════════════════${NC}"
echo ""
echo -e " ${GREEN}1.${NC} 安装 SS-Rust"
echo -e " ${GREEN}2.${NC} 查看配置"
echo -e " ${GREEN}3.${NC} 修改端口"
echo -e " ${GREEN}4.${NC} 重置密钥"
echo -e " ${GREEN}5.${NC} 启动服务"
echo -e " ${GREEN}6.${NC} 停止服务"
echo -e " ${GREEN}7.${NC} 重启服务"
echo -e " ${GREEN}8.${NC} 查看日志"
echo -e " ${RED}9.${NC} 卸载"
echo -e " ${YELLOW}0.${NC} 退出"
echo ""
read -p "请选择 [0-9]: " choice
case "$choice" in
1)
check_os
sync_time
install_ssrust
@@ -319,6 +439,55 @@ main() {
gen_subscribe
show_result
;;
2) show_config ;;
3) change_port ;;
4) reset_keys ;;
5) systemctl start ss-rust && info "服务已启动" ;;
6) systemctl stop ss-rust && info "服务已停止" ;;
7) systemctl restart ss-rust && info "服务已重启" ;;
8) journalctl -u ss-rust --no-pager -n 30 ;;
9) uninstall ;;
0) exit 0 ;;
*) warn "无效选择" ;;
esac
}
# ============ 主流程 ============
main() {
check_root
case "${1:-}" in
install)
check_os; sync_time; install_ssrust; gen_config; setup_service; gen_subscribe; show_result
;;
uninstall|remove)
uninstall
;;
show|config|info)
show_config
;;
restart)
systemctl restart ss-rust && info "服务已重启"
;;
start)
systemctl start ss-rust && info "服务已启动"
;;
stop)
systemctl stop ss-rust && info "服务已停止"
;;
log|logs)
journalctl -u ss-rust --no-pager -n 30
;;
reset)
reset_keys
;;
*)
# 无参数时:已安装显示菜单,未安装直接安装
if [[ -f /etc/shadowsocks-rust/config.json ]]; then
show_menu
else
check_os; sync_time; install_ssrust; gen_config; setup_service; gen_subscribe; show_result
fi
;;
esac
}