From 8a18baa9213687c3b1471fbb1c3f6a491d4aa555 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 11 May 2025 14:36:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=A4=B1=E8=B4=A5=E9=80=A0=E6=88=90=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E9=87=8D=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/config/util.ts | 72 ---------------------------------- back/services/http.ts | 2 +- back/services/notify.ts | 2 +- back/services/user.ts | 13 +++++- shell/preload/sitecustomize.py | 2 +- 5 files changed, 14 insertions(+), 77 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index 86f6826e..2a5ca86f 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -58,78 +58,6 @@ export function getToken(req: any) { return ''; } -export async function getNetIp(req: any) { - const ipArray = [ - ...new Set([ - ...(req.headers['x-real-ip'] || '').split(','), - ...(req.headers['x-forwarded-for'] || '').split(','), - req.ip, - ...req.ips, - req.socket.remoteAddress, - ]), - ].filter(Boolean); - - let ip = ipArray[0]; - - if (ipArray.length > 1) { - for (let i = 0; i < ipArray.length; i++) { - const ipNumArray = ipArray[i].split('.'); - const tmp = ipNumArray[0] + '.' + ipNumArray[1]; - if ( - tmp === '192.168' || - (ipNumArray[0] === '172' && - ipNumArray[1] >= 16 && - ipNumArray[1] <= 32) || - tmp === '10.7' || - tmp === '127.0' - ) { - continue; - } - ip = ipArray[i]; - break; - } - } - - ip = ip.substr(ip.lastIndexOf(':') + 1, ip.length); - if (ip.includes('127.0') || ip.includes('192.168') || ip.includes('10.7')) { - ip = ''; - } - - if (!ip) { - return { address: `获取失败`, ip }; - } - - try { - const csdnApi = got - .get(`https://searchplugin.csdn.net/api/v1/ip/get?ip=${ip}`, { - timeout: 10000, - retry: 0, - }) - .text(); - const pconlineApi = got - .get(`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`, { - timeout: 10000, - retry: 0, - }) - .buffer(); - const [csdnBody, pconlineBody] = await await Promise.all([ - csdnApi, - pconlineApi, - ]); - const csdnRes = JSON.parse(csdnBody); - const pconlineRes = JSON.parse(iconv.decode(pconlineBody, 'GBK')); - let address = ''; - if (csdnBody && csdnRes.code == 200) { - address = csdnRes.data.address; - } else if (pconlineRes && pconlineRes.addr) { - address = pconlineRes.addr; - } - return { address, ip }; - } catch (error) { - return { address: `获取失败`, ip }; - } -} - export function getPlatform(userAgent: string): 'mobile' | 'desktop' { const ua = userAgent.toLowerCase(); const testUa = (regexp: RegExp) => regexp.test(ua); diff --git a/back/services/http.ts b/back/services/http.ts index c21f5e07..5391c842 100644 --- a/back/services/http.ts +++ b/back/services/http.ts @@ -19,7 +19,7 @@ export class HttpServerService { resolve(this.server); }); - this.server.on('error', (err: Error) => { + this.server?.on('error', (err: Error) => { Logger.error('Failed to start HTTP service:', err); reject(err); }); diff --git a/back/services/notify.ts b/back/services/notify.ts index 10e3bf2a..12e3b4b8 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -59,7 +59,7 @@ export default class NotificationService { try { return await notificationModeAction?.call(this); } catch (error: any) { - throw error; + console.error(error); } } return false; diff --git a/back/services/user.ts b/back/services/user.ts index 5f552390..7f5e9d8e 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -1,6 +1,6 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; -import { createRandomString, getNetIp } from '../config/util'; +import { createRandomString } from '../config/util'; import config from '../config'; import jwt from 'jsonwebtoken'; import { authenticator } from '@otplib/preset-default'; @@ -271,7 +271,16 @@ export default class UserService { if (isValid) { return this.login({ username, password }, req, false); } else { - const { ip, address } = await getNetIp(req); + const ip = requestIp.getClientIp(req) || ''; + const query = new IP2Region(); + const ipAddress = query.search(ip); + let address = ''; + if (ipAddress) { + const { country, province, city, isp } = ipAddress; + address = uniq([country, province, city, isp]) + .filter(Boolean) + .join(' '); + } await this.updateAuthInfo(authInfo, { lastip: ip, lastaddr: address, diff --git a/shell/preload/sitecustomize.py b/shell/preload/sitecustomize.py index 6e1b95b2..ae67263b 100644 --- a/shell/preload/sitecustomize.py +++ b/shell/preload/sitecustomize.py @@ -54,7 +54,7 @@ def run(): task_before = os.getenv("task_before") if task_before: - escaped_task_before = task_before.replace("'", "'\\''") + escaped_task_before = task_before.replace('"', '\\"').replace("$", "\\$") commands.append(f"eval '{escaped_task_before}'") print("执行前置命令\n")