aboutsummaryrefslogtreecommitdiff
path: root/connections_controller.go
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-10-05 21:46:18 +0000
committerEmiliano Ciavatta2020-10-05 21:46:18 +0000
commite905618113309eaba7227ff1328a20f6846e4afd (patch)
treef6dd471683ac8ed7e630ce84956508ead28eab83 /connections_controller.go
parentf11e5d9e55c963109af8b8517c7790bf2eb7cac8 (diff)
Implement timeline
Diffstat (limited to 'connections_controller.go')
-rw-r--r--connections_controller.go52
1 files changed, 33 insertions, 19 deletions
diff --git a/connections_controller.go b/connections_controller.go
index 2773506..2894193 100644
--- a/connections_controller.go
+++ b/connections_controller.go
@@ -31,40 +31,40 @@ type Connection struct {
}
type ConnectionsFilter struct {
- From string `form:"from" binding:"omitempty,hexadecimal,len=24"`
- To string `form:"to" binding:"omitempty,hexadecimal,len=24"`
- ServicePort uint16 `form:"service_port"`
- ClientAddress string `form:"client_address" binding:"omitempty,ip"`
- ClientPort uint16 `form:"client_port"`
- MinDuration uint `form:"min_duration"`
- MaxDuration uint `form:"max_duration" binding:"omitempty,gtefield=MinDuration"`
- MinBytes uint `form:"min_bytes"`
- MaxBytes uint `form:"max_bytes" binding:"omitempty,gtefield=MinBytes"`
- StartedAfter int64 `form:"started_after" `
- StartedBefore int64 `form:"started_before" binding:"omitempty,gtefield=StartedAfter"`
- ClosedAfter int64 `form:"closed_after" `
- ClosedBefore int64 `form:"closed_before" binding:"omitempty,gtefield=ClosedAfter"`
- Hidden bool `form:"hidden"`
- Marked bool `form:"marked"`
+ From string `form:"from" binding:"omitempty,hexadecimal,len=24"`
+ To string `form:"to" binding:"omitempty,hexadecimal,len=24"`
+ ServicePort uint16 `form:"service_port"`
+ ClientAddress string `form:"client_address" binding:"omitempty,ip"`
+ ClientPort uint16 `form:"client_port"`
+ MinDuration uint `form:"min_duration"`
+ MaxDuration uint `form:"max_duration" binding:"omitempty,gtefield=MinDuration"`
+ MinBytes uint `form:"min_bytes"`
+ MaxBytes uint `form:"max_bytes" binding:"omitempty,gtefield=MinBytes"`
+ StartedAfter int64 `form:"started_after" `
+ StartedBefore int64 `form:"started_before" binding:"omitempty,gtefield=StartedAfter"`
+ ClosedAfter int64 `form:"closed_after" `
+ ClosedBefore int64 `form:"closed_before" binding:"omitempty,gtefield=ClosedAfter"`
+ Hidden bool `form:"hidden"`
+ Marked bool `form:"marked"`
MatchedRules []string `form:"matched_rules" binding:"dive,hexadecimal,len=24"`
- Limit int64 `form:"limit"`
+ Limit int64 `form:"limit"`
}
type ConnectionsController struct {
- storage Storage
+ storage Storage
servicesController *ServicesController
}
func NewConnectionsController(storage Storage, servicesController *ServicesController) ConnectionsController {
return ConnectionsController{
- storage: storage,
+ storage: storage,
servicesController: servicesController,
}
}
func (cc ConnectionsController) GetConnections(c context.Context, filter ConnectionsFilter) []Connection {
var connections []Connection
- query := cc.storage.Find(Connections).Context(c).Sort("_id", false)
+ query := cc.storage.Find(Connections).Context(c)
from, _ := RowIDFromHex(filter.From)
if !from.IsZero() {
@@ -73,6 +73,8 @@ func (cc ConnectionsController) GetConnections(c context.Context, filter Connect
to, _ := RowIDFromHex(filter.To)
if !to.IsZero() {
query = query.Filter(OrderedDocument{{"_id", UnorderedDocument{"$gt": to}}})
+ } else {
+ query = query.Sort("_id", false)
}
if filter.ServicePort > 0 {
query = query.Filter(OrderedDocument{{"port_dst", filter.ServicePort}})
@@ -146,6 +148,10 @@ func (cc ConnectionsController) GetConnections(c context.Context, filter Connect
}
}
+ if !to.IsZero() {
+ connections = reverseConnections(connections)
+ }
+
return connections
}
@@ -178,3 +184,11 @@ func (cc ConnectionsController) setProperty(c context.Context, id RowID, propert
}
return updated
}
+
+func reverseConnections(connections []Connection) []Connection {
+ for i := 0; i < len(connections)/2; i++ {
+ j := len(connections) - i - 1
+ connections[i], connections[j] = connections[j], connections[i]
+ }
+ return connections
+}