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


    可以代理苹果ID服务器

    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:有人访问虚拟代理服务器时
      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:转发Header给目标真实的服务器
      const client_req = client.request(client_options, (client_res) => {
        //3:目标服务器返回Header
        server_res.writeHead(client_res.statusCode, client_res.headers);
        client_res.on('data', (d) => {
          //4:收到目标服务器的数据,并转发给用户
          server_res.write(d);
        });
        client_res.on('end', (d) => {
          //5:目标服务器数据发完了
          server_res.end(d);
        });
      }).on('error', (e) => {
        //6:访问目标服务器时出错
        console.error(e);
      });
      server_req.on('data', (data) => {
        //7:收到用户发来的数据,并转发给目标服务器
        console.log(data.toString());
        client_req.write(data);
      });
      server_req.on('end', (data) => {
        //8:用户数据发完了。
        client_req.end(data);
      });
    });
    server.on('clientError', (err, socket) => {
      socket.end('HTTP/1.1 400 Bad Request
    
    ');
    });
    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('启动成功');
    });

    代码更新。下面这个可以记录到文件里面去

    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:有人访问虚拟代理服务器时
      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);//将用户提交的数据写入文件
      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:转发Header给目标真实的服务器
      const client_req = client.request(client_options, (client_res) => {
        //3:目标服务器返回Header
        fs.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 cases
          case 'gzip':
            client_res.pipe(zlib.createGunzip()).pipe(response_output);
            break;
          case 'deflate':
            client_res.pipe(zlib.createInflate()).pipe(response_output);
            break;
          default:
            //目标服务器返回的是纯文本,并写入文件
            client_res.pipe(response_output);
            break;
        }
        client_res.on('data', (data) => {
          //4:收到目标服务器的数据,并转发给用户
          server_res.write(data);
        });
        client_res.on('end', () => {
          //5:目标服务器数据发完了
          server_res.end();
        });
      }).on('error', (e) => {
        //6:访问目标服务器时出错
        console.error(e);
      });
      server_req.on('data', (data) => {
        //7:收到用户发来的数据,并转发给目标服务器
        client_req.write(data);
      });
      server_req.on('end', () => {
        //8:用户数据发完了。
        client_req.end();
      });
    });
    server.on('clientError', (err, socket) => {
      socket.end('HTTP/1.1 400 Bad Request
    
    ');
    });
    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('启动成功');
    });
  • 相关阅读:
    c# 泛型集合Dictionary
    int+? int后带问题是什么意思,请看内容。
    在ASP.NET中实现Url ReWriting 示例
    #DataDirectory是什么意思呢?
    vs2008生成自定义dll,VS2008发布、生成网站时设置固定的dll文件名?
    平台安装注意事项
    快速开发平台程序运行环境
    快速开发平台介绍(动态)
    快速开发平台程序安装包20120612
    JavaFx 2.0总结
  • 原文地址:https://www.cnblogs.com/xiangxisheng/p/8052699.html
Copyright © 2020-2023  润新知