Skip to content

Commit

Permalink
gopls deleted a bunch of code, fixed now
Browse files Browse the repository at this point in the history
  • Loading branch information
moloch-- committed Jan 17, 2021
1 parent 82d9b9f commit fce8028
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
6 changes: 5 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ const (
proxyFlagStr = "proxy"

// Compile - Standard Flags
outputFlagStr = "output"
outputFlagStr = "output"
allCodeFlagStr = "all"
verboseFlagStr = "verbose"

// Compile - Obfuscation Flags
bcfFlagStr = "bcf"
Expand Down Expand Up @@ -97,6 +99,8 @@ func init() {

// Compile - Standard options
compileCmd.Flags().StringP(outputFlagStr, "o", "", "output file")
compileCmd.Flags().BoolP(allCodeFlagStr, "a", false, "obfuscate all code including nim stdlib")
compileCmd.Flags().BoolP(verboseFlagStr, "v", false, "display verbose information")
rootCmd.AddCommand(compileCmd)

}
Expand Down
33 changes: 27 additions & 6 deletions cmd/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,43 @@ var compileCmd = &cobra.Command{
if !preflight() {
return
}

if len(args) < 1 {
fmt.Printf(Warn + "Missing input files\n")
return
}

obfArgs, err := getObfArgs(cmd)
allCode, err := cmd.Flags().GetBool(allCodeFlagStr)
if err != nil {
fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", allCodeFlagStr, err)
return
}
output, err := cmd.Flags().GetString(outputFlagStr)
if err != nil {
fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", outputFlagStr, err)
return
}
verbose, err := cmd.Flags().GetBool(verboseFlagStr)
if err != nil {
fmt.Printf(Warn+"Failed to parse --%s flag: %s\n", verboseFlagStr, err)
return
}
buildArgs := &build.Build{
Name: filepath.Base(args[0]),
NimFiles: args,
UserCodeOnly: true,
Name: filepath.Base(args[0]),
NimFiles: args,
Output: output,
ObfAllCode: allCode,
Verbose: verbose,
}

obfArgs, err := getObfArgs(cmd)
if err != nil {
return
}

err = build.Compile(buildArgs, obfArgs)
if err != nil {
fmt.Printf(Warn+"%s", err)
}
build.Compile(buildArgs, obfArgs)
},
}

Expand Down
2 changes: 1 addition & 1 deletion make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@echo off
SETLOCAL

SET VERSION=0.0.1
SET VERSION=0.0.2

SET CMD_PKG=github.com/moloch--/denim/cmd
SET LDFLAGS=-s -w
Expand Down
57 changes: 47 additions & 10 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ type Build struct {
Name string
NimFiles []string

UserCodeOnly bool
Output string
ObfAllCode bool

Verbose bool
}

// Compile a nim program with Obfuscator-LLVM
Expand All @@ -42,7 +45,7 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error {
}

// Compile Nim
nimCache, err := compileNimCode(build.Name, build.NimFiles, clang)
nimCache, err := compileNimCode(build, clang)
if err != nil {
return err
}
Expand All @@ -56,17 +59,30 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error {
if len(step) != 2 {
return fmt.Errorf("Malformed step: %v", step)
}

cFile := filepath.Base(step[0])
compileCmd := strings.Fields(step[1])
if compileCmd[0] == "clang" || compileCmd[0] == "clang.exe" {
compileCmd = compileCmd[1:]
}

var stdout []byte
var stderr []byte
var err error
if strings.HasPrefix(cFile, "@") || !build.UserCodeOnly {
_, _, err = clang.ObfCompile(nimCache, compileCmd, obfArgs)
if strings.HasPrefix(cFile, "@") || build.ObfAllCode {
stdout, stderr, err = clang.ObfCompile(nimCache, compileCmd, obfArgs)
} else {
_, _, err = clang.Compile(nimCache, compileCmd)
stdout, stderr, err = clang.Compile(nimCache, compileCmd)
}
if build.Verbose {
if 0 < len(stdout) {
fmt.Printf(string(stdout))
}
if 0 < len(stderr) {
fmt.Printf(string(stderr))
}
}

if err != nil {
return err
}
Expand All @@ -75,7 +91,15 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error {
if linker[0] == "clang" || linker[0] == "clang.exe" {
linker = linker[1:]
}
_, _, err = clang.Compile(nimCache, linker)
stdout, stderr, err := clang.Compile(nimCache, linker)
if build.Verbose {
if 0 < len(stdout) {
fmt.Printf(string(stdout))
}
if 0 < len(stderr) {
fmt.Printf(string(stderr))
}
}
if err != nil {
return err
}
Expand All @@ -84,14 +108,27 @@ func Compile(build *Build, obfArgs *ollvm.ObfArgs) error {
}

// nim compile --genScript --compileOnly --cc=clang --clang.exe:PATH --nimcache:PATH helloworld.nim
func compileNimCode(project string, nimFiles []string, clang *ollvm.Clang) (string, error) {
nimCache := filepath.Join(assets.GetNimCacheRoot(), project)
func compileNimCode(build *Build, clang *ollvm.Clang) (string, error) {
nimCache := filepath.Join(assets.GetNimCacheRoot(), build.Name)
args := []string{"--genScript", "--compileOnly", "--cc:clang"}
args = append(args, fmt.Sprintf("--clang.exe=%s", clang.ClangExe))
args = append(args, fmt.Sprintf("--nimcache:%s", nimCache))
args = append(args, nimFiles...)
if build.Output != "" {
args = append(args, fmt.Sprintf("--out:%s", build.Output))
}
args = append(args, build.NimFiles...)

workDir, _ := os.Getwd()
_, _, err := nim.Compile(workDir, os.Environ(), args)
stdout, stderr, err := nim.Compile(workDir, os.Environ(), args)
if build.Verbose {
if 0 < len(stdout) {
fmt.Printf(string(stdout))
}
if 0 < len(stderr) {
fmt.Printf(string(stderr))
}
}

return nimCache, err
}

Expand Down

0 comments on commit fce8028

Please sign in to comment.