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
|
// Copyright 2021 SNIX LLC sina@snix.ir
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// 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 General Public License for more details.
package rgxp
import (
"context"
"goshkan/opts"
"goshkan/rgdb"
"regexp"
"strings"
)
// used from ntcp package (TLS or HTTP connections)
var regexComp *regexp.Regexp
func RegexpCompiled() *regexp.Regexp { return regexComp }
func compileReg(rwRegex *string) error {
tempReg, err := regexp.Compile(*rwRegex)
if err == nil {
regexComp = tempReg
}
return err
}
// load all regex from mysql database and compile them
func LoadRegexpInit(db *rgdb.MariaSQLDB) {
ctx, cancel := context.WithTimeout(context.Background(), databaseCtxTime)
defer cancel()
allReg, err := db.LoadAllRegex(ctx)
if err != nil {
opts.MYSQLD(err)
}
opts.SYSLOG(readRegex)
allReg = append(allReg, &rgdb.RegStruct{Regex: regMatchN})
for i, rg := range allReg {
allReg[i].Regex = "(" + rg.Regex + ")"
}
ppstr := joinRegexTo(allReg, "|")
if err := compileReg(&ppstr); err != nil {
opts.OSEXIT(err)
}
}
// recompile regexp , when new regex string applied by user
func AddRecompileReg(new *string) error {
regtext := regexComp.String() + "|(" + *new + ")"
return compileReg(®text)
}
func DelRecompileReg(ptrn *string) error {
cutd := strings.TrimPrefix(
strings.TrimSuffix(strings.ReplaceAll(
strings.ReplaceAll(regexComp.String(),
"("+*ptrn+")", ""), "||", "|"), "|"), "|")
return compileReg(&cutd)
}
func joinRegexTo(elems rgdb.RegexList, sep string) string {
switch len(elems) {
case 0:
return ""
case 1:
return elems[0].Regex
}
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
n += len(elems[i].Regex)
}
var b strings.Builder
b.Grow(n)
b.WriteString(elems[0].Regex)
for _, s := range elems[1:] {
b.WriteString(sep)
b.WriteString(s.Regex)
}
return b.String()
}
|