-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotebook.go
More file actions
144 lines (124 loc) · 2.85 KB
/
notebook.go
File metadata and controls
144 lines (124 loc) · 2.85 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
package main
import (
"fmt"
"path/filepath"
"sync"
)
type Notebook struct {
mutex sync.Mutex
storage string
pipes map[string]*Rizin
jsvm *JavaScript
rizin string
cmds map[string]RizinCommand
rizinInfoCache []string
}
func NewNotebook(storage, rizinbin string) *Notebook {
jsvm := NewJavaScript()
if jsvm == nil {
panic("failed to create scripting engine")
}
return &Notebook{
storage: storage,
pipes: map[string]*Rizin{},
jsvm: jsvm,
rizin: rizinbin,
cmds: map[string]RizinCommand{},
rizinInfoCache: nil,
}
}
// LoadCommands fetches the list of rizin commands by spawning rizin.
// This MUST be called after the HTTP server is listening, because the
// spawned rizin process loads rz_notebook which health-checks us.
func (n *Notebook) LoadCommands() {
cmds, err := RizinCommands(n.rizin)
if err != nil {
fmt.Printf("warning: failed to load rizin commands: %v\n", err)
}
info, infoErr := RizinInfo(n.rizin)
if infoErr != nil {
fmt.Printf("warning: failed to load rizin info: %v\n", infoErr)
}
n.mutex.Lock()
if cmds != nil {
n.cmds = cmds
}
if len(info) > 0 {
n.rizinInfoCache = append([]string(nil), info...)
}
n.mutex.Unlock()
}
func (n *Notebook) info() ([]string, error) {
n.mutex.Lock()
if len(n.rizinInfoCache) > 0 {
info := append([]string(nil), n.rizinInfoCache...)
n.mutex.Unlock()
return info, nil
}
n.mutex.Unlock()
info, err := RizinInfo(n.rizin)
if err != nil {
return nil, err
}
n.mutex.Lock()
n.rizinInfoCache = append([]string(nil), info...)
n.mutex.Unlock()
return info, nil
}
// Returns existing pipe or opens a new one, extracting the binary to a temp file.
func (n *Notebook) open(unique string, open bool) *Rizin {
if !IsValidNonce(unique, PageNonceSize) {
return nil
}
n.mutex.Lock()
defer n.mutex.Unlock()
if p, ok := n.pipes[unique]; ok {
return p
}
if !open {
return nil
}
page, err := catalog.GetPage(unique)
if err != nil || page == nil {
return nil
}
binaryPath, err := catalog.GetBinaryForPipe(unique, n.storage)
if err != nil {
fmt.Printf("warning: failed to extract binary for pipe: %v\n", err)
return nil
}
prjPath := filepath.Join(n.storage, unique, "project.rzdb")
rizin := NewRizin(n.rizin, binaryPath, prjPath)
if rizin == nil {
return nil
}
n.pipes[unique] = rizin
return rizin
}
func (n *Notebook) closePipe(unique string) {
if !IsValidNonce(unique, PageNonceSize) {
return
}
n.mutex.Lock()
p, ok := n.pipes[unique]
if ok {
delete(n.pipes, unique)
}
n.mutex.Unlock()
if ok && p != nil {
p.close()
}
}
func (n *Notebook) closeAllPipes() {
n.mutex.Lock()
pipesToClose := make(map[string]*Rizin)
for k, v := range n.pipes {
pipesToClose[k] = v
}
n.pipes = map[string]*Rizin{}
n.mutex.Unlock()
for unique, p := range pipesToClose {
fmt.Printf("Closing pipe for page %s...\n", unique)
p.close()
}
}