proxy iptables

  • dnsmasq 配置

    https://github.com/cokebar/gfwlist2dnsmasq
    通过 dnsmasq 将dns请求转发到程序中,dnsmasq_gfwlist.conf 里的端口和程序监听端口保持一致

    vi /etc/dnsmasq.conf
    增加一行: 
    conf-dir=/etc/dnsmasq.d
    
    wget https://raw.githubusercontent.com/cokebar/gfwlist2dnsmasq/master/gfwlist2dnsmasq.sh
    chmod +x gfwlist2dnsmasq.sh
    ./gfwlist2dnsmasq.sh -p 20053 -o ./dnsmasq_gfwlist.conf
    cp ./dnsmasq_gfwlist.conf /etc/dnsmasq.d
    
    重启路由器
    
  • iptables 转发

    iptables -t nat -N SOCKS5
    
    # 直连服务器ip
    iptables -t nat -A SOCKS5 -d $serverip -j RETURN
    # 如果配置了 dnsmasq 则不需要转发dns
    #iptables -t nat -A SOCKS5 -p udp --dport 53 -j REDIRECT --to-port=$localDns
    
    # 内网地址
    iptables -t nat -A SOCKS5 -d 0.0.0.0/8 -j RETURN
    iptables -t nat -A SOCKS5 -d 10.0.0.0/8 -j RETURN
    iptables -t nat -A SOCKS5 -d 127.0.0.0/8 -j RETURN
    iptables -t nat -A SOCKS5 -d 169.254.0.0/16 -j RETURN
    iptables -t nat -A SOCKS5 -d 172.16.0.0/12 -j RETURN
    iptables -t nat -A SOCKS5 -d 192.168.0.0/16 -j RETURN
    iptables -t nat -A SOCKS5 -d 224.0.0.0/4 -j RETURN
    iptables -t nat -A SOCKS5 -d 240.0.0.0/4 -j RETURN
    
    iptables -t nat -A SOCKS5 -p tcp -j REDIRECT --to-port=$localport
    
    # 转发
    iptables -t nat -A PREROUTING -j SOCKS5 
    
    # 国内ip 本机出去需要走 OUTPUT 
    #iptables -t nat -A OUTPUT -d 114.114.114.114 -j RETURN
    #iptables -t nat -A OUTPUT -p all -m set --match-set china dst -j RETURN
    #iptables -t nat -A OUTPUT -j SOCKS5
    
    ##### 停止 #####
    iptables -t nat -D PREROUTING -j SOCKS5
    iptables -t nat -D OUTPUT -d 114.114.114.114 -j RETURN
    iptables -t nat -D OUTPUT -p all -m set --match-set china dst -j RETURN
    iptables -t nat -D OUTPUT -j SOCKS5 
    iptables -t nat -F SOCKS5
    
  • 分流
    ss-redir+iptables+ipset+dnsmasq 配置记录 | 玛修·基列莱特