forked from rizinorg/rizin-notebook
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscripting.go
More file actions
177 lines (152 loc) · 4.06 KB
/
scripting.go
File metadata and controls
177 lines (152 loc) · 4.06 KB
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
170
171
172
173
174
175
176
177
package main
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/dop251/goja"
"golang.org/x/sync/semaphore"
)
// scriptTimeout is the maximum execution time for a single script.
const scriptTimeout = 5 * time.Minute
type JavaScript struct {
semaphore *semaphore.Weighted
runtime *goja.Runtime
rizin *Rizin
output strings.Builder
}
func rizinCmd(command string, rz *Rizin) (string, error) {
if _, err := rz.exec("e scr.color=0"); err != nil {
return "", err
}
result, err := rz.exec(command)
// Restore color regardless of command success.
rz.exec("e scr.color=3")
return result, err
}
func convertValue(ivalue interface{}) string {
switch value := ivalue.(type) {
case []interface{}:
bytes, _ := json.MarshalIndent(value, "", "\t")
return string(bytes)
case map[string]interface{}:
bytes, _ := json.MarshalIndent(value, "", "\t")
return string(bytes)
default:
return fmt.Sprintf("%v", value)
}
}
func NewJavaScript() *JavaScript {
runtime := goja.New()
if runtime == nil {
fmt.Println("error: cannot create JavaScript runtime")
return nil
}
sem := semaphore.NewWeighted(1)
js := &JavaScript{semaphore: sem, runtime: runtime, rizin: nil}
// Register the rizin API object.
rizinAPI := map[string]interface{}{}
rizinAPI["cmd"] = func(args ...interface{}) goja.Value {
if js.rizin == nil {
panic(js.runtime.ToValue("Rizin pipe is closed."))
}
if len(args) < 1 {
panic(js.runtime.ToValue("No string was passed."))
}
cmdStr, ok := args[0].(string)
if !ok {
panic(js.runtime.ToValue("input is not a string."))
}
result, err := rizinCmd(cmdStr, js.rizin)
if err != nil {
panic(js.runtime.ToValue(err.Error()))
}
return js.runtime.ToValue(result)
}
rizinAPI["cmdj"] = func(args ...interface{}) goja.Value {
if js.rizin == nil {
panic(js.runtime.ToValue("Rizin pipe is closed."))
}
if len(args) < 1 {
panic(js.runtime.ToValue("No string was passed."))
}
cmdStr, ok := args[0].(string)
if !ok {
panic(js.runtime.ToValue("input is not a string."))
}
result, err := rizinCmd(cmdStr, js.rizin)
if err != nil {
panic(js.runtime.ToValue(err.Error()))
}
var data interface{}
if jsonErr := json.Unmarshal([]byte(result), &data); jsonErr != nil {
panic(js.runtime.ToValue(jsonErr.Error()))
}
return js.runtime.ToValue(data)
}
// Register the console API object.
consoleAPI := map[string]interface{}{}
// writeArgs writes all arguments to js.output separated by spaces.
writeArgs := func(prefix string, args []interface{}) {
if len(prefix) > 0 {
js.output.WriteString(prefix)
js.output.WriteString(" ")
}
for i, value := range args {
js.output.WriteString(convertValue(value))
if i+1 < len(args) {
js.output.WriteString(" ")
}
}
js.output.WriteString("\n")
}
consoleAPI["log"] = func(args ...interface{}) {
writeArgs("", args)
}
consoleAPI["warn"] = func(args ...interface{}) {
writeArgs("[WARN]", args)
}
consoleAPI["error"] = func(args ...interface{}) {
writeArgs("[ERROR]", args)
}
consoleAPI["info"] = func(args ...interface{}) {
writeArgs("[INFO]", args)
}
consoleAPI["clear"] = func() {
js.output.Reset()
}
consoleAPI["table"] = func(args ...interface{}) {
if len(args) < 1 {
return
}
bytes, err := json.MarshalIndent(args[0], "", " ")
if err != nil {
js.output.WriteString(fmt.Sprintf("[TABLE] %v\n", args[0]))
return
}
js.output.WriteString(string(bytes))
js.output.WriteString("\n")
}
runtime.Set("rizin", rizinAPI)
runtime.Set("console", consoleAPI)
return js
}
func (js *JavaScript) exec(script string, rz *Rizin) (string, error) {
if !js.semaphore.TryAcquire(1) {
return "", errors.New("a script is already running")
}
defer js.semaphore.Release(1)
js.rizin = rz
js.output.Reset()
timer := time.AfterFunc(scriptTimeout, func() {
js.runtime.Interrupt("The script execution has timed out.")
})
defer timer.Stop()
_, err := js.runtime.RunScript("script.js", script)
result := js.output.String()
// Clean up state for next execution.
js.rizin = nil
js.output.Reset()
return result, err
}