simplify the code to obfuscate literals a bit

pull/34/head
Daniel Martí 5 years ago
parent dd1fc4ed87
commit 1bb85bbf9b

@ -13,23 +13,13 @@ import (
func obfuscateLiterals(files []*ast.File) []*ast.File { func obfuscateLiterals(files []*ast.File) []*ast.File {
pre := func(cursor *astutil.Cursor) bool { pre := func(cursor *astutil.Cursor) bool {
t, ok := cursor.Node().(*ast.GenDecl) decl, ok := cursor.Node().(*ast.GenDecl)
if !ok { if !ok || decl.Tok != token.CONST {
return true return true
} }
if t.Tok != token.CONST { for _, spec := range decl.Specs {
return true for _, val := range spec.(*ast.ValueSpec).Values {
}
for _, spec := range t.Specs {
spec, ok := spec.(*ast.ValueSpec)
if !ok {
// cannot happen because ast.GenDecl with token.Const are always of type ast.ValueSpec
return false
}
for _, val := range spec.Values {
if v, ok := val.(*ast.BasicLit); !ok || v.Kind != token.STRING { if v, ok := val.(*ast.BasicLit); !ok || v.Kind != token.STRING {
return false // skip the block if it contains non basic literals return false // skip the block if it contains non basic literals
} }
@ -38,20 +28,18 @@ func obfuscateLiterals(files []*ast.File) []*ast.File {
// constants are not possible if we want to obfuscate literals, therefore // constants are not possible if we want to obfuscate literals, therefore
// move all constant blocks which only contain strings to variables // move all constant blocks which only contain strings to variables
t.Tok = token.VAR decl.Tok = token.VAR
return true return true
} }
var ( key := genAesKey()
key = genAesKey() addedToPkg := false // we only want to inject the code and imports once
fset = token.NewFileSet()
addedToPkg bool // we only want to inject the code and imports once
)
post := func(cursor *astutil.Cursor) bool { post := func(cursor *astutil.Cursor) bool {
switch x := cursor.Node().(type) { switch x := cursor.Node().(type) {
case *ast.File: case *ast.File:
if !addedToPkg { if addedToPkg {
break
}
x.Decls = append(x.Decls, funcStmt) x.Decls = append(x.Decls, funcStmt)
x.Decls = append(x.Decls, keyStmt(key)) x.Decls = append(x.Decls, keyStmt(key))
@ -65,10 +53,10 @@ func obfuscateLiterals(files []*ast.File) []*ast.File {
} }
addedToPkg = true addedToPkg = true
return true
}
case *ast.BasicLit: case *ast.BasicLit:
if !(cursor.Name() == "Values" || cursor.Name() == "Rhs" || cursor.Name() == "Value" || cursor.Name() == "Args") { switch cursor.Name() {
case "Values", "Rhs", "Value", "Args":
default:
return true // we don't want to obfuscate imports etc. return true // we don't want to obfuscate imports etc.
} }
if x.Kind != token.STRING { if x.Kind != token.STRING {
@ -79,7 +67,6 @@ func obfuscateLiterals(files []*ast.File) []*ast.File {
if err != nil { if err != nil {
panic(fmt.Sprintf("cannot unquote string: %v", err)) panic(fmt.Sprintf("cannot unquote string: %v", err))
} }
ciphertext, err := encAES([]byte(value), key) ciphertext, err := encAES([]byte(value), key)
if err != nil { if err != nil {
panic(fmt.Sprintf("cannot encrypt string: %v", err)) panic(fmt.Sprintf("cannot encrypt string: %v", err))
@ -87,14 +74,12 @@ func obfuscateLiterals(files []*ast.File) []*ast.File {
cursor.Replace(ciphertextStmt(ciphertext)) cursor.Replace(ciphertextStmt(ciphertext))
} }
return true return true
} }
for i := range files { for i := range files {
files[i] = astutil.Apply(files[i], pre, post).(*ast.File) files[i] = astutil.Apply(files[i], pre, post).(*ast.File)
} }
return files return files
} }

Loading…
Cancel
Save