Skip to content

Commit

Permalink
Merge pull request #378 from BishopFox/stage
Browse files Browse the repository at this point in the history
Improved CLI UX
  • Loading branch information
moloch-- authored Apr 8, 2021
2 parents deef600 + 64ab365 commit 33ffca6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
6 changes: 4 additions & 2 deletions server/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ func saveAssetVersion(appDir string) {
}

// Setup - Extract or create local assets
func Setup(force bool) {
func Setup(force bool, echo bool) {
appDir := GetRootAppDir()
localVer := assetVersion()
if force || localVer == "" || localVer != ver.GitCommit {
setupLog.Infof("Version mismatch %v != %v", localVer, ver.GitCommit)
fmt.Printf("Unpacking assets ...\n")
if echo {
fmt.Printf("Unpacking assets ...\n")
}
setupGo(appDir)
setupCodenames(appDir)
setupDllPath(appDir)
Expand Down
17 changes: 9 additions & 8 deletions server/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,24 @@ func init() {

// Operator
operatorCmd.Flags().StringP(nameFlagStr, "n", "", "operator name")
operatorCmd.Flags().StringP(lhostFlagStr, "l", "", "listener host")
operatorCmd.Flags().Uint16P(lportFlagStr, "p", uint16(1337), "listener port")
operatorCmd.Flags().StringP(lhostFlagStr, "l", "", "multiplayer listener host")
operatorCmd.Flags().Uint16P(lportFlagStr, "p", uint16(31337), "multiplayer listener port")
operatorCmd.Flags().StringP(saveFlagStr, "s", "", "save file to ...")
rootCmd.AddCommand(operatorCmd)

// Certs
cmdExportCA.Flags().StringP(saveFlagStr, "s", "", "save CA to file ...")
cmdExportCA.Flags().StringP(caTypeFlagStr, "t", "",
fmt.Sprintf("ca type (%s)", strings.Join(validCATypes(), ", ")))
cmdExportCA.Flags().StringP(caTypeFlagStr, "t", "", fmt.Sprintf("ca type (%s)", strings.Join(validCATypes(), ", ")))
rootCmd.AddCommand(cmdExportCA)

cmdImportCA.Flags().StringP(loadFlagStr, "l", "", "load CA from file ...")
cmdImportCA.Flags().StringP(caTypeFlagStr, "t", "",
fmt.Sprintf("ca type (%s)", strings.Join(validCATypes(), ", ")))
cmdImportCA.Flags().StringP(caTypeFlagStr, "t", "", fmt.Sprintf("ca type (%s)", strings.Join(validCATypes(), ", ")))
rootCmd.AddCommand(cmdImportCA)

// Daemon
daemonCmd.Flags().StringP(lhostFlagStr, "l", daemon.BlankHost, "multiplayer listener host")
daemonCmd.Flags().Uint16P(lportFlagStr, "p", daemon.BlankPort, "multiplayer listener port")
daemonCmd.Flags().BoolP(forceFlagStr, "f", false, "force unpack and overwrite static assets")
rootCmd.AddCommand(daemonCmd)

// Version
Expand All @@ -121,14 +122,14 @@ var rootCmd = &cobra.Command{
}
}()

assets.Setup(false)
assets.Setup(false, true)
certs.SetupCAs()
certs.SetupWGKeys()

serverConfig := configs.GetServerConfig()
c2.StartPersistentJobs(serverConfig)
if serverConfig.DaemonMode {
daemon.Start()
daemon.Start(daemon.BlankHost, daemon.BlankPort)
} else {
os.Args = os.Args[:1] // Hide cli from grumble console
console.Start()
Expand Down
48 changes: 46 additions & 2 deletions server/cli/daemon.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,59 @@
package cli

import (
"fmt"
"log"
"os"
"runtime/debug"

"github.com/bishopfox/sliver/server/assets"
"github.com/bishopfox/sliver/server/c2"
"github.com/bishopfox/sliver/server/certs"
"github.com/bishopfox/sliver/server/configs"
"github.com/bishopfox/sliver/server/daemon"
"github.com/spf13/cobra"
)

var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Start server in daemon mode",
Short: "Force start server in daemon mode",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
daemon.Start()
force, err := cmd.Flags().GetBool(forceFlagStr)
if err != nil {
fmt.Printf("Failed to parse --%s flag %s\n", forceFlagStr, err)
return
}
lhost, err := cmd.Flags().GetString(lhostFlagStr)
if err != nil {
fmt.Printf("Failed to parse --%s flag %s\n", lhostFlagStr, err)
return
}
lport, err := cmd.Flags().GetUint16(lportFlagStr)
if err != nil {
fmt.Printf("Failed to parse --%s flag %s\n", lportFlagStr, err)
return
}

appDir := assets.GetRootAppDir()
logFile := initLogging(appDir)
defer logFile.Close()

defer func() {
if r := recover(); r != nil {
log.Printf("panic:\n%s", debug.Stack())
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
os.Exit(99)
}
}()

assets.Setup(force, false)
certs.SetupCAs()
certs.SetupWGKeys()

serverConfig := configs.GetServerConfig()
c2.StartPersistentJobs(serverConfig)

daemon.Start(lhost, uint16(lport))
},
}
2 changes: 1 addition & 1 deletion server/cli/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ var unpackCmd = &cobra.Command{
return
}

assets.Setup(force)
assets.Setup(force, true)
},
}
18 changes: 15 additions & 3 deletions server/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@ import (
var (
serverConfig = configs.GetServerConfig()
daemonLog = log.NamedLogger("daemon", "main")

BlankHost = "-"
BlankPort = uint16(0)
)

// Start - Start as daemon process
func Start() {
host := serverConfig.DaemonConfig.Host
port := uint16(serverConfig.DaemonConfig.Port)
func Start(host string, port uint16) {

// cli args take president over config
if host == BlankHost {
daemonLog.Info("No cli lhost, using config file or default value")
host = serverConfig.DaemonConfig.Host
}
if port == BlankPort {
daemonLog.Info("No cli lport, using config file or default value")
port = uint16(serverConfig.DaemonConfig.Port)
}

daemonLog.Infof("Starting Sliver daemon %s:%d ...", host, port)
_, ln, err := transport.StartClientListener(host, port)
if err != nil {
Expand Down

0 comments on commit 33ffca6

Please sign in to comment.