/
analyzerExecutor.go
260 lines (237 loc) · 8.96 KB
/
analyzerExecutor.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package homescript
import (
"errors"
"fmt"
"net/http"
"net/url"
"time"
"unicode/utf8"
"github.com/go-ping/ping"
"github.com/smarthome-go/homescript/v2/homescript"
"github.com/smarthome-go/smarthome/core/database"
)
type AnalyzerExecutor struct {
Username string
}
func (self AnalyzerExecutor) IsAnalyzer() bool { return true }
// Resolves a Homescript module
func (self *AnalyzerExecutor) ResolveModule(id string) (string, string, bool, bool, error) {
script, found, err := database.GetUserHomescriptById(id, self.Username)
if !found || err != nil {
return "", "", found, true, err
}
return script.Data.Code, id, true, true, nil
}
// Resolves a Homescript module
func (self *AnalyzerExecutor) ReadFile(path string) (string, error) {
script, found, err := database.GetUserHomescriptById(path, self.Username)
if err != nil {
return "", err
}
if !found {
return "", fmt.Errorf("Script `%s` was not found but required", path)
}
return script.Data.Code, nil
}
func (self *AnalyzerExecutor) Sleep(seconds float64) {
}
func (self *AnalyzerExecutor) Print(args ...string) error {
return nil
}
func (self *AnalyzerExecutor) Println(args ...string) error {
return nil
}
func (self *AnalyzerExecutor) GetSwitch(switchId string) (homescript.SwitchResponse, error) {
switchData, found, err := database.GetSwitchById(switchId)
if !found {
return homescript.SwitchResponse{}, fmt.Errorf("switch '%s' was not found", switchId)
}
if err != nil {
return homescript.SwitchResponse{}, err
}
return homescript.SwitchResponse{
Name: switchData.Name,
Power: switchData.PowerOn,
Watts: uint(switchData.Watts),
}, nil
}
func (self *AnalyzerExecutor) Switch(switchId string, powerOn bool) error {
_, switchExists, err := database.GetSwitchById(switchId)
if err != nil {
return err
}
if !switchExists {
return fmt.Errorf("Failed to set power: switch '%s' does not exist", switchId)
}
userHasPowerPermission, err := database.UserHasPermission(self.Username, database.PermissionPower)
if err != nil {
return fmt.Errorf("Failed to set power: could not check if user is allowed to interact with switches: %s", err.Error())
}
if !userHasPowerPermission {
return errors.New("Failed to set power: user is not allowed to interact with switches")
}
userHasSwitchPermission, err := database.UserHasSwitchPermission(self.Username, switchId)
if err != nil {
return fmt.Errorf("Failed to set power: could not check if user is allowed to interact with this switch: %s", err.Error())
}
if !userHasSwitchPermission {
return fmt.Errorf("Failed to set power: user is not allowed to interact with switch '%s'", switchId)
}
return nil
}
func (self *AnalyzerExecutor) Ping(ip string, timeoutSecs float64) (bool, error) {
_, err := ping.NewPinger(ip)
if err != nil {
return false, err
}
return false, nil
}
func (self *AnalyzerExecutor) Get(requestUrl string) (homescript.HttpResponse, error) {
// The permissions can be validated beforehand
hasPermission, err := database.UserHasPermission(self.Username, database.PermissionHomescriptNetwork)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("could not send GET request: failed to validate your permissions: %s", err.Error())
}
if !hasPermission {
return homescript.HttpResponse{}, fmt.Errorf("will not send GET request: you lack permission to access the network via homescript. If this is unintentional, contact your administrator")
}
// Check if the URL is already cached
cached, err := database.IsHomescriptUrlCached(requestUrl)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("internal error: Could not check URL cache: %s", err.Error())
}
if cached {
log.Trace(fmt.Sprintf("Homescript URL `%s` is cached, omitting checks...", requestUrl))
return homescript.HttpResponse{}, nil
}
log.Trace(fmt.Sprintf("Homescript URL `%s` is not cached, running checks...", requestUrl))
url, err := url.ParseRequestURI(requestUrl)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: could not parse URL: %s", err.Error())
}
if url.Scheme != "http" && url.Scheme != "https" {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: Invalid scheme: `%s`.\n=> Valid schemes are `http` and `https`", url.Scheme)
}
if url.Scheme != "http" && url.Scheme != "https" {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: Invalid scheme: `%s`.\n=> Valid schemes are `http` and `https`", url.Scheme)
}
_, err = http.Head(requestUrl)
if err != nil {
return homescript.HttpResponse{}, err
}
// If all checks were successful, insert the URL into the URL cache
if err := insertCacheEntry(*url); err != nil {
return homescript.HttpResponse{}, fmt.Errorf("internal error: Could not update URL cache entry: %s", err.Error())
}
log.Trace(fmt.Sprintf("Updated URL cache to include `%s`", requestUrl))
return homescript.HttpResponse{}, nil
}
func (self *AnalyzerExecutor) Http(
requestUrl string,
method string,
body string,
headers map[string]string,
cookies map[string]string,
) (homescript.HttpResponse, error) {
// Check permissions and request building beforehand
hasPermission, err := database.UserHasPermission(self.Username, database.PermissionHomescriptNetwork)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("could not perform %s request: failed to validate your permissions: %s", method, err.Error())
}
if !hasPermission {
return homescript.HttpResponse{}, fmt.Errorf("will not perform %s request: you lack permission to access the network via Homescript. If this is unintentional, contact your administrator", method)
}
// Check if the URL is already cached
cached, err := database.IsHomescriptUrlCached(requestUrl)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("Internal error: Could not check URL cache: %s", err.Error())
}
if cached {
log.Trace(fmt.Sprintf("Homescript URL `%s` is cached, omitting checks...", requestUrl))
return homescript.HttpResponse{}, nil
}
log.Trace(fmt.Sprintf("Homescript URL `%s` is not cached, running checks...", requestUrl))
// URL-specific checks
url, err := url.ParseRequestURI(requestUrl)
if err != nil {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: could not parse URL: %s", err.Error())
}
if url.Scheme != "http" && url.Scheme != "https" {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: Invalid scheme: `%s`.\n=> Valid schemes are `http` and `https`", url.Scheme)
}
if url.Scheme != "http" && url.Scheme != "https" {
return homescript.HttpResponse{}, fmt.Errorf("invalid URL provided: Invalid scheme: `%s`.\n=> Valid schemes are `http` and `https`", url.Scheme)
}
_, err = http.Head(requestUrl)
if err != nil {
return homescript.HttpResponse{}, err
}
// If all checks were successful, insert the URL into the URL cache
if err := insertCacheEntry(*url); err != nil {
return homescript.HttpResponse{}, fmt.Errorf("internal error: Could not update URL cache entry: %s", err.Error())
}
log.Trace(fmt.Sprintf("updated URL cache to include `%s`", requestUrl))
return homescript.HttpResponse{}, nil
}
func (self *AnalyzerExecutor) Notify(
title string,
description string,
level homescript.NotificationLevel,
) error {
return nil
}
func (self *AnalyzerExecutor) Remind(
title string,
description string,
urgency homescript.ReminderUrgency,
dueDate time.Time,
) (uint, error) {
return 0, nil
}
func (self *AnalyzerExecutor) Log(
title string,
description string,
level homescript.LogLevel,
) error {
hasPermission, err := database.UserHasPermission(self.Username, database.PermissionLogging)
if err != nil {
return err
}
if !hasPermission {
return fmt.Errorf("failed to add log event: you lack permission to add records to the internal logging system.")
}
if level > 5 {
return fmt.Errorf("failed to add log event: invalid logging level <%d>: valid logging levels are 1, 2, 3, 4, or 5", level)
}
return nil
}
// Executes another Homescript based on its Id
func (self AnalyzerExecutor) Exec(homescriptId string, args map[string]string) (homescript.ExecResponse, error) {
_, found, err := database.GetUserHomescriptById(homescriptId, self.Username)
if err != nil {
return homescript.ExecResponse{}, err
}
if !found {
return homescript.ExecResponse{}, fmt.Errorf("invalid script: homescript '%s' was not found", homescriptId)
}
return homescript.ExecResponse{ReturnValue: homescript.ValueNull{}}, nil
}
// Returns the name of the user who is currently running the script
func (self *AnalyzerExecutor) GetUser() string {
return self.Username
}
func (self *AnalyzerExecutor) GetWeather() (homescript.Weather, error) {
return homescript.Weather{}, nil
}
func (self *AnalyzerExecutor) GetStorage(key string) (*string, error) {
if utf8.RuneCountInString(key) > 50 {
return nil, errors.New("key is larger than 50 characters")
}
return database.GetHmsStorageEntry(self.Username, key)
}
func (self *AnalyzerExecutor) SetStorage(key string, value string) error {
if utf8.RuneCountInString(key) > 50 {
return errors.New("key is larger than 50 characters")
}
return nil
}