Linux利用iptables实现真-全局代理

Linux利用iptables实现真-全局代理

服务器

浏览数:695

2019-9-11

对于经常要浏览油管等被墙网站的人而言,利用代理来实现fq是非常有必要的。现在fq的方法中,最为主流的应该要数ssr了,因此本教程都是基于ssr的socks5代理而言的。
在windows中,ssr客户端设置的系统代理,大部分应用还是能够起到作用的,但是也有不少却不走代理。这里有个不错的解决方案就是通过sstap来实现网卡层的代理,这样一来,所有的流量都会通过代理流通转发出去了。
然而,windows并不是我们的全部,偶尔我也会用用linux,这样的话,对于linux有没有较好的全局代理方案呢?这就是本文将要介绍的。

下面我将简单介绍了linux常见的设置代理方法,以及全局代理的方法。

常见代理

最为常见的是

http_proxy=http://localhost:1080
https_proxy=http://localhost:1080
export http_proxy https_proxy

其中localhost为代理的站点,这里只是以localhost为例;1080为端口,同样只是为例。
然后导出这两个环境变量。最后可以在bash里面试试网络连通性

curl baidu.com
# or
curl google.com

取消这两个环境变量则用以下命令

unset http_proxy
unset https_proxy

这样设置完成后,只会在当前bash实例中起作用,如果想在每个实例起作用可以这样:
首先 vim /etc/profile 然后在末尾加上

http_proxy=http://localhost:1080
https_proxy=http://localhost:1080
export http_proxy https_proxy

保存后再

source /etc/profile

使其生效。如果想将socks代理转为http代理可以这样设置

http_proxy=socks5:http://localhost:1080
# or
https_proxy=socks5:http://localhost:1080

这里是socks5的例子,socks4则直接将数字5去掉即可。

iptables全局代理

安装

redsocks

首先执行这些命令

sudo apt-get install iptables git-core libevent libevent-dev
git clone http://github.com/darkk/redsocks.git
cd redsocks/
make
echo 'base{log_debug = on; log_info = on; log = "file:/tmp/reddi.log";
       daemon = on; redirector = iptables;}
       redsocks { local_ip = 127.0.0.1; local_port = 12345; ip = 127.0.0.1;
       port = 1080; type = socks5; }' > redsocks.conf

大概流程是先克隆redsocks这个项目,然后编译,添加配置文件。
配置文件中local_ip和local_port表示redsocks这个软件将要监听的地址和端口;后面ip和port表示代理服务器的地址和端口。
然后type还有这几种类型 socks4, socks5, http-connect, http-relay,具体用法可以参考官网github

iptables

接下来配置iptables防火墙相关的。
直接创建一个脚本文件,方便运行,这里以proxy_iptables_start.sh为例:

#!/bin/bash
# Create new chain
iptables -t nat -N REDSOCKS
  
# Ignore LANs and some other reserved addresses.
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
  
# Anything else should be redirected to port 12345
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345
 
iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDSOCKS
iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDSOCKS

首先就是防火墙nat表中新建一个名为REDSOCKS的chain,然后忽略一些局域网地址的转发, 接着就是设置转发规则了,即转发80和443端口的流量。

这样的配置基本就可用了。不过这里我按照官网的另一种配置方法

iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner root -j REDSOCKS

即直接转发root用户发起的所有数据包,而不是转发相应的端口流量,却并不能起到作用!如有大神,还望指导指导!

运行

还是在之前的redoscks目录下,

./redsocks -c redsocks.conf

注:若redsocks没有相应执行权限时,先 chmod +x redsocks 给予执行权限。
这样redsocks已经开启在端口12345的监听了。
然后运行前面那个iptables配置的脚本文件

./proxy_iptables_start.sh

执行完毕后,不出意外的话,打开浏览器,不设置代理或直接设置直连

然后百度ip就可以看到ip也变成了代理ip。

这里根据代理ip而定!

停止

停止运行的话,也建议创建一个脚本文件proxy_iptables_stop.sh

iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
killall redsocks

然后./proxy_iptables_stop.sh执行该脚本文件即可停止。

 

关于 Clash 透明代理涉及 iptables 的一些问题

发布于 2021-03-15 22:13:40

clash 架设成功,但是在 iptables 表出了点小问题,我用这个表可以成功透明代理

##### TCP #####

# Bypass private IP address ranges
iptables -t nat -N CLASH
iptables -t nat -A CLASH -d 0.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 10.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 127.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 169.254.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 172.16.0.0/12 -j RETURN
iptables -t nat -A CLASH -d 192.168.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 224.0.0.0/4 -j RETURN
iptables -t nat -A CLASH -d 240.0.0.0/4 -j RETURN

# Redirect all TCP traffic to redir port, where Clash listens
iptables -t nat -A CLASH -p tcp -j REDIRECT --to-ports 7892
iptables -t nat -A PREROUTING -p tcp -j CLASH

##### UDP #####

# IP rules
ip rule add fwmark 1 table 100
ip route add local default dev lo table 100

# Bypass private IP address ranges
iptables -t mangle -N CLASH
iptables -t mangle -A CLASH -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A CLASH -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A CLASH -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A CLASH -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A CLASH -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A CLASH -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A CLASH -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A CLASH -d 240.0.0.0/4 -j RETURN

# Redirect
iptables -t mangle -A CLASH -p udp -j TPROXY --on-port 7892 --tproxy-mark 1
iptables -t mangle -A PREROUTING -p udp -j CLASH

##### DNS #####

# Redirect 53 to 5353 
iptables -t nat -I PREROUTING -p udp --dport 53 -d 192.168.0.0/16 -j REDIRECT --to 1053

但是用这个表就不成功

#tcp
iptables -t nat -N clash
iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
iptables -t nat -A clash -p tcp -j REDIRECT --to-port "7892"
iptables -t nat -A PREROUTING -p tcp -j clash

#udp
ip rule add fwmark 1 table 100
ip route add local default dev lo table 100
iptables -t mangle -N clash
iptables -t mangle -A clash -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A clash -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A clash -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A clash -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A clash -d 240.0.0.0/4 -j RETURN
iptables -t mangle -A clash -p udp -j TPROXY --on-port "7892" --tproxy-mark 1
iptables -t mangle -A PREROUTING -p udp -j clash

iptables -t nat -N CLASH_DNS
iptables -t nat -F CLASH_DNS 
iptables -t nat -A CLASH_DNS -p udp -j REDIRECT --to-port 1053
iptables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS
iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053

有没有熟悉 iptables 的大佬来看看,我感觉第一个比较粗暴,把 192.168.0.0/16 的 53 口 udp 直接转发去 1053,第二个比较优雅,新建了一个 nat 表 CLASH_DNS,但是却不能运行?

 

 

https://www.nuomiphp.com/t/60e1b67e04ee6e6f121527af.html

posted @ 2023-10-24 09:34  技术颜良  阅读(736)  评论(0编辑  收藏  举报