-
Notifications
You must be signed in to change notification settings - Fork 7
/
HFP.go
474 lines (393 loc) · 13.7 KB
/
HFP.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package main
import (
//"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
"time"
"github.com/guumaster/logsymbols"
)
const AppVersion = "1.0"
var localAddr *string = flag.String("l", ":9060", "Local HEP listening address")
var remoteAddr *string = flag.String("r", "192.168.2.2:9060", "Remote HEP address")
var IPfilter *string = flag.String("ipf", "", "IP filter address from HEP SRC or DST chunks. Option can use multiple IP as comma sepeated values. Default is no filter without processing HEP acting as high performance HEP proxy")
var IPfilterAction *string = flag.String("ipfa", "pass", "IP filter Action. Options are pass or reject")
var Debug *string = flag.String("d", "off", "Debug options are off or on")
var PrometheusPort *string = flag.String("prom", "8090", "Prometheus metrics port")
var (
AppLogger *log.Logger
filterIPs []string
HFPlog string = "HFP.log"
HEPsavefile string = "HEP/HEP-saved.arch"
)
func initLoopbackConn(wg *sync.WaitGroup) {
//Connect loopback in
outnet, err := net.Dial("tcp4", *localAddr)
if err != nil {
log.Println("c==>", logsymbols.Error, "|| INITIAL Loopback IN", err)
AppLogger.Println("c==>", logsymbols.Error, "|| INITIAL Loopback IN error", err)
} else {
_, err := outnet.Write([]byte("HELLO HFP"))
if err != nil {
log.Println("HELLO HFP c==>", logsymbols.Error, "|| Send HELLO HFP error", err)
AppLogger.Println("HELLO HFP c==>", logsymbols.Error, "|| Send HELLO HFP error", err)
} else {
log.Println("HELLO HFP c==>", logsymbols.Success, "|| INITIAL Dial LOOPBACK IN success")
AppLogger.Println("HELLO HFP c==>", logsymbols.Success, "|| INITIAL Dial LOOPBACK IN success")
}
}
wg.Add(1)
wg.Done()
}
func connectToHEPBackend(dst string) net.Conn {
for {
conn, err := net.Dial("tcp", dst)
if err != nil {
log.Println("Unable to connect to server: ", err)
connectionStatus.Set(0)
time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
} else {
log.Println("Connected to server successfully ", conn)
connectionStatus.Set(1)
copyHEPFileOut(conn)
return conn
}
}
}
func handleConnection(clientConn net.Conn, destAddr string) {
var destConn net.Conn
//var err error
// use a buffer to transfer data between connections
buf := make([]byte, 65535)
// for {
// destConn, err = net.Dial("tcp", destAddr)
// if err != nil {
// log.Println("||-->", logsymbols.Error, "Dial OUT reconnect failure - retrying", err)
// AppLogger.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying")
// copyHEPbufftoFile(buf[:n2], HEPsavefile)
//
// //log.Println(err)
// time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
// continue
// }
// break
// }
//defer destConn.Close()
go func() {
destConn = connectToHEPBackend(destAddr)
}()
//reader := bufio.NewReader(clientConn)
for {
//n, err := reader.Read(buf)
n, err := clientConn.Read(buf)
if err != nil {
log.Println(err)
return
}
if *Debug == "on" {
log.Println("-->|| Got", n, "bytes on wire -- Total buffer size:", len(buf))
}
//Prometheus timestamp metric of incoming packet to detect lack of inbound HEP traffic
clientLastMetricTimestamp.SetToCurrentTime()
if destConn != nil {
//
if *IPfilter != "" && *IPfilterAction == "pass" {
hepPkt, err := DecodeHEP(buf[:n])
if err != nil {
log.Println("Error decoding HEP", err)
}
if *Debug == "on" {
//log.Println("HEP decoded ", hepPkt)
log.Println("HEP decoded SRC IP", hepPkt.SrcIP)
log.Println("HEP decoded DST IP", hepPkt.DstIP)
}
var accepted bool = false
for _, ipf := range filterIPs {
if hepPkt.SrcIP == string(ipf) || hepPkt.DstIP == string(ipf) || string(buf[:n]) == "HELLO HFP" {
//Send HEP out to backend
if _, err_HEPout := destConn.Write(buf[:n]); err_HEPout != nil {
log.Println("||-->", logsymbols.Error, " Sending HEP OUT error:", err_HEPout)
// rb := bytes.NewReader(buf[:data])
connectionStatus.Set(0)
copyHEPbufftoFile(buf[:n], HEPsavefile)
accepted = true
for {
destConn, err = net.Dial("tcp4", destAddr)
if err != nil {
log.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying", err)
AppLogger.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying")
time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
continue
} else {
connectionStatus.Set(1)
copyHEPFileOut(destConn)
}
break
}
continue
} else {
if *Debug == "on" {
if string(buf[:n]) == "HELLO HFP" {
log.Println("||--> Sending init HELLO HFP successful with filter for", string(ipf), "to", destConn.RemoteAddr())
} else {
log.Println("||--> Sending HEP OUT successful with filter for", string(ipf), "to", destConn.RemoteAddr())
}
}
accepted = true
}
}
}
if !accepted {
if *Debug == "on" {
log.Println("-->", logsymbols.Error, "|| HEP filter not matched with source or destination IP in HEP packet", hepPkt.SrcIP, "or", hepPkt.DstIP)
}
}
} else if *IPfilter != "" && *IPfilterAction == "reject" {
hepPkt, err := DecodeHEP(buf[:n])
if err != nil {
log.Println("Error decoding HEP", err)
}
if *Debug == "on" {
//log.Println("HEP decoded ", hepPkt)
log.Println("HEP decoded SRC IP", hepPkt.SrcIP)
log.Println("HEP decoded DST IP", hepPkt.DstIP)
}
var rejected bool = false
for _, ipf := range filterIPs {
if hepPkt.SrcIP == string(ipf) || hepPkt.DstIP == string(ipf) {
clientConn.Write([]byte("Rejecting IP"))
if *Debug == "on" {
log.Printf("-->|| Rejecting IP:%q", ipf)
}
rejected = true
break
}
}
if !rejected {
//Send HEP out to backend
if _, err_HEPout := destConn.Write(buf[:n]); err_HEPout != nil {
log.Println("||-->", logsymbols.Error, " Sending HEP OUT error:", err_HEPout)
//rb := bytes.NewReader(buf[:data])
connectionStatus.Set(0)
copyHEPbufftoFile(buf[:n], HEPsavefile)
for {
destConn, err = net.Dial("tcp4", destAddr)
if err != nil {
log.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying", err)
AppLogger.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying")
time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
continue
} else {
connectionStatus.Set(1)
copyHEPFileOut(destConn)
}
break
}
continue
//return
} else {
if *Debug == "on" {
log.Println("||-->", logsymbols.Success, " Sending HEP OUT successful with filter to", destConn.RemoteAddr())
}
}
}
} else {
//Send HEP out to backend
_, err_HEPout := destConn.Write(buf[:n])
if err_HEPout != nil {
log.Println("||-->", logsymbols.Error, " Sending HEP OUT error:", err_HEPout)
// rb := bytes.NewReader(buf[:data])
connectionStatus.Set(0)
copyHEPbufftoFile(buf[:n], HEPsavefile)
for {
destConn, err = net.Dial("tcp4", destAddr)
if err != nil {
log.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying", err)
AppLogger.Println("||-->", logsymbols.Error, " Dial OUT reconnect failure - retrying")
time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
continue
} else {
connectionStatus.Set(1)
copyHEPFileOut(destConn)
}
break
}
continue
// return
} else {
if *Debug == "on" {
if string(buf[:n]) == "HELLO HFP" {
log.Println("||-->", logsymbols.Success, " Sending init HELLO HFP successful without filters to", destConn.RemoteAddr())
} else {
log.Println("||-->", logsymbols.Success, " Sending HEP OUT successful without filters to", destConn.RemoteAddr())
}
}
}
}
//
//_, err = destConn.Write(buf[:n])
// if err != nil {
// log.Println(logsymbols.Error, err)
// destConn.Close()
// copyHEPbufftoFile(buf[:n], HEPsavefile)
// for {
// destConn, err = net.Dial("tcp4", destAddr)
// if err != nil {
// log.Println("||-->X Dial OUT reconnect failure - retrying", err)
// AppLogger.Println("||-->X Dial OUT reconnect failure - retrying")
// time.Sleep(time.Second * 5) // wait for 5 seconds before reconnecting
// continue
// } else {
// copyHEPFileOut(destConn)
// }
// break
// }
// continue
// }
} else {
hepPkt, err := DecodeHEP(buf[:n])
if err != nil {
log.Println("Error decoding HEP", err)
}
if *Debug == "on" {
//log.Println("HEP decoded ", hepPkt)
log.Println("HEP decoded SRC IP", hepPkt.SrcIP)
log.Println("HEP decoded DST IP", hepPkt.DstIP)
}
for _, ipf := range filterIPs {
if ((hepPkt.SrcIP == string(ipf) || hepPkt.DstIP == string(ipf) || string(buf[:n]) == "HELLO HFP") && *IPfilterAction == "pass") || ((hepPkt.SrcIP != string(ipf) || hepPkt.DstIP != string(ipf)) && *IPfilterAction == "reject") {
copyHEPbufftoFile(buf[:n], HEPsavefile)
} else {
log.Println("Not logging filtered HEP traffic")
}
}
}
}
}
func copyHEPbufftoFile(inbytes []byte, file string) (int64, error) {
destination, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println("Open HEP file error", err)
}
defer destination.Close()
nBytes, err := destination.Write(inbytes)
if err != nil {
log.Println("||-->", logsymbols.Error, " File Send HEP from buffer to file error", err)
AppLogger.Println("||-->", logsymbols.Error, " File Send HEP from buffer to file error", err)
} else {
log.Println("||-->", logsymbols.Success, " File Send HEP from buffer to file OK")
AppLogger.Println("||-->", logsymbols.Success, "File Send HEP from buffer to file OK")
go hepBytesInFile.Add(float64(nBytes))
}
return int64(nBytes), err
}
func copyHEPFileOut(outnet net.Conn) (int, error) {
HEPFileData, HEPFileDataerr := ioutil.ReadFile(HEPsavefile)
if HEPFileDataerr != nil {
fmt.Println("Read HEP file error", HEPFileDataerr)
}
//Send Logged HEP upon reconnect out to backend
hl, err := outnet.Write(HEPFileData)
if err != nil {
log.Println("||-->X Send HEP from LOG error", err)
AppLogger.Println("||-->X Send HEP from LOG error", err)
hepFileFlushesError.Inc()
} else {
fi, err := os.Stat(HEPsavefile)
if err != nil {
log.Println("Cannot stat HEP log file", err)
AppLogger.Println("Cannot stat HEP log file", err)
}
if fi.Size() > 0 {
log.Println("||-->", logsymbols.Success, " Send HEP from LOG OK -", hl, "bytes")
log.Println("Clearing HEP file")
AppLogger.Println("||-->", logsymbols.Success, " Send HEP from LOG OK -", hl, "bytes")
AppLogger.Println("Clearing HEP file")
//Recreate file, thus cleaning the content
os.Create(HEPsavefile)
hepFileFlushesSuccess.Inc()
}
}
return hl, err
}
func main() {
var wg sync.WaitGroup
logsymbols.ForceColors()
version := flag.Bool("v", false, "Prints current HFP version")
flag.Parse()
if *version {
fmt.Println(AppVersion)
os.Exit(0)
}
filterIPs = strings.Split(*IPfilter, ",")
errmkdir := os.Mkdir("HEP", 0755)
if errmkdir != nil {
log.Println(errmkdir)
}
if _, errhfexist := os.Stat(HEPsavefile); errhfexist != nil {
if os.IsNotExist(errhfexist) {
fmt.Println("HEP File doesnt exists - Creating", errhfexist)
_, errhfcreate := os.Create(HEPsavefile)
fmt.Println(logsymbols.Info, "-->|| Creating HEP file")
if errhfcreate != nil {
fmt.Println("Create file error", errhfcreate)
return
}
}
}
applog, err := os.OpenFile(HFPlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
AppLogger = log.New(applog, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
fi, err := os.Stat(HEPsavefile)
if err != nil {
log.Println(logsymbols.Error, err)
}
fmt.Println(logsymbols.Info, "Saved HEP file is ", fi.Size(), "bytes\n")
fmt.Printf("Listening for HEP on: %v\nProxying HEP to: %v\nIPFilter: %v\nIPFilterAction: %v\nPrometheus metrics: %v\n\n", *localAddr, *remoteAddr, *IPfilter, *IPfilterAction, *PrometheusPort)
AppLogger.Println("Listening for HEP on:", *localAddr, "\n", "Proxying HEP to:", *remoteAddr, "\n", "IPFilter:", *IPfilter, "\n", "IPFilterAction:", *IPfilterAction, "\n", "Prometheus metrics:", *PrometheusPort, "\n")
if *IPfilter == "" {
fmt.Println(logsymbols.Success, "HFP starting in proxy high performance mode\n__________________________________________\n")
AppLogger.Println(logsymbols.Success, "HFP starting in proxy high performance mode\n__________________________________________\n")
} else {
fmt.Println(logsymbols.Success, "HFP starting in proxy processing mode\n_____________________________________\n")
AppLogger.Println(logsymbols.Success, "HFP starting in proxy processing mode\n_____________________________________\n")
}
addr, err := net.ResolveTCPAddr("tcp", *localAddr)
if err != nil {
log.Println(logsymbols.Error, err)
return
}
listener, err := net.ListenTCP("tcp4", addr)
if err != nil {
fmt.Println(logsymbols.Error, "|| HFP starting error", err)
os.Exit(1)
// } else {
// fmt.Println(logsymbols.Success, "|| HFP listener started")
//
// }
}
defer listener.Close()
go startMetrics(&wg)
go initLoopbackConn(&wg)
wg.Wait()
for {
clientConn, err := listener.AcceptTCP()
log.Println(logsymbols.Success, "-->|| New connection from", clientConn.RemoteAddr())
AppLogger.Println(logsymbols.Success, "-->|| New connection from", clientConn.RemoteAddr())
connectedClients.Inc()
if err != nil {
log.Println(logsymbols.Error, err)
return
}
for i := 1; i <= 2; i++ {
go handleConnection(clientConn, *remoteAddr)
}
}
}