Skip to content

Instantly share code, notes, and snippets.

@nukdokplex
Created October 9, 2022 04:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nukdokplex/884c7d2a213f5a10344738184c6816b2 to your computer and use it in GitHub Desktop.
Save nukdokplex/884c7d2a213f5a10344738184c6816b2 to your computer and use it in GitHub Desktop.
Selective routing to WireGuard on Keenetic
#!/bin/sh
# /opt/etc/ndm/fs.d/100_hirkn-ipsets.sh
[ "$1" != "start" ] && exit 0
echo " --- HIRKN ENTRY POINT --- "
RKN_SET_FILE="/opt/root/rkn.lst"
GOOGLE_SET_FILE="/opt/root/google.lst"
CUSTOM_SET_FILE="/opt/root/custom.lst"
function create_ipset()
{
IPSET_NAME=$1
ipset create $IPSET_NAME hash:net family inet -!
}
function fill_ipset()
{
FILE="$1"
IPSET_NAME="$2"
data=$(cat $FILE)
for row_data in $data
do
ipset add $IPSET_NAME ${row_data} -!
done
}
if [ -z "$(ip route list table 1000)" ]; then
ip rule add fwmark 0x1000 table 1000 priority 1000
ip route add table 1000 default dev nwg0
ip route add table 1000 192.168.1.0/24 via 192.168.1.1 dev br0
fi
create_ipset "HIRKN"
create_ipset "HIGOOGLE"
create_ipset "HICUSTOM"
fill_ipset $RKN_SET_FILE "HIRKN" &
fill_ipset $GOOGLE_SET_FILE "HIGOOGLE" &
fill_ipset $CUSTOM_SET_FILE "HICUSTOM" &
exit 0
#!/bin/sh
#/opt/etc/ndm/netfilter.d/99_hirkn-fwmarks.sh
[ "$type" != "iptables" ] && exit 0
[ "$table" != "mangle" ] && exit 0
echo "HIRKN: Creating $type rule on table $table !"
function create_rule()
{
IPSET=$1
echo HIRKN: Creating rule for $IPSET ipset!
if [ -z "$(iptables-save | grep $IPSET)" ]; then
iptables -t mangle -A PREROUTING -m conntrack --ctstate NEW -m set --match-set $IPSET dst -j CONNMARK --set-mark 0x1000
iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
else
echo Rule for $IPSET already exists!
fi
}
create_rule HIRKN
create_rule HIGOOGLE
create_rule HICUSTOM
iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment