This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_routes
executable file
·113 lines (101 loc) · 4 KB
/
split_routes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Check if at least two arguments are provided (action + at least one domain)
if [ $# -lt 2 ]; then
if [ $# -eq 1 ]; then
ARGUMENTS="argument"
else
ARGUMENTS="arguments"
fi
echo "$# $ARGUMENTS found, at least 2 arguments are required"
echo "Usage: $0 <action> <table> <pref> <domain1> [domain2] [...]"
echo "action: add, del, refresh"
echo "Examples: "
echo "$0 refresh someroutename 60 plex.tv app.plex.tv"
echo "$0 add someroutename 60 plex.tv app.plex.tv"
echo "$0 del someroutename"
exit 1
fi
# Configuration variables
ARGUMENTS=$# # Reuse arguments variable to store num arguments
ACTION=$1 # Action to perform
TABLE=$2 # Table to route through
PREF=$3 # Preference for the rules
GATEWAY="192.168.0.1" # Gateway to route through
LOCAL_NETWORK="192.168.0.0/24" # Local network to not mark
INTERFACE="eth0" # Interface to route through
PORT=32400 # Service port
shift 3 # Remove the handled arguments, so now it should just be domains left
DOMAINS=("$@") # Remaining arguments are treated as domains
# Function to clear existing rules and routes for the table
clearRulesAndRoutes() {
echo "Clearing existing rules and routes for table $TABLE..."
sudo iptables -t mangle -D PREROUTING -p tcp --dport $PORT -j MARK --set-mark 1
sudo iptables -t mangle -D OUTPUT -p tcp --sport $PORT ! -d $LOCAL_NETWORK -j MARK --set-mark 1
sudo /sbin/ip rule del fwmark 1 table $TABLE
# Find rules associated with the 'plexroute' table and delete them
/sbin/ip rule show | grep "lookup $TABLE" | while read -r rule; do
# Extract the from and to parts
from=$(echo $rule | grep -oP 'from \S+')
to=$(echo $rule | grep -oP 'to \S+')
# Delete the rule, using the extracted parts for specificity
sudo /sbin/ip rule del $from $to table $TABLE
done
# Clear routes from the table
/sbin/ip route show table $TABLE | while read -r route; do
sudo /sbin/ip route del $route table $TABLE
done
}
# Function to add routes and rules for each IP address of a domain
addRoutesAndRules() {
for domain in "${DOMAINS[@]}"; do
echo "Processing domain: $domain"
# Get IP addresses for the domain
IPs=$(dig +short $domain)
if [ -z "$IPs" ]; then # Check if IPs is empty
echo "No IPs found for domain $domain. Skipping..."
continue # Skip to the next domain
fi
sudo /sbin/ip route add default via $GATEWAY dev $INTERFACE table $TABLE
# Add routes and rules for each IP address
for ip in $IPs; do
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then # Simple check for IPv4 address
echo "Adding route and rule for $ip..."
sudo /sbin/ip rule add to $ip pref $PREF table $TABLE
else
echo "Skipping non-IPv4 address: $ip"
fi
done
done
echo Marking service traffic to avoid it being routed through the vpn
sudo iptables -t mangle -A PREROUTING -p tcp --dport $PORT -j MARK --set-mark 1
sudo iptables -t mangle -A OUTPUT -p tcp --sport $PORT ! -d $LOCAL_NETWORK -j MARK --set-mark 1
sudo /sbin/ip rule add fwmark 1 table $TABLE
}
# Main
case $ACTION in
add | refresh)
if [ $# -lt 1 ]; then
echo "\`$ACTION\` requires at least 4 arguments, $ARGUMENTS found"
echo "Example: $0 $ACTION someroutename 60 plex.tv app.plex.tv"
exit
fi
case $ACTION in
add)
addRoutesAndRules
;;
refresh)
clearRulesAndRoutes
addRoutesAndRules
;;
*)
echo "Something weird happened handling \`$ACTION\`"
esac
;;
del)
clearRulesAndRoutes
;;
*)
echo "Invalid action \`$ACTION\`specified. Please use 'add', 'del', or 'refresh'."
exit 1
;;
esac