日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Node.js做的代理转发服务器

發(fā)布時(shí)間:2025/7/14 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Node.js做的代理转发服务器 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

可以代理蘋果ID服務(wù)器

const http = require('http'); const https = require('https'); const client = require('https'); const fs = require('fs'); const server_options = {pfx: fs.readFileSync('appleid.apple.com.pfx'),//passphrase: 'sample' }; const server = https.createServer(server_options, (server_req, server_res) => {//1:有人訪問(wèn)虛擬代理服務(wù)器時(shí) console.log(server_req.url);const client_options = {};client_options.hostname = 'appleid.apple.com.akadns.net';client_options.port = 443;client_options.method = server_req.method;client_options.path = server_req.url;client_options.headers = server_req.headers;client_options.headers.host = 'appleid.apple.com';client_options.timeout = 5000;//2:轉(zhuǎn)發(fā)Header給目標(biāo)真實(shí)的服務(wù)器const client_req = client.request(client_options, (client_res) => {//3:目標(biāo)服務(wù)器返回Header server_res.writeHead(client_res.statusCode, client_res.headers);client_res.on('data', (d) => {//4:收到目標(biāo)服務(wù)器的數(shù)據(jù),并轉(zhuǎn)發(fā)給用戶 server_res.write(d);});client_res.on('end', (d) => {//5:目標(biāo)服務(wù)器數(shù)據(jù)發(fā)完了 server_res.end(d);});}).on('error', (e) => {//6:訪問(wèn)目標(biāo)服務(wù)器時(shí)出錯(cuò) console.error(e);});server_req.on('data', (data) => {//7:收到用戶發(fā)來(lái)的數(shù)據(jù),并轉(zhuǎn)發(fā)給目標(biāo)服務(wù)器 console.log(data.toString());client_req.write(data);});server_req.on('end', (data) => {//8:用戶數(shù)據(jù)發(fā)完了。 client_req.end(data);}); }); server.on('clientError', (err, socket) => {socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); server.on('error', (err, socket) => {if (err.code === 'EADDRINUSE') {console.log('綁定地址被占用');} }); const options = {}; options['host'] = '0.0.0.0'; options['port'] = 443; server.listen(options, () => {console.log('啟動(dòng)成功'); });

?

?

代碼更新。下面這個(gè)可以記錄到文件里面去

const http = require('http'); const https = require('https'); const client = require('https'); const fs = require('fs'); const zlib = require('zlib'); /* const crypto = require('crypto'); const md5 = (str) => {const md5 = crypto.createHash("md5");md5.update(str);return md5.digest('hex'); }; const base64 = (str) => {const b = new Buffer(str);return b.toString('base64'); }; //*/ const rootDir = 'data';//文件保存路徑 const server_options = {pfx: fs.readFileSync('appleid.apple.com.pfx'),//passphrase: 'sample' }; const server = https.createServer(server_options, (server_req, server_res) => {//1:有人訪問(wèn)虛擬代理服務(wù)器時(shí)const url_encoded = encodeURIComponent(server_req.url);console.log(server_req.url);fs.writeFileSync(rootDir + '/request_header-' + url_encoded, JSON.stringify(server_req.headers));const request_output = fs.createWriteStream(rootDir + '/request_body-' + url_encoded);server_req.pipe(request_output);//將用戶提交的數(shù)據(jù)寫入文件const client_options = {};client_options.hostname = 'appleid.apple.com.akadns.net';client_options.port = 443;client_options.method = server_req.method;client_options.path = server_req.url;client_options.headers = server_req.headers;client_options.headers.host = 'appleid.apple.com';client_options.timeout = 5000;//2:轉(zhuǎn)發(fā)Header給目標(biāo)真實(shí)的服務(wù)器const client_req = client.request(client_options, (client_res) => {//3:目標(biāo)服務(wù)器返回Headerfs.writeFileSync(rootDir + '/response_header-' + url_encoded, JSON.stringify(client_res.headers));const response_output = fs.createWriteStream(rootDir + '/response_body-' + url_encoded);server_res.writeHead(client_res.statusCode, client_res.headers);switch (client_res.headers['content-encoding']) {// or, just use zlib.createUnzip() to handle both casescase 'gzip':client_res.pipe(zlib.createGunzip()).pipe(response_output);break;case 'deflate':client_res.pipe(zlib.createInflate()).pipe(response_output);break;default://目標(biāo)服務(wù)器返回的是純文本,并寫入文件 client_res.pipe(response_output);break;}client_res.on('data', (data) => {//4:收到目標(biāo)服務(wù)器的數(shù)據(jù),并轉(zhuǎn)發(fā)給用戶 server_res.write(data);});client_res.on('end', () => {//5:目標(biāo)服務(wù)器數(shù)據(jù)發(fā)完了 server_res.end();});}).on('error', (e) => {//6:訪問(wèn)目標(biāo)服務(wù)器時(shí)出錯(cuò) console.error(e);});server_req.on('data', (data) => {//7:收到用戶發(fā)來(lái)的數(shù)據(jù),并轉(zhuǎn)發(fā)給目標(biāo)服務(wù)器 client_req.write(data);});server_req.on('end', () => {//8:用戶數(shù)據(jù)發(fā)完了。 client_req.end();}); }); server.on('clientError', (err, socket) => {socket.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); server.on('error', (err, socket) => {if (err.code === 'EADDRINUSE') {console.log('綁定地址被占用');} }); const options = {}; options['host'] = '0.0.0.0'; options['port'] = 443; server.listen(options, () => {console.log('啟動(dòng)成功'); });

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiangxisheng/p/8052699.html

總結(jié)

以上是生活随笔為你收集整理的Node.js做的代理转发服务器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。