使用 tinyproxy 设置从正向代理到反向代理的通信

使用 tinyproxy 设置从正向代理到反向代理的通信

我在连接正向代理和反向代理时遇到了问题,两者都由 tinyproxy(版本 1.8.3)支持。我不知道这是否是配置问题。

我正在使用 4 台运行 Debian GNU/Linux 8.9 (jessie) 的虚拟机测试这些代理。这些机器代表一个客户端、一个正向代理、一个反向代理和服务器。我正在设置正向代理和反向代理之间的通信。

该服务器正在运行 Apache 服务器,它返回默认 HTML 页面,显示“它起作用了”。

期望的行为是客户端通过正向代理与服务器通信,该正向代理上游到与服务器交互的反向代理:客户端(10.0.2.33)->正向代理(10.0.2.35)->反向代理(10.0.2.36)->服务器(10.0.2.34)

为了测试系统,我在客户端机器上使用这个 curl 命令:

curl -v --proxy http://10.0.2.35:8888 http://10.0.2.34:80/

正向代理配置文件:

## tinyproxy.conf -- tinyproxy daemon configuration file

User nobody
Group nogroup

Port 8888
Listen 10.0.2.35
BindSame yes
Timeout 600

DefaultErrorFile "/usr/share/tinyproxy/default.html"
StatFile "/usr/share/tinyproxy/stats.html"
Logfile "/var/log/tinyproxy/tinyproxy.log"
#Syslog On
LogLevel Info
PidFile "/var/run/tinyproxy/tinyproxy.pid"

#Upstream 10.0.2.36:8888 "10.0.2.0/24"
Upstream 10.0.2.36:8888

MaxClients 100
MinSpareServers 2
MaxSpareServers 5
StartServers 2
MaxRequestsPerChild 0

Allow 127.0.0.1
Allow 10.0.2.0/24

ViaProxyName "tinyproxy1"

ConnectPort 8888
ConnectPort 80

# The following two ports are used by SSL.
ConnectPort 443
ConnectPort 563

反向代理配置文件:

## tinyproxy.conf -- tinyproxy daemon configuration file

User nobody
Group nogroup

Port 8888
Listen 10.0.2.36

BindSame yes
Timeout 600

StatFile "/usr/share/tinyproxy/stats.html"
Logfile "/var/log/tinyproxy/tinyproxy.log"
#Syslog On
LogLevel Info
PidFile "/var/run/tinyproxy/tinyproxy.pid"

no upstream "10.0.2.34:80"
#no upstream "."

MaxClients 5
MinSpareServers 2
MaxSpareServers 5
StartServers 2

MaxRequestsPerChild 0

Allow 127.0.0.1
Allow 10.0.2.0/24
Allow 10.0.2.35

ViaProxyName "tinyproxy2"

ConnectPort 8888
ConnectPort 80

# The following two ports are used by SSL.
ConnectPort 443
ConnectPort 563

ReversePath "/" "http://10.0.2.34:80/"
ReversePath "/wired/" "http://www.wired.com/"

ReverseOnly Yes
ReverseMagic Yes
ReverseBaseURL "http://10.0.2.34:80/"

这些是 Wireshark 捕获的 TCP 流:

流程 [客户端 <-> 正向代理]

GET http://10.0.2.34:80/ HTTP/1.1
User-Agent: curl/7.38.0
Host: 10.0.2.34
Accept: */*
Proxy-Connection: Keep-Alive

HTTP/1.0 400 Bad Request
Via: 1.1 tinyproxy1 (tinyproxy/1.8.3)
Server: tinyproxy/1.8.3
Content-Type: text/html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>Bad Request</h1>
<p>Request has an invalid URL</p>
<hr />
<p><em>Generated by tinyproxy version 1.8.3.</em></p>
</body>
</html>

流程 [正向代理 <-> 反向代理]

GET http://10.0.2.34:80/ HTTP/1.0
Host: 10.0.2.34
Connection: close
Via: 1.1 tinyproxy1 (tinyproxy/1.8.3)
User-Agent: curl/7.38.0
Accept: */*

HTTP/1.0 400 Bad Request
Server: tinyproxy/1.8.3
Content-Type: text/html
Connection: close

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head><title>400 Bad Request</title></head>
<body>
<h1>Bad Request</h1>
<p>Request has an invalid URL</p>
<hr />
<p><em>Generated by tinyproxy version 1.8.3.</em></p>
</body>
</html>

这些是 tinyproxy 日志文件的输出:

转发代理

CONNECT   Dec 10 22:05:08 [2788]: Connect (file descriptor 6): 10.0.2.33 [10.0.2.33] at [10.0.2.35]
CONNECT   Dec 10 22:05:08 [2788]: Request (file descriptor 6): GET http://10.0.2.34:80/ HTTP/1.1
INFO      Dec 10 22:05:08 [2788]: Found upstream proxy 10.0.2.36:8888 for 10.0.2.34
CONNECT   Dec 10 22:05:08 [2788]: Established connection to upstream proxy "10.0.2.36" using file descriptor 7.
INFO      Dec 10 22:05:08 [2788]: Closed connection between local client (fd:6) and remote client (fd:7)

反向代理

CONNECT   Dec 10 22:05:08 [4487]: Connect (file descriptor 6): 10.0.2.35 [10.0.2.35] at [10.0.2.36]
CONNECT   Dec 10 22:05:08 [4487]: Request (file descriptor 6): GET http://10.0.2.34:80/ HTTP/1.0
ERROR     Dec 10 22:05:08 [4487]: Bad request
INFO      Dec 10 22:05:08 [4487]: no entity

目前,我可以通过“curl”访问反向代理机器内的服务器页面http://10.0.2.34:80/http://10.0.2.36:8888/因为我的反向代理配置(ReversePath)。下图显示了反向代理机器中的 Wireshark 捕获。

通过反向代理访问服务器捕获的图像

通过所有这些配置,我在客户端机器上收到的只是来自反向代理的 400 个错误请求错误消息。

答案1

这个问题已经解决了。测试的方法不是直接向服务器请求页面,而是向反向代理机器请求。另外,正向代理不应该将流量上游到反向代理。

如果正向代理上游流量,则反向代理无法理解发送到主机/服务器的请求,因为它们的工作方式不同。

正向代理的正确配置必须排除该行:

upstream 10.0.2.36:8888

因为反向代理有以下规则:

ReversePath “/” “http://10.0.2.34:80/”

当我们请求服务器资源(Apache 的“It works”页面)时,反向代理(10.0.2.36)将请求服务器(10.0.2.34)http://10.0.2.36:8888/

我们应该使用以下命令运行 curl 到反向代理:

curl -v --proxy http://10.0.2.35:8888 http://10.0.2.36:8888/

然后我们可以看到配置生效了,输出如下:

root@debian:/home/debian# curl -v --proxy http://10.0.2.35:8888 http://10.0.2.36:8888
* Rebuilt URL to: http://10.0.2.36:8888/
* Hostname was NOT found in DNS cache
*   Trying 10.0.2.35...
* Connected to 10.0.2.35 (10.0.2.35) port 8888 (#0)
> GET http://10.0.2.36:8888/ HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 10.0.2.36:8888
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 OK
< Via: 1.0 tinyproxy2 (tinyproxy/1.8.3), 1.1 tinyproxy1 (tinyproxy/1.8.3)
< Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
< Date: Tue, 12 Dec 2017 23:01:37 GMT
< Content-Type: text/html
< ETag: "2d-432a5e4a73a80"
< Set-Cookie: yummy_magical_cookie=/; path=/
* Server Apache/2.4.29 (Unix) is not blacklisted
< Server: Apache/2.4.29 (Unix)
< Content-Length: 45
< Accept-Ranges: bytes
< 
<html><body><h1>It works!</h1></body></html>
* Connection #0 to host 10.0.2.35 left intact

相关内容