Skip to content

Commit

Permalink
add garble support
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed May 31, 2024
1 parent d262dc5 commit 4c9981b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type GoBuilder struct {
ArchOSList []string `json:"arch_os_list"`
EnableCompress bool `json:"compress"`
EnableUPX bool `json:"upx"`
EnableGarble bool `json:"garble"`
BuildArgs []string `json:"build_args"`
WorkDir string `json:"work_dir"`
}
Expand Down Expand Up @@ -115,6 +116,11 @@ func (g *GoBuilder) saveBuilder(buildFile string) {
cfm.Run()
g.EnableUPX = cfm.Result()

// Enable garble for windows.
cfm = confirm.NewConfirm(confirm.WithTitle("Use garble to obfuscate for windows binary or not?"))
cfm.Run()
g.EnableGarble = cfm.Result()

g.parseArgs()

g.WorkDir = GetCurrentWorkingDir()
Expand Down Expand Up @@ -144,14 +150,16 @@ func (g *GoBuilder) parseArgs() {
}

func (g *GoBuilder) PackWithUPX(osInfo, archInfo, binDir, bName string) {
if !IsUPXInstalled() {
gprint.PrintWarning("upx if not found!")
return
}
// UPX cannot pack binaries for MacOS. Segment fault occurrs.
if !g.EnableUPX || osInfo == gutils.Darwin || (osInfo == gutils.Windows && archInfo != "amd64") {
return
}

if !IsUPXInstalled() {
gprint.PrintWarning("upx if not found!")
return
}

fmt.Println(gprint.YellowStr("Packing with UPX..."))
if g.EnableUPX {
binPath := filepath.Join(binDir, bName)
Expand Down Expand Up @@ -302,7 +310,15 @@ func (g *GoBuilder) prepareArgs(osInfo, archInfo string) (args []string, targetD
func (g *GoBuilder) build(osInfo, archInfo string) {
gprint.PrintInfo("Building for %s/%s...", osInfo, archInfo)
inputArgs, binDir, binName := g.prepareArgs(osInfo, archInfo)
args := append([]string{"go", "build"}, inputArgs...)

compiler := "go"
if g.EnableGarble && osInfo == gutils.Windows {
// use garble to obfuscate go binary.
CheckAndInstallGarble()
compiler = "garble"
}

args := append([]string{compiler, "build"}, inputArgs...)
g.clearArgs(args)

os.Setenv("GOOS", osInfo)
Expand Down
13 changes: 13 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"os"
"path/filepath"
"runtime"
"strings"

"github.com/gvcgo/goutils/pkgs/gutils"
Expand Down Expand Up @@ -80,3 +81,15 @@ func SetCurrentWorkingDir(dPath string) {
func GetCurrentWorkingDir() string {
return os.Getenv(CurrentWorkingDirEnv)
}

func CheckAndInstallGarble() {
garbleBin := "garble"
if runtime.GOOS == gutils.Windows {
garbleBin += ".exe"
}
_, err := gutils.ExecuteSysCommand(true, "", "garble", "version")
if err != nil {
// install garble
gutils.ExecuteSysCommand(true, "", "go", "install", "mvdan.cc/garble@latest")
}
}

0 comments on commit 4c9981b

Please sign in to comment.