This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (135 loc) · 3.81 KB
/
main.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
162
163
164
165
166
167
168
169
package main
//#cgo CFLAGS: -g -Wall
import "C"
import (
"log"
"runtime"
"tekao.net/jnigi"
)
func main() {}
var jvm *jnigi.JVM
var env *jnigi.Env
var skAnalyzer *jnigi.ObjectRef
//export InitJava
func InitJava(javaHome *C.char, analyzerJar *C.char) {
if err := jnigi.LoadJVMLib(C.GoString(javaHome) + "/bin/server/jvm.dll"); err != nil {
log.Fatal(err)
}
args := []string{"-Djava.class.path=" + C.GoString(analyzerJar)}
var err error
jvm, env, err = jnigi.CreateJVM(jnigi.NewJVMInitArgs(false, false, jnigi.JNI_VERSION_10, args))
if err != nil {
log.Fatal(err)
}
runtime.LockOSThread()
jvm.AttachCurrentThread()
}
func getEnumField(class string, index int) *jnigi.ObjectRef {
values := jnigi.NewObjectArrayRef(class)
if err := env.CallStaticMethod(class, "values", values); err != nil {
log.Fatal(err)
}
return env.FromObjectArray(values)[index]
}
type AnalyzerFlag uint8
const (
ForceVaultHook AnalyzerFlag = 1 << iota
ForceRegionsHook
)
func findAnalyzerFlags(analyzerFlags AnalyzerFlag) []*jnigi.ObjectRef {
var flags []*jnigi.ObjectRef
if analyzerFlags&ForceVaultHook != 0 {
flags = append(flags, findAnalyzerFlag(0))
}
if analyzerFlags&ForceRegionsHook != 0 {
flags = append(flags, findAnalyzerFlag(1))
}
return flags
}
func findAnalyzerFlag(analyzerFlag AnalyzerFlag) *jnigi.ObjectRef {
return getEnumField("me/glicz/skanalyzer/AnalyzerFlag", int(analyzerFlag))
}
type LoggerType uint8
const (
Disabled LoggerType = iota
Normal
Plain
)
func findLoggerType(loggerType LoggerType) *jnigi.ObjectRef {
return getEnumField("me/glicz/skanalyzer/LoggerType", int(loggerType))
}
func javaFile(path *C.char) *jnigi.ObjectRef {
if path == nil {
return jnigi.NewObjectRef("java/io/File")
}
file, err := env.NewObject("java/io/File", c2javaString(path))
if err != nil {
log.Fatal(err)
}
return file
}
//export InitAnalyzer
func InitAnalyzer(analyzerFlags AnalyzerFlag, loggerType LoggerType, workingDir *C.char) {
flags := env.ToObjectArray(findAnalyzerFlags(analyzerFlags), "me/glicz/skanalyzer/AnalyzerFlag")
var err error
skAnalyzer, err = env.NewObject("me/glicz/skanalyzer/SkAnalyzer", flags, findLoggerType(loggerType), javaFile(workingDir))
if err != nil {
log.Fatal(err)
}
}
func c2javaString(str *C.char) *jnigi.ObjectRef {
javaStr, err := env.NewObject("java/lang/String", []byte(C.GoString(str)))
if err != nil {
log.Fatal(err)
}
return javaStr
}
func java2cString(str *jnigi.ObjectRef) *C.char {
var bytes []byte
if err := str.CallMethod(env, "getBytes", &bytes); err != nil {
log.Fatal(err)
}
return C.CString(string(bytes))
}
//export ParseScript
func ParseScript(path *C.char, load bool) *C.char {
future := jnigi.NewObjectRef("java/util/concurrent/CompletableFuture")
if err := skAnalyzer.CallMethod(env, "parseScript", future, c2javaString(path), load); err != nil {
log.Fatal(err)
}
joinResult := jnigi.NewObjectRef("java/lang/Object")
if err := future.CallMethod(env, "join", joinResult); err != nil {
log.Fatal(err)
}
analyzeResults := joinResult.Cast("me/glicz/skanalyzer/ScriptAnalyzeResults")
jsonResult := jnigi.NewObjectRef("java/lang/String")
if err := analyzeResults.CallMethod(env, "jsonResult", jsonResult); err != nil {
log.Fatal(err)
}
return java2cString(jsonResult)
}
//export UnloadScript
func UnloadScript(path *C.char) bool {
var result bool
if err := skAnalyzer.CallMethod(env, "unloadScript", &result, c2javaString(path)); err != nil {
log.Fatal(err)
}
return result
}
//export UnloadAllScripts
func UnloadAllScripts() {
if err := skAnalyzer.CallMethod(env, "unloadAllScripts", nil); err != nil {
log.Fatal(err)
}
}
//export Exit
func Exit() {
env.DeleteLocalRef(skAnalyzer)
if err := jvm.DetachCurrentThread(env); err != nil {
log.Fatal(err)
}
runtime.UnlockOSThread()
if err := jvm.Destroy(); err != nil {
log.Fatal(err)
}
}