1- import { get } from 'https' ;
21import { isValidIP } from '../common/valid-ip.js' ;
32import { refererCheck } from '../common/referer-check.js' ;
4- import verifyToken from '../common/verify-token.js' ;
53
64export default async ( req , res ) => {
75
@@ -11,10 +9,6 @@ export default async (req, res) => {
119 return res . status ( 403 ) . json ( { error : referer ? 'Access denied' : 'What are you doing?' } ) ;
1210 }
1311
14- // 验证用户 Token
15- const { isValid, email } = await verifyToken ( req ) ;
16- const isValidUser = isValid ;
17-
1812 // 从请求中获取 IP 地址
1913 const ipAddress = req . query . ip ;
2014 if ( ! ipAddress ) {
@@ -34,39 +28,24 @@ export default async (req, res) => {
3428
3529 const lang = req . query . lang || 'en' ;
3630
37- // 构建请求 IPCheck.ing 的 URL
38- const url = new URL ( `https://api.ipcheck.ing/ipinfo?key=${ key } &ip=${ ipAddress } &lang=${ lang } &email=${ email } ` ) ;
39-
40- get ( url , apiRes => {
41- let data = '' ;
42- apiRes . on ( 'data' , chunk => data += chunk ) ;
43- apiRes . on ( 'end' , ( ) => {
44- try {
45- const originalJson = JSON . parse ( data ) ;
46- if ( isValidUser ) {
47- res . json ( originalJson ) ;
48- } else {
49- const modifiedJson = modifyJsonForIPChecking ( originalJson ) ;
50- res . json ( modifiedJson ) ;
51- }
31+ // 构建请求
32+ const url = new URL ( `https://api.ipcheck.ing/ipinfo?key=${ key } &ip=${ ipAddress } &lang=${ lang } ` ) ;
5233
53- } catch ( e ) {
54- res . status ( 500 ) . json ( { error : 'Error parsing JSON' } ) ;
34+ try {
35+ const apiResponse = await fetch ( url , {
36+ headers : {
37+ ...req . headers ,
5538 }
5639 } ) ;
57- } ) . on ( 'error' , ( e ) => {
58- res . status ( 500 ) . json ( { error : e . message } ) ;
59- } ) ;
60- }
6140
62- // 修改返回的数据
63- function modifyJsonForIPChecking ( json ) {
64- const modifiedJson = JSON . parse ( JSON . stringify ( json ) ) ;
65- if ( modifiedJson . proxyDetect ) {
66- for ( const key in modifiedJson . proxyDetect ) {
67- modifiedJson . proxyDetect [ key ] = "sign_in_required" ;
41+ if ( ! apiResponse . ok ) {
42+ throw new Error ( `API responded with status: ${ apiResponse . status } ` ) ;
6843 }
69- }
7044
71- return modifiedJson ;
45+ const data = await apiResponse . json ( ) ;
46+ res . json ( data ) ;
47+ } catch ( error ) {
48+ console . error ( "Error during API request:" , error ) ;
49+ res . status ( 500 ) . json ( { error : error . message } ) ;
50+ }
7251}
0 commit comments