/
agentSub.go
303 lines (255 loc) · 10.3 KB
/
agentSub.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package service
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"sort"
"github.com/Jeffail/gabs/v2"
"github.com/sipcapture/homer-app/config"
"github.com/sipcapture/homer-app/model"
"github.com/sipcapture/homer-app/utils/exportwriter"
"github.com/sipcapture/homer-app/utils/logger"
)
type AgentsubService struct {
ServiceConfig
}
// GetAgentsub gets all active HEPSUB agent sessions from database
func (hs *AgentsubService) GetAgentsub() (string, error) {
var AgentsubObject []model.TableAgentLocationSession
var count int
if err := hs.Session.Debug().Table("agent_location_session").
Find(&AgentsubObject).Count(&count).Error; err != nil {
return "", err
}
sort.Slice(AgentsubObject[:], func(i, j int) bool {
return AgentsubObject[i].GUID < AgentsubObject[j].GUID
})
response, _ := json.Marshal(AgentsubObject)
dataElement, _ := gabs.ParseJSON(response)
reply := gabs.New()
reply.Set("successfully retrieved agent records", "message")
reply.Set(dataElement.Data(), "data")
return reply.String(), nil
}
// GetAgentsubAgainstGUID gets a specific active HEPSUB agent session from database identified by GUID
func (hs *AgentsubService) GetAgentsubAgainstGUID(guid string) (string, error) {
var AgentsubObject []model.TableAgentLocationSession
var count int
if err := hs.Session.Debug().Table("agent_location_session").
Where("guid = ?", guid).
Find(&AgentsubObject).Count(&count).Error; err != nil {
return "", err
}
if len(AgentsubObject) == 0 {
return "", fmt.Errorf("no agent's record found for guid %s", guid)
}
sort.Slice(AgentsubObject[:], func(i, j int) bool {
return AgentsubObject[i].GUID < AgentsubObject[j].GUID
})
response, _ := json.Marshal(AgentsubObject)
dataElement, _ := gabs.ParseJSON(response)
reply := gabs.New()
reply.Set("agent record", "message")
reply.Set(dataElement.Data(), "data")
return reply.String(), nil
}
// GetAgentsubAgainstType gets all active HEPSUB agent sessions from database for a given type (pattern matched)
func (hs *AgentsubService) GetAgentsubAgainstType(typeRequest string) (string, error) {
var AgentsubObject []model.TableAgentLocationSession
var count int
whereSQL := fmt.Sprintf("expire_date > NOW() AND type LIKE '%%%s%%'", typeRequest)
if err := hs.Session.Debug().Table("agent_location_session").
Where(whereSQL).
Find(&AgentsubObject).Count(&count).Error; err != nil {
return "", err
}
if len(AgentsubObject) == 0 {
reply := gabs.New()
reply.Set(fmt.Sprintf("no agent subscription object found for type [%s]", typeRequest), "message")
reply.Set("", "data")
return reply.String(), fmt.Errorf("no agent subscription object found for type %s", typeRequest)
}
sort.Slice(AgentsubObject[:], func(i, j int) bool {
return AgentsubObject[i].GUID < AgentsubObject[j].GUID
})
response, _ := json.Marshal(AgentsubObject)
dataElement, _ := gabs.ParseJSON(response)
reply := gabs.New()
reply.Set("agent record", "message")
reply.Set(dataElement.Data(), "data")
return reply.String(), nil
}
// GetAgentsubAgainstGUIDAndType gets an active HEPSUB agent session by GUID for a given type (pattern matched)
func (hs *AgentsubService) GetAgentsubAgainstGUIDAndType(guid string, typeRequest string) (model.TableAgentLocationSession, error) {
var AgentsubObject model.TableAgentLocationSession
var count int
whereSQL := fmt.Sprintf("expire_date > NOW() AND guid = '%s' AND type LIKE '%%%s%%'", guid, typeRequest)
if err := hs.Session.Debug().Table("agent_location_session").
Where(whereSQL).
Find(&AgentsubObject).Count(&count).Error; err != nil {
return AgentsubObject, err
}
return AgentsubObject, nil
}
// GetAuthKeyByHeaderToken gets user auth tokens from database
func (hs *AgentsubService) GetAuthKeyByHeaderToken(token string) (string, error) {
var tokenObject model.TableAuthToken
var count int
if err := hs.Session.Debug().Table("auth_token").
Where("expire_date > NOW() AND active = true AND token = ? ", token).
Find(&tokenObject).Count(&count).Error; err != nil {
return "", err
}
if count == 0 {
return "", fmt.Errorf("no auth_token found or it has been expired: [%s]", token)
}
tokenObject.LastUsageDate = time.Now()
if err := hs.Session.Debug().Table("auth_token").
Where("token = ?", token).
Update(&tokenObject).Error; err != nil {
return "", err
}
response, _ := json.Marshal(tokenObject)
dataElement, _ := gabs.ParseJSON(response)
reply := gabs.New()
reply.Set("auth record", "message")
reply.Set(dataElement.Data(), "data")
return reply.String(), nil
}
// AddAgentsub creates a new HEPSUB agent session
func (hs *AgentsubService) AddAgentsub(data model.TableAgentLocationSession) (string, error) {
if err := hs.Session.Debug().Table("agent_location_session").
Create(&data).Error; err != nil {
return "", err
}
sidData := gabs.New()
sidData.Set(data.ExpireDate, "expire_date")
sidData.Set(data.ExpireDate.Unix(), "expire_ts")
sidData.Set(data.GUID, "uuid")
reply := gabs.New()
reply.Set("successfully created agent record", "message")
reply.Set(sidData.Data(), "data")
return reply.String(), nil
}
// UpdateAgentsubAgainstGUID updates an existing HEPSUB agent session
func (hs *AgentsubService) UpdateAgentsubAgainstGUID(guid string, data model.TableAgentLocationSession) (string, error) {
if err := hs.Session.Debug().Table("agent_location_session").
Where("guid = ?", guid).
Update(&data).Error; err != nil {
return "", err
}
response := fmt.Sprintf("{\"message\":\"successfully updated agent record\",\"data\":\"%s\"}", guid)
return response, nil
}
// DeleteAgentsubAgainstGUID removes an existing HEPSUB agent session
func (hs *AgentsubService) DeleteAgentsubAgainstGUID(guid string) (string, error) {
var AgentsubObject []model.TableAgentLocationSession
if err := hs.Session.Debug().Table("agent_location_session").
Where("guid = ? OR expire_date < NOW()", guid).
Delete(&AgentsubObject).Error; err != nil {
logger.Debug(err.Error())
return "", err
}
response := fmt.Sprintf("{\"message\":\"successfully deleted agent record\",\"data\":\"%s\"}", guid)
return response, nil
}
// DoSearchByPost loads the subscriber mappings from the database and builds a HEPSUB search query using request data
func (hs *AgentsubService) DoSearchByPost(agentObject model.TableAgentLocationSession, searchObject model.SearchObject, typeRequest string) ([]byte, error) {
var lookupField string
var hepsubObject []model.TableHepsubSchema
Data, _ := json.Marshal(searchObject.Param.Search)
sData, _ := gabs.ParseJSON(Data)
dataPost := gabs.New()
// process the search request(s), arriving "[hepid]_[type]" i.e. "1_call"
for key := range sData.ChildrenMap() {
elemArray := strings.Split(key, "_")
if len(elemArray) != 2 {
return nil, fmt.Errorf("Agent HEPSUB: key is wrong: %d", len(elemArray))
}
hepID, _ := strconv.Atoi(elemArray[0])
if err := hs.Session.Debug().Table("hepsub_mapping_schema").
Where("profile = ? AND hepid = ?", elemArray[1], hepID).
Find(&hepsubObject).Error; err != nil {
return nil, err
}
break
}
if len(hepsubObject) == 0 {
logger.Debug("Agent HEPSUB couldn't find agent mapping")
return nil, fmt.Errorf("agent HEPSUB couldn't find agent mapping")
}
sMapping, _ := gabs.ParseJSON(hepsubObject[0].Mapping)
if !sMapping.Exists("lookup_profile") || (!sMapping.Exists("lookup_field") && !sMapping.Exists("lookup_fields")) || !sMapping.Exists("lookup_range") {
return nil, fmt.Errorf("agent HEPSUB: the hepsub mapping corrupted: lookup_profile, lookup_field, lookup_range - have to be present")
}
lookupFields := sMapping.Search("source_fields")
for _, value := range sData.ChildrenMap() {
if lookupFields != nil {
for k := range lookupFields.ChildrenMap() {
dataV := value.Search(k).Data().([]interface{})
dataPost.Set(dataV, k)
}
} else {
dataV := value.Search("callid").Data()
dataPost.Set(dataV, "callid")
}
break
}
//lookupProfile := sMapping.Search("lookup_profile").Data().(string)
lookupField = sMapping.Search("lookup_field").Data().(string)
lookupRange := sMapping.Search("lookup_range").Data().([]interface{})
timeFrom := time.Unix(searchObject.Timestamp.From/int64(time.Microsecond), 0)
timeTo := time.Unix(searchObject.Timestamp.To/int64(time.Microsecond), 0)
if len(lookupRange) > 0 {
timeFrom = timeFrom.Add(time.Duration(lookupRange[0].(float64)) * time.Second).UTC()
timeTo = timeTo.Add(time.Duration(lookupRange[1].(float64)) * time.Second).UTC()
}
lookupField = strings.ReplaceAll(lookupField, "$source_field", dataPost.String())
lookupField = strings.ReplaceAll(lookupField, "$fromts", fmt.Sprintf("%v", timeFrom.Unix()*int64(time.Microsecond)+(int64(timeFrom.Nanosecond())/int64(time.Microsecond))))
lookupField = strings.ReplaceAll(lookupField, "$tots", fmt.Sprintf("%v", timeTo.Unix()*int64(time.Microsecond)+(int64(timeTo.Nanosecond())/int64(time.Microsecond))))
serverURL := fmt.Sprintf("%s://%s:%d%s/%s", agentObject.Protocol, agentObject.Host, agentObject.Port, agentObject.Path, typeRequest)
serverNODE := agentObject.Node
req, err := http.NewRequest("POST", serverURL, bytes.NewBuffer([]byte(lookupField)))
if err != nil {
logger.Error("Couldn't make a request to agent. Query: ", serverURL)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// This one line implements the authentication required for the task.
//req.SetBasicAuth(ps.User, ps.Password)
defer config.Setting.MAIN_SETTINGS.SubscribeHttpClient.CloseIdleConnections()
resp, err := config.Setting.MAIN_SETTINGS.SubscribeHttpClient.Do(req)
if err != nil {
logger.Error("Agent HEPSUB: Couldn't make http query:", serverURL)
return nil, err
}
defer resp.Body.Close()
buf, _ := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Error("Agent HEPSUB: Couldn't read the data from IO-Buffer")
return nil, err
}
// when downloading the file, return only the file data
if typeRequest == "download" {
var buffer bytes.Buffer
export := exportwriter.NewWriter(buffer)
_, err := export.Buffer.Write(buf)
if err != nil {
logger.Error("write error to the download buffer", err)
}
return export.Buffer.Bytes(), nil
}
// respond to lookup requests
responseData, _ := gabs.ParseJSON(buf)
reply := gabs.New()
reply.Set("request answer", "message")
reply.Set(serverNODE, "node")
reply.Set(agentObject.GUID, "uuid")
reply.Set(responseData.Data(), "data")
return reply.Bytes(), nil
}