-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.go
102 lines (89 loc) · 1.99 KB
/
plugin.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
package openrasp
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
v8 "github.com/baidu-security/openrasp-v8/go"
)
type UpdateListener interface {
OnPluginUpdate()
}
type PluginManager struct {
dirPath string
plugins []v8.Plugin
listeners []UpdateListener
mu sync.RWMutex
}
func NewPluginManager(dir string) *PluginManager {
pm := &PluginManager{
dirPath: dir,
}
return pm
}
func (pm *PluginManager) AttachListener(listener UpdateListener) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.listeners = append(pm.listeners, listener)
}
func (pm *PluginManager) walkFunc(path string, info os.FileInfo, err error) error {
if !info.IsDir() && filepath.Ext(path) == ".js" {
plugin, err := newPlugin(path)
if err == nil {
pm.plugins = append(pm.plugins, *plugin)
}
}
return nil
}
func (pm *PluginManager) loadLocalPlugins() {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.plugins = nil
filepath.Walk(pm.dirPath, pm.walkFunc)
}
func (pm *PluginManager) buildLocalSnapshot() {
pm.loadLocalPlugins()
pm.createSnapshot()
}
func (pm *PluginManager) createSnapshot() {
pm.mu.RLock()
defer pm.mu.RUnlock()
if len(pm.plugins) > 0 {
if v8.CreateSnapshot("", pm.plugins) {
for _, l := range pm.listeners {
l.OnPluginUpdate()
}
}
}
}
func (pm *PluginManager) OnUpdate(absPath string) {
if filepath.Ext(absPath) == ".js" {
pm.buildLocalSnapshot()
}
}
func (pm *PluginManager) OnUpdateCloud(source string, filename string) {
pm.plugins = []v8.Plugin{v8.Plugin{
Source: source,
Filename: filename,
}}
pm.createSnapshot()
}
func newPlugin(path string) (*v8.Plugin, error) {
if filepath.Ext(path) == ".js" {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
fileWithExt := filepath.Base(path)
filename := strings.TrimSuffix(fileWithExt, filepath.Ext(fileWithExt))
p := &v8.Plugin{
Source: string(content),
Filename: filename,
}
return p, nil
} else {
return nil, errors.New("plugin must be javascript file")
}
}