This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
286 lines (276 loc) · 7.21 KB
/
config.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2021 - 2023 PurpleSec Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
package watcher
import (
"errors"
"html"
"strconv"
"strings"
"time"
"github.com/PurpleSec/logx"
// Import for the Golang MySQL driver
_ "github.com/go-sql-driver/mysql"
)
// Defaults is a string representation of a JSON formatted default configuration
// for a Watcher instance.
const Defaults = `{
"db": {
"host": "tcp(localhost:3306)",
"user": "watcher_user",
"password": "password",
"database": "watcher_db"
},
"log": {
"file": "watcher.log",
"level": 2
},
"blocked": [],
"allowed": [],
"twitter": {
"consumer_key": "",
"consumer_secret": ""
},
"timeouts": {
"backoff": 5000000000,
"resolve": 21600000000000,
"database": 180000000000
},
"telegram_key": ""
}
`
const (
errmsg = `I'm sorry, There seems to have been an error trying to process your request
Please try again later.`
invalid = `I'm sorry I don't understand that command.
Please use a command from the following list:
/list
/clear
/add <@username1,@usernameN,..> [keyword1,keywordN,..]
/remove <@username1,@usernameN,..|clear|all>`
)
type config struct {
Twitter struct {
ConsumerKey string `json:"consumer_key"`
ConsumerSecret string `json:"consumer_secret"`
} `json:"twitter"`
Database struct {
Name string `json:"database"`
Server string `json:"host"`
Username string `json:"user"`
Password string `json:"password"`
} `json:"db"`
Telegram string `json:"telegram_key"`
Blocked []string `json:"blocked"`
Allowed []string `json:"allowed"`
Log struct {
File string `json:"file"`
Level int `json:"level"`
} `json:"log"`
Timeouts struct {
Resolve time.Duration `json:"resolver"`
Backoff time.Duration `json:"backoff"`
Database time.Duration `json:"database"`
} `json:"timeouts"`
}
func isValid(s string) bool {
if len(s) == 0 || s[0] != '@' || len(s) > 16 {
return false
}
for i := range s {
if i == 0 {
continue
}
switch {
case s[i] == '_':
continue
case s[i] < 48 || s[i] > 122:
return false
case s[i] > 57 && s[i] < 65:
return false
case s[i] > 90 && s[i] < 96:
return false
}
}
return true
}
func (c *config) check() error {
if len(c.Twitter.ConsumerKey) == 0 {
return errors.New("missing Twitter consumer key")
}
if len(c.Twitter.ConsumerSecret) == 0 {
return errors.New("missing Twitter consumer secret")
}
if c.Log.Level > int(logx.Fatal) || c.Log.Level < int(logx.Trace) {
return errors.New(`invalid log level "` + strconv.Itoa(c.Log.Level) + `"`)
}
if len(c.Database.Name) == 0 {
return errors.New("missing database name")
}
if len(c.Database.Server) == 0 {
return errors.New("missing database server")
}
if len(c.Database.Username) == 0 {
return errors.New("missing database username")
}
if c.Timeouts.Resolve == 0 {
c.Timeouts.Resolve = time.Hour * 6
}
if c.Timeouts.Backoff == 0 {
c.Timeouts.Backoff = time.Second * 5
}
if c.Timeouts.Database == 0 {
c.Timeouts.Database = time.Minute * 3
}
return nil
}
func stringLowMatch(s, m string) bool {
if len(s) != len(m) {
return false
}
for i := range s {
switch {
case s[i] == m[i]:
case m[i] > 96 && s[i]+32 == m[i]:
case s[i] > 96 && m[i]+32 == s[i]:
default:
return false
}
}
return true
}
func canUseACL(n string, a, d []string) bool {
if len(d) == 0 && len(a) == 0 {
return true
}
if len(d) > 0 {
for i := range d {
if stringLowMatch(n, d[i]) {
return false
}
}
}
if len(a) == 0 {
return true
}
for i := range a {
if stringLowMatch(n, a[i]) {
return true
}
}
return false
}
func stringSplitContainsNLA(s, m string) bool {
if len(s) == 0 {
return false
}
if len(m) == 0 {
return true
}
// NOTE(dij): This is to see if we have a valid minus char '-' next to a
// comma as this indicates that we need to validate it.
n := strings.IndexByte(m, '-')
// NOTE(dij): Loop through to see for valids.
// If the '-' is at 0 or non-exist, skip this.
for n > 0 && n < len(m) {
if m[n-1] == ',' {
break
}
// NOTE(dij): Find a minus symbol next to a comma (the only
// negative look-ahead valid value).
n = strings.IndexByte(m[n+1:], '-')
}
var r bool
for i, e, o := 0, strings.IndexByte(m, ','), n > -1; i < len(m); i, e = e+1, strings.IndexByte(m[e+1:], ',') {
if e == -1 {
// NOTE(dij): Last (or only) entry.
if m[i] == '-' && i+1 < len(m) {
// NOTE(dij): Found negative, takes president.
if !strings.Contains(s, m[i+1:]) {
// NOTE(dij): If i == 0, the we're the only, we can rely
// on this being true.
if i == 0 {
return true
}
// NOTE(dij): Rely on r to determine if we match.
break
}
}
// NOTE(dij): Fix escape chars.
if (m[i] == '+' || m[i] == '\\') && i+1 < len(m) {
i++
}
// NOTE(dij): Only one here, since it's the last value, non-negative.
// means everything else /must/ have passed
return strings.Contains(s, m[i:])
}
if e += i; m[i] == '-' && i+1 < e {
// NOTE(dij): Negative in list, if fails, we bail!
if strings.Contains(s, m[i+1:e]) {
return false
}
// NOTE(dij): Passed, continue..
continue
}
// NOTE(dij): Fix escape chars.
if (m[i] == '+' || m[i] == '\\') && i+1 < e {
i++
}
// NOTE(dij): Non-negative, lets check if keyword exists.
// We also check r to see if true, since if r == true, we
// already hit a positive keyword match, no need to redo.
if !r && strings.Contains(s, m[i:e]) {
// NOTE(dij): Passed keyword match!
if o {
// NOTE(dij): If negative is enabled, we set r to true, then
// continue.
r = true
continue
}
// NOTE(dij): No negative, we just bail true.
return true
}
}
return r
}
func split(s string) ([]string, string, string) {
var (
z = strings.IndexByte(s, ' ')
v, k = s, ""
r []string
t string
)
if z > 0 {
k, v = strings.TrimSpace(s[z+1:]), strings.TrimSpace(s[:z])
}
for i, e := 0, strings.IndexByte(v, ','); i < len(v); i, e = e+1, strings.IndexByte(v[e+1:], ',') {
if e == -1 {
if t = strings.TrimSpace(v[i:]); !isValid(t) {
return nil, k, `The username "` + t + `" is not a valid Twitter username!` + "\n\nTwitter names must start with \"@\" and contain no special characters or spaces."
}
r = append(r, t[1:])
break
}
e += i
if t = strings.TrimSpace(v[i:e]); !isValid(t) {
return nil, k, `The username "` + t + `" is not a valid Twitter username!` + "\n\nTwitter names must start with \"@\" and contain no special characters or spaces."
}
r = append(r, t[1:])
}
if len(k) == 0 {
return r, "", ""
}
return r, html.EscapeString(k), ""
}