From 04f38e26cfc3a88131fc2b13eb1e065e22e3e835 Mon Sep 17 00:00:00 2001 From: Jrohy Date: Sun, 23 Feb 2020 16:33:29 +0800 Subject: [PATCH] Add install.sh --- asset/upload_asset.sh | 49 +++++++++++++++ install.sh | 135 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 asset/upload_asset.sh diff --git a/asset/upload_asset.sh b/asset/upload_asset.sh new file mode 100644 index 00000000..4c3ed5a1 --- /dev/null +++ b/asset/upload_asset.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +GITHUB_TOKEN="" + +PROJECT="Jrohy/trojan" + +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/,//'` + +function uploadfile() { + 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)" + + 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 +} + +pushd `pwd` &>/dev/null + +go get -u github.com/gobuffalo/packr/packr + +packr build -ldflags "-s -w" -o "result/trojan" .. + +cd result + +UPLOAD_ITEM=($(ls -l|awk '{print $9}'|xargs -r)) + +for ITEM in ${UPLOAD_ITEM[@]} +do + upload $ITEM +done + +echo "" +echo "upload completed!" + +popd &>/dev/null + +rm -rf result \ No newline at end of file diff --git a/install.sh b/install.sh index 45e54451..178874cb 100644 --- a/install.sh +++ b/install.sh @@ -1,4 +1,135 @@ +#!/bin/bash +# Author: Jrohy +# github: https://github.com/Jrohy/trojan -docker run -d --name test --net=host --privileged trojan-manager /sbin/init +#定义操作变量, 0为否, 1为是 +HELP=0 -docker run -it -d --name test -v /var/run/docker.sock:/var/run/docker.sock -v `which docker`:`which docker` centos tail -f \ No newline at end of file +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" + echo " no params to new install" + return 0 +} + +removeTrojan() { + #移除trojan + rm -rf /usr/bin/trojan + rm -rf /usr/local/etc/trojan + rm -f /etc/systemd/system/trojan.service + systemctl daemon-reload + + #移除trojan管理程序 + rm -f /usr/local/bin/trojan + rm -rf /var/lib/trojan-manager + + #移除环境变量 + 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 + if [[ ${PACKAGE_MANAGER} == 'yum' ]];then + ${PACKAGE_MANAGER} ntpdate -y + fi + ${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 | sed 's/v//g') + curl -L "$DOWNLAOD_URL/$LASTEST_VERSION/trojan" -o /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管理" + trojan + else + colorEcho $GREEN "更新trojan管理程序成功!\n" + fi +} + +main(){ + [[ ${HELP} == 1 ]] && help && return + [[ ${REMOVE} == 1 ]] && removeTrojan && return + checkSys + installDependent + installTrojan +} + +main \ No newline at end of file