forked from atrandys/xray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_kcptun_udp2raw.sh
384 lines (360 loc) · 10.2 KB
/
install_kcptun_udp2raw.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
384
#!/bin/bash
blue(){
echo -e "\033[34m\033[01m$1\033[0m"
}
green(){
echo -e "\033[32m\033[01m$1\033[0m"
}
red(){
echo -e "\033[31m\033[01m$1\033[0m"
}
yellow(){
echo -e "\033[33m\033[01m$1\033[0m"
}
logcmd(){
eval $1 | tee -ai /var/atrandys.log
}
randpwd(){
mpasswd=$(cat /dev/urandom | head -1 | md5sum | head -c 4)
echo ${mpasswd}
}
rand(){
min=$1
max=$(($2-$min+1))
num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
echo $(($num%$max+$min))
}
echo "ulimit -n 65535" >>/etc/profile
cat >>/etc/sysctl.conf<<-EOF
net.core.rmem_max=26214400
net.core.rmem_default=26214400
net.core.wmem_max=26214400
net.core.wmem_default=26214400
net.core.netdev_max_backlog=2048
EOF
sysctl -p
source /etc/os-release
RELEASE=$ID
VERSION=$VERSION_ID
cat >> /usr/src/atrandys.log <<-EOF
== Script: atrandys/xray/install.sh
== Time : $(date +"%Y-%m-%d %H:%M:%S")
== OS : $RELEASE $VERSION
== Kernel: $(uname -r)
== User : $(whoami)
EOF
sleep 2s
check_release(){
green "$(date +"%Y-%m-%d %H:%M:%S") ==== 检查系统版本"
if [ "$RELEASE" == "centos" ]; then
systemPackage="yum"
yum install -y wget
if [ "$VERSION" == "6" ] ;then
red "$(date +"%Y-%m-%d %H:%M:%S") - 暂不支持CentOS 6.\n== Install failed."
exit
fi
if [ "$VERSION" == "5" ] ;then
red "$(date +"%Y-%m-%d %H:%M:%S") - 暂不支持CentOS 5.\n== Install failed."
exit
fi
if [ -f "/etc/selinux/config" ]; then
CHECK=$(grep SELINUX= /etc/selinux/config | grep -v "#")
if [ "$CHECK" == "SELINUX=enforcing" ]; then
green "$(date +"%Y-%m-%d %H:%M:%S") - SELinux状态非disabled,关闭SELinux."
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#loggreen "SELinux is not disabled, add port 80/443 to SELinux rules."
#loggreen "==== Install semanage"
#logcmd "yum install -y policycoreutils-python"
#semanage port -a -t http_port_t -p tcp 80
#semanage port -a -t http_port_t -p tcp 443
#semanage port -a -t http_port_t -p tcp 37212
#semanage port -a -t http_port_t -p tcp 37213
elif [ "$CHECK" == "SELINUX=permissive" ]; then
green "$(date +"%Y-%m-%d %H:%M:%S") - SELinux状态非disabled,关闭SELinux."
setenforce 0
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
fi
fi
firewall_status=`firewall-cmd --state`
if [ "$firewall_status" == "running" ]; then
green "$(date +"%Y-%m-%d %H:%M:%S") - FireWalld状态非disabled,添加80/443到FireWalld rules."
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload
fi
yum install -y epel-release
elif [ "$RELEASE" == "ubuntu" ]; then
systemPackage="apt-get"
if [ "$VERSION" == "14" ] ;then
red "$(date +"%Y-%m-%d %H:%M:%S") - 暂不支持Ubuntu 14.\n== Install failed."
exit
fi
if [ "$VERSION" == "12" ] ;then
red "$(date +"%Y-%m-%d %H:%M:%S") - 暂不支持Ubuntu 12.\n== Install failed."
exit
fi
ufw_status=`systemctl status ufw | grep "Active: active"`
if [ -n "$ufw_status" ]; then
ufw allow 80/tcp
ufw allow 443/tcp
ufw reload
fi
apt-get update >/dev/null 2>&1
elif [ "$RELEASE" == "debian" ]; then
systemPackage="apt-get"
ufw_status=`systemctl status ufw | grep "Active: active"`
if [ -n "$ufw_status" ]; then
ufw allow 80/tcp
ufw allow 443/tcp
ufw reload
fi
apt-get update >/dev/null 2>&1
else
red "$(date +"%Y-%m-%d %H:%M:%S") - 当前系统不被支持. \n== Install failed."
exit
fi
}
check_port(){
green "$(date +"%Y-%m-%d %H:%M:%S") ==== 检查端口"
$systemPackage -y install net-tools
Port443=`netstat -tlpn | awk -F '[: ]+' '$1=="tcp"{print $5}' | grep -w 443`
if [ -n "$Port443" ]; then
process443=`netstat -tlpn | awk -F '[: ]+' '$5=="443"{print $9}'`
red "$(date +"%Y-%m-%d %H:%M:%S") - 443端口被占用,占用进程:${process443}\n== Install failed."
exit 1
fi
}
install_xray(){
green "$(date +"%Y-%m-%d %H:%M:%S") ==== 安装xray"
mkdir /usr/local/etc/xray/
mkdir /usr/local/etc/xray/cert
bash <(curl -L https://raw.githubusercontent.com/XTLS/Xray-install/main/install-release.sh)
cd /usr/local/etc/xray/
rm -f config.json
serverip=$(curl ipv4.icanhazip.com)
v2uuid=$(cat /proc/sys/kernel/random/uuid)
seed=$(randpwd)
cat > /usr/local/etc/xray/config.json<<-EOF
{
"log": {
"loglevel": "warning"
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": 11234,
"protocol": "vless",
"settings": {
"clients": [
{
"id": "$v2uuid",
"email": "[email protected]"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp"
}
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": { }
}
]
}
EOF
mkdir /usr/src/udp
cd /usr/src/udp
wget https://github.com/atrandys/wireguard/raw/master/udp2raw
wget https://raw.githubusercontent.com/atrandys/wireguard/master/run.sh
wget https://github.com/xtaci/kcptun/releases/download/v20201126/kcptun-linux-amd64-20201126.tar.gz
tar -xvf kcptun-linux-amd64-20201126.tar.gz
chmod +x udp2raw run.sh server_linux_amd64
password=$(randpwd)
kcpkey=$(randpwd)
cat > /usr/src/udp/server.json <<-EOF
{
"listen": "127.0.0.1:11235",
"target": "127.0.0.1:11234",
"key": "$kcpkey",
"crypt": "aes-128",
"mode": "manual",
"mtu": 1300,
"sndwnd": 1024,
"rcvwnd": 1024,
"datashard": 30,
"parityshard": 15,
"dscp": 46,
"nocomp": true,
"acknodelay": false,
"nodelay": 0,
"interval": 20,
"resend": 2,
"nc": 1,
"sockbuf": 4194304,
"keepalive": 10
}
EOF
cat > /usr/src/udp/client.json <<-EOF
{
"localaddr": "127.0.0.1:3090",
"remoteaddr": "127.0.0.1:3091",
"key": "$kcpkey",
"crypt": "aes-128",
"mode": "manual",
"conn": 1,
"autoexpire": 300,
"mtu": 1300,
"sndwnd": 128,
"rcvwnd": 1024,
"datashard": 30,
"parityshard": 15,
"dscp": 46,
"nocomp": true,
"acknodelay": false,
"nodelay": 0,
"interval": 20,
"resend": 2,
"nc": 1,
"sockbuf": 4194304,
"keepalive": 10
}
EOF
cat > /usr/src/udp/udp.sh <<-EOF
#!/bin/bash
nohup /usr/src/udp/udp2raw -s -l0.0.0.0:443 -r 127.0.0.1:11235 --raw-mode faketcp --cipher-mode xor -a -k $password >udp2raw.log 2>&1 &
nohup /usr/src/udp/server_linux_amd64 -c /usr/src/udp/server.json >kcptun.log 2>&1 &
EOF
chmod +x /usr/src/udp/udp.sh
#增加自启动脚本
cat > /etc/systemd/system/autoudp.service<<-EOF
[Unit]
Description=autoudp
After=network.target
[Service]
Type=forking
ExecStart=/usr/src/udp/udp.sh
ExecReload=/bin/kill -9 \$(pidof udp2raw) && /bin/kill -9 \$(pidof udpspeeder)
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
EOF
#设置脚本权限
chmod +x /etc/systemd/system/autoudp.service
systemctl enable autoudp.service
systemctl start autoudp.service
systemctl enable xray.service
sed -i "s/User=nobody/User=root/;" /etc/systemd/system/xray.service
systemctl daemon-reload
systemctl restart xray
cat > /usr/local/etc/xray/myconfig.json<<-EOF
==xray配置==
IP:127.0.0.1
端口:3090
id:${v2uuid}
加密:none
别名:自定义
传输协议:kcp
mtu:1300
tti::10
uplinkCapacity:50
downlinkCapacity:10
congestion:false
readBufferSize:1
writeBufferSize:1
seed: ${seed}
==udp2raw==
IP:${serverip}
端口:443
password:${password}
raw-mode:faketcp
==kcptun==
{
"localaddr": "127.0.0.1:3090",
"remoteaddr": "127.0.0.1:3091",
"key": "$kcpkey",
"crypt": "aes-128",
"mode": "manual",
"conn": 1,
"autoexpire": 300,
"mtu": 1300,
"sndwnd": 128,
"rcvwnd": 1024,
"datashard": 30,
"parityshard": 15,
"dscp": 46,
"nocomp": true,
"acknodelay": false,
"nodelay": 0,
"interval": 20,
"resend": 2,
"nc": 1,
"sockbuf": 4194304,
"keepalive": 10
}
EOF
green "== 安装完成."
green "==配置参数=="
cat /usr/local/etc/xray/myconfig.json
green "本次安装检测信息如下,如udp2raw与xray正常启动,表示安装正常:"
ps -aux | grep -e udp2raw -e xray -e kcptun
}
remove_xray(){
green "$(date +"%Y-%m-%d %H:%M:%S") - 删除xray."
systemctl stop xray.service
systemctl disable xray.service
rm -rf /usr/local/share/xray/ /usr/local/etc/xray/
rm -f /usr/local/bin/xray
rm -rf /etc/systemd/system/xray*
rm -rf /etc/systemd/system/autoudp
rm -rf /usr/src/udp
green "xray & udp2raw & kcptun has been deleted."
}
function start_menu(){
clear
green " ====================================================="
green " 描述:xray + kcp + udp2raw一键安装脚本"
green " 系统:支持centos7/debian9+/ubuntu16.04+ "
green " 作者:atrandys www.atrandys.com"
green " ====================================================="
echo
green " 1. 安装 xray + kcp + udp2raw"
green " 2. 更新 xray"
red " 3. 删除 xray"
green " 4. 查看配置参数"
yellow " 0. Exit"
echo
read -p "输入数字:" num
case "$num" in
1)
check_release
check_port
install_xray
;;
2)
bash <(curl -L https://raw.githubusercontent.com/XTLS/Xray-install/main/install-release.sh)
systemctl restart xray
;;
3)
remove_xray
;;
4)
cat /usr/local/etc/xray/myconfig.json
;;
0)
exit 1
;;
*)
clear
red "Enter a correct number"
sleep 2s
start_menu
;;
esac
}
start_menu