我配置了privoxy,并在端口8118上工作。我可以在定义http_proxy
和https_proxy
变量时转发HTTP和HTTPS流量,以向代理指出。示例:
https_proxy=http://127.0.0.1:8118 curl -vvv https://www.google.com
http_proxy=http://127.0.0.1:8118 curl -vvv http://www.google.com
请注意,对于HTTPS代理,我仍然使用http://
。Privoxy以某种方式转发请求。
但是,我需要透明地转发它,因为我使用的是Node.js,我不想更改应用程序代码来支持代理。在Windows上,这很容易由质子器完成,但是应用程序是专有的,在WSL或GNU/Linux上不工作。在WSL/Linux上,我尝试使用iptables将包转发到privoxy端口:
sudo iptables -t nat -N CUSTOM_PROXY
# Ignore LANs and some other reserved addresses.
sudo iptables -t nat -A CUSTOM_PROXY -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 192.168.0.0/16 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A CUSTOM_PROXY -d 240.0.0.0/4 -j RETURN
# Everything else is redirected to the privoxy port
sudo iptables -t nat -A CUSTOM_PROXY -p tcp -j REDIRECT --to-ports 8118
# Then I tried to forward the ports I need to the chain:
sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j CUSTOM_PROXY
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j CUSTOM_PROXY
sudo iptables -t nat -A OUTPUT -p tcp --dport 443 -j CUSTOM_PROXY
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j CUSTOM_PROXY
# other ports here...
激活这些规则后,HTTP和HTTPS调用停止工作:
shell> curl -vvv http://www.google.com
* Trying 142.250.74.36:80...
* TCP_NODELAY set
* Connected to www.google.com (142.250.74.36) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Invalid header received from client
< Content-Type: text/plain
< Connection: close
<
Invalid header received from client.
* Closing connection 0
shell> curl -vvv https://www.google.com
* Trying 142.250.74.36:443...
* TCP_NODELAY set
* Connected to www.google.com (142.250.74.36) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3
HTTP请求不起作用,HTTPS请求永远不会结束。Privoxy还支持socks5。
我不明白这些前瞻是如何发生的。有人能帮我找出我做错了什么吗?
以下是一些可能有帮助的补充意见/问题:
https_proxy
设置为指向http://...
?这跟privoxy有关吗?我的privoxy安装程序使用所有默认值,但以下转发配置除外:
forward / .
forward-socks5 .something.net 127.0.0.1:12345 .
我不认为这个privoxy配置真的很重要,因为我通过代理变量使用的任何东西都能正常工作。问题在iptables规则和privoxy之间。
任何帮助都是非常感谢的。提前感谢!
发布于 2022-04-02 11:45:03
我并不完全熟悉Privoxy是如何工作的,但是我知道在Linux上拦截代理是如何工作的。
HTTP代理和拦截代理以非常不同的方式工作。HTTP代理从查询的第一行获取目的地,其中将包含域名(例如,应该是GET http://www.google.com/ HTTP/1.1
)。对于HTTPS,它使用要连接的域:端口(例如CONNECT www.google.com:443 HTTP/1.1
)执行HTTP请求。
截取代理通过执行带有特定参数的getsockopt()从内核获取原始目标地址。它不了解更高级的协议。
通常,使用iptable重定向到HTTP代理的请求由于这些差异而无法工作。尽管如此,Privoxy似乎有一个配置选项accept-intercepted-requests
,您可以使用它从主机读取目标: HTTP头。使用该配置,它应该能够处理使用iptables重定向的HTTP请求。正如文档所述,HTTPS不支持这一点。您将需要使用一些额外的软件来完成对HTTP代理的转发,可能就像Proxifier一样。我知道莫代尔可以做到这一点。它不应该是唯一的,但我不认识其他人。
补充问题:
因为HTTP和透明/拦截代理以不同的方式工作。当变量存在时,curl (和其他变量)改变了它们发送查询的方式,但是对于iptables,它们不会(因为它们不知道您使用代理)。
在您的环境变量中,http://用于描述如何连接到代理(它实际上可以是https://,如果您的代理有一个tls证书,并且它也可以处理http请求,尽管它们只能从您加密到代理,而不是从代理到远程服务器)。
我不知道Windows上的代理是如何透明的。假设它很像Linux,Proxifier可能会将请求重定向到本地端口,获取实际目的地,以HTTP/SOCKS代理所理解的方式包装数据,并将其发送到您让它使用的代理。也许捕获部分有点不同(创建一个新的网络接口,要求Windows发送流量,从原始tcp数据包重构流,包装数据)。这通常被称为透明代理,因为客户端不知道使用了代理,有些人称它为拦截代理。而且我们可以有类似的东西,only就是一个例子(可能不是唯一的例子)。
https://stackoverflow.com/questions/71709157
复制相似问题