diff --git a/backend/src/controllers/setting.ts b/backend/src/controllers/setting.ts index 659792b..c70f525 100644 --- a/backend/src/controllers/setting.ts +++ b/backend/src/controllers/setting.ts @@ -3,6 +3,7 @@ import { sendSuccess, sendError } from "../utils/response"; import Searcher from "../services/Searcher"; import UserSetting from "../models/UserSetting"; import GlobalSetting from "../models/GlobalSetting"; +import { iamgesInstance } from "./teleImages"; export const settingController = { async get(req: Request, res: Response): Promise { @@ -45,6 +46,7 @@ export const settingController = { await UserSetting.update(userSettings, { where: { userId } }); if (role === 1 && globalSetting) await GlobalSetting.update(globalSetting, { where: {} }); Searcher.updateAxiosInstance(); + iamgesInstance.updateProxyConfig(); sendSuccess(res, { message: "保存成功", }); diff --git a/backend/src/controllers/teleImages.ts b/backend/src/controllers/teleImages.ts index 7038eb5..6da7ab4 100644 --- a/backend/src/controllers/teleImages.ts +++ b/backend/src/controllers/teleImages.ts @@ -1,43 +1,64 @@ import axios, { AxiosInstance } from "axios"; -import { Request, Response } from "express"; +import e, { Request, Response } from "express"; import tunnel from "tunnel"; import GlobalSetting from "../models/GlobalSetting"; import { GlobalSettingAttributes } from "../models/GlobalSetting"; export class ImageControll { private axiosInstance: AxiosInstance | null = null; - private isUpdate = false; + private settings: GlobalSetting | null = null; constructor() { this.initializeAxiosInstance(); } - private async initializeAxiosInstance(isUpdate = false): Promise { - let settings = null; - if (isUpdate) { - settings = await GlobalSetting.findOne(); - this.isUpdate = isUpdate; - } else { - return; + private async initializeAxiosInstance(): Promise { + try { + this.settings = await GlobalSetting.findOne(); + } catch (error) { + console.error("Error fetching global settings:", error); } - const globalSetting = settings?.dataValues || ({} as GlobalSettingAttributes); + const globalSetting = this.settings?.dataValues || ({} as GlobalSettingAttributes); this.axiosInstance = axios.create({ timeout: 3000, - httpsAgent: tunnel.httpsOverHttp({ - proxy: { - host: globalSetting.httpProxyHost, - port: globalSetting.httpProxyPort, - headers: { - "Proxy-Authorization": "", - }, - }, - }), + httpsAgent: globalSetting.isProxyEnabled + ? tunnel.httpsOverHttp({ + proxy: { + host: globalSetting.httpProxyHost, + port: globalSetting.httpProxyPort, + headers: { + "Proxy-Authorization": "", + }, + }, + }) + : undefined, withCredentials: true, }); } + public async updateProxyConfig(): Promise { + try { + this.settings = await GlobalSetting.findOne(); + const globalSetting = this.settings?.dataValues || ({} as GlobalSettingAttributes); + if (this.axiosInstance) { + this.axiosInstance.defaults.httpsAgent = globalSetting.isProxyEnabled + ? tunnel.httpsOverHttp({ + proxy: { + host: globalSetting.httpProxyHost, + port: globalSetting.httpProxyPort, + headers: { + "Proxy-Authorization": "", + }, + }, + }) + : undefined; + } + } catch (error) { + console.error("Error updating proxy config:", error); + } + } + async getImages(req: Request, res: Response, url: string): Promise { try { - if (!this.isUpdate) await this.initializeAxiosInstance(true); const response = await this.axiosInstance?.get(url, { responseType: "stream" }); res.set("Content-Type", response?.headers["content-type"]); response?.data.pipe(res); @@ -47,7 +68,7 @@ export class ImageControll { } } -const iamgesInstance = new ImageControll(); +export const iamgesInstance = new ImageControll(); export const imageControll = { getImages: async (req: Request, res: Response): Promise => {