diff --git a/.inscode b/.inscode index ecc8bdde946f464a2c4d431d8f8196413737d922..e1777316cc8a34b6c53f0ae12504b6c66fbdb272 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 2d7e6834fb6366b3120c7a37cc5f637bc4a33928..0e33a2bcb90ebbd8fbc3defca1d772918283b2d5 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