-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshecan.sh
More file actions
189 lines (166 loc) · 4.62 KB
/
shecan.sh
File metadata and controls
189 lines (166 loc) · 4.62 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# Configuration
SERVICE_NAME="shecan-dns"
DNS_SERVERS="185.51.200.2 178.22.122.100"
CONFIG_FILE="/etc/systemd/resolved.conf"
BACKUP_FILE="/etc/systemd/resolved.conf.bak"
CUSTOM_DNS_FILE="/etc/dnsmasq.d/shecan-custom.conf"
DNSMASQ_SERVICE="dnsmasq"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}Error: Requires root. Use sudo.${NC}" >&2
exit 1
fi
}
ensure_dnsmasq_installed() {
if ! command -v dnsmasq &>/dev/null; then
echo -e "${RED}Error: 'dnsmasq' is not installed.${NC}"
echo -e "${YELLOW}Please install it using: sudo dnf install dnsmasq${NC}"
exit 1
fi
}
backup_config() {
if [ ! -f "$BACKUP_FILE" ]; then
cp "$CONFIG_FILE" "$BACKUP_FILE" 2>/dev/null || true
fi
}
restore_config() {
if [ -f "$BACKUP_FILE" ]; then
cp "$BACKUP_FILE" "$CONFIG_FILE"
else
cat <<EOF > "$CONFIG_FILE"
[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=
#MulticastDNS=
#DNSSEC=
#DNSOverTLS=
#Cache=
#DNSStubListener=
EOF
fi
}
start() {
check_root
backup_config
echo "[Resolve]" > "$CONFIG_FILE"
echo "DNS=$DNS_SERVERS" >> "$CONFIG_FILE"
echo "FallbackDNS=1.1.1.1 8.8.8.8" >> "$CONFIG_FILE"
echo "Domains=~." >> "$CONFIG_FILE"
echo "DNSOverTLS=no" >> "$CONFIG_FILE"
systemctl restart systemd-resolved
echo -e "${GREEN}Shecan DNS has been started with servers: $DNS_SERVERS${NC}"
}
stop() {
check_root
restore_config
systemctl restart systemd-resolved
echo -e "${YELLOW}Shecan DNS has been stopped. Original DNS settings restored.${NC}"
}
status() {
current_dns=$(grep -E '^DNS=' "$CONFIG_FILE" 2>/dev/null || echo "DNS=Not configured")
current_doh=$(grep -E '^DNSOverTLS=' "$CONFIG_FILE" 2>/dev/null || echo "DNSOverTLS=Not configured")
echo -e "Current DNS Configuration:"
echo -e " ${current_dns}"
echo -e " ${current_doh}"
if [[ "$current_dns" == *"185.51.200.2"* ]] && [[ "$current_dns" == *"178.22.122.100"* ]]; then
echo -e "${GREEN}Status: Shecan DNS is active${NC}"
else
echo -e "${YELLOW}Status: Shecan DNS is not active${NC}"
fi
}
custom_add() {
check_root
ensure_dnsmasq_installed
domain="$1"
if [ -z "$domain" ]; then
echo -e "${RED}Error: No domain provided.${NC}"
exit 1
fi
mkdir -p "$(dirname "$CUSTOM_DNS_FILE")"
echo "server=/$domain/185.51.200.2" >> "$CUSTOM_DNS_FILE"
echo "server=/$domain/178.22.122.100" >> "$CUSTOM_DNS_FILE"
systemctl restart $DNSMASQ_SERVICE
echo -e "${GREEN}Custom DNS rule added for domain: $domain${NC}"
}
custom_remove() {
check_root
ensure_dnsmasq_installed
domain="$1"
if [ -z "$domain" ]; then
echo -e "${RED}Error: No domain provided.${NC}"
exit 1
fi
sed -i "/$domain/d" "$CUSTOM_DNS_FILE"
systemctl restart $DNSMASQ_SERVICE
echo -e "${YELLOW}Custom DNS rule removed for domain: $domain${NC}"
}
custom_list() {
if [ -f "$CUSTOM_DNS_FILE" ]; then
echo -e "${GREEN}Custom DNS entries:${NC}"
cat "$CUSTOM_DNS_FILE"
else
echo -e "${YELLOW}No custom DNS entries found.${NC}"
fi
}
show_help() {
echo -e "${GREEN}Shecan DNS Manager${NC}"
echo "Usage: shecan-dns <command> [options]"
echo
echo "Commands:"
echo " start - Enable Shecan DNS servers"
echo " stop - Disable Shecan DNS and restore original settings"
echo " status - Show current DNS configuration status"
echo " custom add <url> - Add custom domain to resolve via Shecan DNS (uses dnsmasq)"
echo " custom remove <url>- Remove custom domain rule"
echo " custom list - List all custom DNS domains"
echo " help - Show this help message"
echo
echo "Note: Custom DNS needs 'dnsmasq'. Run 'sudo dnf install dnsmasq' if missing."
}
# Main dispatcher
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
custom)
case "$2" in
add)
custom_add "$3"
;;
remove)
custom_remove "$3"
;;
list)
custom_list
;;
*)
echo -e "${RED}Unknown subcommand for custom: '$2'${NC}"
show_help
exit 1
;;
esac
;;
help|--help|-h|"")
show_help
;;
*)
echo -e "${RED}Unknown command: '$1'${NC}"
show_help
exit 1
;;
esac
exit 0