diff options
-rw-r--r-- | application_context.go | 17 | ||||
-rw-r--r-- | application_router.go | 99 | ||||
-rw-r--r-- | application_router_test.go | 40 | ||||
-rw-r--r-- | caronte.go | 5 | ||||
-rw-r--r-- | connections.go | 19 | ||||
-rw-r--r-- | connections_controller.go | 171 | ||||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | go.sum | 40 | ||||
-rw-r--r-- | pcap_importer.go | 23 | ||||
-rw-r--r-- | pcap_importer_test.go | 4 | ||||
-rw-r--r-- | services_controller.go | 64 | ||||
-rw-r--r-- | storage.go | 35 | ||||
-rw-r--r-- | utils.go | 4 |
13 files changed, 465 insertions, 57 deletions
diff --git a/application_context.go b/application_context.go index 50a1665..3ac3031 100644 --- a/application_context.go +++ b/application_context.go @@ -13,12 +13,14 @@ type Config struct { } type ApplicationContext struct { - Storage Storage - Config Config - Accounts gin.Accounts - RulesManager RulesManager - PcapImporter *PcapImporter - IsConfigured bool + Storage Storage + Config Config + Accounts gin.Accounts + RulesManager RulesManager + PcapImporter *PcapImporter + ConnectionsController ConnectionsController + ServicesController *ServicesController + IsConfigured bool } func CreateApplicationContext(storage Storage) (*ApplicationContext, error) { @@ -88,5 +90,8 @@ func (sm *ApplicationContext) configure() { } sm.RulesManager = rulesManager sm.PcapImporter = NewPcapImporter(sm.Storage, serverIP, sm.RulesManager) + sm.ServicesController = NewServicesController(sm.Storage) + sm.ConnectionsController = NewConnectionsController(sm.Storage, sm.ServicesController) sm.IsConfigured = true + } diff --git a/application_router.go b/application_router.go index 5ee3d62..be01e11 100644 --- a/application_router.go +++ b/application_router.go @@ -117,6 +117,105 @@ func CreateApplicationRouter(applicationContext *ApplicationContext) *gin.Engine c.JSON(http.StatusAccepted, gin.H{"session": sessionID}) } }) + + api.GET("/pcap/sessions", func(c *gin.Context) { + success(c, applicationContext.PcapImporter.GetSessions()) + }) + + api.GET("/pcap/sessions/:id", func(c *gin.Context) { + sessionID := c.Param("id") + if session, isPresent := applicationContext.PcapImporter.GetSession(sessionID); isPresent { + success(c, session) + } else { + notFound(c, gin.H{"session": sessionID}) + } + }) + + api.DELETE("/pcap/sessions/:id", func(c *gin.Context) { + sessionID := c.Param("id") + session := gin.H{"session": sessionID} + if cancelled := applicationContext.PcapImporter.CancelSession(sessionID); cancelled { + c.JSON(http.StatusAccepted, session) + } else { + notFound(c, session) + } + }) + + api.GET("/connections", func(c *gin.Context) { + var filter ConnectionsFilter + if err := c.ShouldBindQuery(&filter); err != nil { + badRequest(c, err) + return + } + success(c, applicationContext.ConnectionsController.GetConnections(c, filter)) + }) + + api.GET("/connections/:id", func(c *gin.Context) { + if id, err := RowIDFromHex(c.Param("id")); err != nil { + badRequest(c, err) + } else { + if connection, isPresent := applicationContext.ConnectionsController.GetConnection(c, id); isPresent { + success(c, connection) + } else { + notFound(c, gin.H{"connection": id}) + } + } + }) + + api.POST("/connections/:id/:action", func(c *gin.Context) { + id, err := RowIDFromHex(c.Param("id")) + if err != nil { + badRequest(c, err) + return + } + + var result bool + switch action := c.Param("action"); action { + case "hide": + result = applicationContext.ConnectionsController.SetHidden(c, id, true) + case "show": + result = applicationContext.ConnectionsController.SetHidden(c, id, false) + case "mark": + result = applicationContext.ConnectionsController.SetMarked(c, id, true) + case "unmark": + result = applicationContext.ConnectionsController.SetMarked(c, id, false) + case "comment": + var comment struct { + Comment string `json:"comment" binding:"required"` + } + if err := c.ShouldBindJSON(&comment); err != nil { + badRequest(c, err) + return + } + result = applicationContext.ConnectionsController.SetComment(c, id, comment.Comment) + default: + badRequest(c, errors.New("invalid action")) + return + } + + if result { + c.Status(http.StatusAccepted) + } else { + notFound(c, gin.H{"connection": id}) + } + }) + + api.GET("/services", func(c *gin.Context) { + success(c, applicationContext.ServicesController.GetServices()) + }) + + api.PUT("/services", func(c *gin.Context) { + var service Service + if err := c.ShouldBindJSON(&service); err != nil { + badRequest(c, err) + return + } + if err := applicationContext.ServicesController.SetService(c, service); err == nil { + success(c, service) + } else { + unprocessableEntity(c, err) + } + }) } return router diff --git a/application_router_test.go b/application_router_test.go index 8c94127..c6f902c 100644 --- a/application_router_test.go +++ b/application_router_test.go @@ -10,6 +10,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" ) func TestSetupApplication(t *testing.T) { @@ -50,7 +51,7 @@ func TestRulesApi(t *testing.T) { assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/rules", Rule{Name: "testRule", Color: "invalidColor"}).Code) w := toolkit.MakeRequest("POST", "/api/rules", Rule{Name: "testRule", Color: "#fff"}) - var testRuleID struct {ID string} + var testRuleID struct{ ID string } assert.Equal(t, http.StatusOK, w.Code) assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRuleID)) assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/rules", @@ -61,15 +62,15 @@ func TestRulesApi(t *testing.T) { Rule{Name: "invalidRule", Color: "#000"}).Code) assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("PUT", "/api/rules/000000000000000000000000", Rule{Name: "invalidRule", Color: "#000"}).Code) - assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/" + testRuleID.ID, Rule{}).Code) - assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/" + testRuleID.ID, + assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID, Rule{}).Code) + assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID, Rule{Name: "invalidRule", Color: "invalidColor"}).Code) w = toolkit.MakeRequest("POST", "/api/rules", Rule{Name: "testRule2", Color: "#eee"}) - var testRule2ID struct {ID string} + var testRule2ID struct{ ID string } assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule2ID)) - assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/" + testRule2ID.ID, + assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("PUT", "/api/rules/"+testRule2ID.ID, Rule{Name: "testRule", Color: "#fff"}).Code) // duplicate - w = toolkit.MakeRequest("PUT", "/api/rules/" + testRuleID.ID, Rule{Name: "newRule1", Color: "#ddd"}) + w = toolkit.MakeRequest("PUT", "/api/rules/"+testRuleID.ID, Rule{Name: "newRule1", Color: "#ddd"}) var testRule Rule assert.Equal(t, http.StatusOK, w.Code) assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule)) @@ -79,7 +80,7 @@ func TestRulesApi(t *testing.T) { // GetRule assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("GET", "/api/rules/invalidID", nil).Code) assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("GET", "/api/rules/000000000000000000000000", nil).Code) - w = toolkit.MakeRequest("GET", "/api/rules/" + testRuleID.ID, nil) + w = toolkit.MakeRequest("GET", "/api/rules/"+testRuleID.ID, nil) assert.Equal(t, http.StatusOK, w.Code) assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &testRule)) assert.Equal(t, testRuleID.ID, testRule.ID.Hex()) @@ -99,17 +100,40 @@ func TestRulesApi(t *testing.T) { func TestPcapImporterApi(t *testing.T) { toolkit := NewRouterTestToolkit(t, true) + // Import pcap assert.Equal(t, http.StatusBadRequest, toolkit.MakeRequest("POST", "/api/pcap/file", nil).Code) assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/pcap/file", gin.H{"path": "invalidPath"}).Code) w := toolkit.MakeRequest("POST", "/api/pcap/file", gin.H{"path": "test_data/ping_pong_10000.pcap"}) - var sessionID struct {Session string} + var sessionID struct{ Session string } assert.Equal(t, http.StatusAccepted, w.Code) assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &sessionID)) assert.Equal(t, "369ef4b6abb6214b4ee2e0c81ecb93c49e275c26c85e30493b37727d408cf280", sessionID.Session) assert.Equal(t, http.StatusUnprocessableEntity, toolkit.MakeRequest("POST", "/api/pcap/file", gin.H{"path": "test_data/ping_pong_10000.pcap"}).Code) // duplicate + // Get sessions + var sessions []ImportingSession + w = toolkit.MakeRequest("GET", "/api/pcap/sessions", nil) + assert.Equal(t, http.StatusOK, w.Code) + assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &sessions)) + assert.Len(t, sessions, 1) + assert.Equal(t, sessionID.Session, sessions[0].ID) + + // Get session + var session ImportingSession + w = toolkit.MakeRequest("GET", "/api/pcap/sessions/"+sessionID.Session, nil) + assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &session)) + assert.Equal(t, sessionID.Session, session.ID) + + // Cancel session + assert.Equal(t, http.StatusNotFound, toolkit.MakeRequest("DELETE", "/api/pcap/sessions/invalidSession", + nil).Code) + assert.Equal(t, http.StatusAccepted, toolkit.MakeRequest("DELETE", "/api/pcap/sessions/"+sessionID.Session, + nil).Code) + + time.Sleep(1*time.Second) // wait for termination + toolkit.wrapper.Destroy(t) } @@ -1,7 +1,6 @@ package main import ( - "context" "flag" "fmt" log "github.com/sirupsen/logrus" @@ -18,8 +17,8 @@ func main() { flag.Parse() logFields := log.Fields{"host": *mongoHost, "port": *mongoPort, "dbName": *dbName} - storage := NewMongoStorage(*mongoHost, *mongoPort, *dbName) - if err := storage.Connect(context.Background()); err != nil { + storage, err := NewMongoStorage(*mongoHost, *mongoPort, *dbName) + if err != nil { log.WithError(err).WithFields(logFields).Fatal("failed to connect to MongoDB") } diff --git a/connections.go b/connections.go deleted file mode 100644 index 380c8a1..0000000 --- a/connections.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import "time" - -type Connection struct { - ID RowID `json:"id" bson:"_id"` - SourceIP string `json:"ip_src" bson:"ip_src"` - DestinationIP string `json:"ip_dst" bson:"ip_dst"` - SourcePort uint16 `json:"port_src" bson:"port_src"` - DestinationPort uint16 `json:"port_dst" bson:"port_dst"` - StartedAt time.Time `json:"started_at" bson:"started_at"` - ClosedAt time.Time `json:"closed_at" bson:"closed_at"` - ClientBytes int `json:"client_bytes" bson:"client_bytes"` - ServerBytes int `json:"server_bytes" bson:"server_bytes"` - ClientDocuments int `json:"client_documents" bson:"client_documents"` - ServerDocuments int `json:"server_documents" bson:"server_documents"` - ProcessedAt time.Time `json:"processed_at" bson:"processed_at"` - MatchedRules []RowID `json:"matched_rules" bson:"matched_rules"` -} diff --git a/connections_controller.go b/connections_controller.go new file mode 100644 index 0000000..7da464d --- /dev/null +++ b/connections_controller.go @@ -0,0 +1,171 @@ +package main + +import ( + "context" + "fmt" + log "github.com/sirupsen/logrus" + "time" +) + +const DefaultQueryLimit = 50 +const MaxQueryLimit = 200 + +type Connection struct { + ID RowID `json:"id" bson:"_id"` + SourceIP string `json:"ip_src" bson:"ip_src"` + DestinationIP string `json:"ip_dst" bson:"ip_dst"` + SourcePort uint16 `json:"port_src" bson:"port_src"` + DestinationPort uint16 `json:"port_dst" bson:"port_dst"` + StartedAt time.Time `json:"started_at" bson:"started_at"` + ClosedAt time.Time `json:"closed_at" bson:"closed_at"` + ClientBytes int `json:"client_bytes" bson:"client_bytes"` + ServerBytes int `json:"server_bytes" bson:"server_bytes"` + ClientDocuments int `json:"client_documents" bson:"client_documents"` + ServerDocuments int `json:"server_documents" bson:"server_documents"` + ProcessedAt time.Time `json:"processed_at" bson:"processed_at"` + MatchedRules []RowID `json:"matched_rules" bson:"matched_rules"` + Hidden bool `json:"hidden" bson:"hidden,omitempty"` + Marked bool `json:"marked" bson:"marked,omitempty"` + Comment string `json:"comment" bson:"comment,omitempty"` + Service Service `json:"service" bson:"-"` +} + +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"` + MatchedRules []RowID `form:"matched_rules"` + Limit int64 `form:"limit"` +} + +type ConnectionsController struct { + storage Storage + servicesController *ServicesController +} + +func NewConnectionsController(storage Storage, servicesController *ServicesController) ConnectionsController { + return ConnectionsController{ + 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) + + from, _ := RowIDFromHex(filter.From) + if !from.IsZero() { + query = query.Filter(OrderedDocument{{"_id", UnorderedDocument{"$lt": from}}}) + } + to, _ := RowIDFromHex(filter.To) + if !to.IsZero() { + query = query.Filter(OrderedDocument{{"_id", UnorderedDocument{"$gt": to}}}) + } + if filter.ServicePort > 0 { + query = query.Filter(OrderedDocument{{"port_dst", filter.ServicePort}}) + } + if len(filter.ClientAddress) > 0 { + query = query.Filter(OrderedDocument{{"ip_src", filter.ClientAddress}}) + } + if filter.ClientPort > 0 { + query = query.Filter(OrderedDocument{{"port_src", filter.ClientPort}}) + } + if filter.MinDuration > 0 { + query = query.Filter(OrderedDocument{{"$where", fmt.Sprintf("this.closed_at - this.started_at >= %v", filter.MinDuration)}}) + } + if filter.MaxDuration > 0 { + query = query.Filter(OrderedDocument{{"$where", fmt.Sprintf("this.closed_at - this.started_at <= %v", filter.MaxDuration)}}) + } + if filter.MinBytes > 0 { + query = query.Filter(OrderedDocument{{"$where", fmt.Sprintf("this.client_bytes + this.server_bytes >= %v", filter.MinBytes)}}) + } + if filter.MaxBytes > 0 { + query = query.Filter(OrderedDocument{{"$where", fmt.Sprintf("this.client_bytes + this.server_bytes <= %v", filter.MaxBytes)}}) + } + if filter.StartedAfter > 0 { + query = query.Filter(OrderedDocument{{"started_at", UnorderedDocument{"$gt": time.Unix(filter.StartedAfter, 0)}}}) + } + if filter.StartedBefore > 0 { + query = query.Filter(OrderedDocument{{"started_at", UnorderedDocument{"$lt": time.Unix(filter.StartedBefore, 0)}}}) + } + if filter.ClosedAfter > 0 { + query = query.Filter(OrderedDocument{{"closed_at", UnorderedDocument{"$gt": time.Unix(filter.ClosedAfter, 0)}}}) + } + if filter.ClosedBefore > 0 { + query = query.Filter(OrderedDocument{{"closed_at", UnorderedDocument{"$lt": time.Unix(filter.ClosedBefore, 0)}}}) + } + if filter.Hidden { + query = query.Filter(OrderedDocument{{"hidden", true}}) + } + if filter.Marked { + query = query.Filter(OrderedDocument{{"marked", true}}) + } + if filter.MatchedRules != nil && len(filter.MatchedRules) > 0 { + query = query.Filter(OrderedDocument{{"matched_rules", UnorderedDocument{"$all": filter.MatchedRules}}}) + } + if filter.Limit > 0 && filter.Limit <= MaxQueryLimit { + query = query.Limit(filter.Limit) + } else { + query = query.Limit(DefaultQueryLimit) + } + + if err := query.All(&connections); err != nil { + log.WithError(err).WithField("filter", filter).Panic("failed to get connections") + } + + if connections == nil { + return []Connection{} + } + + services := cc.servicesController.GetServices() + for i, connection := range connections { + if service, isPresent := services[connection.DestinationPort]; isPresent { + connections[i].Service = service + } + } + + return connections +} + +func (cc ConnectionsController) GetConnection(c context.Context, id RowID) (Connection, bool) { + var connection Connection + if err := cc.storage.Find(Connections).Context(c).Filter(byID(id)).First(&connection); err != nil { + log.WithError(err).WithField("id", id).Panic("failed to get connection") + } + + return connection, !connection.ID.IsZero() +} + +func (cc ConnectionsController) SetHidden(c context.Context, id RowID, hidden bool) bool { + return cc.setProperty(c, id, "hidden", hidden) +} + +func (cc ConnectionsController) SetMarked(c context.Context, id RowID, marked bool) bool { + return cc.setProperty(c, id, "marked", marked) +} + +func (cc ConnectionsController) SetComment(c context.Context, id RowID, comment string) bool { + return cc.setProperty(c, id, "comment", comment) +} + +func (cc ConnectionsController) setProperty(c context.Context, id RowID, propertyName string, propertyValue interface{}) bool { + updated, err := cc.storage.Update(Connections).Context(c).Filter(byID(id)). + One(UnorderedDocument{propertyName: propertyValue}) + if err != nil { + log.WithError(err).WithField("id", id).Panic("failed to update a connection property") + } + return updated +} @@ -3,6 +3,7 @@ module github.com/eciavatta/caronte go 1.14 require ( + github.com/apex/log v1.1.4 github.com/flier/gohs v1.0.0 github.com/gin-gonic/gin v1.6.2 github.com/go-playground/validator/v10 v10.2.0 @@ -1,13 +1,23 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/apex/log v1.1.4 h1:3Zk+boorIQAAGBrHn0JUtAau4ihMamT4WdnfdnXM1zQ= +github.com/apex/log v1.1.4/go.mod h1:AlpoD9aScyQfJDVHmLMEcx4oU6LqzkWp4Mg9GdAcEvQ= +github.com/apex/logs v0.0.4/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo= +github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy8kCu4PNA+aP7WUV72eXWJeP9/r3/K9aLE= +github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys= +github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= +github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/flier/gohs v1.0.0 h1:Q0mmufGWTigzKb140WmJ0+k3EGAf335Qgv/pz5SOPvU= github.com/flier/gohs v1.0.0/go.mod h1:Jlg6A1xXSMhPorF74/LkYHkCHZ87Txi8CqIHHyIKgKg= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.2 h1:88crIK23zO6TqlQBt+f9FrPJNKm9ZEr7qjp9vl/d5TM= github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= @@ -43,6 +53,7 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ= github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.3.5 h1:F768QJ1E9tib+q5Sc8MkdJi1RxLTbRcTf8LJV56aRls= @@ -55,14 +66,18 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/gopacket v1.1.14/go.mod h1:UCLx9mCmAwsVbn6qQl1WIEt2SO7Nd2fD0th1TBAsqBw= github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY= github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw= github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= @@ -74,6 +89,7 @@ github.com/klauspost/compress v1.9.5 h1:U+CaK85mrNNb4k8BNOfgJtJ/gr6kswUCFj6miSzV github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -81,8 +97,13 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= @@ -92,23 +113,30 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM= +github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbmil4Ui/dDdFBExb7/cmkNjyX5F97oglmvCDo= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= +github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -120,6 +148,10 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0= +github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0= +github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao= +github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= @@ -135,19 +167,24 @@ go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -174,10 +211,13 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/go-playground/validator.v9 v9.31.0 h1:bmXmP2RSNtFES+bn4uYuHT7iJFJv7Vj+an+ZQdDaD1M= gopkg.in/go-playground/validator.v9 v9.31.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pcap_importer.go b/pcap_importer.go index 24ce2cf..2bdbd1a 100644 --- a/pcap_importer.go +++ b/pcap_importer.go @@ -92,6 +92,16 @@ func (pi *PcapImporter) ImportPcap(fileName string) (string, error) { return hash, nil } +func (pi *PcapImporter) GetSessions() []ImportingSession { + pi.mSessions.Lock() + sessions := make([]ImportingSession, 0, len(pi.sessions)) + for _, session := range pi.sessions { + sessions = append(sessions, session) + } + pi.mSessions.Unlock() + return sessions +} + func (pi *PcapImporter) GetSession(sessionID string) (ImportingSession, bool) { pi.mSessions.Lock() defer pi.mSessions.Unlock() @@ -99,15 +109,14 @@ func (pi *PcapImporter) GetSession(sessionID string) (ImportingSession, bool) { return session, isPresent } -func (pi *PcapImporter) CancelSession(sessionID string) error { +func (pi *PcapImporter) CancelSession(sessionID string) bool { pi.mSessions.Lock() - defer pi.mSessions.Unlock() - if session, isPresent := pi.sessions[sessionID]; !isPresent { - return errors.New("session " + sessionID + " not found") - } else { + session, isPresent := pi.sessions[sessionID] + if isPresent { session.cancelFunc() - return nil } + pi.mSessions.Unlock() + return isPresent } // Read the pcap and save the tcp stream flow to the database @@ -211,7 +220,7 @@ func (pi *PcapImporter) progressUpdate(session ImportingSession, completed bool, if _, _err := pi.storage.Insert(ImportingSessions).One(session); _err != nil { log.WithError(_err).WithField("session", session).Error("failed to insert importing stats") } - session.completed <- session.ImportingError + close(session.completed) } } diff --git a/pcap_importer_test.go b/pcap_importer_test.go index cafa7a0..c79556a 100644 --- a/pcap_importer_test.go +++ b/pcap_importer_test.go @@ -48,8 +48,8 @@ func TestCancelImportSession(t *testing.T) { sessionID, err := pcapImporter.ImportPcap("test_data/ping_pong_10000.pcap") require.NoError(t, err) - assert.Error(t, pcapImporter.CancelSession("invalid")) - assert.NoError(t, pcapImporter.CancelSession(sessionID)) + assert.False(t, pcapImporter.CancelSession("invalid")) + assert.True(t, pcapImporter.CancelSession(sessionID)) session := waitSessionCompletion(t, pcapImporter, sessionID) assert.Zero(t, session.CompletedAt) diff --git a/services_controller.go b/services_controller.go new file mode 100644 index 0000000..9907b5e --- /dev/null +++ b/services_controller.go @@ -0,0 +1,64 @@ +package main + +import ( + "context" + "errors" + log "github.com/sirupsen/logrus" + "sync" +) + +type Service struct { + Port uint16 `json:"port" bson:"_id"` + Name string `json:"name" binding:"min=3" bson:"name"` + Color string `json:"color" binding:"hexcolor" bson:"color"` + Notes string `json:"notes" bson:"notes"` +} + +type ServicesController struct { + storage Storage + services map[uint16]Service + mutex sync.Mutex +} + +func NewServicesController(storage Storage) *ServicesController { + var result []Service + if err := storage.Find(Services).All(&result); err != nil { + log.WithError(err).Panic("failed to retrieve services") + return nil + } + + services := make(map[uint16]Service, len(result)) + for _, service := range result { + services[service.Port] = service + } + + return &ServicesController{ + storage: storage, + services: services, + } +} + +func (sc *ServicesController) SetService(c context.Context, service Service) error { + sc.mutex.Lock() + defer sc.mutex.Unlock() + var upsert interface{} + updated, err := sc.storage.Update(Services).Context(c).Filter(OrderedDocument{{"_id", service.Port}}). + Upsert(&upsert).One(service) + if err != nil { + return errors.New("duplicate name") + } + if updated || upsert != nil { + sc.services[service.Port] = service + } + return nil +} + +func (sc *ServicesController) GetServices() map[uint16]Service { + sc.mutex.Lock() + services := make(map[uint16]Service, len(sc.services)) + for _, service := range sc.services { + services[service.Port] = service + } + sc.mutex.Unlock() + return services +} @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -17,6 +16,7 @@ const ConnectionStreams = "connection_streams" const ImportingSessions = "importing_sessions" const Rules = "rules" const Settings = "settings" +const Services = "services" var ZeroRowID [12]byte @@ -27,7 +27,6 @@ type Storage interface { } type MongoStorage struct { - client *mongo.Client collections map[string]*mongo.Collection } @@ -36,12 +35,17 @@ type UnorderedDocument = bson.M type Entry = bson.E type RowID = primitive.ObjectID -func NewMongoStorage(uri string, port int, database string) *MongoStorage { +func NewMongoStorage(uri string, port int, database string) (*MongoStorage, error) { + ctx := context.Background() opt := options.Client() opt.ApplyURI(fmt.Sprintf("mongodb://%s:%v", uri, port)) client, err := mongo.NewClient(opt) if err != nil { - log.WithError(err).Panic("failed to create mongo client") + return nil, err + } + + if err := client.Connect(ctx); err != nil { + return nil, err } db := client.Database(database) @@ -51,16 +55,19 @@ func NewMongoStorage(uri string, port int, database string) *MongoStorage { ImportingSessions: db.Collection(ImportingSessions), Rules: db.Collection(Rules), Settings: db.Collection(Settings), + Services: db.Collection(Services), } - return &MongoStorage{ - client: client, - collections: collections, + if _, err := collections[Services].Indexes().CreateOne(ctx, mongo.IndexModel{ + Keys: bson.D{{"name", 1}}, + Options: options.Index().SetUnique(true), + }); err != nil { + return nil, err } -} -func (storage *MongoStorage) Connect(ctx context.Context) error { - return storage.client.Connect(ctx) + return &MongoStorage{ + collections: collections, + }, nil } // InsertOne and InsertMany @@ -153,7 +160,9 @@ func (fo MongoUpdateOperation) Context(ctx context.Context) UpdateOperation { } func (fo MongoUpdateOperation) Filter(filter OrderedDocument) UpdateOperation { - fo.filter = filter + for _, elem := range filter { + fo.filter = append(fo.filter, primitive.E{Key: elem.Key, Value: elem.Value}) + } return fo } @@ -242,7 +251,9 @@ func (fo MongoFindOperation) Context(ctx context.Context) FindOperation { } func (fo MongoFindOperation) Filter(filter OrderedDocument) FindOperation { - fo.filter = filter + for _, elem := range filter { + fo.filter = append(fo.filter, primitive.E{Key: elem.Key, Value: elem.Value}) + } return fo } @@ -73,3 +73,7 @@ func FileSize(filename string) int64 { } return info.Size() } + +func byID(id RowID) OrderedDocument { + return OrderedDocument{{"_id", id}} +} |