【问题标题】:How to redirect HTTPS traffic to a proxy on localhost:443?如何将 HTTPS 流量重定向到 localhost:443 上的代理?
【发布时间】:2019-12-05 02:14:57
【问题描述】:

我正在使用 Cesanta Mongoose c++ 网络库在 C++ 中构建 HTTP 服务器。本质上,我试图将流量从我的网络浏览器重定向到本地主机上的代理。我最近通过购买证书和域并按照说明启用 ssl with mongoose (https://cesanta.com/docs/http/ssl.html) 添加了对 HTTPS 的支持。服务器现在正在侦听端口 443,并且运行良好。我已将 Web 浏览器配置为将 HTTPS 请求定向到 localhost:443,但它似乎没有触发 HTTP 服务器。当我的服务器正在监听端口 8080 并且 Web 浏览器被配置为向 localhost:8080 发送请求时,它会触发 Web 浏览器,但它不支持 https。配置网页浏览器发送请求到 localhost:443 有什么问题吗?

【问题讨论】:

  • 在您的情况下可以使用ngrok 之类的东西吗?它通过 Internet 提供到 localhost 的安全隧道,并且还支持 https。
  • @mrstack999 似乎正是我所需要的,我会试一试。谢谢
  • 没问题。祝你好运

标签: c++ http https network-programming mongoose-web-server


【解决方案1】:

ngrok 解决方案相当不错。

如果您想直接在应用程序中添加重定向,请设置两个侦听连接:一个 HTTP,一个 HTTPS。

为这两个监听连接使用不同的事件处理函数:HTTP 处理程序只会发送重定向,而 HTTPS 处理程序会做一个真正的工作:

static void f1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  if (ev == MG_EV_HTTP_MSG) {
    struct mg_http_message *hm = (struct mg_http_message *) ev_data;
    struct mg_str *host = mg_http_get_header(hm, "Host");
    if (host == NULL) {
      mg_printf(c, "HTTP/1.1 500 Failed\r\n\r\n");
    } else {
      mg_printf(c, "HTTP/1.1 301 Moved\r\n"
                   "Location: https://%.*s%.*s\r\n\r\n",
                   (int) host->len, host->ptr,);
                   (int) hm->uri.len, hm->uri.ptr);  // Send redirect to HTTPS!
    }
    c->is_draining = 1;
  }
}

...
  mg_http_listen(&mgr, "http://0.0.0.0:80", f1, NULL);    // HTTP
  mg_http_listen(&mgr, "https://0.0.0.0:443", f2, NULL);  // HTTPS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 2016-12-09
    • 2015-08-07
    • 2012-04-19
    • 2016-09-01
    • 2020-12-28
    • 2019-09-15
    • 2021-01-01
    相关资源
    最近更新 更多