-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
433 lines (389 loc) · 12.8 KB
/
main.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"log/slog"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/jackc/pgx/v5/pgtype"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
)
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
func main() {
var port int = 514
var err error
var filter net.IP
var dbconn string
slog.SetDefault(logger)
// Input Args
for i, v := range os.Args {
switch v {
// Port
case "-p":
port, err = strconv.Atoi(os.Args[(i + 1)])
if err != nil {
log.Fatalln(err)
}
// Filter address
case "-f":
filter = net.ParseIP(os.Args[(i + 1)])
// Database string
case "-d":
dbconn = os.Args[(i + 1)]
}
}
ln, err := net.Listen("tcp4", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalln(err)
return
}
slog.Info(fmt.Sprintf("Listening on %s\n", ln.Addr().String()))
for {
conn, err := ln.Accept()
if err != nil {
slog.Error("Failed to Accept Incoming", slog.Any("err", err))
continue
}
if checkConnection(conn, filter) {
go handleConnection(conn, dbconn)
} else {
conn.Close()
}
}
}
// CheckConnection prevents any unwanted incomming logs from an ip or port
func checkConnection(conn net.Conn, filter net.IP) bool {
if filter == nil {
return true
}
if strings.Contains(conn.RemoteAddr().String(), filter.String()) {
return true
}
slog.Warn(fmt.Sprintf("Blocked Connection From: %s\n", conn.RemoteAddr().String()))
return false
}
func handleConnection(conn net.Conn, dbconn string) {
defer conn.Close()
buf := make([]byte, 2096)
_, err := conn.Read(buf)
if err != nil {
slog.Error("Failed to handle connection",
slog.String("function", "conn.Read"),
slog.Any("err", err))
return
}
smdr, err := parseBuffer(buf)
if err != nil {
slog.Error("Failed to handle connection",
slog.String("function", "parseBuffer"),
slog.Any("err", err))
}
db := pgInit(dbconn)
pgInsertSMDR(db, smdr)
}
type SMDR_Packet struct {
CallStart sql.NullTime `db:"CallStart"`
ConnectedTime pgtype.Interval `db:"ConnectedTime"`
RingTime sql.NullInt64 `db:"RingTime"`
Caller sql.NullString `db:"Caller"`
CallDirection sql.NullString `db:"CallDirection"`
CalledNumber sql.NullString `db:"CalledNumber"`
DialedNumber sql.NullString `db:"DialedNumber"`
Account sql.NullString `db:"Account"`
IsInternal sql.NullBool `db:"IsInternal"`
CallId sql.NullInt64 `db:"CallId"`
Continuation sql.NullBool `db:"Continuation"`
Party1Device sql.NullString `db:"Party1Device"`
Party1Name sql.NullString `db:"Party1Name"`
Party2Device sql.NullString `db:"Party2Device"`
Party2Name sql.NullString `db:"Party2Name"`
ExternalTargeterId sql.NullString `db:"ExternalTargeterId"`
HoldTime sql.NullInt64 `db:"HoldTime"`
ParkTime sql.NullInt64 `db:"ParkTime"`
AuthValid sql.NullString `db:"AuthValid"`
AuthCode sql.NullString `db:"AuthCode"`
UserCharged sql.NullString `db:"UserCharged"`
CallCharge sql.NullString `db:"CallCharge"`
Currency sql.NullString `db:"Currency"`
AmountatLastUserChange sql.NullString `db:"AmountatLastUserChange"`
CallUnits sql.NullString `db:"CallUnits"`
UnitsatLastUserChange sql.NullString `db:"UnitsatLastUserChange"`
CostperUnit sql.NullString `db:"CostperUnit"`
MarkUp sql.NullString `db:"MarkUp"`
ExternalTargetingCause sql.NullString `db:"ExternalTargetingCause"`
ExternalTargetedNumber sql.NullString `db:"ExternalTargetedNumber"`
CallingPartyServerIpAddress sql.NullString `db:"CallingPartyServerIpAddress"`
UniqueCallIDForTheCallerExt sql.NullString `db:"UniqueCallIDForTheCallerExt"`
CalledPartyServerIP sql.NullString `db:"CalledPartyServerIP"`
UniqueCallIDforCalledExt sql.NullString `db:"UniqueCallIDforCalledExt"`
SMDRRecordTime sql.NullTime `db:"SMDRRecordTime"`
CallerConsentDirective sql.NullString `db:"CallerConsentDirective"`
CallingNumberVerification sql.NullString `db:"CallingNumberVerification"`
Undefined sql.NullString `db:"Undefined"`
}
func parseBuffer(buf []byte) (SMDR_Packet, error) {
var smdr SMDR_Packet
// 0 \r\n\u0000\u0000
str := string(buf)
newline := strings.Index(str, "\n")
if newline > 0 {
str = str[:newline]
}
arr := strings.Split(str, ",")
if len(arr) != 38 {
for i, v := range arr {
fmt.Printf("%d: %s\n", (i + 1), v)
}
return smdr, errors.New(fmt.Sprintf("parseBuffer: %d columns received instead of 38", len(arr)))
}
smdr = SMDR_Packet{
CallStart: validateDateTime(arr[0]),
ConnectedTime: validateInterval(arr[1]),
RingTime: validateInt64(arr[2]),
Caller: validateString(arr[3]),
CallDirection: validateString(arr[4]),
CalledNumber: validateString(arr[5]),
DialedNumber: validateString(arr[6]),
Account: validateString(arr[7]),
IsInternal: validateBool(arr[8]),
CallId: validateInt64(arr[9]),
Continuation: validateBool(arr[10]),
Party1Device: validateString(arr[11]),
Party1Name: validateString(arr[12]),
Party2Device: validateString(arr[13]),
Party2Name: validateString(arr[14]),
ExternalTargeterId: validateString(arr[15]),
HoldTime: validateInt64(arr[16]),
ParkTime: validateInt64(arr[17]),
AuthValid: validateString(arr[18]),
AuthCode: validateString(arr[19]),
UserCharged: validateString(arr[20]),
CallCharge: validateString(arr[21]),
Currency: validateString(arr[22]),
AmountatLastUserChange: validateString(arr[23]),
CallUnits: validateString(arr[24]),
UnitsatLastUserChange: validateString(arr[25]),
CostperUnit: validateString(arr[26]),
MarkUp: validateString(arr[27]),
ExternalTargetingCause: validateString(arr[28]),
ExternalTargetedNumber: validateString(arr[29]),
CallingPartyServerIpAddress: validateString(arr[30]),
UniqueCallIDForTheCallerExt: validateString(arr[31]),
CalledPartyServerIP: validateString(arr[32]),
UniqueCallIDforCalledExt: validateString(arr[33]),
SMDRRecordTime: validateDateTime(arr[34]),
CallerConsentDirective: validateString(arr[35]),
CallingNumberVerification: validateString(arr[36]),
Undefined: validateString(arr[37]),
}
return smdr, nil
}
// Expects 00:00:00 input. Hour:Minute:Second
func validateInterval(s string) pgtype.Interval {
timeArr := strings.Split(s, ":")
if len(timeArr) != 3 {
return pgtype.Interval{Valid: false}
}
second, err := strconv.ParseInt(timeArr[2], 10, 0)
if err != nil {
return pgtype.Interval{Valid: false}
}
minute, err := strconv.ParseInt(timeArr[1], 10, 0)
if err != nil {
return pgtype.Interval{Valid: false}
}
hour, err := strconv.ParseInt(timeArr[0], 10, 0)
if err != nil {
return pgtype.Interval{Valid: false}
}
var ms int64 = 0
ms = ms + (second * 1e6)
ms = ms + (minute * 60e6)
ms = ms + (hour * 3600e6)
// not converting days and months since it is VERY unlikley and will have a performance cost.
return pgtype.Interval{Months: 0, Days: 0, Microseconds: ms, Valid: true}
}
func validateDateTime(s string) sql.NullTime {
dt, err := time.Parse("2006/01/02 15:04:05", s)
if err != nil {
return sql.NullTime{Valid: false}
}
return sql.NullTime{Time: dt, Valid: true}
}
func validateString(s string) sql.NullString {
if len(s) < 1 {
return sql.NullString{Valid: false}
}
return sql.NullString{String: s, Valid: true}
}
func validateInt64(s string) sql.NullInt64 {
i, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return sql.NullInt64{Valid: false}
}
return sql.NullInt64{Int64: i, Valid: true}
}
func validateBool(s string) sql.NullBool {
b, err := strconv.ParseBool(s)
if err != nil {
return sql.NullBool{Valid: false}
}
return sql.NullBool{Bool: b, Valid: true}
}
var pgSchema = `
CREATE TABLE IF NOT EXISTS AvayaData(
CallStart timestamp,
ConnectedTime interval,
RingTime integer,
Caller Text,
CallDirection Text,
CalledNumber Text,
DialedNumber Text,
Account Text,
IsInternal boolean,
CallId integer,
Continuation boolean,
Party1Device Text,
Party1Name Text,
Party2Device Text,
Party2Name Text,
ExternalTargeterId Text,
HoldTime integer,
ParkTime integer,
AuthValid Text,
AuthCode Text,
UserCharged Text,
CallCharge Text,
Currency Text,
AmountatLastUserChange Text,
CallUnits Text,
UnitsatLastUserChange Text,
CostperUnit Text,
MarkUp Text,
ExternalTargetingCause Text,
ExternalTargetedNumber Text,
CallingPartyServerIpAddress Text,
UniqueCallIDForTheCallerExt Text,
CalledPartyServerIP Text,
UniqueCallIDforCalledExt Text,
SMDRRecordTime timestamp,
CallerConsentDirective Text,
CallingNumberVerification Text,
Undefined Text
);
`
// Open connection to PG DB. If connection cannot be opened
// exit fatal to prevent running unregulated.
func pgInit(dbconn string) *sqlx.DB {
//db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/avaya?sslmode=disable")
db, err := sqlx.Open("pgx", dbconn)
if err != nil {
slog.Error("Failed to open DB",
slog.String("function", "sqlx.open"),
slog.Any("err", err))
log.Fatal("Failed to open DB")
}
_, err = db.Exec(pgSchema)
if err != nil {
slog.Error("Failed to init schema",
slog.String("function", "db.Exec"),
slog.Any("err", err))
log.Fatal("Failed to init schema")
}
return db
}
var insertQuery = `
INSERT INTO AvayaData (
CallStart,
ConnectedTime,
RingTime,
Caller,
CallDirection,
CalledNumber,
DialedNumber,
Account,
IsInternal,
CallId,
Continuation,
Party1Device,
Party1Name,
Party2Device,
Party2Name,
ExternalTargeterId,
HoldTime,
ParkTime,
AuthValid,
AuthCode,
UserCharged,
CallCharge,
Currency,
AmountatLastUserChange,
CallUnits,
UnitsatLastUserChange,
CostperUnit,
MarkUp,
ExternalTargetingCause,
ExternalTargetedNumber,
CallingPartyServerIpAddress,
UniqueCallIDForTheCallerExt,
CalledPartyServerIP,
UniqueCallIDforCalledExt,
SMDRRecordTime,
CallerConsentDirective,
CallingNumberVerification,
Undefined)
VALUES(
:CallStart,
:ConnectedTime,
:RingTime,
:Caller,
:CallDirection,
:CalledNumber,
:DialedNumber,
:Account,
:IsInternal,
:CallId,
:Continuation,
:Party1Device,
:Party1Name,
:Party2Device,
:Party2Name,
:ExternalTargeterId,
:HoldTime,
:ParkTime,
:AuthValid,
:AuthCode,
:UserCharged,
:CallCharge,
:Currency,
:AmountatLastUserChange,
:CallUnits,
:UnitsatLastUserChange,
:CostperUnit,
:MarkUp,
:ExternalTargetingCause,
:ExternalTargetedNumber,
:CallingPartyServerIpAddress,
:UniqueCallIDForTheCallerExt,
:CalledPartyServerIP,
:UniqueCallIDforCalledExt,
:SMDRRecordTime,
:CallerConsentDirective,
:CallingNumberVerification,
:Undefined)
`
func pgInsertSMDR(db *sqlx.DB, smdr SMDR_Packet) {
defer db.Close()
_, err := db.NamedQuery(insertQuery, smdr)
if err != nil {
slog.Error("Failed to insert",
slog.String("function", "db.NamedQuery"),
slog.Any("err", err))
}
}