From 13686aa255703d726d3780edb83b6dab0dc71640 Mon Sep 17 00:00:00 2001 From: Jrohy Date: Sun, 24 Sep 2023 22:59:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 58 ++++++++++++------------ install.sh | 130 ++++++++++++++++++++++++++--------------------------- update.sh | 6 +-- 3 files changed, 97 insertions(+), 97 deletions(-) diff --git a/build.sh b/build.sh index 67dbd966..2de4122a 100644 --- a/build.sh +++ b/build.sh @@ -1,62 +1,62 @@ #!/bin/bash -GITHUB_TOKEN="" +github_token="" -PROJECT="Jrohy/trojan" +project="Jrohy/trojan" #获取当前的这个脚本所在绝对路径 -SHELL_PATH=$(cd `dirname $0`; pwd) +shell_path=$(cd `dirname $0`; pwd) function uploadfile() { - FILE=$1 - CTYPE=$(file -b --mime-type $FILE) + file=$1 + ctype=$(file -b --mime-type $file) - 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)" + 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)" echo "" } function upload() { - FILE=$1 - DGST=$1.dgst - openssl dgst -md5 $FILE | sed 's/([^)]*)//g' >> $DGST - openssl dgst -sha1 $FILE | sed 's/([^)]*)//g' >> $DGST - openssl dgst -sha256 $FILE | sed 's/([^)]*)//g' >> $DGST - openssl dgst -sha512 $FILE | sed 's/([^)]*)//g' >> $DGST - uploadfile $FILE - uploadfile $DGST + file=$1 + dgst=$1.dgst + openssl dgst -md5 $file | sed 's/([^)]*)//g' >> $dgst + openssl dgst -sha1 $file | sed 's/([^)]*)//g' >> $dgst + openssl dgst -sha256 $file | sed 's/([^)]*)//g' >> $dgst + openssl dgst -sha512 $file | sed 's/([^)]*)//g' >> $dgst + uploadfile $file + uploadfile $dgst } -cd $SHELL_PATH +cd $shell_path -VERSION=`git describe --tags $(git rev-list --tags --max-count=1)` -NOW=`TZ=Asia/Shanghai date "+%Y%m%d-%H%M"` -GO_VERSION=`go version|awk '{print $3,$4}'` -GIT_VERSION=`git rev-parse HEAD` -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'" +version=`git describe --tags $(git rev-list --tags --max-count=1)` +now=`TZ=Asia/Shanghai date "+%Y%m%d-%H%M"` +go_version=`go version|awk '{print $3,$4}'` +git_version=`git rev-parse HEAD` +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'" -GOOS=linux GOARCH=amd64 go build -ldflags "$LDFLAGS" -o "result/trojan-linux-amd64" . -GOOS=linux GOARCH=arm64 go build -ldflags "$LDFLAGS" -o "result/trojan-linux-arm64" . +GOOS=linux GOARCH=amd64 go build -ldflags "$ldflags" -o "result/trojan-linux-amd64" . +GOOS=linux GOARCH=arm64 go build -ldflags "$ldflags" -o "result/trojan-linux-arm64" . if [[ $# == 0 ]];then cd result - UPLOAD_ITEM=($(ls -l|awk '{print $9}'|xargs -r)) + upload_item=($(ls -l|awk '{print $9}'|xargs -r)) - 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'"}' + 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'"}' sleep 2 - 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/,//'` + 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/,//'` - for ITEM in ${UPLOAD_ITEM[@]} + for item in ${upload_item[@]} do - upload $ITEM + upload $item done echo "upload completed!" - cd $SHELL_PATH + cd $shell_path rm -rf result -fi \ No newline at end of file +fi diff --git a/install.sh b/install.sh index 899bfc60..0ddd86c1 100644 --- a/install.sh +++ b/install.sh @@ -3,46 +3,46 @@ # github: https://github.com/Jrohy/trojan #定义操作变量, 0为否, 1为是 -HELP=0 +help=0 -REMOVE=0 +remove=0 -UPDATE=0 +update=0 -DOWNLAOD_URL="https://github.com/Jrohy/trojan/releases/download/" +download_url="https://github.com/Jrohy/trojan/releases/download/" -VERSION_CHECK="https://api.github.com/repos/Jrohy/trojan/releases/latest" +version_check="https://api.github.com/repos/Jrohy/trojan/releases/latest" -SERVICE_URL="https://raw.githubusercontent.com/Jrohy/trojan/master/asset/trojan-web.service" +service_url="https://raw.githubusercontent.com/Jrohy/trojan/master/asset/trojan-web.service" -[[ -e /var/lib/trojan-manager ]] && UPDATE=1 +[[ -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" +[[ -z $(echo $SHELL|grep zsh) ]] && shell_way="bash" || shell_way="zsh" #######color code######## -RED="31m" -GREEN="32m" -YELLOW="33m" -BLUE="36m" -FUCHSIA="35m" +red="31m" +green="32m" +yellow="33m" +blue="36m" +fuchsia="35m" colorEcho(){ - COLOR=$1 - echo -e "\033[${COLOR}${@:2}\033[0m" + color=$1 + echo -e "\033[${color}${@:2}\033[0m" } #######get params######### while [[ $# > 0 ]];do - KEY="$1" - case $KEY in + key="$1" + case $key in --remove) - REMOVE=1 + remove=1 ;; -h|--help) - HELP=1 + help=1 ;; *) # unknown option @@ -77,30 +77,30 @@ removeTrojan() { rm -rf /home/mysql /home/mariadb >/dev/null 2>&1 #移除环境变量 - sed -i '/trojan/d' ~/.${SHELL_WAY}rc - source ~/.${SHELL_WAY}rc + sed -i '/trojan/d' ~/.${shell_way}rc + source ~/.${shell_way}rc - colorEcho ${GREEN} "uninstall success!" + colorEcho ${green} "uninstall success!" } checkSys() { #检查是否为Root - [ $(id -u) != "0" ] && { colorEcho ${RED} "Error: You must be root to run this script"; exit 1; } + [ $(id -u) != "0" ] && { colorEcho ${red} "Error: You must be root to run this script"; exit 1; } - ARCH=$(uname -m 2> /dev/null) - if [[ $ARCH != x86_64 && $ARCH != aarch64 ]];then - colorEcho $YELLOW "not support $ARCH machine". + arch=$(uname -m 2> /dev/null) + if [[ $arch != x86_64 && $arch != aarch64 ]];then + colorEcho $yellow "not support $arch machine". exit 1 fi if [[ `command -v apt-get` ]];then - PACKAGE_MANAGER='apt-get' + package_manager='apt-get' elif [[ `command -v dnf` ]];then - PACKAGE_MANAGER='dnf' + package_manager='dnf' elif [[ `command -v yum` ]];then - PACKAGE_MANAGER='yum' + package_manager='yum' else - colorEcho $RED "Not support OS!" + colorEcho $red "Not support OS!" exit 1 fi @@ -110,11 +110,11 @@ checkSys() { #安装依赖 installDependent(){ - if [[ ${PACKAGE_MANAGER} == 'dnf' || ${PACKAGE_MANAGER} == 'yum' ]];then - ${PACKAGE_MANAGER} install socat crontabs bash-completion -y + if [[ ${package_manager} == 'dnf' || ${package_manager} == 'yum' ]];then + ${package_manager} install socat crontabs bash-completion -y else - ${PACKAGE_MANAGER} update - ${PACKAGE_MANAGER} install socat cron bash-completion xz-utils -y + ${package_manager} update + ${package_manager} install socat cron bash-completion xz-utils -y fi } @@ -122,19 +122,19 @@ setupCron() { if [[ `crontab -l 2>/dev/null|grep acme` ]]; then if [[ -z `crontab -l 2>/dev/null|grep trojan-web` || `crontab -l 2>/dev/null|grep trojan-web|grep "&"` ]]; then #计算北京时间早上3点时VPS的实际时间 - ORIGIN_TIME_ZONE=$(date -R|awk '{printf"%d",$6}') - LOCAL_TIME_ZONE=${ORIGIN_TIME_ZONE%00} - BEIJING_ZONE=8 - BEIJING_UPDATE_TIME=3 - DIFF_ZONE=$[$BEIJING_ZONE-$LOCAL_TIME_ZONE] - LOCAL_TIME=$[$BEIJING_UPDATE_TIME-$DIFF_ZONE] - if [ $LOCAL_TIME -lt 0 ];then - LOCAL_TIME=$[24+$LOCAL_TIME] - elif [ $LOCAL_TIME -ge 24 ];then - LOCAL_TIME=$[$LOCAL_TIME-24] + origin_time_zone=$(date -R|awk '{printf"%d",$6}') + local_time_zone=${origin_time_zone%00} + beijing_zone=8 + beijing_update_time=3 + diff_zone=$[$beijing_zone-$local_time_zone] + local_time=$[$beijing_update_time-$diff_zone] + if [ $local_time -lt 0 ];then + local_time=$[24+$local_time] + elif [ $local_time -ge 24 ];then + local_time=$[$local_time-24] fi crontab -l 2>/dev/null|sed '/acme.sh/d' > crontab.txt - 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 + 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 crontab crontab.txt rm -f crontab.txt fi @@ -142,28 +142,28 @@ setupCron() { } installTrojan(){ - local SHOW_TIP=0 - if [[ $UPDATE == 1 ]];then + local show_tip=0 + if [[ $update == 1 ]];then systemctl stop trojan-web >/dev/null 2>&1 rm -f /usr/local/bin/trojan fi - LASTEST_VERSION=$(curl -H 'Cache-Control: no-cache' -s "$VERSION_CHECK" | grep 'tag_name' | cut -d\" -f4) - echo "正在下载管理程序`colorEcho $BLUE $LASTEST_VERSION`版本..." - [[ $ARCH == x86_64 ]] && BIN="trojan-linux-amd64" || BIN="trojan-linux-arm64" - curl -L "$DOWNLAOD_URL/$LASTEST_VERSION/$BIN" -o /usr/local/bin/trojan + lastest_version=$(curl -H 'Cache-Control: no-cache' -s "$version_check" | grep 'tag_name' | cut -d\" -f4) + echo "正在下载管理程序`colorEcho $blue $lastest_version`版本..." + [[ $arch == x86_64 ]] && bin="trojan-linux-amd64" || bin="trojan-linux-arm64" + curl -L "$download_url/$lastest_version/$bin" -o /usr/local/bin/trojan chmod +x /usr/local/bin/trojan if [[ ! -e /etc/systemd/system/trojan-web.service ]];then - SHOW_TIP=1 - curl -L $SERVICE_URL -o /etc/systemd/system/trojan-web.service + show_tip=1 + curl -L $service_url -o /etc/systemd/system/trojan-web.service systemctl daemon-reload systemctl enable trojan-web fi #命令补全环境变量 - [[ -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 -e "运行命令`colorEcho $BLUE trojan`可进行trojan管理\n" + [[ -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 -e "运行命令`colorEcho $blue trojan`可进行trojan管理\n" /usr/local/bin/trojan else if [[ `cat /usr/local/etc/trojan/config.json|grep -w "\"db\""` ]];then @@ -175,19 +175,19 @@ installTrojan(){ /usr/local/bin/trojan upgrade config fi systemctl restart trojan-web - colorEcho $GREEN "更新trojan管理程序成功!\n" + colorEcho $green "更新trojan管理程序成功!\n" fi setupCron - [[ $SHOW_TIP == 1 ]] && echo "浏览器访问'`colorEcho $BLUE https://域名`'可在线trojan多用户管理" + [[ $show_tip == 1 ]] && echo "浏览器访问'`colorEcho $blue https://域名`'可在线trojan多用户管理" } main(){ - [[ ${HELP} == 1 ]] && help && return - [[ ${REMOVE} == 1 ]] && removeTrojan && return - [[ $UPDATE == 0 ]] && echo "正在安装trojan管理程序.." || echo "正在更新trojan管理程序.." + [[ ${help} == 1 ]] && help && return + [[ ${remove} == 1 ]] && removeTrojan && return + [[ $update == 0 ]] && echo "正在安装trojan管理程序.." || echo "正在更新trojan管理程序.." checkSys - [[ $UPDATE == 0 ]] && installDependent + [[ $update == 0 ]] && installDependent installTrojan } -main \ No newline at end of file +main diff --git a/update.sh b/update.sh index 785eac1e..776cb426 100644 --- a/update.sh +++ b/update.sh @@ -1,9 +1,9 @@ #!/bin/bash #获取当前的这个脚本所在绝对路径 -SHELL_PATH=$(cd `dirname $0`; pwd) +shell_path=$(cd `dirname $0`; pwd) -cd $SHELL_PATH +cd $shell_path mkdir -p web/templates @@ -11,4 +11,4 @@ touch web/templates/test go get -u -rm -rf web/templates \ No newline at end of file +rm -rf web/templates