19

Socks5 透明代理

 3 years ago
source link: https://chenjiehua.me/linux/socks5-transparent-proxy.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Socks5 透明代理 

由于国内特殊的网络环境,想要使用谷歌搜索引擎真是困难重重,虽然电脑上可以使用 Chrome+SwitchyOmega+socks5 代理来轻松解决,然而对于移动设备来说却几乎没有选择,特别是 iOS 设备(国内 Apple Store 上可用的app基本全部下架了)。因此,这便寻思着来配置一个具备透明代理的wifi热点。

因为我们只是想要一个wifi热点来作透明代理,因此成本当然是越低越好,手头闲置的树莓派便成为了我们今天的主角。

将树莓派配置为无线热点可以参考前文《树莓派做无线热点》,在配置好wifi热点之后,我们就可以进行后续的工作了。

反其道而行之

随着GFW的升级,目前大部分的代理流量都可以被轻易检测到,特别是ss等基本各种加密混淆都没什么作用,一旦被检测到,IP 基本就 gg 了。

所以……既然流量特征如此明显,混淆之后反而更加容易暴露你在做着偷鸡摸狗的事情,那为什么还要费劲心思去瞎折腾呢,直接来一个socks5代理不就完事了!

简单粗暴更有效,况且我们也只是自己上上网而已,这点流量没人会关心的,就让它淹没在浩瀚的网络流量吧。

socks5 代理转发

由于我们VPS上部署的是一个 socks5 代理,相应的我们需要一个 socks5 的客户端。寻寻觅觅,终于让我找到了:redsocks

redsocks – transparent TCP-to-proxy redirector

This tool allows you to redirect any TCP connection to SOCKS or HTTPS proxy using your firewall, so redirection may be system-wide or network-wide.

详细的功能与说明可以在 Github 上查看,我们直接把它 git clone 下来:

Default
git clone [email protected]:darkk/redsocks.git

由于编译需要依赖到 libevent,我们先安装一下:

Default
sudo apt install gcc libevent-dev

然后就可以开始编译 redsocks:

Default
cd redsocks
  • 虽然我们也可以直接用 github 上 release 的版本,不过尽量使用最新的代码避免一些bug;
  • 之前在 OpenWrt 上测试,无奈默认的 ipk 版本是 0.4 ,bug比较多无法正常使用,而自己也不想折腾着去编译 openwrt 版本的 ipk,故放弃了;

配置文件也比较简单,参考目录下的 redsocks.conf.example,主要修改:

Default
base {
log_debug = on;          # 打开debug log,出现问题容易排查
log_info = on;
log = stderr;
daemon = off;
redirector = iptables;   # 我们后面将使用 iptables 来转发tcp流量
redsocks {
local_ip = 0.0.0.0;      # 这个需要绑定到全部接口
local_port = 12345;
ip = your socks5 proxy server;
port = your socks5 proxy port;
type = socks5;

然后就可以开始运行:

Default
./redsocks -c redsocks.conf

设置 iptables

参考官方的示例,添加 iptables 规则:

Default
# Create new chain
root# iptables -t nat -N REDSOCKS
# Ignore LANs and some other reserved addresses.
root# iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 100.64.0.0/10 -j RETURN
root# iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
root# iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
root# iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
root# iptables -t nat -A REDSOCKS -d 198.18.0.0/15 -j RETURN
root# iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
root# iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
# Anything else should be redirected to port 12345
root# iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345
# 这里假设我们使用的wifi网卡接口是 wlan0
# 将 wlan0 的 tcp 流量转发到 redsocks
root# iptables -t nat -A PREROUTING -i wlan0 -p tcp -j REDSOCKS

这时候,如果我们手机连接上这个 wifi 热点,那么所有的请求流量都会转发到 redsocks,再转发到我们的 socks5 代理服务器。

DNS 污染

通过上面的配置之后,虽然流量已经走 socks5 代理服务器了,然而我们很大概率会发现 google 仍然打不开。

What the f**k?(黑人问号)

仔细观察后我们便发现了,redsocks 虽然转发了流量,但是在 socks5代理服务器的日志中请求的 ip 却似乎有点问题,也就是说本地的 DNS 域名解析存在猫腻。

这便是所谓的 DNS污染,又称DNS投毒(总有刁民想害朕系列……)

对于DNS污染的问题,我们可以通过自建DNS服务器来解决,网上也有比较多的文章介绍,这里便不再重复了。需要注意的是,如果自建DNS服务器,不能使用境外的VPS,否则经过GFW的数据包回来之后大概率又中毒了。

不过,在这里我们可以更加简单地解决这个问题,因为我们树莓派搭建wifi热点的时候,使用的是 dnsmasq,它本身就是一个DNS服务器,所以我们可以直接配置 ChinaDNS 来解决。

ChinaDNS

从 github 下载它的 release 源码,然后解压并编译:

Default
wget https://github.com/shadowsocks/ChinaDNS/releases/download/1.3.2/chinadns-1.3.2.tar.gz
tar -zxvf chinadns-1.3.2.tar.gz
cd chinadns-1.3.2
./configure && make
# 编译后的二进制文件在 src 目录中
cd src

然后我们需要更新一下 chnroute,获取国内的ip地址段。如果通过 114 DNS(114.114.114.114)解析后得到的是国内ip,则可直接使用;否则应该使用OpenDNS(208.67.222.222:443)解析的结果:

Default
curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > chnroute.txt

然后运行 chinadns(这里我们将本地DNS服务监听在15353,避免跟dnsmasq冲突):

Default
./chinadns -v -p 15353 -m -c chnroute.txt -s 114.114.114.114,208.67.222.222:443

然后修改一下 dsnmasq 的配置:

Default
sudo vim /etc/dnsmasq.conf
# 添加配置
no-resolv
server=127.0.0.1#15353

然后重启一下 dnsmasq:

Default
sudo service dnsmasq restart

至此,我们应该就可以成功 google 啦(撒花~

区分国内外流量

虽然我们已经成功达到了最开始的目标,不过目前看来还有一点小小的遗憾:连上这个wifi热点之后,所有的流量都会经过 socks5 代理。这样子访问国内的网站却反而变慢了,毕竟出国旅游回来绕了一大圈啊!

这个问题可以通过 iptables 来解决,我们前面已经获取到国内的ip地址段 chnroute.txt,直接全部添加到 iptables :

Default
cat chnroute.txt |sudo xargs -I % iptables -t nat -A REDSOCKS -d % -j RETURN

这下子终于完美解决了~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK