-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker_tx.go
152 lines (129 loc) · 3.23 KB
/
ticker_tx.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
package risk
import (
"encoding/json"
"github.com/vela-ssoc/vela-kit/auxlib"
"github.com/vela-ssoc/vela-kit/lua"
"io"
"strconv"
"strings"
"time"
)
type Tx struct {
cfg *config `json:"-"`
ent *Event `json:"-'"` //key event demo
Id string `json:"id"`
Cookie *Cookie `json:"cookie"`
}
func (tx *Tx) String() string { return lua.B2S(tx.Byte()) }
func (tx *Tx) Type() lua.LValueType { return lua.LTObject }
func (tx *Tx) AssertFloat64() (float64, bool) { return 0, false }
func (tx *Tx) AssertString() (string, bool) { return "", false }
func (tx *Tx) AssertFunction() (*lua.LFunction, bool) { return nil, false }
func (tx *Tx) Peek() lua.LValue { return tx }
func (tx *Tx) Byte() []byte {
chunk, _ := json.Marshal(tx)
return chunk
}
func (tx *Tx) LocalIP() string {
if tx.ent.LocalIP != "" {
return tx.ent.LocalIP
}
return tx.Cookie.Data["local_ip"]
}
func (tx *Tx) LocalPort() int {
if tx.ent.LocalPort != -1 {
return tx.ent.LocalPort
}
v := tx.Cookie.Data["local_port"]
n, _ := strconv.Atoi(v)
return n
}
func (tx *Tx) RemoteIP() string {
if tx.ent.RemoteIP != "" {
return tx.ent.RemoteIP
}
return tx.Cookie.Data["remote_ip"]
}
func (tx *Tx) RemotePort() int {
if tx.ent.RemotePort != -1 {
return tx.ent.RemotePort
}
v := tx.Cookie.Data["remote_port"]
n, _ := strconv.Atoi(v)
return n
}
func (tx *Tx) Reference() string {
return tx.cfg.refTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
return w.Write(auxlib.S2B(tx.Index(nil, tag).String()))
})
}
func (tx *Tx) Class() TClass {
return tx.cfg.class
}
func (tx *Tx) Level() string {
return tx.cfg.level
}
func (tx *Tx) Subject() string {
if tx.cfg.subTmpl == nil {
return "事件未定义"
}
return tx.cfg.subTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
return w.Write(auxlib.S2B(tx.Index(nil, tag).String()))
})
}
func (tx *Tx) alert(cnd string) {
if tx.Cookie.TriggerInfo(cnd) != 0 {
return
}
ev := &Event{
MinionId: xEnv.ID(),
Inet: xEnv.Inet(),
Time: time.Now(),
Level: tx.Level(),
Class: tx.Class(),
LocalIP: tx.LocalIP(),
LocalPort: tx.LocalPort(),
RemoteIP: tx.RemoteIP(),
RemotePort: tx.RemotePort(),
Payload: strings.Join(tx.Cookie.Payload, "\n"),
FromCode: tx.Cookie.From,
Alert: true,
Subject: tx.Subject(),
}
ev.SearchRegion()
ev.Send()
tx.Cookie.TriggerHit(cnd, 1)
}
func (tx *Tx) drop(cnd string) {
tx.Cookie.StateBit(Delete)
}
func (tx *Tx) Index(L *lua.LState, key string) lua.LValue {
switch key {
case "id":
return lua.S2L(tx.Id)
case "cookie":
return tx.Cookie
case "node":
return lua.S2L(xEnv.Inet())
case "after":
after := time.Now().Unix() - tx.Cookie.Last
return lua.LInt(after)
case "count":
return lua.LInt(tx.Cookie.Count)
case "local_ip":
return lua.S2L(tx.LocalIP())
case "remote_ip":
return lua.S2L(tx.RemoteIP())
case "local_port":
return lua.LInt(tx.LocalPort())
case "remote_port":
return lua.LInt(tx.RemotePort())
}
if strings.HasPrefix(key, "ev_") && len(key) >= 5 {
return tx.ent.Index(L, key[3:])
}
if strings.HasPrefix(key, "cookie_") {
return tx.Cookie.Index(L, key[7:])
}
return lua.LNil
}