-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinstall.sh
More file actions
357 lines (306 loc) · 10.6 KB
/
Copy pathinstall.sh
File metadata and controls
357 lines (306 loc) · 10.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# ----------------- Colors -----------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# ----------------- Constants -----------------
NETPLAN_FILE="/etc/netplan/dev-ir.yaml"
SERVICE_NAME="gvtunnel-connector.service"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}"
CONNECTOR_SCRIPT="/root/connector.sh"
# ----------------- Root check -----------------
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}Fatal error:${NC} Please run this script with root privilege.\n"
exit 1
fi
# ----------------- Helpers -----------------
install_jq() {
if command -v jq &> /dev/null; then
return
fi
if command -v apt-get &> /dev/null; then
echo -e "${YELLOW}jq is not installed. Installing...${NC}"
sleep 1
apt-get update
apt-get install -y jq
else
echo -e "${RED}Error:${NC} Unsupported package manager. Please install jq manually.\n"
read -rp "Press any key to exit..."
exit 1
fi
}
netplan_setup() {
if command -v netplan &> /dev/null; then
return
fi
if command -v apt-get &> /dev/null; then
echo -e "${YELLOW}netplan is not installed. Installing...${NC}"
apt-get update
apt-get install -y netplan.io && echo "netplan installed successfully." || echo "netplan installation failed."
else
echo -e "${RED}Error:${NC} netplan is not installed and automatic installation is only supported with apt-get.\n"
exit 1
fi
}
check_core_status() {
if [ -f "$NETPLAN_FILE" ]; then
echo "Installed"
else
echo "Not installed"
fi
}
# Generate random ULA IPv6 prefix like fd16:a803:2234
generate_ipv6_prefix() {
local part1 part2 part3
part1=$(printf 'fd%02x' $((RANDOM % 256)))
part2=$(printf '%04x' $((RANDOM % 65536)))
part3=$(printf '%04x' $((RANDOM % 65536)))
echo "${part1}:${part2}:${part3}"
}
get_tunnel_ipv6() {
local ipv6=""
if [ -f "$NETPLAN_FILE" ]; then
ipv6=$(grep -E '^[[:space:]]*- .*::[0-9]+/64' "$NETPLAN_FILE" 2>/dev/null | awk '{print $2}' | cut -d'/' -f1)
fi
echo "$ipv6"
}
create_service() {
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=GV Tunnel IPv6 Connector
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/bin/bash $CONNECTOR_SCRIPT
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME"
}
remove_service() {
if systemctl is-enabled "$SERVICE_NAME" &> /dev/null; then
systemctl disable --now "$SERVICE_NAME"
else
systemctl stop "$SERVICE_NAME" &> /dev/null || true
fi
[ -f "$SERVICE_FILE" ] && rm -f "$SERVICE_FILE"
systemctl daemon-reload
}
ensure_systemd_networkd() {
# اگر اصلاً یونیت تعریف نشده باشه، بیخیال میشیم
if ! systemctl list-unit-files | grep -q '^systemd-networkd.service'; then
echo -e "${YELLOW}systemd-networkd.service not found. Skipping networkd check.${NC}"
return
fi
echo -e "${YELLOW}Ensuring systemd-networkd is unmasked and enabled...${NC}"
systemctl unmask systemd-networkd.service 2>/dev/null || true
systemctl enable --now systemd-networkd.service 2>/dev/null || {
echo -e "${RED}Failed to enable/start systemd-networkd.${NC}"
return 1
}
echo -e "${GREEN}systemd-networkd is active and enabled.${NC}"
}
apply_netplan_safe() {
netplan_setup
ensure_systemd_networkd
echo -e "${YELLOW}Applying netplan...${NC}"
if ! netplan apply 2>/tmp/netplan_error.log; then
echo -e "${RED}netplan apply failed!${NC}"
echo "------ netplan error output ------"
cat /tmp/netplan_error.log
echo "----------------------------------"
read -rp "Press Enter to return to menu..."
return 1
fi
}
gv_menu() {
clear
local SERVER_IP SERVER_COUNTRY SERVER_ISP IP_INFO
SERVER_IP=$(hostname -I | awk '{print $1}')
SERVER_COUNTRY="Unknown"
SERVER_ISP="Unknown"
if command -v curl &> /dev/null && command -v jq &> /dev/null; then
IP_INFO=$(curl -sS "http://ip-api.com/json/$SERVER_IP" || true)
if [ -n "$IP_INFO" ]; then
SERVER_COUNTRY=$(echo "$IP_INFO" | jq -r '.country // "Unknown"')
SERVER_ISP=$(echo "$IP_INFO" | jq -r '.isp // "Unknown"')
fi
fi
local GV_CORE TUN_IPV6 core_color
GV_CORE=$(check_core_status)
TUN_IPV6=$(get_tunnel_ipv6)
if [ "$GV_CORE" = "Installed" ]; then
core_color="$GREEN"
else
core_color="$RED"
fi
echo "+-------------------------------------------------------------------------+"
echo "| |"
echo "| ██████ ██ ██ ████████ ██ ██ ███ ██ ███ ██ ███████ ██ |"
echo "| ██ ██ ██ ██ ██ ██ ████ ██ ████ ██ ██ ██ |"
echo "| ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████ ██ |"
echo "| ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |"
echo "| ██████ ████ ██ ██████ ██ ████ ██ ████ ███████ ███████ |"
echo "| version : 2.6.1 |"
echo "+-------------------------------------------------------------------------+"
echo -e "|${GREEN}Server Country |${NC} $SERVER_COUNTRY"
echo -e "|${GREEN}Server ISP |${NC} $SERVER_ISP"
echo -e "|${GREEN}Server IP |${NC} $SERVER_IP"
if [ -n "$TUN_IPV6" ]; then
echo -e "|${GREEN}Tunnel IPv6 |${NC} $TUN_IPV6"
fi
echo -e "|${GREEN}Server Tunnel |${NC} ${core_color}${GV_CORE}${NC}"
echo "+-------------------------------------------------------------------------+"
echo -e "|${YELLOW}Please choose an option:${NC}"
echo "+-------------------------------------------------------------------------+"
echo -e "$1"
echo "+-------------------------------------------------------------------------+"
echo -e "${NC}"
}
check_service_status() {
gv_menu "| 3 - Check Service Status\n"
echo "----- $SERVICE_NAME status -----"
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN}Service is ACTIVE${NC}"
else
echo -e "${RED}Service is INACTIVE or not installed${NC}"
fi
echo
systemctl --no-pager --full status "$SERVICE_NAME" 2>/dev/null | sed -n '1,15p'
echo
read -rp "Press Enter to go back to menu..."
}
# Unified setup for IRAN / Kharej
setup_tunnel() {
local role="$1"
local iran_ip kharej_ip ipv6_prefix
local local_ip remote_ip
local local_suffix remote_suffix
local side_label side_text other_side_text
case "$role" in
iran)
side_label="IRAN"
local_suffix="1"
remote_suffix="2"
read -rp "Enter IRAN IP : " iran_ip
read -rp "Enter Kharej IP : " kharej_ip
local_ip="$iran_ip"
remote_ip="$kharej_ip"
side_text="IRAN Tunnel IPv6"
other_side_text="Use this prefix on the KHAREJ side as well"
;;
kharej)
side_label="Kharej"
local_suffix="2"
remote_suffix="1"
read -rp "Enter IRAN IP : " iran_ip
read -rp "Enter Kharej IP : " kharej_ip
local_ip="$kharej_ip"
remote_ip="$iran_ip"
side_text="Kharej Tunnel IPv6"
other_side_text="Use the same prefix on the IRAN side"
;;
*)
echo "Unknown role: $role"
return 1
;;
esac
read -rp "Enter IPv6 Local Prefix (blank = auto): " ipv6_prefix
if [ -z "$ipv6_prefix" ]; then
ipv6_prefix=$(generate_ipv6_prefix)
echo -e "${YELLOW}Auto-generated IPv6 prefix:${NC} $ipv6_prefix"
fi
cat > "$NETPLAN_FILE" <<EOL
network:
version: 2
tunnels:
tunnel0858:
mode: sit
local: $local_ip
remote: $remote_ip
addresses:
- ${ipv6_prefix}::${local_suffix}/64
EOL
apply_netplan_safe || return 1
cat > "$CONNECTOR_SCRIPT" <<EOL
#!/bin/bash
while true; do
ping -6 -c 3 ${ipv6_prefix}::${remote_suffix}
sleep 5
done
EOL
chmod +x "$CONNECTOR_SCRIPT"
create_service
local final_ipv6="${ipv6_prefix}::${local_suffix}"
echo
echo "Your job is great..."
echo "####################################"
printf "# %-28s #\n" "$side_text :"
printf "# %s\n" " $final_ipv6"
echo "####################################"
echo
echo "$other_side_text: $ipv6_prefix"
echo
read -rp "Press Enter to return to menu..."
}
install_tunnel() {
gv_menu "| 1 - IRAN\n| 2 - Kharej\n| 0 - Back"
read -rp "Enter option number: " setup
case $setup in
1) setup_tunnel "iran" ;;
2) setup_tunnel "kharej" ;;
0) return ;;
*) echo "Not valid" ;;
esac
}
uninstall_tunnel() {
echo -e "${GREEN}Uninstalling GVTUNNEL in 3 seconds...${NC}"
sleep 1 && echo -e "${GREEN}2...${NC}"
sleep 1 && echo -e "${GREEN}1...${NC}"
sleep 1
remove_service
[ -f "$NETPLAN_FILE" ] && rm -f "$NETPLAN_FILE"
[ -f "$CONNECTOR_SCRIPT" ] && rm -f "$CONNECTOR_SCRIPT"
apply_netplan_safe || true
clear
echo 'GVTUNNEL Uninstalled :('
}
loader() {
gv_menu "| 1 - Config Tunnel\n| 2 - Uninstall\n| 3 - Check Service\n| 0 - Exit"
read -rp "Enter option number: " choice
case $choice in
1) install_tunnel ;;
2) uninstall_tunnel ;;
3) check_service_status ;;
0)
echo -e "${GREEN}Exiting program...${NC}"
exit 0
;;
*)
echo "Not valid"
;;
esac
}
init() {
install_jq
# Ensure iproute2/ip command exists
if ! command -v ip &> /dev/null; then
if command -v apt-get &> /dev/null; then
echo -e "${YELLOW}iproute2 is not installed. Installing...${NC}"
apt-get update
apt-get install -y iproute2
else
echo -e "${YELLOW}Warning:${NC} 'ip' command not found and automatic installation is only supported with apt-get. Please install iproute2 manually."
fi
fi
}
# ---- main ----
init
while true; do
loader
done