This repository was archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathezg
More file actions
executable file
·85 lines (75 loc) · 2.54 KB
/
ezg
File metadata and controls
executable file
·85 lines (75 loc) · 2.54 KB
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
#!/bin/bash
update() {
if [[ $1 == "firewall" ]]; then
chmod +x configurenftables.sh
./configurenftables.sh
elif [[ $1 == "dhcp" ]]; then
chmod +x configuredhcp.sh
./configuredhcp.sh
elif [[ $1 == "dns" ]]; then
chmod +x configuredns.sh
./configuredns.sh
elif [[ $1 == "easyguard" ]]; then
chmod +x update.sh
./update.sh
else
echo "Invalid subcommand $1" >&2
exit 1
fi
}
firewall() {
if [[ $1 == "rule" ]]; then
# ex: ./ezg firewall rule wan input add tcp 80
# ex: ./ezg firewall rule wan lan add tcp 80
# second example shows "lan" as the chain to get forwarded to.
# so it wants to add a rule to forward port 80 from wan to lan.
# syntax: ./ezg firewall rule <zone> <chain (input|output|zone to forward to)> <add|remove> <protocol> <port>
zone=$2
chain=$3
action=$4
protocol=$5
port=$6
if [[ $action == "add" ]]; then
if [[ $chain == "input" || $chain == "output" ]]; then
jq -r "(.zones[] | select(.name == \"$zone\") | .[\"$chain\"].ports) += [{"protocol": \"$protocol\", "port": $port}]" firewall.json > firewall.json.tmp
mv firewall.json.tmp firewall.json
else
jq -r "(.zones[] | select(.name == \"$zone\") | .forward[] | select(.dest == \"$chain\") | .ports) += [{\"protocol\": \"$protocol\", \"port\": $port}]" firewall.json > firewall.json.tmp
mv firewall.json.tmp firewall.json
fi
update firewall
elif [[ $action == "remove" ]]; then
if [[ $chain == "input" || $chain == "output" ]]; then
jq -r "(.zones[] | select(.name == \"$zone\") | .[\"$chain\"].ports) |= map(select(.protocol != \"$protocol\" or .port != $port))" firewall.json > firewall.json.tmp
mv firewall.json.tmp firewall.json
else
jq -r "(.zones[] | select(.name == \"$zone\") | .forward[] | select(.dest == \"$chain\") | .ports) |= map(select(.protocol != \"$protocol\" or .port != $port))" firewall.json > firewall.json.tmp
mv firewall.json.tmp firewall.json
fi
update firewall
else
echo "Invalid action $action" >&2
exit 1
fi
fi
}
dns() {
if [[ $1 == "set" ]]; then
option=$2
value=$3
# set the option to the value in dns.json
jq -r --arg value $value "if \$value == \"true\" then .[\"$option\"] = true elif \$value == \"false\" then .[\"$option\"] = false else .[\"$option\"] = \$value end" dns.json > dns.json.tmp
mv dns.json.tmp dns.json
update dns
fi
}
help() {
echo EasyGuard
echo "Usage: $0 [firewall|dns|update <firewall|dhcp|dns|easyguard>|help]"
}
if [[ $1 =~ ^(update|help|firewall|dns)$ ]]; then
"$@"
else
echo "Invalid subcommand $1" >&2
exit 1
fi