Skip to content

Commit

Permalink
添加安装指定版本trojan
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed May 28, 2021
1 parent f82b7a7 commit 191856f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
31 changes: 29 additions & 2 deletions asset/trojan-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ set -eo pipefail
# trojan: 0, trojan-go: 1
TYPE=0

[[ $1 == "go" ]] && TYPE=1
INSTALL_VERSION=""

while [[ $# > 0 ]];do
KEY="$1"
case $KEY in
-v|--version)
INSTALL_VERSION="$2"
echo -e "prepare install $INSTALL_VERSION version..\n"
shift
;;
-g|--go)
TYPE=1
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
#############################

function prompt() {
while true; do
Expand Down Expand Up @@ -38,7 +57,15 @@ else
CHECKVERSION="https://api.github.com/repos/p4gefau1t/trojan-go/releases"
fi
NAME=trojan
VERSION=$(curl -H 'Cache-Control: no-cache' -s "$CHECKVERSION" | grep 'tag_name' | cut -d\" -f4 | sed 's/v//g' | head -n 1)
if [[ -z $INSTALL_VERSION ]];then
VERSION=$(curl -H 'Cache-Control: no-cache' -s "$CHECKVERSION" | grep 'tag_name' | cut -d\" -f4 | sed 's/v//g' | head -n 1)
else
if [[ -z `curl -H 'Cache-Control: no-cache' -s "$CHECKVERSION"|grep $INSTALL_VERSION` ]];then
echo "no $INSTALL_VERSION version file!"
exit 1
fi
VERSION=`echo "$INSTALL_VERSION"|sed 's/v//g'`
fi
if [[ $TYPE == 0 ]];then
TARBALL="$NAME-$VERSION-linux-amd64.tar.xz"
DOWNLOADURL="https://github.com/trojan-gfw/$NAME/releases/download/v$VERSION/$TARBALL"
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Execute() {
func check() {
if !util.IsExists("/usr/local/etc/trojan/config.json") {
fmt.Println("本机未安装trojan, 正在自动安装...")
trojan.InstallTrojan()
trojan.InstallTrojan("")
core.WritePassword(nil)
trojan.InstallTls()
trojan.InstallMysql()
Expand Down
8 changes: 7 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import (
var updateCmd = &cobra.Command{
Use: "update",
Short: "更新trojan",
Long: "可添加版本号更新特定版本, 比如'trojan update v0.10.0', 不添加版本号则安装最新版",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
trojan.InstallTrojan()
version := ""
if len(args) == 1 {
version = args[0]
}
trojan.InstallTrojan(version)
},
}

Expand Down
7 changes: 5 additions & 2 deletions trojan/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func InstallMenu() {
menu := []string{"更新trojan", "证书申请", "安装mysql"}
switch util.LoopInput("请选择: ", menu, true) {
case 1:
InstallTrojan()
InstallTrojan("")
case 2:
InstallTls()
case 3:
Expand All @@ -49,13 +49,16 @@ func InstallDocker() {
}

// InstallTrojan 安装trojan
func InstallTrojan() {
func InstallTrojan(version string) {
fmt.Println()
data := string(asset.GetAsset("trojan-install.sh"))
checkTrojan := util.ExecCommandWithResult("systemctl list-unit-files|grep trojan.service")
if (checkTrojan == "" && runtime.GOARCH != "amd64") || Type() == "trojan-go" {
data = strings.ReplaceAll(data, "TYPE=0", "TYPE=1")
}
if version != "" {
data = strings.ReplaceAll(data, "INSTALL_VERSION=\"\"", "INSTALL_VERSION=\""+version+"\"")
}
util.ExecCommand(data)
util.OpenPort(443)
util.ExecCommand("systemctl restart trojan")
Expand Down
2 changes: 1 addition & 1 deletion trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func SwitchType(tType string) error {
if err := core.SetValue("trojanType", tType); err != nil {
return err
}
InstallTrojan()
InstallTrojan("")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion web/controller/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Restart() *ResponseBody {
func Update() *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
trojan.InstallTrojan()
trojan.InstallTrojan("")
return &responseBody
}

Expand Down

0 comments on commit 191856f

Please sign in to comment.