#!/usr/bin/env bash
# 针对网关设置mark
{% for gateway in gateways %}

{% if gateway.dev_or_via != "" %}

{% if gateway.isTun %}
DEV={{gateway.dev_or_via}}
IPTABLES_MARK_CONDITION="-i $DEV"
ip route del default dev $DEV table {{gateway.mark}}
{% else %}
ping {{gateway.dev_or_via}} -c -
NEIGH_LINE=$(ip neigh show {{gateway.dev_or_via}})
DEV=$(echo $NEIGH_LINE | awk '{print $3}')
MAC=$(echo $NEIGH_LINE | awk '{print $5}')
IPTABLES_MARK_CONDITION="-i $DEV -m mac --mac-source $MAC"
ip route del default via {{gateway.dev_or_via}} dev $DEV table {{gateway.mark}}
{% endif %}

bash -c "iptables -t mangle -D PREROUTING $IPTABLES_MARK_CONDITION -m set ! --match-set mycard src -j CONNMARK --set-xmark {{gateway.mark}} ; iptables -t mangle -D PREROUTING -m connmark --mark {{gateway.mark}} -j CONNMARK --restore-mark ; iptables -t mangle -D OUTPUT -m connmark --mark {{gateway.mark}} -j CONNMARK --restore-mark"

{% endif %}

ip rule del pref 301 fwmark {{gateway.mark}} lookup {{gateway.mark}}

{% endfor %}

# 临时梯子
iptables -t mangle -D PREROUTING -m set --match-set ladder_needed src -m set --match-set gfwiplist dst -j MARK --set-xmark 812/0xffffffff

# ipset
{% for list in route_list_names %}
ipset destroy {{list}}
{% endfor %}

exit 0
