-
Notifications
You must be signed in to change notification settings - Fork 8
/
v2ray.sh
executable file
·316 lines (308 loc) · 9.37 KB
/
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
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
#!/usr/bin/env bash
#=================================================
# Recommend OS: Debian/Ubuntu
# Description: V2ray auto-deploy
# Version: 2.0.0
# Author: IITII
# Blog: https://IITII.github.io
#=================================================
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/v2ray/
#=================================================
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
siteName=""
wsPath=""
# ssl public: *.cer *.crt *.pem
# ssl key: *.key
sslPath=""
sslPublic=""
sslPrivate=""
uuid=""
he_net_ddns_key=""
# Don't modify
release="ubuntu"
flag=0
nginx_default_ssl="/etc/nginx/ssl"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
SKYBLUE='\033[0;36m'
PLAIN='\033[0m'
check_root() {
[[ $(id -u) != "0" ]] && {
log "Error: You must be root to run this script"
exit 1
}
}
pre_command_run_status() {
if [[ $? -eq 0 ]]; then
log_success "Success"
else
log_err "Failed"
exit 1
fi
}
log() {
echo -e "[$(/bin/date)] $1"
}
log_success() {
echo -e "${GREEN}[$(/bin/date)] $1${PLAIN}"
}
log_info() {
echo -e "${YELLOW}[$(/bin/date)] $1${PLAIN}"
}
log_prompt() {
echo -e "${SKYBLUE}[$(/bin/date)] $1${PLAIN}"
}
log_err() {
echo -e "${RED}[$(/bin/date)] $1${PLAIN}"
}
check_release() {
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
release="centos"
elif cat /etc/issue | grep -Eqi "alpine"; then
release="alpine"
elif cat /proc/version | grep -Eqi "debian"; then
release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
release="centos"
fi
}
show_help() {
echo "Usage:
$0 -h, --help Show this page
$0 -w siteName
$0 -p, --path v2ray web socket path, default \"/bin/date +\"%S\" | /usr/bin/base64\"
$0 -u, --uuid v2ray uuid
$0 --ddns dns.he.net ddns's key
$0 --sslPath ssl cert path
"
}
check_command() {
if ! command -v $2 >/dev/null 2>&1; then
log "Installing $2 from $1 repo"
if [[ "$1" = "centos" ]]; then
yum update >/dev/null 2>&1
yum -y install $3 >/dev/null 2>&1
elif [[ "$1" = "alpine" ]]; then
apk update >/dev/null 2>&1
apk --no-cache add $3 >/dev/null 2>&1
else
apt-get update >/dev/null 2>&1
apt-get install $3 -y >/dev/null 2>&1
fi
pre_command_run_status
fi
}
check_path() {
log "Checking Path $1"
if ! [[ -d $1 ]]; then
log "Create Un-exist Path $1"
mkdir -p $1
pre_command_run_status
else
log "Existed !"
fi
}
ssl_pub_key() {
sslPublic=$(ls $1 | grep -v "key" | head -n 1)
sslPrivate=$(ls $1 | grep "key" | head -n 1)
cd $1
if [[ -z ${sslPublic} ]] && [[ -z ${sslPrivate} ]]; then
log_err "Right cert files?"
log_info "Require: PublicKey: *.cer| *.crt| *.pem; PrivateKey: *.key"
exit 1
fi
cd $2
}
pre_check_var() {
log "Check necessary variable..."
if [[ -z ${siteName} ]]; then
log "SiteName can not be empty!!!"
exit 1
fi
if [[ -z ${uuid} ]]; then
log "uuid is Empty, Generating..."
uuid=$(v2ray uuid)
log "Now uuid is $uuid"
fi
if [[ -z ${wsPath} ]]; then
log "wsPath is Empty, Generating..."
wsPath=$(/bin/date +"%S" | base64)
log "Now wsPath is $wsPath"
fi
if [[ -z ${he_net_ddns_key} ]]; then
flag=1
else
sslPath=${nginx_default_ssl}/${siteName}
check_path ${sslPath}
bash ${CURRENT_DIR}/letsencrypt.sh ${siteName} ${he_net_ddns_key} ${sslPath}
ssl_pub_key ${sslPath} ${CURRENT_DIR}
fi
if [[ -z ${sslPath} ]] && [[ ${flag} -eq 1 ]]; then
log_info "It looks that you have nothing..."
log "Generate self signed cert..."
sslPath=${nginx_default_ssl}/${siteName}
check_path ${sslPath}
v2ray tls cert --ca --expire=8760h --file=${sslPath}/v2 \
--name="${siteName}" --org="${siteName}" --domain="${siteName}" >/dev/null 2>&1
ssl_pub_key ${sslPath} ${CURRENT_DIR}
fi
if [[ ${flag} -eq 0 ]]; then
log_info \
"Check again: siteName: $siteName , uuid: $uuid , wsPath: $wsPath , he_net_ddns_key: $he_net_ddns_key"
tree ${sslPath}
else
log_info \
"Check again: siteName: $siteName , uuid: $uuid , wsPath: $wsPath , sslPath: $sslPath , sslPublic: $sslPublic , sslPrivate: $sslPrivate"
tree ${sslPath}
fi
}
firewall_rule() {
log "Adding iptable rules..."
if [[ "$release" = "centos" ]]; then
systemctl stop firewalld.service >/dev/null 2>&1
systemctl disable firewalld.service >/dev/null 2>&1
else
ufw allow 22 >/dev/null 2>&1
ufw allow 80 >/dev/null 2>&1
ufw allow 443 >/dev/null 2>&1
ufw reload >/dev/null 2>&1
fi
iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT
iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443 -j ACCEPT
log "Finished!!!"
}
vmess_gen() {
tempRaw=$(/bin/cat ${CURRENT_DIR}/conf/share.json | /bin/sed \
-e "s/baidu.com\",$/$siteName\",/g" \
-e "s/\"id\": \"\S\+/\"id\": \"$uuid\",/g" \
-e "s/\"path\": \"\S\+/\"path\": \"\/$wsPath\",/g")
temp=$(echo $tempRaw | base64 -w 0)
temp=$(echo vmess://${temp})
clash_yml=$(/bin/cat ${CURRENT_DIR}/conf/clash.yml | /bin/sed \
-e "s/v2ray.com/$siteName/g" \
-e "s/ruuid/$uuid/g" \
-e "s/\/path/\/$wsPath/g")
log_prompt "v2ray info: \n ${tempRaw}"
log_prompt "v2ray info for clash: \n ${clash_yml}"
log_prompt "v2ray link: ${SKYBLUE}${temp}${PLAIN}"
echo "${tempRaw} \n ${clash_yml} \n ${temp}" >/root/v2ray_link &&
log_success "v2ray link save to /root/v2ray_link"
}
main() {
log "Ensure service is started...."
service nginx restart
service v2ray restart
log "Modifying config file..."
log "Modifying nginx config file"
#-e "s/fastcgi_pass \S\+/fastcgi_pass unix:\/run\/php\/$(ls /run/php/ | grep sock | head -n 1);/g" \
/bin/cat ${CURRENT_DIR}/conf/tls.nginx | /bin/sed \
-e "s/server_name \S\+/server_name $3;/g" \
-e "s/ssl_certificate \S\+/ssl_certificate \/etc\/nginx\/ssl\/$3\/$4;/g" \
-e "s/ssl_certificate_key \S\+/ssl_certificate_key \/etc\/nginx\/ssl\/$3\/$5;/g" \
-e "s/location \/china \S\+/location \/$2 {/g" \
-e "s/root \S\+/root \/var\/www\/html;/g" \
>/etc/nginx/sites-available/default
cp -R ${CURRENT_DIR}/www/* /var/www/html/
log "Testing nginx config..."
nginx -t >/dev/null 2>&1
pre_command_run_status
log "Reload nginx..." && nginx -s reload >/dev/null 2>&1
pre_command_run_status
log "Modifying v2ray config file"
/bin/cat ${CURRENT_DIR}/conf/server.json | /bin/sed \
-e "s/\"id\": \"\S\+/\"id\": \"$1\",/g" \
-e "s/\"path\": \"\S\+/\"path\": \"\/$2\"/g" |
tee >/usr/local/etc/v2ray/config.json
log "Reload v2ray..."
service v2ray restart && log_success "Success"
log "Enable auto start..." && systemctl enable v2ray && log_success "Success"
firewall_rule
}
if [[ -z "$1" ]]; then
show_help
exit 1
fi
cd ${CURRENT_DIR}
check_release
check_command ${release} getopt "util-linux"
check_command ${release} tee "tee"
check_command ${release} base64 "coreutils"
check_command ${release} nginx "nginx"
check_command ${release} curl "curl"
check_command ${release} tree "tree"
ARGS=$(getopt -a -o hw:p:u: -l help,path:,ddns:,uuid:,sslPath: -- "$@")
#set -- "${ARGS}"
#log "\$@: $@"
eval set -- "${ARGS}"
while [[ -n $1 ]]; do
case "$1" in
-w)
if [[ -n $2 ]]; then
siteName="$2"
else
log "SiteName can not be empty!!!"
exit 1
fi
shift
;;
-h | --help)
show_help
;;
-p | --path)
wsPath="$2"
shift
;;
-u | --uuid)
uuid="$2"
shift
;;
--ddns)
he_net_ddns_key="$2"
shift
;;
--sslPath)
if [[ -e $2 ]]; then
sslPath=$(cd $2 && pwd)
check_path "${nginx_default_ssl}/${siteName}"
cp ${sslPath}/* "${nginx_default_ssl}/${siteName}/"
sslPath=${nginx_default_ssl}/${siteName}
ssl_pub_key ${sslPath} ${CURRENT_DIR}
else
log_err "sslPath don't exist!!!"
exit 1
fi
shift
;;
--)
shift
break
;;
*)
log "unknown argument"
exit 1
;;
esac
shift
done
if ! ( (command -v v2ray) && (command -v v2ctl)) >/dev/null 2>&1; then
log "Install main program..."
rm -rf /usr/bin/v2ray
#See: https://github.com/v2fly/fhs-install-v2ray/blob/master/README.zh-Hans-CN.md
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) >/dev/null 2>&1
pre_command_run_status
else
log_prompt "Already installed"
fi
pre_check_var
main ${uuid} ${wsPath} ${siteName} ${sslPublic} ${sslPrivate}
vmess_gen