#!/bin/bash set -e function init_env() { echo "🔧 正在初始化环境..." sudo apt update sudo apt install -y python3-pip screen curl wget git unzip htop echo 'export PIP_BREAK_SYSTEM_PACKAGES=1' >> ~/.bashrc export PIP_BREAK_SYSTEM_PACKAGES=1 echo "✅ 初始化完成" } function install_j_ki() { echo "📦 正在安装 j / ki 会话管理函数..." if ! grep -q '# === k1-script-start ===' ~/.bashrc; then cat << 'EOF' >> ~/.bashrc # === k1-script-start === function j { if [ -n "$1" ]; then current_date=$(date +"%Y%m%d") session_name="${1}_${current_date}" screen -d -r "$session_name" 2>/dev/null || screen -S "$session_name" return fi sessions=$(screen -ls | grep -P "\t" | awk '{print $1}') if [ -z "$sessions" ]; then echo "当前没有会话" return fi names=() while IFS= read -r line; do names+=("${line#*.}") done < <(echo "$sessions" | sed "s/^[0-9]*\.//") echo "请选择要进入的会话:" echo "(输入数字进入会话,输入k+数字删除会话,如:k1)" select name in "${names[@]}"; do if [ -n "$REPLY" ]; then if [[ $REPLY == k* ]]; then num=${REPLY#k} if [[ $num =~ ^[0-9]+$ ]] && [ $num -le ${#names[@]} ]; then target=$(echo "$sessions" | sed -n "${num}p") screen -S "$target" -X quit echo "已删除会话:${names[$num-1]}" break fi elif [[ $REPLY =~ ^[0-9]+$ ]] && [ $REPLY -le ${#names[@]} ]; then target=$(echo "$sessions" | sed -n "${REPLY}p") screen -d -r "$target" break fi fi done } function ki { sessions=$(screen -ls | grep -P "\t" | awk '{print $1}') if [ -z "$sessions" ]; then echo "当前没有会话需要删除" return fi echo "正在删除所有screen会话..." for session in $sessions; do screen -S $session -X quit done echo "所有会话已删除" } # === k1-script-end === EOF source ~/.bashrc echo "✅ 函数已写入 ~/.bashrc 并立即生效" else echo "✅ 函数已存在,无需重复添加" fi } function reverse_proxy_setup() { cat << 'EOF' > /tmp/proxy_setup.py #!/usr/bin/env python3 import os, subprocess, sys NGINX_CONF_DIR = '/home/web/conf.d' NGINX_CONTAINER_NAME = 'nginx' def ensure_docker(): if not subprocess.run(['which', 'docker'], capture_output=True).stdout.strip(): print('🔧 安装 Docker...') subprocess.run(['curl', '-fsSL', 'https://get.docker.com', '-o', 'get-docker.sh']) subprocess.run(['sh', 'get-docker.sh']) subprocess.run(['systemctl', 'start', 'docker']) subprocess.run(['systemctl', 'enable', 'docker']) def ensure_nginx_container(): os.makedirs(NGINX_CONF_DIR, exist_ok=True) result = subprocess.run(['docker', 'ps', '-a', '--filter', f'name={NGINX_CONTAINER_NAME}'], capture_output=True, text=True) if NGINX_CONTAINER_NAME not in result.stdout: print('🚀 创建 Nginx 容器...') subprocess.run(['docker', 'run', '-d', '--name', NGINX_CONTAINER_NAME, '--restart', 'always', '-p', '80:80', '-v', f'{NGINX_CONF_DIR}:/etc/nginx/conf.d', 'nginx']) else: running = subprocess.run(['docker', 'inspect', '-f', '{{.State.Running}}', NGINX_CONTAINER_NAME], capture_output=True, text=True) if 'false' in running.stdout: subprocess.run(['docker', 'start', NGINX_CONTAINER_NAME]) def reload_nginx(): subprocess.run(['docker', 'exec', NGINX_CONTAINER_NAME, 'nginx', '-s', 'reload']) def add_proxy_config(domain, target_ip, target_port): config_path = f'{NGINX_CONF_DIR}/{domain}.conf' with open(config_path, 'w') as f: f.write(f''' server {{ listen 80; listen [::]:80; server_name {domain}; location / {{ proxy_pass http://{target_ip}:{target_port}; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }} }} ''') print(f'✅ 配置已写入: {config_path}') def get_ip_info(): ipv4 = subprocess.run(['curl', '-s', 'ipv4.ip.sb'], capture_output=True, text=True).stdout.strip() ipv6 = subprocess.run(['curl', '-s', '--max-time', '1', 'ipv6.ip.sb'], capture_output=True, text=True).stdout.strip() return ipv4, ipv6 def main(): ensure_docker() ensure_nginx_container() while True: print("\n--- 添加新的反向代理 ---") domain = input("请输入域名 (或留空退出): ").strip() if not domain: break target_ip = input("请输入目标 IP: ").strip() target_port = input("请输入目标端口: ").strip() if not (domain and target_ip and target_port.isdigit()): print("⚠️ 无效输入,请重试") continue add_proxy_config(domain, target_ip, target_port) reload_nginx() print(f"🎯 http://{domain} → http://{target_ip}:{target_port}") ipv4, ipv6 = get_ip_info() print("\n✅ 所有配置完成!") print("你的服务器公网地址:") if ipv4: print(f" - http://{ipv4}") if ipv6: print(f" - http://[{ipv6}]") print("\n如需 HTTPS,请手动配置证书或使用 acme.sh / certbot") if __name__ == "__main__": main() EOF chmod +x /tmp/proxy_setup.py python3 /tmp/proxy_setup.py } # 主菜单 while true; do echo "" echo "🛠️ 欢迎使用 AutoServer 工具脚本" echo "1) 初始化服务器环境" echo "2) 安装会话管理工具(j / ki)" echo "3) 添加反向代理(基于 Docker + Nginx)" echo "0) 退出" read -p "请输入选项 [0-3]: " choice case "$choice" in 1) init_env ;; 2) install_j_ki ;; 3) reverse_proxy_setup ;; 0) echo "👋 再见!"; exit 0 ;; *) echo "❌ 无效选项,请重新输入" ;; esac done