obfuscate literals as part of transformGo (#299)

This is easier to understand, since now the modification of the
*ast.File is all within a single chunk of code. We can also simplify
literals.Obfuscate to work on a single file, as transformGo runs in a
loop.

We also remove the "use receiver" TODOs, since the code is now in a
different package and it can't declare methods on a type here.
pull/300/head
Daniel Martí 3 years ago committed by GitHub
parent 91dd310bfe
commit b995c1b589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,8 +33,8 @@ func randObfuscator() obfuscator {
return obfuscators[randPos]
}
// Obfuscate replace literals with obfuscated lambda functions
func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, ignoreObj map[types.Object]bool) []*ast.File {
// Obfuscate replaces literals with obfuscated anonymous functions.
func Obfuscate(file *ast.File, info *types.Info, fset *token.FileSet, ignoreObj map[types.Object]bool) *ast.File {
pre := func(cursor *astutil.Cursor) bool {
switch x := cursor.Node().(type) {
case *ast.GenDecl:
@ -174,10 +174,7 @@ func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, ignoreO
return true
}
for i := range files {
files[i] = astutil.Apply(files[i], pre, post).(*ast.File)
}
return files
return astutil.Apply(file, pre, post).(*ast.File)
}
func obfuscateString(data string) *ast.CallExpr {

@ -607,11 +607,6 @@ func transformCompile(args []string) ([]string, error) {
tf.recordReflectArgs(files)
if opts.GarbleLiterals {
// TODO: use transformer here?
files = literals.Obfuscate(files, tf.info, fset, tf.ignoreObjects)
}
// Add our temporary dir to the beginning of -trimpath, so that we don't
// leak temporary dirs. Needs to be at the beginning, since there may be
// shorter prefixes later in the list, such as $PWD if TMPDIR=$PWD/tmp.
@ -964,7 +959,6 @@ func (tf *transformer) recordReflectArgs(files []*ast.File) {
visit := func(node ast.Node) bool {
if opts.GarbleLiterals {
// TODO: use transformer here?
literals.RecordUsedAsConstants(node, tf.info, tf.ignoreObjects)
}
@ -1014,8 +1008,12 @@ type transformer struct {
ignoreObjects map[types.Object]bool
}
// transformGo garbles the provided Go syntax node.
// transformGo obfuscates the provided Go syntax file.
func (tf *transformer) transformGo(file *ast.File) *ast.File {
if opts.GarbleLiterals {
file = literals.Obfuscate(file, tf.info, fset, tf.ignoreObjects)
}
pre := func(cursor *astutil.Cursor) bool {
node, ok := cursor.Node().(*ast.Ident)
if !ok {

Loading…
Cancel
Save