Skip to content

Commit 13686aa

Browse files
committed
优化脚本
1 parent 1a94fd2 commit 13686aa

File tree

3 files changed

+97
-97
lines changed

3 files changed

+97
-97
lines changed

build.sh

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
#!/bin/bash
22

3-
GITHUB_TOKEN=""
3+
github_token=""
44

5-
PROJECT="Jrohy/trojan"
5+
project="Jrohy/trojan"
66

77
#获取当前的这个脚本所在绝对路径
8-
SHELL_PATH=$(cd `dirname $0`; pwd)
8+
shell_path=$(cd `dirname $0`; pwd)
99

1010
function uploadfile() {
11-
FILE=$1
12-
CTYPE=$(file -b --mime-type $FILE)
11+
file=$1
12+
ctype=$(file -b --mime-type $file)
1313

14-
curl -H "Authorization: token ${GITHUB_TOKEN}" -H "Content-Type: ${CTYPE}" --data-binary @$FILE "https://uploads.github.com/repos/$PROJECT/releases/${RELEASE_ID}/assets?name=$(basename $FILE)"
14+
curl -H "Authorization: token ${github_token}" -H "Content-Type: ${ctype}" --data-binary @$file "https://uploads.github.com/repos/$project/releases/${release_id}/assets?name=$(basename $file)"
1515

1616
echo ""
1717
}
1818

1919
function upload() {
20-
FILE=$1
21-
DGST=$1.dgst
22-
openssl dgst -md5 $FILE | sed 's/([^)]*)//g' >> $DGST
23-
openssl dgst -sha1 $FILE | sed 's/([^)]*)//g' >> $DGST
24-
openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >> $DGST
25-
openssl dgst -sha512 $FILE | sed 's/([^)]*)//g' >> $DGST
26-
uploadfile $FILE
27-
uploadfile $DGST
20+
file=$1
21+
dgst=$1.dgst
22+
openssl dgst -md5 $file | sed 's/([^)]*)//g' >> $dgst
23+
openssl dgst -sha1 $file | sed 's/([^)]*)//g' >> $dgst
24+
openssl dgst -sha256 $file | sed 's/([^)]*)//g' >> $dgst
25+
openssl dgst -sha512 $file | sed 's/([^)]*)//g' >> $dgst
26+
uploadfile $file
27+
uploadfile $dgst
2828
}
2929

30-
cd $SHELL_PATH
30+
cd $shell_path
3131

32-
VERSION=`git describe --tags $(git rev-list --tags --max-count=1)`
33-
NOW=`TZ=Asia/Shanghai date "+%Y%m%d-%H%M"`
34-
GO_VERSION=`go version|awk '{print $3,$4}'`
35-
GIT_VERSION=`git rev-parse HEAD`
36-
LDFLAGS="-w -s -X 'trojan/trojan.MVersion=$VERSION' -X 'trojan/trojan.BuildDate=$NOW' -X 'trojan/trojan.GoVersion=$GO_VERSION' -X 'trojan/trojan.GitVersion=$GIT_VERSION'"
32+
version=`git describe --tags $(git rev-list --tags --max-count=1)`
33+
now=`TZ=Asia/Shanghai date "+%Y%m%d-%H%M"`
34+
go_version=`go version|awk '{print $3,$4}'`
35+
git_version=`git rev-parse HEAD`
36+
ldflags="-w -s -X 'trojan/trojan.MVersion=$version' -X 'trojan/trojan.BuildDate=$now' -X 'trojan/trojan.GoVersion=$go_version' -X 'trojan/trojan.GitVersion=$git_version'"
3737

38-
GOOS=linux GOARCH=amd64 go build -ldflags "$LDFLAGS" -o "result/trojan-linux-amd64" .
39-
GOOS=linux GOARCH=arm64 go build -ldflags "$LDFLAGS" -o "result/trojan-linux-arm64" .
38+
GOOS=linux GOARCH=amd64 go build -ldflags "$ldflags" -o "result/trojan-linux-amd64" .
39+
GOOS=linux GOARCH=arm64 go build -ldflags "$ldflags" -o "result/trojan-linux-arm64" .
4040

4141
if [[ $# == 0 ]];then
4242
cd result
4343

44-
UPLOAD_ITEM=($(ls -l|awk '{print $9}'|xargs -r))
44+
upload_item=($(ls -l|awk '{print $9}'|xargs -r))
4545

46-
curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$PROJECT/releases -d '{"tag_name":"'$VERSION'", "name":"'$VERSION'"}'
46+
curl -X POST -H "Authorization: token ${github_token}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$project/releases -d '{"tag_name":"'$version'", "name":"'$version'"}'
4747

4848
sleep 2
4949

50-
RELEASE_ID=`curl -H 'Cache-Control: no-cache' -s https://api.github.com/repos/$PROJECT/releases/latest|grep id|awk 'NR==1{print $2}'|sed 's/,//'`
50+
release_id=`curl -H 'Cache-Control: no-cache' -s https://api.github.com/repos/$project/releases/latest|grep id|awk 'NR==1{print $2}'|sed 's/,//'`
5151

52-
for ITEM in ${UPLOAD_ITEM[@]}
52+
for item in ${upload_item[@]}
5353
do
54-
upload $ITEM
54+
upload $item
5555
done
5656

5757
echo "upload completed!"
5858

59-
cd $SHELL_PATH
59+
cd $shell_path
6060

6161
rm -rf result
62-
fi
62+
fi

install.sh

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,46 @@
33
# github: https://github.com/Jrohy/trojan
44

55
#定义操作变量, 0为否, 1为是
6-
HELP=0
6+
help=0
77

8-
REMOVE=0
8+
remove=0
99

10-
UPDATE=0
10+
update=0
1111

12-
DOWNLAOD_URL="https://github.com/Jrohy/trojan/releases/download/"
12+
download_url="https://github.com/Jrohy/trojan/releases/download/"
1313

14-
VERSION_CHECK="https://api.github.com/repos/Jrohy/trojan/releases/latest"
14+
version_check="https://api.github.com/repos/Jrohy/trojan/releases/latest"
1515

16-
SERVICE_URL="https://raw.githubusercontent.com/Jrohy/trojan/master/asset/trojan-web.service"
16+
service_url="https://raw.githubusercontent.com/Jrohy/trojan/master/asset/trojan-web.service"
1717

18-
[[ -e /var/lib/trojan-manager ]] && UPDATE=1
18+
[[ -e /var/lib/trojan-manager ]] && update=1
1919

2020
#Centos 临时取消别名
2121
[[ -f /etc/redhat-release && -z $(echo $SHELL|grep zsh) ]] && unalias -a
2222

23-
[[ -z $(echo $SHELL|grep zsh) ]] && SHELL_WAY="bash" || SHELL_WAY="zsh"
23+
[[ -z $(echo $SHELL|grep zsh) ]] && shell_way="bash" || shell_way="zsh"
2424

2525
#######color code########
26-
RED="31m"
27-
GREEN="32m"
28-
YELLOW="33m"
29-
BLUE="36m"
30-
FUCHSIA="35m"
26+
red="31m"
27+
green="32m"
28+
yellow="33m"
29+
blue="36m"
30+
fuchsia="35m"
3131

3232
colorEcho(){
33-
COLOR=$1
34-
echo -e "\033[${COLOR}${@:2}\033[0m"
33+
color=$1
34+
echo -e "\033[${color}${@:2}\033[0m"
3535
}
3636

3737
#######get params#########
3838
while [[ $# > 0 ]];do
39-
KEY="$1"
40-
case $KEY in
39+
key="$1"
40+
case $key in
4141
--remove)
42-
REMOVE=1
42+
remove=1
4343
;;
4444
-h|--help)
45-
HELP=1
45+
help=1
4646
;;
4747
*)
4848
# unknown option
@@ -77,30 +77,30 @@ removeTrojan() {
7777
rm -rf /home/mysql /home/mariadb >/dev/null 2>&1
7878

7979
#移除环境变量
80-
sed -i '/trojan/d' ~/.${SHELL_WAY}rc
81-
source ~/.${SHELL_WAY}rc
80+
sed -i '/trojan/d' ~/.${shell_way}rc
81+
source ~/.${shell_way}rc
8282

83-
colorEcho ${GREEN} "uninstall success!"
83+
colorEcho ${green} "uninstall success!"
8484
}
8585

8686
checkSys() {
8787
#检查是否为Root
88-
[ $(id -u) != "0" ] && { colorEcho ${RED} "Error: You must be root to run this script"; exit 1; }
88+
[ $(id -u) != "0" ] && { colorEcho ${red} "Error: You must be root to run this script"; exit 1; }
8989

90-
ARCH=$(uname -m 2> /dev/null)
91-
if [[ $ARCH != x86_64 && $ARCH != aarch64 ]];then
92-
colorEcho $YELLOW "not support $ARCH machine".
90+
arch=$(uname -m 2> /dev/null)
91+
if [[ $arch != x86_64 && $arch != aarch64 ]];then
92+
colorEcho $yellow "not support $arch machine".
9393
exit 1
9494
fi
9595

9696
if [[ `command -v apt-get` ]];then
97-
PACKAGE_MANAGER='apt-get'
97+
package_manager='apt-get'
9898
elif [[ `command -v dnf` ]];then
99-
PACKAGE_MANAGER='dnf'
99+
package_manager='dnf'
100100
elif [[ `command -v yum` ]];then
101-
PACKAGE_MANAGER='yum'
101+
package_manager='yum'
102102
else
103-
colorEcho $RED "Not support OS!"
103+
colorEcho $red "Not support OS!"
104104
exit 1
105105
fi
106106

@@ -110,60 +110,60 @@ checkSys() {
110110

111111
#安装依赖
112112
installDependent(){
113-
if [[ ${PACKAGE_MANAGER} == 'dnf' || ${PACKAGE_MANAGER} == 'yum' ]];then
114-
${PACKAGE_MANAGER} install socat crontabs bash-completion -y
113+
if [[ ${package_manager} == 'dnf' || ${package_manager} == 'yum' ]];then
114+
${package_manager} install socat crontabs bash-completion -y
115115
else
116-
${PACKAGE_MANAGER} update
117-
${PACKAGE_MANAGER} install socat cron bash-completion xz-utils -y
116+
${package_manager} update
117+
${package_manager} install socat cron bash-completion xz-utils -y
118118
fi
119119
}
120120

121121
setupCron() {
122122
if [[ `crontab -l 2>/dev/null|grep acme` ]]; then
123123
if [[ -z `crontab -l 2>/dev/null|grep trojan-web` || `crontab -l 2>/dev/null|grep trojan-web|grep "&"` ]]; then
124124
#计算北京时间早上3点时VPS的实际时间
125-
ORIGIN_TIME_ZONE=$(date -R|awk '{printf"%d",$6}')
126-
LOCAL_TIME_ZONE=${ORIGIN_TIME_ZONE%00}
127-
BEIJING_ZONE=8
128-
BEIJING_UPDATE_TIME=3
129-
DIFF_ZONE=$[$BEIJING_ZONE-$LOCAL_TIME_ZONE]
130-
LOCAL_TIME=$[$BEIJING_UPDATE_TIME-$DIFF_ZONE]
131-
if [ $LOCAL_TIME -lt 0 ];then
132-
LOCAL_TIME=$[24+$LOCAL_TIME]
133-
elif [ $LOCAL_TIME -ge 24 ];then
134-
LOCAL_TIME=$[$LOCAL_TIME-24]
125+
origin_time_zone=$(date -R|awk '{printf"%d",$6}')
126+
local_time_zone=${origin_time_zone%00}
127+
beijing_zone=8
128+
beijing_update_time=3
129+
diff_zone=$[$beijing_zone-$local_time_zone]
130+
local_time=$[$beijing_update_time-$diff_zone]
131+
if [ $local_time -lt 0 ];then
132+
local_time=$[24+$local_time]
133+
elif [ $local_time -ge 24 ];then
134+
local_time=$[$local_time-24]
135135
fi
136136
crontab -l 2>/dev/null|sed '/acme.sh/d' > crontab.txt
137-
echo "0 ${LOCAL_TIME}"' * * * systemctl stop trojan-web; "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null; systemctl start trojan-web' >> crontab.txt
137+
echo "0 ${local_time}"' * * * systemctl stop trojan-web; "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null; systemctl start trojan-web' >> crontab.txt
138138
crontab crontab.txt
139139
rm -f crontab.txt
140140
fi
141141
fi
142142
}
143143

144144
installTrojan(){
145-
local SHOW_TIP=0
146-
if [[ $UPDATE == 1 ]];then
145+
local show_tip=0
146+
if [[ $update == 1 ]];then
147147
systemctl stop trojan-web >/dev/null 2>&1
148148
rm -f /usr/local/bin/trojan
149149
fi
150-
LASTEST_VERSION=$(curl -H 'Cache-Control: no-cache' -s "$VERSION_CHECK" | grep 'tag_name' | cut -d\" -f4)
151-
echo "正在下载管理程序`colorEcho $BLUE $LASTEST_VERSION`版本..."
152-
[[ $ARCH == x86_64 ]] && BIN="trojan-linux-amd64" || BIN="trojan-linux-arm64"
153-
curl -L "$DOWNLAOD_URL/$LASTEST_VERSION/$BIN" -o /usr/local/bin/trojan
150+
lastest_version=$(curl -H 'Cache-Control: no-cache' -s "$version_check" | grep 'tag_name' | cut -d\" -f4)
151+
echo "正在下载管理程序`colorEcho $blue $lastest_version`版本..."
152+
[[ $arch == x86_64 ]] && bin="trojan-linux-amd64" || bin="trojan-linux-arm64"
153+
curl -L "$download_url/$lastest_version/$bin" -o /usr/local/bin/trojan
154154
chmod +x /usr/local/bin/trojan
155155
if [[ ! -e /etc/systemd/system/trojan-web.service ]];then
156-
SHOW_TIP=1
157-
curl -L $SERVICE_URL -o /etc/systemd/system/trojan-web.service
156+
show_tip=1
157+
curl -L $service_url -o /etc/systemd/system/trojan-web.service
158158
systemctl daemon-reload
159159
systemctl enable trojan-web
160160
fi
161161
#命令补全环境变量
162-
[[ -z $(grep trojan ~/.${SHELL_WAY}rc) ]] && echo "source <(trojan completion ${SHELL_WAY})" >> ~/.${SHELL_WAY}rc
163-
source ~/.${SHELL_WAY}rc
164-
if [[ $UPDATE == 0 ]];then
165-
colorEcho $GREEN "安装trojan管理程序成功!\n"
166-
echo -e "运行命令`colorEcho $BLUE trojan`可进行trojan管理\n"
162+
[[ -z $(grep trojan ~/.${shell_way}rc) ]] && echo "source <(trojan completion ${shell_way})" >> ~/.${shell_way}rc
163+
source ~/.${shell_way}rc
164+
if [[ $update == 0 ]];then
165+
colorEcho $green "安装trojan管理程序成功!\n"
166+
echo -e "运行命令`colorEcho $blue trojan`可进行trojan管理\n"
167167
/usr/local/bin/trojan
168168
else
169169
if [[ `cat /usr/local/etc/trojan/config.json|grep -w "\"db\""` ]];then
@@ -175,19 +175,19 @@ installTrojan(){
175175
/usr/local/bin/trojan upgrade config
176176
fi
177177
systemctl restart trojan-web
178-
colorEcho $GREEN "更新trojan管理程序成功!\n"
178+
colorEcho $green "更新trojan管理程序成功!\n"
179179
fi
180180
setupCron
181-
[[ $SHOW_TIP == 1 ]] && echo "浏览器访问'`colorEcho $BLUE https://域名`'可在线trojan多用户管理"
181+
[[ $show_tip == 1 ]] && echo "浏览器访问'`colorEcho $blue https://域名`'可在线trojan多用户管理"
182182
}
183183

184184
main(){
185-
[[ ${HELP} == 1 ]] && help && return
186-
[[ ${REMOVE} == 1 ]] && removeTrojan && return
187-
[[ $UPDATE == 0 ]] && echo "正在安装trojan管理程序.." || echo "正在更新trojan管理程序.."
185+
[[ ${help} == 1 ]] && help && return
186+
[[ ${remove} == 1 ]] && removeTrojan && return
187+
[[ $update == 0 ]] && echo "正在安装trojan管理程序.." || echo "正在更新trojan管理程序.."
188188
checkSys
189-
[[ $UPDATE == 0 ]] && installDependent
189+
[[ $update == 0 ]] && installDependent
190190
installTrojan
191191
}
192192

193-
main
193+
main

update.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

33
#获取当前的这个脚本所在绝对路径
4-
SHELL_PATH=$(cd `dirname $0`; pwd)
4+
shell_path=$(cd `dirname $0`; pwd)
55

6-
cd $SHELL_PATH
6+
cd $shell_path
77

88
mkdir -p web/templates
99

1010
touch web/templates/test
1111

1212
go get -u
1313

14-
rm -rf web/templates
14+
rm -rf web/templates

0 commit comments

Comments
 (0)