通过 SSL/TLS 通过 HTTP 隧道(CONNECT)使用 socat 转发器

通过 SSL/TLS 通过 HTTP 隧道(CONNECT)使用 socat 转发器

就上下文而言,我正在使用 nginx 的反向代理ngx_http_proxy_module缓存来自上游 https 服务器的内容。对于一个特定项目,我需要 nginx 通过企业 HTTP 代理(使用 HTTP CONNECT 方法),不幸的是nginx 不支持此功能(并且也没有计划)。另外,该项目要求通过 SSL/TLS 连接代理。

为了解决这个问题,我发现stackoverflow 上的相关问题建议使用下面的 PROXY 功能运行 socat 作为转发器,并让 nginx 将其用作上游服务器。

socat TCP4-LISTEN:8443,reuseaddr,fork PROXY:yourproxy:backendserver:443,proxyport=<yourproxyport>

socat 的手册页指出:

PROXY:<proxy>:<hostname>:<port>
Connects to an HTTP proxy server on port 8080 using TCP/IP version 4 or 6 depending on address specification, name resolution, or option pf, and sends a CONNECT request for hostname:port. If the proxy grants access and succeeds to connect to the target, data transfer between socat and the target can start. Note that the traffic need not be HTTP but can be an arbitrary protocol.
Option groups: FD,SOCKET,IP4,IP6,TCP,HTTP,RETRY
Useful options: proxyport, ignorecr, proxyauth, resolve, crnl, bind, connect-timeout, mss, sourceport, retry
See also: SOCKS, TCP

换句话说,这适用于 http 代理,只是 socat 的 PROXY 功能不支持 SSL/TLS(即它不能使用 https 代理,只能使用普通的 http 代理),而这在当今的几个专业环境中是必需的。

请记住,我无法轻松地将 nginx 换成其他东西,有没有办法通过 TLS 转换 socat-> 代理连接,而无需编写自定义 socat 替代方案? 有没有可以实现此目的的替代工具?

答案1

这将是一个“繁重”的解决方法 - 但你总是可以使用隧道将您的连接封装在 TLS 中。以下是如何使用的示例:

sudo apt install stunnel4

# /etc/stunnel/service.conf
[service]
client = yes
accept = 127.0.0.1:<local-port>
connect = <target-server>:<target-port>
checkHost = <target-server-name>
verifyChain = yes
sslVersionMin = TLSv1.2
# if internal CA is used
CAfile = <path-to-ca-crt>
# if client-cert-auth is used
cert = <path-to-client-crt>
key = <path-to-client-key>

来源:Google LDAP-STunnel

或者

使用另一个 Proxying-Tool 代替 socat。例如:

  • 去去去
  • 我在 GitHub 上遇到了其他人......

相关内容