-
Notifications
You must be signed in to change notification settings - Fork 158
/
pkg_names.go
161 lines (145 loc) · 3.19 KB
/
pkg_names.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"errors"
"fmt"
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"path/filepath"
"strings"
"golang.org/x/tools/refactor/rename"
)
func ObfuscatePackageNames(gopath string, n NameHasher) error {
ctx := build.Default
ctx.GOPATH = gopath
level := 1
srcDir := filepath.Join(gopath, "src")
doneChan := make(chan struct{})
defer close(doneChan)
for {
resChan := make(chan string)
go func() {
scanLevel(srcDir, level, resChan, doneChan)
close(resChan)
}()
var gotAny bool
for dirPath := range resChan {
gotAny = true
if containsCGO(dirPath) {
continue
}
isMain := isMainPackage(dirPath)
encPath := encryptPackageName(dirPath, n)
srcPkg, err := filepath.Rel(srcDir, dirPath)
if err != nil {
return err
}
dstPkg, err := filepath.Rel(srcDir, encPath)
if err != nil {
return err
}
if err := rename.Move(&ctx, srcPkg, dstPkg, ""); err != nil {
return fmt.Errorf("package move: %s", err)
}
if isMain {
if err := makeMainPackage(encPath); err != nil {
return fmt.Errorf("make main package %s: %s", encPath, err)
}
}
}
if !gotAny {
break
}
level++
}
return nil
}
func scanLevel(dir string, depth int, res chan<- string, done <-chan struct{}) {
if depth == 0 {
select {
case res <- dir:
case <-done:
return
}
return
}
listing, _ := ioutil.ReadDir(dir)
for _, item := range listing {
if item.IsDir() {
scanLevel(filepath.Join(dir, item.Name()), depth-1, res, done)
}
select {
case <-done:
return
default:
}
}
}
func encryptPackageName(dir string, p NameHasher) string {
subDir, base := filepath.Split(dir)
return filepath.Join(subDir, p.Hash(base))
}
func isMainPackage(dir string) bool {
listing, err := ioutil.ReadDir(dir)
if err != nil {
return false
}
for _, item := range listing {
if isGoFile(item.Name()) {
path := filepath.Join(dir, item.Name())
set := token.NewFileSet()
contents, err := ioutil.ReadFile(path)
if err != nil {
return false
}
file, err := parser.ParseFile(set, path, contents, 0)
if err != nil {
return false
}
fields := strings.Fields(string(contents[int(file.Package)-1:]))
if len(fields) < 2 {
return false
}
return fields[1] == "main"
}
}
return false
}
func makeMainPackage(dir string) error {
listing, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, item := range listing {
if !isGoFile(item.Name()) {
continue
}
path := filepath.Join(dir, item.Name())
contents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
set := token.NewFileSet()
file, err := parser.ParseFile(set, path, contents, 0)
if err != nil {
return err
}
pkgNameIdx := int(file.Package) + len("package") - 1
prePkg := contents[:pkgNameIdx]
postPkg := string(contents[pkgNameIdx:])
fields := strings.Fields(postPkg)
if len(fields) < 1 {
return errors.New("no fields after package keyword")
}
packageName := fields[0]
var newData bytes.Buffer
newData.Write(prePkg)
newData.WriteString(strings.Replace(postPkg, packageName, "main", 1))
if err := ioutil.WriteFile(path, newData.Bytes(), item.Mode()); err != nil {
return err
}
}
return nil
}