Skip to content

Commit

Permalink
🛸使用go-github-update框架,替代原本go-update框架。原本框架在Linux体验并不是很好
Browse files Browse the repository at this point in the history
  • Loading branch information
SummerSec committed May 18, 2022
1 parent 25708c0 commit 3548f88
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 119 deletions.
11 changes: 9 additions & 2 deletions cmd/commons/core/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ func (r *Runner) Run() {
} else if r.options.IP != "" {
urls = utils.GetIPToUrlsLinks(r.options.IP, urls)
} else if r.options.Update {
selfUpdate()
// go update
//selfUpdate()
// go github update
doSelfUpdate()
return
} else if r.options.Version {
getLatestVersion()
// go update
//getLatestVersion()
// go github update
confirmAndSelfUpdate()
return
} else if r.options.SP {
return
Expand Down
217 changes: 113 additions & 104 deletions cmd/commons/core/update.go
Original file line number Diff line number Diff line change
@@ -1,106 +1,115 @@
package core

import (
log "github.com/sirupsen/logrus"
"github.com/tj/go-update"
"github.com/tj/go-update/progress"
githubUpdateStore "github.com/tj/go-update/stores/github"
"runtime"
"strings"
)

const (
Owner = "SummerSec"
Repo = "SpringExploit"
)

func selfUpdate() {
var command string
switch runtime.GOOS {
case "windows":
command = Repo + ".exe"
default:
command = Repo
}
m := &update.Manager{
Command: command,
Store: &githubUpdateStore.Store{
Owner: Owner,
Repo: Repo,
Version: version,
},
}

releases, err := m.LatestReleases()
if err != nil {
log.Error("Failed to get releases", err)
return
}
if len(releases) == 0 {
log.Info("No updates available")
return
}
latest := releases[0]
var currentOS string
switch runtime.GOOS {
case "darwin":
currentOS = "macOS"
default:
currentOS = runtime.GOOS
}
final := latest.FindZip(currentOS, runtime.GOARCH)
if final == nil {
log.Error("No update available for", currentOS, "and", runtime.GOARCH)
}
tarball, err := final.DownloadProxy(progress.Reader)
if err != nil {
log.Error("could not install latest release ", err)
return
}
if err := m.Install(tarball); err != nil {
log.Error("could not install latest release", err)
return
}

log.Infof("Successfully Updated to %s Version %s", Repo, latest.Version)

}

// 获取最新版本
func getLatestVersion() {
log.Info("Crrunent Version : ", version)
latestverion := getLatestVersionFromGithub()
log.Infof("Latest Version: %s", latestverion)
if strings.Compare(latestverion, version) > 0 {
log.Info("Use Command SpringExploit -update to update to latest version ")
}

}

// 从github获取最新版本
func getLatestVersionFromGithub() string {
m := &update.Manager{
Store: &githubUpdateStore.Store{
Owner: Owner,
Repo: Repo,
Version: version,
},
}
releases, err := m.LatestReleases()
if err != nil {
log.Error("Failed to get releases ", err)
return ""
}
defer func() {
if errs := recover(); errs != nil {
log.Debug("No updates available ", errs)
}
}()

if releases == nil {
log.Info("No updates available")
return version
} else {
return releases[0].Version
}
}
//
//import (
// log "github.com/sirupsen/logrus"
// "github.com/tj/go-update"
// "github.com/tj/go-update/progress"
// githubUpdateStore "github.com/tj/go-update/stores/github"
// "runtime"
// "strings"
//)
//
//const (
// Owner = "SummerSec"
// Repo = "SpringExploit"
//)
//
//func selfUpdate() {
// var command string
//
// //// get current executable path
// //executable, err := os.Executable()
// //if err != nil {
// // log.Fatal(err)
// //}
//
// switch runtime.GOOS {
// case "windows":
// command = Repo + ".exe"
// default:
// command = Repo
// }
// log.Debugf("command: %s", command)
// m := &update.Manager{
// Command: command,
// Store: &githubUpdateStore.Store{
// Owner: Owner,
// Repo: Repo,
// Version: version,
// },
// }
//
// releases, err := m.LatestReleases()
// if err != nil {
// log.Error("Failed to get releases", err)
// return
// }
// if len(releases) == 0 {
// log.Info("No updates available")
// return
// }
// latest := releases[0]
// var currentOS string
// switch runtime.GOOS {
// case "darwin":
// currentOS = "macOS"
// default:
// currentOS = runtime.GOOS
// }
// final := latest.FindZip(currentOS, runtime.GOARCH)
// if final == nil {
// log.Error("No update available for", currentOS, "and", runtime.GOARCH)
// }
// tarball, err := final.DownloadProxy(progress.Reader)
// if err != nil {
// log.Error("could not install latest release ", err)
// return
// }
// if err := m.Install(tarball); err != nil {
// log.Error("could not install latest release", err)
// return
// }
//
// log.Infof("Successfully Updated to %s Version %s", Repo, latest.Version)
//
//}
//
//// 获取最新版本
//func getLatestVersion() {
// log.Info("Crrunent Version : ", version)
// latestverion := getLatestVersionFromGithub()
// log.Infof("Latest Version: %s", latestverion)
// if strings.Compare(latestverion, version) > 0 {
// log.Info("Use Command SpringExploit -update to update to latest version ")
// }
//
//}
//
//// 从github获取最新版本
//func getLatestVersionFromGithub() string {
// m := &update.Manager{
// Store: &githubUpdateStore.Store{
// Owner: Owner,
// Repo: Repo,
// Version: version,
// },
// }
// releases, err := m.LatestReleases()
// if err != nil {
// log.Error("Failed to get releases ", err)
// return ""
// }
// defer func() {
// if errs := recover(); errs != nil {
// log.Debug("No updates available ", errs)
// }
// }()
//
// if releases == nil {
// log.Info("No updates available")
// return version
// } else {
// return releases[0].Version
// }
//}
71 changes: 71 additions & 0 deletions cmd/commons/core/update2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package core

import (
"bufio"
"fmt"
"github.com/blang/semver"
"github.com/rhysd/go-github-selfupdate/selfupdate"
log "github.com/sirupsen/logrus"
"os"
)

const info = "SummerSec/SpringExploit"

func doSelfUpdate() {
latest, found, err := selfupdate.DetectLatest(info)
if err != nil {
log.Infoln("Error occurred while detecting version:", err)
return
}

v := semver.MustParse(version)
if !found || latest.Version.LTE(v) {
log.Infof("Current binary is the latest version %s", version)
return
}

latest1, err := selfupdate.UpdateSelf(v, info)

if latest1.Version.LTE(v) {
log.Infof("Current binary is the latest version %s", version)

} else {
log.Infoln("Successfully updated to version", latest.Version)
log.Infoln("Release note:\n", latest.ReleaseNotes)
}
}

func confirmAndSelfUpdate() {
latest, found, err := selfupdate.DetectLatest(info)
if err != nil {
log.Infoln("Error occurred while detecting version:", err)
return
}

v := semver.MustParse(version)
if !found || latest.Version.LTE(v) {
log.Infof("Current version is the latest version %s", version)
return
}

fmt.Print("Do you want to update to", latest.Version, "? (y/n): ")
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil || (input != "y\n" && input != "n\n") {
log.Println("Invalid input")
return
}
if input == "n\n" {
return
}

exe, err := os.Executable()
if err != nil {
log.Println("Could not locate executable path")
return
}
if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
log.Println("Error occurred while updating binary:", err)
return
}
log.Println("Successfully updated to version", latest.Version)
}
21 changes: 21 additions & 0 deletions cmd/test/ip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"strings"
)

Expand All @@ -21,4 +22,24 @@ func main() {

println(u1)
println(u2)

// get the current directory
dir, err := os.Getwd()
if err != nil {
panic(err)
}
println(dir)

// get current executable path
executable, err := os.Executable()
if err != nil {
}
println(executable)

d, err := os.Stat("SpringExploit.exe")
if err != nil {

}
println(d.Name())

}
23 changes: 10 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ require (
)

require (
github.com/apex/log v1.9.0 // indirect
github.com/c-bata/go-prompt v0.2.6 // indirect
github.com/c4milo/unpackit v0.1.0 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
//github.com/apex/log v1.9.0 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/c-bata/go-prompt v0.2.6
//github.com/c4milo/unpackit v0.1.0 // indirect
//github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gosuri/uilive v0.0.4 // indirect
github.com/gosuri/uiprogress v0.0.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/panjf2000/ants/v2 v2.5.0 // indirect
github.com/projectdiscovery/blackrock v0.0.0-20210415162320-b38689ae3a2e // indirect
github.com/tj/go-update v2.2.5-0.20200519121640-62b4b798fd68+incompatible
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect
golang.org/x/text v0.3.7 // indirect
//github.com/gosuri/uilive v0.0.4 // indirect
//github.com/gosuri/uiprogress v0.0.1 // indirect
github.com/panjf2000/ants/v2 v2.5.0
github.com/rhysd/go-github-selfupdate v1.2.3
//github.com/tj/go-update v2.2.5-0.20200519121640-62b4b798fd68+incompatible
)
Loading

0 comments on commit 3548f88

Please sign in to comment.