-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.sh
239 lines (199 loc) · 6.93 KB
/
setup.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
#!/bin/bash
# install requirements
if ! command -v qrencode &> /dev/null; then
echo "qrencode is not installed. Installing..."
sudo apt-get install qrencode -y
# Check if installation was successful
if [ $? -eq 0 ]; then
echo "qrencode is now installed."
else
echo "Error: Failed to install qrencode."
fi
else
echo "qrencode is already installed."
fi
if ! command -v jq &>/dev/null; then
echo "Installing jq..."
if ! sudo apt-get install jq -y; then
echo "Error: Failed to install jq."
exit 1
fi
echo "jq installed successfully."
fi
# Determine the appropriate TUIC_FOLDER based on the user
if [ "$EUID" -eq 0 ]; then
TUIC_FOLDER="/root/tuic"
WORKING_DIR="/root"
else
TUIC_FOLDER="$HOME/tuic"
WORKING_DIR="$HOME"
fi
CONFIG_FILE="$TUIC_FOLDER/config.json"
# Detect server architecture
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
TUIC_ARCH="x86_64-unknown-linux-gnu"
elif [ "$ARCH" = "aarch64" ]; then
TUIC_ARCH="aarch64-unknown-linux-gnu"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
# Fetch all releases from the GitHub API
ALL_VERSIONS=$(curl -s "https://api.github.com/repos/EAimTY/tuic/releases" | jq -r '.[].tag_name')
# Find the latest TUIC server version
LATEST_SERVER_VERSION=""
for VERSION in $ALL_VERSIONS; do
if [[ "$VERSION" == *"tuic-server-"* && "$VERSION" =~ ^tuic-server-[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
LATEST_SERVER_VERSION=$VERSION
break
fi
done
if [ -z "$LATEST_SERVER_VERSION" ]; then
echo "No TUIC server version found in GitHub releases."
exit 1
fi
# Construct the URL for the latest TUIC server binary
TUIC_URL="https://github.com/EAimTY/tuic/releases/download/$LATEST_SERVER_VERSION/$LATEST_SERVER_VERSION-$TUIC_ARCH"
# Check if TUIC directory exists
if [ -d "$TUIC_FOLDER" ]; then
systemctl stop tuic
clear
echo "--------------------------------------------------------------------------------"
echo -e "\e[1;33m TUIC directory already exists. Checking for latest version..\e[0m"
echo "--------------------------------------------------------------------------------"
sleep 2
# Directory exists, download the latest version of TUIC
cd "$TUIC_FOLDER"
echo -e "\e[1;33m---> Installing $LATEST_SERVER_VERSION\e[0m"
echo "--------------------------------------------------------------------------------"
sleep 2
# Construct the URL for the latest TUIC server binary
TUIC_URL="https://github.com/EAimTY/tuic/releases/download/$LATEST_SERVER_VERSION/$LATEST_SERVER_VERSION-$TUIC_ARCH"
# Download the latest TUIC server binary
wget -O tuic-server "$TUIC_URL"
chmod 755 tuic-server
# Get password, port, and congestion from config file
PORT=$(jq -r '.server' "$CONFIG_FILE" | awk -F ':' '{print $NF}')
CONGESTION_CONTROL=$(jq -r '.congestion_control' "$CONFIG_FILE")
UUID=$(jq -r '.users | keys[0]' "$CONFIG_FILE")
PASSWORD=$(jq -r ".users[\"$UUID\"]" "$CONFIG_FILE")
# Restart the service
systemctl restart tuic
else
# Update packages
apt update
apt install nano net-tools uuid-runtime wget openssl -y
# Create TUIC directory and navigate to it
mkdir -p "$TUIC_FOLDER"
cd "$TUIC_FOLDER"
# Download the latest TUIC server binary
clear
echo "--------------------------------------------------------------------------------"
echo -e "\e[1;33m---> Installing $LATEST_SERVER_VERSION\e[0m"
echo "--------------------------------------------------------------------------------"
sleep 2
wget -O tuic-server "$TUIC_URL"
chmod 755 tuic-server
# Generate certificate
openssl ecparam -genkey -name prime256v1 -out "$TUIC_FOLDER/ca.key"
openssl req -new -x509 -days 36500 -key "$TUIC_FOLDER/ca.key" -out "$TUIC_FOLDER/ca.crt" -subj "/CN=bing.com"
# Generate random UUID
UUID=$(uuidgen)
# Prompt for port
read -p "Enter port number: " PORT
# Prompt for password
read -p "Enter a password for the server: " PASSWORD
# Prompt for congestion control
OPTIONS=("cubic" "new_reno" "bbr")
PS3='Select congestion control: '
select OPT in "${OPTIONS[@]}"
do
CONGESTION_CONTROL=$OPT
break
done
# Create config file
cat <<EOF >"$CONFIG_FILE"
{
"server": "[::]:$PORT",
"users": {
"${UUID}": "$PASSWORD"
},
"certificate": "$TUIC_FOLDER/ca.crt",
"private_key": "$TUIC_FOLDER/ca.key",
"congestion_control": "$CONGESTION_CONTROL",
"alpn": ["h3", "spdy/3.1"],
"udp_relay_ipv6": true,
"zero_rtt_handshake": false,
"dual_stack": true,
"auth_timeout": "3s",
"task_negotiation_timeout": "3s",
"max_idle_time": "10s",
"max_external_packet_size": 1500,
"send_window": 16777216,
"receive_window": 8388608,
"gc_interval": "3s",
"gc_lifetime": "15s",
"log_level": "warn"
}
EOF
# Determine the user for the service
SERVICE_USER="root"
if [ "$EUID" -ne 0 ]; then
SERVICE_USER="$USER"
fi
cat <<EOF > /etc/systemd/system/tuic.service
[Unit]
Description=tuic service
After=network.target nss-lookup.target
[Service]
User=$SERVICE_USER
WorkingDirectory=$WORKING_DIR
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
ExecStart=$WORKING_DIR/tuic/tuic-server -c $WORKING_DIR/tuic/config.json
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
# Reload and start service
systemctl daemon-reload
systemctl enable tuic
systemctl start tuic
fi
# Get public IPs
IPV4=$(curl -s https://v4.ident.me)
if [ $? -ne 0 ]; then
echo "Error: Failed to get IPv4 address"
return
fi
IPV6=$(curl -s https://v6.ident.me)
if [ $? -ne 0 ]; then
echo "Error: Failed to get IPv6 address"
return
fi
# Generate and print URLs
IPV4_URL="tuic://$UUID:$PASSWORD@$IPV4:$PORT/?congestion_control=$CONGESTION_CONTROL&udp_relay_mode=native&alpn=h3,spdy/3.1&allow_insecure=1#Tuic"
IPV6_URL="tuic://$UUID:$PASSWORD@[$IPV6]:$PORT/?congestion_control=$CONGESTION_CONTROL&udp_relay_mode=native&alpn=h3,spdy/3.1&allow_insecure=1#Tuic"
echo "----------------config info-----------------"
echo -e "\e[1;33mUUID: $UUID\e[0m"
echo -e "\e[1;33mPassword: $PASSWORD\e[0m"
echo "--------------------------------------------"
echo
echo "----------------IP and Port-----------------"
echo -e "\e[1;33mPort: $PORT\e[0m"
echo -e "\e[1;33mIPv4: $IPV4\e[0m"
echo -e "\e[1;33mIPv6: $IPV6\e[0m"
echo "--------------------------------------------"
echo
echo "----------------Tuic Config IPv4-----------------"
echo -e "\e[1;33m$IPV4_URL\e[0m"
qrencode -t ANSIUTF8 "$IPV4_URL"
echo "--------------------------------------------"
echo
echo "-----------------Tuic Config IPv6----------------"
echo -e "\e[1;33m$IPV6_URL\e[0m"
qrencode -t ANSIUTF8 "$IPV6_URL"
echo "--------------------------------------------"