Skip to content

Commit

Permalink
优化脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Sep 24, 2023
1 parent 1a94fd2 commit 13686aa
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 97 deletions.
58 changes: 29 additions & 29 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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
fi
130 changes: 65 additions & 65 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -110,60 +110,60 @@ 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
}

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
fi
}

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
Expand All @@ -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
main
6 changes: 3 additions & 3 deletions update.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

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

cd $SHELL_PATH
cd $shell_path

mkdir -p web/templates

touch web/templates/test

go get -u

rm -rf web/templates
rm -rf web/templates

0 comments on commit 13686aa

Please sign in to comment.