# 使用 redsocks 和 iptables 配置 Linux 透明代理

# 1 配置 redsocks

1 安装 redsocks：`apt install redsocks`

2 编辑配置文件：`vim /etc/redsocks.conf`

主要编辑这些部分：

```json
local_ip = 0.0.0.0;    // 只截获本机流量可使用 127.0.0.1
local_port = 12345;

// `ip' and `port' are IP and tcp-port of proxy-server
// You can also use hostname instead of IP, only one (random)
// address of multihomed host will be used.
ip = <socks5服务器地址>;
port = <socks5服务器端口>;

// known types: socks4, socks5, http-connect, http-relay
type = socks5;

// 代理服务器不需要认证的话注释掉
// login = "<socks5服务器账号>";
// password = "<socks5服务器密码>";
```


3 重启生效：`systemctl restart redsocks`

查看监听情况： `netstat -anltp | grep 12345` 或 `lsof -i:12345`


# 2 设置 iptables 转发（全局）

**一定注意需要绕过 redsocks 到代理服务器的流量，否则就无限循环了。**


```bash
# 创建新链
iptables -t nat -N REDSOCKS

# 设置直连交付列表（不走代理，主要是私有网络部分）
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

# 将流量转交到 redsocks 对应的端口
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port=12345

# 将流量截获至 RESOCKS 链
iptables -t nat -A OUTPUT -p tcp -j REDSOCKS  # 本机流量
iptables -t nat -A PREROUTING -j REDSOCKS   # 路由流量
```

# 3 基于 CloudFlare 搭建 DNS 服务器（DoH）

DoH 等背景介绍略，感兴趣 Google/百度。

CloudFlare DoH 相关介绍：[https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/dns-over-https-client/](https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/dns-over-https-client/)

1 下载 CloudFlare 客户端：[https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/](https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/)

2 下载 deb 包 `apt` 直接安装即可。

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651252024959/9ny9p6izW.png align="left")

3 检查一下是否安装成功：`cloudflared --version`

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651252083156/TzEn55H3T.png align="left")

4 启动相关服务：`cloudflared proxy-dns --address 0.0.0.0 --port 15353`

可以使用 nohub & 或者用 screen 托管在后台。

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651252160387/SoHscAi4n.png align="left")


# 4 设置 DNS 转发（DNAT）

```bash
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 15353  # 路由流量
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 15353   # 本地流量
```

`dig` 测试一下行不行：`dig +short @127.0.0.1 [www.baidu.com](http://www.baidu.com/)`

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651252215727/A3cKMGXxv.png align="left")

注意：如果 53 端口被 systemd-resolved 占用，需先关闭这些程序。
[https://devnote.pro/posts/10000050301151](https://devnote.pro/posts/10000050301151)



# 5 更改网络 DNS 设置

略




# 6 清理

```bash
iptables -t nat -F OUTPUT
iptables -t nat -F REDSOCKS
iptables -t nat -X REDSOCKS
```
