Last active
February 5, 2020 07:36
-
-
Save RicherMans/04ac52f58c9c215c437a3e3615c88ae5 to your computer and use it in GitHub Desktop.
V2ray AutoInstaller for myself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
[[ $EUID -ne 0 ]] && echo -e "[${red}Error${plain}] This script must be run as root!" && exit 1 | |
error() { | |
echo -e " | |
Error | |
$1 | |
" | |
} | |
do_service(){ | |
cmd=$(ps --no-headers -o comm 1) | |
if [[ $cmd == "systemd" ]]; then | |
systemctl $1 $2 | |
else | |
service $2 $1 | |
fi | |
} | |
set_config_name(){ | |
while :; do | |
echo "Please enter configuration name" | |
echo | |
echo | |
read -p $(echo "Name: ") v2ray_config_name | |
if [ -z ${v2ray_config_name} ]; then | |
error | |
else | |
echo "---------------------------" | |
echo "config = ${v2ray_config_name}" | |
echo "---------------------------" | |
break | |
fi | |
done | |
} | |
set_uuid(){ | |
default_uuid=$(cat /proc/sys/kernel/random/uuid) | |
echo "Please enter a uuid as user for" | |
read -p "$(echo "(Default uuid: ${default_uuid}):")" uuid | |
[ -z "${uuid}" ] && uuid=${default_uuid} | |
echo | |
echo "---------------------------" | |
echo "UUID = ${uuid}" | |
echo "---------------------------" | |
echo | |
} | |
# Set v2ray config port | |
set_v2ray_port(){ | |
port_end=50000 | |
while :; | |
do | |
dport=$(shuf -i 20000-30000 -n 1) | |
echo "Please enter a port for v2ray [20000-${port_end}]" | |
read -p "(Default port: ${dport}):" v2ray_port | |
[ -z "$v2ray_port" ] && v2ray_port=${dport} | |
expr ${v2ray_port} + 1 &>/dev/null | |
if [ $? -eq 0 ]; then | |
if [ ${v2ray_port} -ge 20000 ] && [ ${v2ray_port} -le 60000 ] && [ ${v2ray_port:0:1} != 0 ]; then | |
echo | |
echo "---------------------------" | |
echo "port = ${v2ray_port}" | |
echo "---------------------------" | |
echo | |
break | |
fi | |
fi | |
echo -e "[${red}Error${plain}] Please enter a correct number [1-65535]" | |
done | |
randomize_port_start=$(echo ${v2ray_port} | awk '{print $1+1}') | |
echo | |
echo "---------------------------" | |
echo "Setting dynamic port range = ${randomize_port_start}" - ${port_end} | |
echo "---------------------------" | |
echo | |
} | |
firewall_enable(){ | |
ufw enable | |
ufw allow ssh | |
ufw allow $v2ray_port:$port_end/tcp # Or your v2ray kcp/udp port range | |
ufw allow $v2ray_port:$port_end/udp # Or your v2ray kcp/udp port range | |
ufw allow http | |
ufw allow https | |
ufw default deny incoming | |
ufw reload | |
} | |
firewall_disable(){ | |
ufw reset | |
} | |
config_client(){ | |
cat > ${v2ray_config_name}_client.json <<-EOF | |
{ | |
"inbound": | |
{ | |
"port": 1088, | |
"protocol": "shadowsocks", | |
"settings": { | |
"udp": true, | |
"method": "chacha20-ietf-poly1305", | |
"password": "mypassyo" | |
} | |
}, | |
"inboundDetour": [ | |
{ | |
"port": 1199, | |
"listen": "127.0.0.1", | |
"protocol": "socks", | |
"settings": { | |
"udp": true | |
} | |
} | |
], | |
"outbound": { | |
"mux": { | |
"enabled": true , | |
"concurrency" : 4 | |
}, | |
"protocol": "vmess", | |
"settings": { | |
"vnext": [ | |
{ | |
"address": "${ip}", | |
"port": ${v2ray_port}, | |
"users": [ | |
{ | |
"id": "${uuid}", | |
"alterId":64 | |
} | |
] | |
} | |
] | |
} | |
}, | |
"outboundDetour": [ | |
{ | |
"protocol": "freedom", | |
"tag": "direct", | |
"settings": {} | |
}, | |
{ | |
"protocol": "blackhole", | |
"settings": {}, | |
"tag": "adblock" | |
} | |
], | |
"routing": { | |
"strategy": "rules", | |
"settings": { | |
"domainStrategy": "IPIfNonMatch", | |
"rules": [ | |
{ | |
"type": "field", | |
"inboundTag": [ | |
"campus" | |
], | |
"outboundTag": "direct" | |
} | |
] | |
} | |
} | |
} | |
EOF | |
} | |
config_server(){ | |
cat > ${v2ray_config_name}_server.json <<-EOF | |
{ | |
"log": { | |
"loglevel": "warning", | |
"access": "/var/log/v2ray/v2ray_access.log", | |
"error": "/var/log/v2ray/v2ray_error.log" | |
}, | |
"inbound": { | |
"port": ${v2ray_port}, | |
"protocol": "vmess", | |
"settings": { | |
"clients": [ | |
{ | |
"id": "${uuid}" | |
} | |
], | |
"detour":{ | |
"to":"dynamicPort" | |
} | |
} | |
}, | |
"inboundDetour":[ | |
{ | |
"protocol": "vmess", | |
"port": "${randomize_port_start}-${port_end}", | |
"tag": "dynamicPort", | |
"settings": { | |
"default": { | |
"level": 1, | |
"alterId": 32 | |
} | |
}, | |
"allocate": { | |
"strategy": "random", | |
"concurrency": 3, | |
"refresh": 3 | |
} | |
} | |
], | |
"outbound": { | |
"protocol": "freedom", | |
"settings": {} | |
} | |
} | |
EOF | |
} | |
append_at_service(){ | |
cmd=$(ps --no-headers -o comm 1) | |
if [[ $cmd == "systemd" ]]; then | |
cat > /etc/systemd/system/[email protected] <<-EOF | |
[Unit] | |
Description=V2Ray Service | |
After=network.target | |
Wants=network.target | |
[Service] | |
Type=simple | |
PIDFile=/var/run/v2ray-%i.pid | |
ExecStart=/usr/bin/v2ray/v2ray -config /etc/v2ray/%i.json | |
Restart=on-failure | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
fi | |
} | |
post_install(){ | |
append_at_service # Service to use for multiple configs v2ray@CONFIGNAME | |
firewall_enable | |
mv -v ${v2ray_config_name}_server.json /etc/v2ray/${v2ray_config_name}.json | |
echo -e " | |
Starting Server .... | |
" | |
do_service start v2ray@${v2ray_config_name} | |
do_service enable v2ray@${v2ray_config_name} | |
path=$(pwd) | |
echo -e " | |
Installation complete! | |
Please logout the server and download the generated client configuration to your machine: | |
exit | |
sudo scp ${ip}:${path}/${v2ray_config_name}_client.json /etc/v2ray/${v2ray_config_name}.json | |
sudo systemctl start v2ray@${v2ray_config_name} | |
sudo systemctl enable v2ray@${v2ray_config_name} | |
" | |
} | |
get_ip() { | |
ip=$(curl -s -m 1 https://ipinfo.io/ip) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 https://api.ip.sb/ip) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 https://api.ipify.org) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 https://ip.seeip.org) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 https://ifconfig.co/ip) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 https://api.myip.com | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}") | |
[[ -z $ip ]] && ip=$(curl -s -m 1 icanhazip.com) | |
[[ -z $ip ]] && ip=$(curl -s -m 1 myip.ipip.net | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}") | |
[[ -z $ip ]] && error "Could not obtain IP" | |
} | |
preinstall(){ | |
if [[ $(command -v apt) ]]; then | |
if [[ ! $(command -v curl) ]]; then | |
apt install -y curl | |
fi | |
fi | |
} | |
install_v2ray(){ | |
release=$(awk -F= '/^NAME/{print $2}' /etc/os-release| sed 's/\"\|\ //g') | |
case $release in | |
"Ubuntu") | |
;; | |
*) | |
error "Only Ubuntu is supported!" && exit 1 | |
;; | |
esac | |
preinstall | |
set_config_name | |
set_uuid | |
set_v2ray_port | |
get_ip | |
read -rsp "$(echo -e "Press any key to start, Press Ctrl+C to cancel.")" -d $'\n' | |
echo | |
# Install officiall | |
bash <(curl -L -s https://install.direct/go.sh) | |
config_server | |
config_client | |
post_install | |
} | |
uninstall_v2ray(){ | |
echo "Uninstalling" | |
cmd=$(ps --no-headers -o comm 1) | |
if [[ $cmd == "systemd" ]]; then | |
do_service stop v2ray >/dev/null 2>&1 | |
do_service disable v2ray >/dev/null 2>&1 | |
rm -rfv /lib/systemd/system/v2ray*.service | |
else | |
update-rc.d -f v2ray remove >/dev/null 2>&1 | |
rm -rfv /etc/init.d/v2ray | |
fi | |
rm -rfv /etc/v2ray/ rm -rfv /usr/bin/v2ray | |
firewall_disable | |
} | |
action=${1:-install} | |
case "$action" in | |
install|uninstall) | |
${action}_v2ray | |
;; | |
*) | |
echo "Arguments error! [${action}]" | |
echo "Usage: `basename $0` [install|uninstall]" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment