forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customjs.go
199 lines (167 loc) · 4.03 KB
/
customjs.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package codec
import (
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/pkg/errors"
"github.com/robertkrimen/otto"
)
func init() {
gob.Register(CustomJS{})
}
// CodecMaxExecTime holds the max. time the (custom) codec is allowed to
// run.
var CodecMaxExecTime = 10 * time.Millisecond
// CustomJS is a scriptable JS codec.
type CustomJS struct {
fPort uint8
encodeScript string
decodeScript string
Data interface{}
}
// NewCustomJS creates a new custom JS codec.
func NewCustomJS(fPort uint8, encodeScript, decodeScript string) *CustomJS {
return &CustomJS{
fPort: fPort,
encodeScript: encodeScript,
decodeScript: decodeScript,
}
}
// Object returns the object data.
func (c CustomJS) Object() interface{} {
return c.Data
}
// MarshalJSON implements json.Marshaler.
func (c CustomJS) MarshalJSON() ([]byte, error) {
return json.Marshal(c.Data)
}
// UnmarshalJSON implement json.Unmarshaler.
func (c *CustomJS) UnmarshalJSON(text []byte) error {
return json.Unmarshal(text, &c.Data)
}
// DecodeBytes decodes the payload from a slice of bytes.
func (c *CustomJS) DecodeBytes(data []byte) (err error) {
defer func() {
if caught := recover(); caught != nil {
err = fmt.Errorf("%s", caught)
}
}()
script := c.decodeScript + "\n\nDecode(fPort, bytes);\n"
vm := otto.New()
vm.Interrupt = make(chan func(), 1)
vm.SetStackDepthLimit(32)
vm.Set("bytes", data)
vm.Set("fPort", c.fPort)
go func() {
time.Sleep(CodecMaxExecTime)
vm.Interrupt <- func() {
panic(errors.New("execution timeout"))
}
}()
var val otto.Value
val, err = vm.Run(script)
if err != nil {
return errors.Wrap(err, "js vm error")
}
if !val.IsObject() {
return errors.New("function must return object")
}
c.Data, err = val.Export()
if err != nil {
return errors.Wrap(err, "export error")
}
return nil
}
// EncodeToBytes encodes the payload to a slice of bytes.
func (c CustomJS) EncodeToBytes() (b []byte, err error) {
defer func() {
if caught := recover(); caught != nil {
err = fmt.Errorf("%s", caught)
}
}()
script := c.encodeScript + "\n\nEncode(fPort, obj);\n"
vm := otto.New()
vm.Interrupt = make(chan func(), 1)
vm.SetStackDepthLimit(32)
vm.Set("obj", c.Data)
vm.Set("fPort", c.fPort)
go func() {
time.Sleep(CodecMaxExecTime)
vm.Interrupt <- func() {
panic(errors.New("execution timeout"))
}
}()
var val otto.Value
val, err = vm.Run(script)
if err != nil {
return nil, errors.Wrap(err, "js vm error")
}
if !val.IsObject() {
return nil, errors.New("function must return an array")
}
var out interface{}
out, err = val.Export()
if err != nil {
return nil, errors.Wrap(err, "export error")
}
return interfaceToByteSlice(out)
}
func interfaceToByteSlice(obj interface{}) ([]byte, error) {
if obj == nil {
return nil, errors.New("value must not be nil")
}
if reflect.TypeOf(obj).Kind() != reflect.Slice {
return nil, errors.New("value must be an array")
}
s := reflect.ValueOf(obj)
l := s.Len()
var out []byte
for i := 0; i < l; i++ {
var b int64
el := s.Index(i).Interface()
switch v := el.(type) {
case int:
b = int64(v)
case uint:
b = int64(v)
case uint8:
b = int64(v)
case int8:
b = int64(v)
case uint16:
b = int64(v)
case int16:
b = int64(v)
case uint32:
b = int64(v)
case int32:
b = int64(v)
case uint64:
b = int64(v)
if uint64(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %d", v)
}
case int64:
b = int64(v)
case float32:
b = int64(v)
if float32(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %f", v)
}
case float64:
b = int64(v)
if float64(b) != v {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %f", v)
}
default:
return nil, fmt.Errorf("array value must be an array of ints or floats, got: %T", el)
}
if b < 0 || b > 255 {
return nil, fmt.Errorf("array value must be in byte range (0 - 255), got: %d", b)
}
out = append(out, byte(b))
}
return out, nil
}