11import { handlers } from "@/app/auth"
22import { NextResponse } from 'next/server'
33import { NextRequest } from 'next/server'
4- import { getProxyConfig } from "@/proxy-config"
4+ import { getProxyConfig , disableProxy } from "@/proxy-config"
55import { HttpsProxyAgent } from "https-proxy-agent"
66
77const TIMEOUT_DURATION = 60000 ;
@@ -12,24 +12,63 @@ interface ExtendedRequestInit extends RequestInit {
1212 timeout ?: number ;
1313}
1414
15- // 定义超时 Promise 的类型
16- type TimeoutPromise = Promise < never >
17-
1815export async function GET ( request : NextRequest ) : Promise < Response > {
1916 try {
2017 const { isEnabled, httpsAgent } = getProxyConfig ( ) ;
18+ const nextauthUrl = process . env . NEXTAUTH_URL || '' ;
19+
20+ // 检查是否是回调请求
21+ if ( request . url . includes ( '/api/auth/callback' ) ) {
22+ console . log ( 'Callback request detected:' , request . url ) ;
23+
24+ // 处理请求
25+ const response = await handlers . GET ( request ) ;
26+
27+ // 如果是成功的回调(通常是 302 重定向)
28+ if ( response instanceof Response && response . status === 302 ) {
29+ const location = response . headers . get ( 'location' ) ;
30+
31+ // 检查重定向 URL 是否匹配 NEXTAUTH_URL
32+ if ( location && (
33+ location === nextauthUrl ||
34+ location . startsWith ( nextauthUrl + '/' ) ||
35+ location === '/' ||
36+ ! location . includes ( '/api/auth' )
37+ ) ) {
38+ console . log ( 'Redirecting to app URL, disabling proxy:' , location ) ;
39+ // 在响应发送后禁用代理
40+ setTimeout ( ( ) => {
41+ disableProxy ( ) ;
42+ } , 100 ) ;
43+ }
44+ }
45+
46+ return response instanceof Response ? response : NextResponse . json ( response ) ;
47+ }
2148
22- // 只在认证请求时使用代理
49+ // 处理其他认证请求
2350 if ( isEnabled && request . url . includes ( '/api/auth' ) ) {
24- console . log ( 'originalFetch!!!!!!!!!! :' , global . fetch ) ;
51+ console . log ( 'Auth request with proxy :' , request . url ) ;
2552 const originalFetch = global . fetch ;
53+
2654 global . fetch = async ( input : RequestInfo | URL , init ?: RequestInit ) => {
27- const extendedInit : ExtendedRequestInit = {
28- ...init ,
29- agent : httpsAgent ,
30- timeout : TIMEOUT_DURATION ,
31- } ;
32- return originalFetch ( input , extendedInit ) ;
55+ const inputStr = typeof input === 'string' ? input : input . toString ( ) ;
56+
57+ // 只对 GitHub OAuth 相关 URL 使用代理
58+ if ( inputStr . includes ( 'github.com/login/oauth' ) ||
59+ inputStr . includes ( 'api.github.com/user' ) ) {
60+
61+ console . log ( 'Using proxy for fetch request:' , inputStr ) ;
62+ const extendedInit : ExtendedRequestInit = {
63+ ...init ,
64+ agent : httpsAgent ,
65+ timeout : TIMEOUT_DURATION ,
66+ } ;
67+ return originalFetch ( input , extendedInit ) ;
68+ } else {
69+ // 其他请求不使用代理
70+ return originalFetch ( input , init ) ;
71+ }
3372 } ;
3473
3574 try {
@@ -40,7 +79,7 @@ export async function GET(request: NextRequest): Promise<Response> {
4079 global . fetch = originalFetch ;
4180 }
4281 } else {
43- // 非认证请求使用普通 fetch
82+ // 非认证请求或代理未启用
4483 const response = await handlers . GET ( request ) ;
4584 return response instanceof Response ? response : NextResponse . json ( response ) ;
4685 }
@@ -59,17 +98,72 @@ export async function GET(request: NextRequest): Promise<Response> {
5998
6099export async function POST ( request : NextRequest ) : Promise < Response > {
61100 try {
62- const { isEnabled } = getProxyConfig ( ) ;
63-
64- if ( isEnabled ) {
65- // 使用超时机制
66- const timeoutPromise : TimeoutPromise = new Promise ( ( _ , reject ) =>
67- setTimeout ( ( ) => reject ( new Error ( 'Request timeout' ) ) , TIMEOUT_DURATION )
68- ) ;
69- const response = await Promise . race ( [ handlers . POST ( request ) , timeoutPromise ] ) ;
101+ const { isEnabled, httpsAgent } = getProxyConfig ( ) ;
102+ const nextauthUrl = process . env . NEXTAUTH_URL || '' ;
103+
104+ // 检查是否是回调请求
105+ if ( request . url . includes ( '/api/auth/callback' ) ) {
106+ console . log ( 'Callback POST request detected:' , request . url ) ;
107+
108+ // 处理请求
109+ const response = await handlers . POST ( request ) ;
110+
111+ // 如果是成功的回调(通常是 302 重定向)
112+ if ( response instanceof Response && response . status === 302 ) {
113+ const location = response . headers . get ( 'location' ) ;
114+
115+ // 检查重定向 URL 是否匹配 NEXTAUTH_URL
116+ if ( location && (
117+ location === nextauthUrl ||
118+ location . startsWith ( nextauthUrl + '/' ) ||
119+ location === '/' ||
120+ ! location . includes ( '/api/auth' )
121+ ) ) {
122+ console . log ( 'POST: Redirecting to app URL, disabling proxy:' , location ) ;
123+ // 在响应发送后禁用代理
124+ setTimeout ( ( ) => {
125+ disableProxy ( ) ;
126+ } , 100 ) ;
127+ }
128+ }
129+
70130 return response instanceof Response ? response : NextResponse . json ( response ) ;
131+ }
132+
133+ // 处理其他认证请求
134+ if ( isEnabled && request . url . includes ( '/api/auth' ) ) {
135+ console . log ( 'Auth POST request with proxy:' , request . url ) ;
136+ const originalFetch = global . fetch ;
137+
138+ global . fetch = async ( input : RequestInfo | URL , init ?: RequestInit ) => {
139+ const inputStr = typeof input === 'string' ? input : input . toString ( ) ;
140+
141+ // 只对 GitHub OAuth 相关 URL 使用代理
142+ if ( inputStr . includes ( 'github.com/login/oauth' ) ||
143+ inputStr . includes ( 'api.github.com/user' ) ) {
144+
145+ console . log ( 'Using proxy for POST fetch request:' , inputStr ) ;
146+ const extendedInit : ExtendedRequestInit = {
147+ ...init ,
148+ agent : httpsAgent ,
149+ timeout : TIMEOUT_DURATION ,
150+ } ;
151+ return originalFetch ( input , extendedInit ) ;
152+ } else {
153+ // 其他请求不使用代理
154+ return originalFetch ( input , init ) ;
155+ }
156+ } ;
157+
158+ try {
159+ const response = await handlers . POST ( request ) ;
160+ return response instanceof Response ? response : NextResponse . json ( response ) ;
161+ } finally {
162+ // 请求完成后恢复原始的 fetch
163+ global . fetch = originalFetch ;
164+ }
71165 } else {
72- // 不使用超时机制
166+ // 非认证请求或代理未启用
73167 const response = await handlers . POST ( request ) ;
74168 return response instanceof Response ? response : NextResponse . json ( response ) ;
75169 }
0 commit comments