【问题标题】:How to support https in a local proxy server?如何在本地代理服务器中支持 https?
【发布时间】:2017-02-22 00:35:36
【问题描述】:

我编写了一个在 localhost:8000 上运行的小型代理服务器,它应该替换特定 url 上的一些内容,同时保持其他 url 不变。

它目前所做的是在example.com 页面上将Example Domain 替换为Hello World!。为了构建它,我使用了 proxy-tamper 包。

代码如下(保存在proxy-server.js):

"use strict";

const proxy = require('proxy-tamper').start({port: 8000});

// block share-term.me
proxy.tamper(/share-term.me\/$/, 'This content is blocked!');

// Replace the content on example.com
proxy.tamper(/example.com\/$/, request => {
  delete request.headers['accept-encoding'];
  request.onResponse(response => {
    response.body = response.body.replace(/Example Domain/g, 'Hello World!');
    response.headers['server'] = 'proxy-tamper 1337';
    response.headers['content-length'] = response.body.length;
    response.complete();
  });
});

然后,启动我使用的代理服务器:

node proxy-server.js

最后,通过设置--proxy-server 选项启动google-chrome-stable

google-chrome-stable --proxy-server='http=http://localhost:8000;https=http://localhost:8000' http://domain.com

这适用于http://example.com,但不适用于https://example.com。事实上它不支持https

当我打开http://example.com 时,我看到了替换的内容:

但是当我打开https://example.com(或任何https url)时,它会失败,以ERR_EMPTY_RESPONSE结尾:

如何为我的代理服务器添加https 支持,或者至少让Chrome 绕过https url 到http

我尝试使用 --ignore-certificate-errors--allow-insecure-content 运行 Chrome 进程,但它们没有帮助。

我对拥有强大的 https 相关安全性并不感兴趣,但我想通过我的服务器代理这些 https 请求并在客户端上发送响应。

【问题讨论】:

    标签: javascript html node.js google-chrome proxy


    【解决方案1】:

    代理篡改似乎没有用于 https 请求的功能。它仅支持 http(在撰写本文并查看文档时)。

    您可以改用包node-http-proxy。关于支持 https here 有一个很好的部分。

    编辑 有人可以验证您是否可以实际更改 https 响应主体服务器端吗?这些信息在技术上不会被加密吗?服务器上的解密在技术上是否违反了协议并且不允许?

    【讨论】:

    • 我试过http-proxy,但我读到here,没有简单的解决方案来修改响应内容。有什么想法吗?
    • @IonicăBizău 我需要检查一下,让我回复你。服务器端这应该是可能的,但包可能不支持在不编辑包本身的情况下修改内容。
    • 我很乐意修改包然后贡献,只要我有一个支持 HTTPs 的代理服务器的最小示例。
    • @IonicăBizău 哦,transformer-proxy 是 http-proxy 的中间件。您应该能够在从 http-proxy 包获得的 https 响应中使用它。尝试将它们结合使用。
    • 我也看到了!您能否创建一个仅替换特定页面上的内容(由正则表达式匹配)的最小示例?我为此苦苦挣扎了 3 天......谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多