forked from hijkpw/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu_install_v2ray.sh
237 lines (214 loc) · 6.91 KB
/
ubuntu_install_v2ray.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
#!/bin/bash
# v2ray Ubuntu系统一键安装脚本
# Author: hijk<https://www.hijk.pw>
echo "#############################################################"
echo "# Ubuntu 16.04 TLS v2ray 带一键安装脚本 #"
echo "# 网址: https://www.hijk.pw #"
echo "# 作者: hijk #"
echo "#############################################################"
echo ""
red='\033[0;31m'
green="\033[0;32m"
plain='\033[0m'
function checkSystem()
{
result=$(id | awk '{print $1}')
if [ $result != "uid=0(root)" ]; then
echo "请以root身份执行该脚本"
exit 1
fi
res=`lsb_release -d | grep -i ubuntu`
if [ "$?" != "0" ]; then
res=`which apt`
if [ "$?" != "0" ]; then
echo "系统不是Ubuntu"
exit 1
fi
res=`which systemctl`
if [ "$?" != "0" ]; then
echo "系统版本过低,请重装系统到高版本后再使用本脚本!"
exit 1
fi
else
result=`lsb_release -d | grep -oE "[0-9.]+"`
main=${result%%.*}
if [ $main -lt 16 ]; then
echo "不受支持的Ubuntu版本"
exit 1
fi
fi
}
function getData()
{
while true
do
read -p "请输入v2ray的端口[1-65535]:" port
[ -z "$port" ] && port="21568"
expr $port + 0 &>/dev/null
if [ $? -eq 0 ]; then
if [ $port -ge 1 ] && [ $port -le 65535 ]; then
echo ""
echo "端口号: $port"
echo ""
break
else
echo "输入错误,端口号为1-65535的数字"
fi
else
echo "输入错误,端口号为1-65535的数字"
fi
done
}
function preinstall()
{
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 60/' /etc/ssh/sshd_config
systemctl restart sshd
ret=`nginx -t`
if [ "$?" != "0" ]; then
echo "更新系统..."
apt update && apt -y upgrade
fi
echo "安装必要软件"
apt install -y telnet wget vim net-tools ntpdate unzip
res=`which wget`
[ "$?" != "0" ] && apt install -y wget
res=`which netstat`
[ "$?" != "0" ] && apt install -y net-tools
apt autoremove -y
}
function installV2ray()
{
echo 安装v2ray...
bash <(curl -L -s https://install.direct/go.sh)
if [ ! -f /etc/v2ray/config.json ]; then
bash <(curl -sL https://raw.githubusercontent.com/hijkpw/scripts/master/goV2.sh)
if [ ! -f /etc/v2ray/config.json ]; then
echo "安装失败,请到 https://www.hijk.pw 网站反馈"
exit 1
fi
fi
sed -i -e "s/port\":.*[0-9]*,/port\": ${port},/" /etc/v2ray/config.json
logsetting=`cat /etc/v2ray/config.json|grep loglevel`
if [ "${logsetting}" = "" ]; then
sed -i '1a\ "log": {\n "loglevel": "info",\n "access": "/var/log/v2ray/access.log",\n "error": "/var/log/v2ray/error.log"\n },' /etc/v2ray/config.json
fi
alterid=`shuf -i50-80 -n1`
sed -i -e "s/alterId\":.*[0-9]*/alterId\": ${alterid}/" /etc/v2ray/config.json
uid=`cat /etc/v2ray/config.json | grep id | cut -d: -f2 | tr -d \",' '`
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate -u time.nist.gov
systemctl enable v2ray && systemctl restart v2ray
sleep 3
res=`netstat -nltp | grep ${port} | grep v2ray`
if [ "${res}" = "" ]; then
echo "v2ray启动失败,请检查端口是否被占用!"
exit 1
fi
echo "v2ray安装成功!"
}
function setFirewall()
{
res=`ufw status | grep -i inactive`
if [ "$res" = "" ];then
ufw allow ${port}/tcp
ufw allow ${port}/udp
fi
}
function installBBR()
{
result=$(lsmod | grep bbr)
if [ "$result" != "" ]; then
echo BBR模块已安装
bbr=true
echo "3" > /proc/sys/net/ipv4/tcp_fastopen
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
return;
fi
echo 安装BBR模块...
apt install -y --install-recommends linux-generic-hwe-16.04
grub-set-default 0
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
echo "3" > /proc/sys/net/ipv4/tcp_fastopen
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
bbr=false
}
function info()
{
if [ ! -f /etc/v2ray/config.json ]; then
echo -e "${red}未安装v2ray!${plain}"
exit 1
fi
ip=`curl -s -4 icanhazip.com`
port=`cat /etc/v2ray/config.json | grep port | cut -d: -f2 | tr -d \",' '`
res=`netstat -nltp | grep ${port} | grep v2ray`
[ -z "$res" ] && status="${red}已停止${plain}" || status="${green}正在运行${plain}"
uid=`cat /etc/v2ray/config.json | grep id | cut -d: -f2 | tr -d \",' '`
alterid=`cat /etc/v2ray/config.json | grep alterId | cut -d: -f2 | tr -d \",' '`
res=`cat /etc/v2ray/config.json | grep network`
[ -z "$res" ] && network="tcp" || network=`cat /etc/v2ray/config.json | grep network | cut -d: -f2 | tr -d \",' '`
security="auto"
echo ============================================
echo -e " v2ray运行状态:${status}"
echo -e " v2ray配置文件:${red}/etc/v2ray/config.json${plain}"
echo ""
echo -e "${red}v2ray配置信息:${plain} "
echo -e " IP(address): ${red}${ip}${plain}"
echo -e " 端口(port):${red}${port}${plain}"
echo -e " id(uuid):${red}${uid}${plain}"
echo -e " 额外id(alterid): ${red}${alterid}${plain}"
echo -e " 加密方式(security): ${red}$security${plain}"
echo -e " 传输协议(network): ${red}${network}${plain}"
echo
echo ============================================
}
function bbrReboot()
{
if [ "${bbr}" == "false" ]; then
echo
echo 为使BBR模块生效,系统将在30秒后重启
echo
echo -e "您可以按 ctrl + c 取消重启,稍后输入 ${red}reboot${plain} 重启系统"
sleep 30
reboot
fi
}
function install()
{
echo -n "系统版本: "
lsb_release -a
checkSystem
getData
preinstall
installBBR
installV2ray
setFirewall
info
bbrReboot
}
function uninstall()
{
read -p "您确定真的要卸载v2ray吗?(y/n)" answer
[ -z ${answer} ] && answer="n"
if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
systemctl stop v2ray
systemctl disable v2ray
rm -rf /etc/v2ray/*
rm -rf /usr/bin/v2ray/*
rm -rf /var/log/v2ray/*
rm -rf /etc/systemd/system/v2ray.service
echo -e " ${red}卸载成功${plain}"
fi
}
action=$1
[ -z $1 ] && action=install
case "$action" in
install|uninstall|info)
${action}
;;
*)
echo "参数错误"
echo "用法: `basename $0` [install|uninstall]"
;;
esac