-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
sing-REALITY-box.sh
384 lines (329 loc) · 10.7 KB
/
sing-REALITY-box.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# Function to print characters with delay
print_with_delay() {
text=$1
delay=$2
for ((i = 0; i < ${#text}; i++)); do
echo -n "${text:$i:1}"
sleep $delay
done
}
# Introduction animation
echo ""
echo ""
print_with_delay "s" 0.1
print_with_delay "i" 0.1
print_with_delay "n" 0.1
print_with_delay "g" 0.1
print_with_delay "-" 0.1
print_with_delay "R" 0.1
print_with_delay "E" 0.1
print_with_delay "A" 0.1
print_with_delay "L" 0.1
print_with_delay "I" 0.1
print_with_delay "T" 0.1
print_with_delay "Y" 0.1
print_with_delay "-" 0.1
print_with_delay "b" 0.1
print_with_delay "o" 0.1
print_with_delay "x" 0.1
print_with_delay " " 0.1
print_with_delay "b" 0.1
print_with_delay "y" 0.1
print_with_delay " " 0.1
print_with_delay "D" 0.1
print_with_delay "E" 0.1
print_with_delay "A" 0.1
print_with_delay "T" 0.1
print_with_delay "H" 0.1
print_with_delay "L" 0.1
print_with_delay "I" 0.1
print_with_delay "N" 0.1
print_with_delay "E" 0.1
print_with_delay " " 0.1
print_with_delay "|" 0.1
print_with_delay " " 0.1
print_with_delay "n" 0.1
print_with_delay "a" 0.1
print_with_delay "m" 0.1
print_with_delay "e" 0.1
print_with_delay "l" 0.1
print_with_delay "e" 0.1
print_with_delay "s" 0.1
print_with_delay " " 0.1
print_with_delay "g" 0.1
print_with_delay "h" 0.1
print_with_delay "o" 0.1
print_with_delay "u" 0.1
print_with_delay "l" 0.1
print_with_delay ""
echo ""
echo ""
# Check if jq is installed, and install it if not
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Installing..."
if [ -n "$(command -v apt)" ]; then
apt update
apt install -y jq
elif [ -n "$(command -v yum)" ]; then
yum install -y epel-release
yum install -y jq
elif [ -n "$(command -v dnf)" ]; then
dnf install -y jq
else
echo "Cannot install jq. Please install jq manually and rerun the script."
exit 1
fi
fi
# Check if reality.json, sing-box, and sing-box.service already exist
if [ -f "/root/reality.json" ] && [ -f "/root/sing-box" ] && [ -f "/root/public.key.b64" ] && [ -f "/etc/systemd/system/sing-box.service" ]; then
echo "Reality files already exist."
echo ""
echo "Please choose an option:"
echo ""
echo "1. Reinstall"
echo "2. Modify"
echo "3. Show Current Link"
echo "4. Uninstall"
echo ""
read -p "Enter your choice (1-4): " choice
case $choice in
1)
echo "Reinstalling..."
# Uninstall previous installation
systemctl stop sing-box
systemctl disable sing-box
rm /etc/systemd/system/sing-box.service
rm /root/reality.json
rm /root/sing-box
# Proceed with installation
;;
2)
echo "Modifying..."
# Get current listen port
current_listen_port=$(jq -r '.inbounds[0].listen_port' /root/reality.json)
# Ask for listen port
read -p "Enter desired listen port (Current port is $current_listen_port): " listen_port
listen_port=${listen_port:-$current_listen_port}
# Get current server name
current_server_name=$(jq -r '.inbounds[0].tls.server_name' /root/reality.json)
# Ask for server name (sni)
read -p "Enter server name/SNI (Current value is $current_server_name): " server_name
server_name=${server_name:-$current_server_name}
# Modify reality.json with new settings
jq --arg listen_port "$listen_port" --arg server_name "$server_name" '.inbounds[0].listen_port = ($listen_port | tonumber) | .inbounds[0].tls.server_name = $server_name | .inbounds[0].tls.reality.handshake.server = $server_name' /root/reality.json > /root/reality_modified.json
mv /root/reality_modified.json /root/reality.json
# Restart sing-box service
systemctl restart sing-box
echo ""
echo ""
echo "New Link:"
echo ""
echo ""
# Get current listen port
current_listen_port=$(jq -r '.inbounds[0].listen_port' /root/reality.json)
# Get current server name
current_server_name=$(jq -r '.inbounds[0].tls.server_name' /root/reality.json)
# Get the UUID
uuid=$(jq -r '.inbounds[0].users[0].uuid' /root/reality.json)
# Get the public key from the file, decoding it from base64
public_key=$(base64 --decode /root/public.key.b64)
# Get the short ID
short_id=$(jq -r '.inbounds[0].tls.reality.short_id[0]' /root/reality.json)
# Retrieve the server IP address
server_ip=$(curl -s https://api.ipify.org)
# Generate the link
server_link="vless://$uuid@$server_ip:$current_listen_port?encryption=none&flow=xtls-rprx-vision&security=reality&sni=$current_server_name&fp=chrome&pbk=$public_key&sid=$short_id&type=tcp&headerType=none#SING-BOX-TCP"
echo "$server_link"
echo ""
echo ""
exit 0
;;
3)
echo "Showing current link..."
# Get current listen port
current_listen_port=$(jq -r '.inbounds[0].listen_port' /root/reality.json)
# Get current server name
current_server_name=$(jq -r '.inbounds[0].tls.server_name' /root/reality.json)
# Get the UUID
uuid=$(jq -r '.inbounds[0].users[0].uuid' /root/reality.json)
# Get the public key from the file, decoding it from base64
public_key=$(base64 --decode /root/public.key.b64)
# Get the short ID
short_id=$(jq -r '.inbounds[0].tls.reality.short_id[0]' /root/reality.json)
# Retrieve the server IP address
server_ip=$(curl -s https://api.ipify.org)
# Generate the link
server_link="vless://$uuid@$server_ip:$current_listen_port?encryption=none&flow=xtls-rprx-vision&security=reality&sni=$current_server_name&fp=chrome&pbk=$public_key&sid=$short_id&type=tcp&headerType=none#SING-BOX-TCP"
echo ""
echo ""
echo "$server_link"
echo ""
echo ""
exit 0
;;
4)
echo "Uninstalling..."
# Stop and disable sing-box service
systemctl stop sing-box
systemctl disable sing-box
# Remove files
rm /etc/systemd/system/sing-box.service
rm /root/reality.json
rm /root/sing-box
rm /root/public.key.b64
echo "DONE!"
exit 0
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
fi
# Fetch the latest (including pre-releases) release version number from GitHub API
latest_version=$(curl -s "https://api.github.com/repos/SagerNet/sing-box/releases" | grep -P -m1 -o "(v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}(-beta.[0-9]{1,})?)" | tr -d 'v')
echo "Latest version: $latest_version"
# Detect server architecture
arch=$(uname -m)
echo "Architecture: $arch"
# Map architecture names
case ${arch} in
x86_64)
arch="amd64"
;;
aarch64)
arch="arm64"
;;
armv7l)
arch="armv7"
;;
esac
# Prepare package names
package_name="sing-box-${latest_version}-linux-${arch}"
# Prepare download URL
url="https://github.com/SagerNet/sing-box/releases/download/v${latest_version}/${package_name}.tar.gz"
# Download the latest release package (.tar.gz) from GitHub
curl -sLo "/root/${package_name}.tar.gz" "$url"
# Extract the package and move the binary to /root
tar -xzf "/root/${package_name}.tar.gz" -C /root
mv "/root/${package_name}/sing-box" /root/
# Cleanup the package
rm -r "/root/${package_name}.tar.gz" "/root/${package_name}"
# Set the permissions
chown root:root /root/sing-box
chmod +x /root/sing-box
# Generate key pair
echo "Generating key pair..."
key_pair=$(/root/sing-box generate reality-keypair)
echo "Key pair generation complete."
echo
# Extract private key and public key
private_key=$(echo "$key_pair" | awk '/PrivateKey/ {print $2}' | tr -d '"')
public_key=$(echo "$key_pair" | awk '/PublicKey/ {print $2}' | tr -d '"')
# Save the public key in a file using base64 encoding
echo "$public_key" | base64 > /root/public.key.b64
# Generate necessary values
uuid=$(/root/sing-box generate uuid)
short_id=$(/root/sing-box generate rand --hex 8)
# Ask for listen port
read -p "Enter desired listen port (default: 443): " listen_port
listen_port=${listen_port:-443}
echo ""
# Ask for server name (sni)
read -p "Enter server name/SNI (default: telewebion.com): " server_name
server_name=${server_name:-telewebion.com}
# Retrieve the server IP address
server_ip=$(curl -s https://api.ipify.org)
# Create reality.json using jq
jq -n --arg listen_port "$listen_port" --arg server_name "$server_name" --arg private_key "$private_key" --arg short_id "$short_id" --arg uuid "$uuid" --arg server_ip "$server_ip" '{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "vless",
"tag": "vless-in",
"listen": "::",
"listen_port": ($listen_port | tonumber),
"sniff": true,
"sniff_override_destination": true,
"domain_strategy": "ipv4_only",
"users": [
{
"uuid": $uuid,
"flow": "xtls-rprx-vision"
}
],
"tls": {
"enabled": true,
"server_name": $server_name,
"reality": {
"enabled": true,
"handshake": {
"server": $server_name,
"server_port": 443
},
"private_key": $private_key,
"short_id": [$short_id]
}
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
}
]
}' > /root/reality.json
# Create sing-box.service
cat > /etc/systemd/system/sing-box.service <<EOF
[Unit]
After=network.target nss-lookup.target
[Service]
User=root
WorkingDirectory=/root
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
ExecStart=/root/sing-box run -c /root/reality.json
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
# Check configuration and start the service
if /root/sing-box check -c /root/reality.json; then
echo "Configuration checked successfully. Starting sing-box service..."
systemctl daemon-reload
systemctl enable sing-box
systemctl start sing-box
systemctl restart sing-box
# Generate the link
server_link="vless://$uuid@$server_ip:$listen_port?encryption=none&flow=xtls-rprx-vision&security=reality&sni=$server_name&fp=chrome&pbk=$public_key&sid=$short_id&type=tcp&headerType=none#SING-BOX-TCP"
# Print the server details
echo
echo "Server IP: $server_ip"
echo "Listen Port: $listen_port"
echo "Server Name: $server_name"
echo "Public Key: $public_key"
echo "Short ID: $short_id"
echo "UUID: $uuid"
echo ""
echo ""
echo "Here is the link for v2rayN and v2rayNG :"
echo ""
echo ""
echo "$server_link"
echo ""
echo ""
else
echo "Error in configuration. Aborting."
fi