-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pinohans
committed
May 31, 2021
0 parents
commit f5d4d2f
Showing
12 changed files
with
1,182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package main | ||
|
||
import ( | ||
"go/build" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func Build() error { | ||
ctx := build.Default | ||
ldflags := `-s -w` | ||
if config.WindowsHide == "1" { | ||
ldflags += " -H=windowsgui" | ||
} | ||
if config.NoStatic == "1" { | ||
ldflags += ` -extldflags '-static'` | ||
} | ||
|
||
ctx.Dir = filepath.Join(newGopath, "src") | ||
arguments := []string{"build", "-trimpath", "-ldflags", ldflags, "-tags", config.Tags, "-o", outputPath, "."} | ||
cmd := exec.Command("go", arguments...) | ||
cmd.Env = environBuild | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.Dir = filepath.Join(newGopath, "src") | ||
log.Println("go " + strings.Join(cmd.Args, " ")) | ||
if err := cmd.Run(); err != nil { | ||
log.Fatal("Failed to run BuildSrc: ", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/json" | ||
"fmt" | ||
"go/build" | ||
"io/ioutil" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
MainPkgDir string // dir of main package | ||
OutputPath string // path of dist | ||
NewGopath string // dir of new GOPATH | ||
Tags string // tags are passed to the go compiler | ||
GOOS string // target os | ||
GOARCH string // target arch | ||
CGOENABLED string // cgo enable | ||
WindowsHide string // hide windows GUI | ||
NoStatic string // no static link | ||
} | ||
|
||
var ( | ||
config = Config{ | ||
MainPkgDir: ".", | ||
OutputPath: "dist", | ||
NewGopath: "pkg", | ||
Tags: "", | ||
GOOS: "windows", | ||
GOARCH: "amd64", | ||
CGOENABLED: "0", | ||
WindowsHide: "1", | ||
NoStatic: "1", | ||
} | ||
) | ||
|
||
var ( | ||
mainPkgDir = func() string { | ||
var err error | ||
var ret string | ||
if ret, err = filepath.Abs(config.MainPkgDir); err != nil { | ||
log.Fatalln("Failed to get abs of NewGopath: ", err) | ||
} | ||
if err = os.MkdirAll(ret, 0755); err != nil { | ||
log.Fatalln("Failed to MkdirAll NewGopath: ", err) | ||
} | ||
return ret | ||
}() | ||
|
||
newGopath = func() string { | ||
var err error | ||
var ret string | ||
if ret, err = filepath.Abs(config.NewGopath); err != nil { | ||
log.Fatalln("Failed to get abs of NewGopath: ", err) | ||
} | ||
if err = os.MkdirAll(ret, 0755); err != nil { | ||
log.Fatalln("Failed to MkdirAll NewGopath: ", err) | ||
} | ||
return ret | ||
}() | ||
|
||
outputPath = func() string { | ||
if ret, err := filepath.Abs(config.OutputPath); err != nil { | ||
log.Fatalln("Failed to get abs of OutputDir: ", err) | ||
return "" | ||
} else { | ||
return ret | ||
} | ||
}() | ||
|
||
randMd5 = func() func() string { | ||
rand.Seed(time.Now().Unix()) | ||
return func() string { | ||
buf := make([]byte, 32) | ||
rand.Read(buf) | ||
return fmt.Sprintf("%x", md5.Sum(buf)) | ||
} | ||
}() | ||
|
||
ctxt = func() *build.Context { | ||
ret := build.Default | ||
ret.GOPATH = newGopath | ||
ret.Dir = mainPkgDir | ||
ret.GOOS = config.GOOS | ||
ret.GOARCH = config.GOARCH | ||
return &ret | ||
}() | ||
|
||
environ = func() []string { | ||
return append(os.Environ(), | ||
"GOOS="+ctxt.GOOS, | ||
"GOARCH="+ctxt.GOARCH, | ||
"GOROOT="+ctxt.GOROOT, | ||
"GOPATH="+ctxt.GOPATH, | ||
"CGO_ENABLED="+config.CGOENABLED, | ||
"GO111MODULE=auto", | ||
) | ||
}() | ||
|
||
environBuild = func() []string { | ||
return append(os.Environ(), | ||
"GOOS="+ctxt.GOOS, | ||
"GOARCH="+ctxt.GOARCH, | ||
"GOROOT="+ctxt.GOROOT, | ||
"GOPATH="+ctxt.GOPATH, | ||
"CGO_ENABLED="+config.CGOENABLED, | ||
"GO111MODULE=off", | ||
) | ||
}() | ||
) | ||
|
||
func (c *Config) LoadConfig(configFilename string) error { | ||
|
||
configFile, err := os.OpenFile(configFilename, os.O_RDONLY, 0644) | ||
if err != nil { | ||
log.Println("Failed to Open config File: ", err) | ||
return err | ||
} | ||
configJson, err := ioutil.ReadAll(configFile) | ||
if err != nil { | ||
log.Println("Failed to ReadAll config File: ", err) | ||
return err | ||
} | ||
configTemp := Config{} | ||
if err = json.Unmarshal(configJson, &configTemp); err != nil { | ||
log.Println("Failed to Unmarshal config File: ", err) | ||
return err | ||
} | ||
if configTemp.MainPkgDir != "" { | ||
config.MainPkgDir = configTemp.MainPkgDir | ||
} | ||
if configTemp.OutputPath != "" { | ||
config.OutputPath = configTemp.OutputPath | ||
} | ||
if configTemp.NewGopath != "" { | ||
config.NewGopath = configTemp.NewGopath | ||
} | ||
if configTemp.Tags != "" { | ||
config.Tags = configTemp.Tags | ||
} | ||
if configTemp.GOOS != "" { | ||
config.GOOS = configTemp.GOOS | ||
} | ||
if configTemp.GOARCH != "" { | ||
config.GOARCH = configTemp.GOARCH | ||
} | ||
if configTemp.CGOENABLED != "" { | ||
config.CGOENABLED = configTemp.CGOENABLED | ||
} | ||
if configTemp.WindowsHide != "" { | ||
config.WindowsHide = configTemp.WindowsHide | ||
} | ||
if configTemp.NoStatic != "" { | ||
config.NoStatic = configTemp.NoStatic | ||
} | ||
if err = configFile.Close(); err != nil { | ||
log.Println("Failed to Close config File: ", err) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"MainPkgDir": ".", | ||
"OutputPath": "dist", | ||
"NewGopath": "pkg", | ||
"Tags": "", | ||
"GOOS": "windows", | ||
"GOARCH": "amd64", | ||
"CGOENABLED": "0", | ||
"WindowsHide": "1", | ||
"NoStatic": "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module gobfuscator | ||
|
||
go 1.16 | ||
|
||
require github.com/pkg/errors v0.8.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package dependency | ||
|
||
import ( | ||
"go/build" | ||
"log" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
func Walk(ctx *build.Context, projectDir string, walkFunc func(pkg *build.Package) error) error { | ||
errChan := make(chan error, 0) | ||
done := false | ||
go func() { | ||
defer close(errChan) | ||
wg := sync.WaitGroup{} | ||
mapProcessImports := sync.Map{} | ||
rootPkg, err := ctx.ImportDir(projectDir, 0) | ||
if err != nil { | ||
log.Println("Failed to import projectDir: ", err) | ||
errChan <- err | ||
return | ||
} | ||
wg.Add(1) | ||
go func() { | ||
processImports(ctx, rootPkg, walkFunc, &wg, &mapProcessImports, errChan, &done) | ||
wg.Done() | ||
}() | ||
wg.Wait() | ||
}() | ||
var err error = nil | ||
select { | ||
case err = <-errChan: | ||
if err != nil { | ||
log.Println("Failed to walk: ", err) | ||
done = true | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func processImports(ctx *build.Context, pkg *build.Package, walkFunc func(pkg *build.Package) error, wg *sync.WaitGroup, mapProcessImports *sync.Map, errChan chan error, done *bool) { | ||
value, ok := mapProcessImports.Load(pkg.ImportPath) | ||
if (ok && value.(bool)) || *done { | ||
return | ||
} | ||
mapProcessImports.Store(pkg.ImportPath, true) | ||
log.Println(pkg.ImportPath) | ||
if err := walkFunc(pkg); err != nil { | ||
errChan <- err | ||
return | ||
} | ||
for _, pkgName := range pkg.Imports { | ||
var child *build.Package | ||
var err error | ||
if strings.HasPrefix(pkgName, ".") { | ||
child, err = ctx.Import(pkgName, pkg.Dir, 0) | ||
if err != nil { | ||
log.Println("Failed to Import child start with .: ", err) | ||
errChan <- err | ||
return | ||
} | ||
child.ImportPath = filepath.Join(pkg.ImportPath, child.ImportPath) | ||
} else { | ||
child, err = ctx.Import(pkgName, "", 0) | ||
if err != nil { | ||
log.Println("Failed to Import normal child: ", err) | ||
errChan <- err | ||
return | ||
} | ||
} | ||
if child.Goroot { | ||
continue | ||
} | ||
wg.Add(1) | ||
go func() { | ||
processImports(ctx, child, walkFunc, wg, mapProcessImports, errChan, done) | ||
wg.Done() | ||
}() | ||
} | ||
} |
Oops, something went wrong.