forked from ReturnFI/Hysteria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.sh
158 lines (123 loc) · 4.83 KB
/
upgrade.sh
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
#!/bin/bash
cd /root/
TEMP_DIR=$(mktemp -d)
FILES=(
"/etc/hysteria/ca.key"
"/etc/hysteria/ca.crt"
"/etc/hysteria/users.json"
"/etc/hysteria/config.json"
"/etc/hysteria/.configs.env"
"/etc/hysteria/core/scripts/telegrambot/.env"
"/etc/hysteria/core/scripts/singbox/.env"
"/etc/hysteria/core/scripts/normalsub/.env"
"/etc/hysteria/core/scripts/webpanel/.env"
"/etc/hysteria/core/scripts/webpanel/Caddyfile"
)
echo "Backing up and stopping all cron jobs"
crontab -l > /tmp/crontab_backup
crontab -r
echo "Backing up files to $TEMP_DIR"
for FILE in "${FILES[@]}"; do
mkdir -p "$TEMP_DIR/$(dirname "$FILE")"
cp "$FILE" "$TEMP_DIR/$FILE"
done
echo "Checking and renaming old systemd service files"
declare -A SERVICE_MAP=(
["/etc/systemd/system/hysteria-bot.service"]="hysteria-telegram-bot.service"
["/etc/systemd/system/singbox.service"]="hysteria-singbox.service"
["/etc/systemd/system/normalsub.service"]="hysteria-normal-sub.service"
)
for OLD_SERVICE in "${!SERVICE_MAP[@]}"; do
NEW_SERVICE="/etc/systemd/system/${SERVICE_MAP[$OLD_SERVICE]}"
if [[ -f "$OLD_SERVICE" ]]; then
echo "Stopping old service: $(basename "$OLD_SERVICE")"
systemctl stop "$(basename "$OLD_SERVICE")" 2>/dev/null
echo "Renaming $OLD_SERVICE to $NEW_SERVICE"
mv "$OLD_SERVICE" "$NEW_SERVICE"
echo "Reloading systemd daemon"
systemctl daemon-reload
fi
done
echo "Removing /etc/hysteria directory"
rm -rf /etc/hysteria/
echo "Cloning Hysteria2 repository"
git clone https://github.com/ReturnFI/Hysteria2 /etc/hysteria
echo "Downloading geosite.dat and geoip.dat"
wget -O /etc/hysteria/geosite.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat >/dev/null 2>&1
wget -O /etc/hysteria/geoip.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geoip.dat >/dev/null 2>&1
echo "Restoring backup files"
for FILE in "${FILES[@]}"; do
cp "$TEMP_DIR/$FILE" "$FILE"
done
CADDYFILE="/etc/hysteria/core/scripts/webpanel/Caddyfile"
if [ -f "$CADDYFILE" ]; then
echo "Updating Caddyfile port from 8080 to 28260"
sed -i 's/\(:[[:space:]]*\)8080/\128260/g' "$CADDYFILE"
sed -i 's/0\.0\.0\.0:8080/0.0.0.0:28260/g' "$CADDYFILE"
sed -i 's/127\.0\.0\.1:8080/127.0.0.1:28260/g' "$CADDYFILE"
if ! grep -q ':28260' "$CADDYFILE"; then
echo "Warning: Caddyfile does not contain port 8080 in expected formats. Port replacement may have already been done."
fi
else
echo "Error: Caddyfile not found at $CADDYFILE. Cannot update port."
fi
CONFIG_ENV="/etc/hysteria/.configs.env"
if [ ! -f "$CONFIG_ENV" ]; then
echo ".configs.env not found, creating it with default values."
echo "SNI=bts.com" > "$CONFIG_ENV"
fi
export $(grep -v '^#' "$CONFIG_ENV" | xargs 2>/dev/null)
if [[ -z "$IP4" ]]; then
echo "IP4 not found, fetching from ip.gs..."
IP4=$(curl -s -4 ip.gs || echo "")
echo "IP4=${IP4:-}" >> "$CONFIG_ENV"
fi
if [[ -z "$IP6" ]]; then
echo "IP6 not found, fetching from ip.gs..."
IP6=$(curl -s -6 ip.gs || echo "")
echo "IP6=${IP6:-}" >> "$CONFIG_ENV"
fi
NORMALSUB_ENV="/etc/hysteria/core/scripts/normalsub/.env"
if [[ -f "$NORMALSUB_ENV" ]]; then
echo "Checking if SUBPATH exists in $NORMALSUB_ENV..."
if ! grep -q '^SUBPATH=' "$NORMALSUB_ENV"; then
echo "SUBPATH not found, generating a new one..."
SUBPATH=$(pwgen -s 32 1)
echo -e "\nSUBPATH=$SUBPATH" >> "$NORMALSUB_ENV"
else
echo "SUBPATH already exists, no changes made."
fi
else
echo "$NORMALSUB_ENV not found. Skipping SUBPATH check."
fi
echo "Setting ownership and permissions"
chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt
chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt
chown -R hysteria:hysteria /etc/hysteria/core/scripts/singbox
chown -R hysteria:hysteria /etc/hysteria/core/scripts/telegrambot
echo "Setting execute permissions for user.sh and kick.sh"
chmod +x /etc/hysteria/core/scripts/hysteria2/user.sh
chmod +x /etc/hysteria/core/scripts/hysteria2/kick.sh
cd /etc/hysteria
python3 -m venv hysteria2_venv
source /etc/hysteria/hysteria2_venv/bin/activate
pip install -r requirements.txt
echo "Restarting hysteria-caddy service"
systemctl restart hysteria-caddy.service
echo "Restarting other hysteria services"
systemctl restart hysteria-server.service
systemctl restart hysteria-telegram-bot.service
systemctl restart hysteria-singbox.service
systemctl restart hysteria-normal-sub.service
systemctl restart hysteria-webpanel.service
echo "Checking hysteria-server.service status"
if systemctl is-active --quiet hysteria-server.service; then
echo "Upgrade completed successfully"
else
echo "Upgrade failed: hysteria-server.service is not active"
fi
echo "Restoring cron jobs"
crontab /tmp/crontab_backup
rm /tmp/crontab_backup
chmod +x menu.sh
./menu.sh