forked from Jrohy/trojan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
137 lines (114 loc) · 3.45 KB
/
install.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
#!/bin/bash
# Author: Jrohy
# github: https://github.com/Jrohy/trojan
#定义操作变量, 0为否, 1为是
HELP=0
REMOVE=0
UPDATE=0
DOWNLAOD_URL="https://github.com/Jrohy/trojan/releases/download/"
VERSION_CHECK="https://api.github.com/repos/Jrohy/trojan/releases/latest"
[[ -e /var/lib/trojan-manager ]] && UPDATE=1
#Centos 临时取消别名
[[ -f /etc/redhat-release && -z $(echo $SHELL|grep zsh) ]] && unalias -a
[[ -z $(echo $SHELL|grep zsh) ]] && SHELL_WAY="bash" || SHELL_WAY="zsh"
#######color code########
RED="31m"
GREEN="32m"
YELLOW="33m"
BLUE="36m"
FUCHSIA="35m"
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${@:2}\033[0m"
}
#######get params#########
while [[ $# > 0 ]];do
KEY="$1"
case $KEY in
--remove)
REMOVE=1
;;
-h|--help)
HELP=1
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
#############################
help(){
echo "bash $0 [-h|--help] [--remove]"
echo " -h, --help Show help"
echo " --remove remove trojan"
return 0
}
removeTrojan() {
#移除trojan
rm -rf /usr/bin/trojan >/dev/null 2>&1
rm -rf /usr/local/etc/trojan >/dev/null 2>&1
rm -f /etc/systemd/system/trojan.service >/dev/null 2>&1
systemctl daemon-reload
#移除trojan管理程序
rm -f /usr/local/bin/trojan >/dev/null 2>&1
rm -rf /var/lib/trojan-manager >/dev/null 2>&1
#移除trojan的专用mysql
docker rm -f trojan-mysql
rm -rf /home/mysql >/dev/null 2>&1
#移除环境变量
sed -i '/trojan/d' ~/.${SHELL_WAY}rc
source ~/.${SHELL_WAY}rc
colorEcho ${GREEN} "uninstall success!"
}
checkSys() {
#检查是否为Root
[ $(id -u) != "0" ] && { colorEcho ${RED} "Error: You must be root to run this script"; exit 1; }
if [[ $(uname -m 2> /dev/null) != x86_64 ]]; then
colorEcho $YELLOW "Please run this script on x86_64 machine."
exit 1
fi
if [[ `command -v apt-get` ]];then
PACKAGE_MANAGER='apt-get'
elif [[ `command -v dnf` ]];then
PACKAGE_MANAGER='dnf'
elif [[ `command -v yum` ]];then
PACKAGE_MANAGER='yum'
else
colorEcho $RED "Not support OS!"
exit 1
fi
}
#安装依赖
installDependent(){
if [[ ${PACKAGE_MANAGER} == 'dnf' || ${PACKAGE_MANAGER} == 'yum' ]];then
${PACKAGE_MANAGER} install socat bash-completion -y
else
${PACKAGE_MANAGER} update
${PACKAGE_MANAGER} install socat bash-completion -y
fi
}
installTrojan(){
LASTEST_VERSION=$(curl -H 'Cache-Control: no-cache' -s "$VERSION_CHECK" | grep 'tag_name' | cut -d\" -f4)
curl -L "$DOWNLAOD_URL/$LASTEST_VERSION/trojan" -o /usr/local/bin/trojan
chmod +x /usr/local/bin/trojan
#命令补全环境变量
[[ -z $(grep trojan ~/.${SHELL_WAY}rc) ]] && echo "source <(trojan completion ${SHELL_WAY})" >> ~/.${SHELL_WAY}rc
source ~/.${SHELL_WAY}rc
if [[ $UPDATE == 0 ]];then
colorEcho $GREEN "安装trojan管理程序成功!\n"
echo "运行命令`colorEcho $BLUE trojan`可进行trojan管理\n"
trojan
else
colorEcho $GREEN "更新trojan管理程序成功!\n"
fi
}
main(){
[[ ${HELP} == 1 ]] && help && return
[[ ${REMOVE} == 1 ]] && removeTrojan && return
[[ $UPDATE == 0 ]] && echo "正在安装trojan管理程序.." || echo "正在更新trojan管理程序.."
checkSys
[[ $UPDATE == 0 ]] && installDependent
installTrojan
}
main