@Alex-Zhao
2021-02-07T05:45:05.000000Z
字数 2693
阅读 281
网络

dnsmasq: 用于域名解析缓存和ipset转发ipset: 用于存储IP集合,可以存储IP网段或者IP地址ipset-service: 用于管理ipset的服务。iptables: 使用mangle进行流量mark。策略路由: 为指定的流量制定相关路由策略
# /etc/dnsmasq.confport=53domain-neededresolv-file=/opt/dnsmasq/resolv.dnsmasq.confstrict-order#interface=eth2no-hostscache-size=1000000log-querieslog-facility=/var/log/dnsmasq/dnsmasq.loglog-async=100conf-dir=/etc/dnsmasq.d
# /opt/dnsmasq/resolv.dnsmasq.confnameserver 114.114.114.114
// 更新DNS策略域名// 可以通过Github中的脚本更新 https://github.com/cokebar/gfwlist2dnsmasq.git// 然后通过命令行更新dnsmasq_gfwlist文件sh /opt/gfw/gfwlist2dnsmasq.sh -d 172.16.254.2 -p 53 -s gfwlist -o /etc/dnsmasq.d/dnsmasq_gfwlist.conf --extra-domain-file /tmp/domain.list// 解释:// -d:指定DNS解析转交的上级DNS// -p: 上级DNS端口// -s: 本地的ipset的名称// -o: 生成的文件绝对路径// --extra-domain-file:附增的本地外部域名文件,一行一条域名记录
# /etc/sysconfig/ipset-configIPSET_SAVE_ON_STOP="yes"
ipset create gfwlist hash:net# 解释:# method: bitmap, hash, list, datatype: ip, net, mac, port, iface# ipset的名称叫gfwlist,后边的iptables和策略路由会使用这个名称# 这里使用hash:net格式,hash是因为速度会快一些切长度会长。net是因为可以兼容网段和IP地址。
由于ipset是在内存中进行缓存的ip集合,并不会文件保存,当服务器关闭或者重启,ipset缓存的ip集合会消失。使用我们使用ipset-service来进行维护。每次ipset.service启动时会自动加载/etc/sysconfig/ipset.d/*.set文件中的配置和集合信息,每次服务停止,也会将ipset的集合导出到文件中。
systemctl start ipset.servicesystemctl enable ipset.service
systemct stop firewalldsystemctl disable firewalldsystemctl enable iptablessystemctl start iptables
iptables -P FORWARD DROPiptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPTiptables -A INPUT -p icmp -j ACCEPTiptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -p udp -m udp --dport 53 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPTiptables -A INPUT -j REJECT --reject-with icmp-host-prohibitediptables -P INPUT DROPiptables -A FORWARD -i eth2 -j ACCEPTiptables -t mangle -A PREROUTING -m set --match-set gfwlist dst -j MARK --set-xmark 77iptables -t mangle -A PREROUTING -m mark --mark 77 -j ACCEPT -t mangle //此条感觉加不加并没有啥作用,目的是所有打77标记的流量放行
ip route add table gfwtable default via 172.16.254.2 dev bond4//创建gfwtable路由表,默认路由从bond4设备出,下一跳地址是172.16.254.2ip rule add fwmark 77 table gfwtable//mark 77的流量走gfwtable路由表
echo "77 gfwtable" >> /etc/iproute2/rt_tables# /etc/rc.localip route flush table gfwtableip route add table gfwtable default via 172.16.254.2 dev bond4ip rule add fwmark 77 table gfwtable
注意把文件修改可执行。不然不会执行
PS: chmod +x /etc/rc.d/rc.local