1
1
import { handlers } from "@/app/auth"
2
2
import { NextResponse } from 'next/server'
3
3
import { NextRequest } from 'next/server'
4
- import { getProxyConfig } from "@/proxy-config"
4
+ import { getProxyConfig , disableProxy } from "@/proxy-config"
5
5
import { HttpsProxyAgent } from "https-proxy-agent"
6
6
7
7
const TIMEOUT_DURATION = 60000 ;
@@ -12,24 +12,63 @@ interface ExtendedRequestInit extends RequestInit {
12
12
timeout ?: number ;
13
13
}
14
14
15
- // 定义超时 Promise 的类型
16
- type TimeoutPromise = Promise < never >
17
-
18
15
export async function GET ( request : NextRequest ) : Promise < Response > {
19
16
try {
20
17
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
+ }
21
48
22
- // 只在认证请求时使用代理
49
+ // 处理其他认证请求
23
50
if ( isEnabled && request . url . includes ( '/api/auth' ) ) {
24
- console . log ( 'originalFetch!!!!!!!!!! :' , global . fetch ) ;
51
+ console . log ( 'Auth request with proxy :' , request . url ) ;
25
52
const originalFetch = global . fetch ;
53
+
26
54
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
+ }
33
72
} ;
34
73
35
74
try {
@@ -40,7 +79,7 @@ export async function GET(request: NextRequest): Promise<Response> {
40
79
global . fetch = originalFetch ;
41
80
}
42
81
} else {
43
- // 非认证请求使用普通 fetch
82
+ // 非认证请求或代理未启用
44
83
const response = await handlers . GET ( request ) ;
45
84
return response instanceof Response ? response : NextResponse . json ( response ) ;
46
85
}
@@ -59,17 +98,72 @@ export async function GET(request: NextRequest): Promise<Response> {
59
98
60
99
export async function POST ( request : NextRequest ) : Promise < Response > {
61
100
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
+
70
130
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
+ }
71
165
} else {
72
- // 不使用超时机制
166
+ // 非认证请求或代理未启用
73
167
const response = await handlers . POST ( request ) ;
74
168
return response instanceof Response ? response : NextResponse . json ( response ) ;
75
169
}
0 commit comments