From 3c6481811e79c665af4e7f7b3ec558b74d30e2b7 Mon Sep 17 00:00:00 2001 From: Cuishibing <643237029@qq.com> Date: Sun, 22 Mar 2026 17:31:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DSSH=E9=9A=A7=E9=81=93?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E8=BD=AC=E5=8F=91=E5=92=8CCookie=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/auth/login/route.ts | 2 +- app/api/auth/register/route.ts | 2 +- port_forwarding.sh | 72 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100755 port_forwarding.sh diff --git a/app/api/auth/login/route.ts b/app/api/auth/login/route.ts index c99e67e..4b14788 100644 --- a/app/api/auth/login/route.ts +++ b/app/api/auth/login/route.ts @@ -52,7 +52,7 @@ export async function POST(request: NextRequest) { const response = NextResponse.json({ success: true, username: user.username }); response.cookies.set('auth_token', newToken, { httpOnly: true, - secure: process.env.NODE_ENV === 'production', + secure: false, sameSite: 'lax', maxAge: 60 * 60 * 24 * 7, path: '/' diff --git a/app/api/auth/register/route.ts b/app/api/auth/register/route.ts index a206c9f..e31fefb 100644 --- a/app/api/auth/register/route.ts +++ b/app/api/auth/register/route.ts @@ -68,7 +68,7 @@ export async function POST(request: NextRequest) { const response = NextResponse.json({ success: true, username }); response.cookies.set('auth_token', token, { httpOnly: true, - secure: process.env.NODE_ENV === 'production', + secure: false, sameSite: 'lax', maxAge: 60 * 60 * 24 * 7, path: '/' diff --git a/port_forwarding.sh b/port_forwarding.sh new file mode 100755 index 0000000..12cdbba --- /dev/null +++ b/port_forwarding.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +REMOTE_HOST="47.120.74.73" +REMOTE_PORT="30000" +LOCAL_PORT="3000" +SSH_PORT="22" +SSH_PASSWORD="MyPassword2+" +PID_FILE="/tmp/ssh_tunnel.pid" +LOG_FILE="/tmp/ssh_tunnel.log" + +start_tunnel() { + if [ -f "$PID_FILE" ]; then + OLD_PID=$(cat "$PID_FILE") + if kill -0 "$OLD_PID" 2>/dev/null; then + echo "SSH隧道已在运行 (PID: $OLD_PID)" + return + fi + rm -f "$PID_FILE" + fi + + echo "正在启动SSH反向隧道..." + sshpass -p "$SSH_PASSWORD" ssh -N -g -R "0.0.0.0:$REMOTE_PORT":localhost:"$LOCAL_PORT" -p "$SSH_PORT" -o "ServerAliveInterval=60" -o "ServerAliveCountMax=3" root@"$REMOTE_HOST" > "$LOG_FILE" 2>&1 & + SSH_PID=$! + echo "$SSH_PID" > "$PID_FILE" + sleep 2 + + if kill -0 "$SSH_PID" 2>/dev/null; then + echo "SSH隧道已启动 (本地3000 -> 远程$REMOTE_PORT)" + else + echo "启动失败,查看日志: cat $LOG_FILE" + rm -f "$PID_FILE" + fi +} + +stop_tunnel() { + if [ -f "$PID_FILE" ]; then + PID=$(cat "$PID_FILE") + if kill -0 "$PID" 2>/dev/null; then + kill "$PID" + rm -f "$PID_FILE" + echo "SSH隧道已停止" + else + rm -f "$PID_FILE" + echo "隧道未运行" + fi + else + echo "未找到PID文件,尝试直接终止" + pkill -f "ssh -N -R $REMOTE_PORT:localhost:$LOCAL_PORT" + fi +} + +status_tunnel() { + if [ -f "$PID_FILE" ]; then + PID=$(cat "$PID_FILE") + if kill -0 "$PID" 2>/dev/null; then + echo "SSH隧道正在运行 (PID: $PID)" + return + fi + fi + echo "SSH隧道未运行" + echo "尝试检测进程..." + if pgrep -f "ssh -N -R $REMOTE_PORT" > /dev/null; then + echo "发现隧道进程,但无PID文件" + fi +} + +case "$1" in + start) start_tunnel ;; + stop) stop_tunnel ;; + status) status_tunnel ;; + *) echo "Usage: $0 {start|stop|status}" ;; +esac