From e75bc30545d82c6d1ff7ffc723d8aed8ab3f9dc3 Mon Sep 17 00:00:00 2001 From: u014071104 Date: Wed, 13 Mar 2024 09:51:00 +0800 Subject: [PATCH] Wed Mar 13 09:51:00 CST 2024 inscode --- .inscode | 6 +++++- index.js | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.inscode b/.inscode index ecc8bdd..e177731 100644 --- a/.inscode +++ b/.inscode @@ -1,6 +1,10 @@ run = "npm i && npm run dev" +language = "node" [env] PATH = "/root/${PROJECT_DIR}/.config/npm/node_global/bin:/root/${PROJECT_DIR}/node_modules/.bin:${PATH}" XDG_CONFIG_HOME = "/root/.config" -npm_config_prefix = "/root/${PROJECT_DIR}/.config/npm/node_global" \ No newline at end of file +npm_config_prefix = "/root/${PROJECT_DIR}/.config/npm/node_global" + +[debugger] +program = "main.js" diff --git a/index.js b/index.js index 2d7e683..0e33a2b 100644 --- a/index.js +++ b/index.js @@ -1 +1,39 @@ -console.log("欢迎来到 InsCode"); \ No newline at end of file +const http = require('http'); +const https = require('https'); +const url = require('url'); + +const PORT = 3000; // 你的代理服务器端口 +const httpTool = (url, cb) => { + console.log("https?", url.startsWith("https"), url) + let ishttps = url.startsWith("https"); + if (ishttps) { + return https.get(url, cb); + } + else { + return http.get(url, cb); + } + +} +const server = http.createServer((req, res) => { + const parsedUrl = url.parse(req.url, true); + console.log("href", parsedUrl.query.url) + const targetPath = parsedUrl.query.url; + + // 创建一个请求到原始图片服务器 + const targetReq = httpTool(targetPath, (targetRes) => { + // 将原始服务器的响应转发给客户端 + res.writeHead(targetRes.statusCode, targetRes.headers); + console.log("流数据接受中..") + targetRes.pipe(res); + }); + + targetReq.on('error', (err) => { + console.error(`请求 ${targetPath} 时出错:`, err); + res.writeHead(500); + res.end('图片代理服务器内部错误'); + }); +}); + +server.listen(PORT, () => { + console.log(`图片代理服务器正在运行,监听端口 ${PORT}`); +}); \ No newline at end of file -- GitLab