透明代理配置
1. 只有單個代理
V2ray 配置
{
"port": 12345, //开放的端口号
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true // 这里要为 true 才能接受来自 iptables 的流量
},
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}
在出站连接配置中的streamSettings加入
"sockopt": {
"mark": 2
}
在出站连接上标记 SO_MARK。(這邊建議不要用255,在我的環境下使用255會出問題)
- 仅适用于 Linux 系统。
- 需要 CAP_NET_ADMIN 权限。
系统设置
1. 启用包转发
临时启用包转发:
sudo sysctl net.ipv4.ip_forward=1
编辑/etc/sysctl.d/30-ipforward.conf使包转发可以永久地应用于所有接口上:
net.ipv4.ip_forward=1
net.ipv6.conf.default.forwarding=1
net.ipv6.conf.all.forwarding=1
2. 加载TPROXY模块
临时加载TPROXY模块:
sudo modprobe xt_TPROXY
编辑/etc/modules-load.d/TPROXY.conf 使自动加载TPROXY模块:
xt_TPROXY
3. ipset 設置
添加保留地址
ipset -N reservedip nethash
ipset add reservedip 224.0.0.0/4
ipset add reservedip 100.64.0.0/10
ipset add reservedip 203.0.113.0/24
ipset add reservedip 192.18.0.0/15
ipset add reservedip 192.88.99.0/24
ipset add reservedip 172.16.0.0/12
ipset add reservedip 0.0.0.0/8
ipset add reservedip 127.0.0.0/8
ipset add reservedip 192.0.2.0/24
ipset add reservedip 192.51.100.0/24
ipset add reservedip 10.0.0.0/8
ipset add reservedip 192.168.0.0/16
ipset add reservedip 255.255.255.255
ipset add reservedip 192.0.0.0/24
ipset add reservedip 240.0.0.0/4
ipset add reservedip 169.254.0.0/16
添加不需要代理的地址
ipset -N noproxy nethash
ipset add noproxy <伺服器地址>
4. iptables 設置
-
tcp
#/bin/bash iptables -t nat -N SS_SPEC_WAN_FW iptables -t nat -A SS_SPEC_WAN_FW -p tcp -j REDIRECT --to-ports 12345 iptables -t nat -N SS_SPEC_WAN_AC iptables -t nat -A SS_SPEC_WAN_AC -p tcp -j RETURN -m mark --mark 2 iptables -t nat -A SS_SPEC_WAN_AC -m set --match-set reservedip dst -j RETURN iptables -t nat -A SS_SPEC_WAN_AC -m set --match-set noproxy dst -j RETURN iptables -t nat -A SS_SPEC_WAN_AC -j SS_SPEC_WAN_FW
-
udp
#/bin/sh ip rule add fwmark 0x01/0x01 table 100 ip route add local 0.0.0.0/0 dev lo table 100 iptables -t mangle -N V2RAY iptables -t mangle -N V2RAY_MARK iptables -t mangle -A V2RAY -m set --match-set reservedip dst -j RETURN iptables -t mangle -A V2RAY -m set --match-set noproxy dst -j RETURN iptables -t mangle -A V2RAY -j RETURN -m mark --mark 2 iptables -t mangle -A V2RAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 0x01/0x01 iptables -t mangle -A V2RAY_MARK -m set --match-set reservedip dst -j RETURN iptables -t mangle -A V2RAY_MARK -m set --match-set noproxy dst -j RETURN iptables -t mangle -A V2RAY_MARK -j RETURN -m mark --mark 2 iptables -t mangle -A V2RAY_MARK -p udp -j MARK --set-mark 1
5. 启用iptables规则
tcp
iptables -t nat -I PREROUTING 1 -p tcp -j SS_SPEC_WAN_AC iptables -t nat -I OUTPUT 1 -p tcp -j SS_SPEC_WAN_AC #對本機代理
udp
iptables -t mangle -A PREROUTING -j V2RAY iptables -t mangle -A OUTPUT -j V2RAY_MARK
6. 开机自动应用iptables规则
记录iptables规则:
iptables-save > /etc/iptables/iptables.rules
设置开机自动应用:
systemctl enable iptables
2. 有多個代理,需要代理轉發
代理配置
此處以兩個代理為例子,使用此方法可以讓除最內層代理外的代理無法參看數據任何內容,起到保護隱私的作用。
shadowsocks(例子)
外層代理(比如商業提供的代理)
{
"server":"1.1.1.1",
"server_port":108080,
"local_address":"0.0.0.0",
"local_port":12346,
"password":"password",
"timeout":300,
"method":""
}
內層代理(這個建議使用自建的代理)
{
"server":"2.2.2.2",
"server_port":108080,
"local_address":"0.0.0.0",
"local_port":12345,
"password":"password",
"timeout":300,
"method":""
}
V2Ray(例子)
應該自己會寫吧(咕咕)
系统设置
1. 启用包转发
临时启用包转发:
sudo sysctl net.ipv4.ip_forward=1
编辑/etc/sysctl.d/30-ipforward.conf使包转发可以永久地应用于所有接口上:
net.ipv4.ip_forward=1
net.ipv6.conf.default.forwarding=1
net.ipv6.conf.all.forwarding=1
2. 加载TPROXY模块
临时加载TPROXY模块:
sudo modprobe xt_TPROXY
编辑/etc/modules-load.d/TPROXY.conf 使自动加载TPROXY模块:
xt_TPROXY
3. 設置ipset
添加保留地址
ipset -N reservedip nethash
ipset add reservedip 224.0.0.0/4
ipset add reservedip 100.64.0.0/10
ipset add reservedip 203.0.113.0/24
ipset add reservedip 192.18.0.0/15
ipset add reservedip 192.88.99.0/24
ipset add reservedip 172.16.0.0/12
ipset add reservedip 0.0.0.0/8
ipset add reservedip 127.0.0.0/8
ipset add reservedip 192.0.2.0/24
ipset add reservedip 192.51.100.0/24
ipset add reservedip 10.0.0.0/8
ipset add reservedip 192.168.0.0/16
ipset add reservedip 255.255.255.255
ipset add reservedip 192.0.0.0/24
ipset add reservedip 240.0.0.0/4
ipset add reservedip 169.254.0.0/16
添加自己伺服器的地址
ipset -N myip nethash
ipset add myip 2.2.2.2
添加不需要代理的地址
ipset -N noproxy
ipset add noproxy 1.1.1.1
4. iptables 設置
-
tcp
#/bin/bash iptables -t nat -N SS_SPEC_WAN_FW iptables -t nat -A SS_SPEC_WAN_FW -m set --match-set myip dst -p tcp -j REDIRECT --to-ports 12346 #此處的2.2.2.2是上面例子中設置的內層服務器地址,12346是上面例子中外層服務器地址 iptables -t nat -A SS_SPEC_WAN_FW -m set --match-set noproxy dst -j RETURN #此1.1.1.1為最外層代理的ip iptables -t nat -A SS_SPEC_WAN_FW -p tcp -j REDIRECT --to-ports 12345 #對其餘流量轉發到最內層代理 iptables -t nat -N SS_SPEC_WAN_AC iptables -t nat -A SS_SPEC_WAN_AC -p tcp -j RETURN -m mark --mark 2 iptables -t nat -A SS_SPEC_WAN_AC -m set --match-set reservedip dst -j RETURN iptables -t nat -A SS_SPEC_WAN_AC -j SS_SPEC_WAN_FW
-
udp
#/bin/sh ip rule add fwmark 0x01/0x01 table 100 ip route add local 0.0.0.0/0 dev lo table 100 iptables -t mangle -N V2RAY iptables -t mangle -A V2RAY -p udp -m set --match-set myip dst -j TPROXY --on-port 12346 --tproxy-mark 0x01/0x01 --on-ip 0.0.0.0 iptables -t mangle -A V2RAY -j RETURN -m mark --mark 2 iptables -t mangle -A V2RAY -m set --match-set reservedip dst -j RETURN iptables -t mangle -A V2RAY -m set --match-set noproxy dst -j RETURN iptables -t mangle -A V2RAY -p udp -j TPROXY --on-port 12345 --tproxy-mark 0x01/0x01 --on-ip 0.0.0.0 iptables -t mangle -A V2RAY_MARK -m set --match-set reservedip dst -j RETURN iptables -t mangle -A V2RAY_MARK -m set --match-set noproxy dst -j RETURN iptables -t mangle -A V2RAY_MARK -m mark --mark 2 -j RETURN iptables -t mangle -A V2RAY_MARK -p udp -j MARK --set-mark 1 iptables -t mangle -A PREROUTING -j V2RAY iptables -t mangle -A OUTPUT -j V2RAY_MARK
4. 启用iptables规则
tcp
iptables -t nat -I PREROUTING 1 -p tcp -j SS_SPEC_WAN_AC iptables -t nat -I OUTPUT 1 -p tcp -j SS_SPEC_WAN_AC #對本機代理
udp
iptables -t mangle -A PREROUTING -j V2RAY iptables -t mangle -A OUTPUT -j V2RAY_MARK
5.开机自动应用iptables规则
记录iptables规则:
iptables-save > /etc/iptables/iptables.rules
设置开机自动应用:
systemctl enable iptables
参见
- ss-nat
- ArchWiki iptables
- Project V Official
Comments