aboutsummaryrefslogtreecommitdiff
path: root/application_context.go
blob: 5db8843b21fdf8f5eded831ced2756c1c20bf034 (plain) (blame)
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
/*
 * This file is part of caronte (https://github.com/eciavatta/caronte).
 * Copyright (c) 2020 Emiliano Ciavatta.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package main

import (
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

type Config struct {
	ServerAddress string `json:"server_address" binding:"required,ip|cidr" bson:"server_address"`
	FlagRegex     string `json:"flag_regex" binding:"required,min=8" bson:"flag_regex"`
	AuthRequired  bool   `json:"auth_required" bson:"auth_required"`
}

type ApplicationContext struct {
	Storage                     Storage
	Config                      Config
	Accounts                    gin.Accounts
	RulesManager                RulesManager
	PcapImporter                *PcapImporter
	ConnectionsController       ConnectionsController
	ServicesController          *ServicesController
	ConnectionStreamsController ConnectionStreamsController
	SearchController            *SearchController
	StatisticsController        StatisticsController
	NotificationController      *NotificationController
	IsConfigured                bool
	Version                     string
}

func CreateApplicationContext(storage Storage, version string) (*ApplicationContext, error) {
	var configWrapper struct {
		Config Config
	}
	if err := storage.Find(Settings).Filter(OrderedDocument{{Key: "_id", Value: "config"}}).
		First(&configWrapper); err != nil {
		return nil, err
	}
	var accountsWrapper struct {
		Accounts gin.Accounts
	}

	if err := storage.Find(Settings).Filter(OrderedDocument{{Key: "_id", Value: "accounts"}}).
		First(&accountsWrapper); err != nil {
		return nil, err
	}
	if accountsWrapper.Accounts == nil {
		accountsWrapper.Accounts = make(gin.Accounts)
	}

	applicationContext := &ApplicationContext{
		Storage:  storage,
		Config:   configWrapper.Config,
		Accounts: accountsWrapper.Accounts,
		Version:  version,
	}

	return applicationContext, nil
}

func (sm *ApplicationContext) SetConfig(config Config) {
	sm.Config = config
	sm.Configure()
	var upsertResults interface{}
	if _, err := sm.Storage.Update(Settings).Upsert(&upsertResults).
		Filter(OrderedDocument{{Key: "_id", Value: "config"}}).One(UnorderedDocument{"config": config}); err != nil {
		log.WithError(err).WithField("config", config).Error("failed to update config")
	}
}

func (sm *ApplicationContext) SetAccounts(accounts gin.Accounts) {
	sm.Accounts = accounts
	var upsertResults interface{}
	if _, err := sm.Storage.Update(Settings).Upsert(&upsertResults).
		Filter(OrderedDocument{{Key: "_id", Value: "accounts"}}).One(UnorderedDocument{"accounts": accounts}); err != nil {
		log.WithError(err).Error("failed to update accounts")
	}
}

func (sm *ApplicationContext) SetNotificationController(notificationController *NotificationController) {
	sm.NotificationController = notificationController
}

func (sm *ApplicationContext) Configure() {
	if sm.IsConfigured {
		return
	}
	if sm.Config.ServerAddress == "" || sm.Config.FlagRegex == "" {
		return
	}
	serverNet := ParseIPNet(sm.Config.ServerAddress)
	if serverNet == nil {
		return
	}

	rulesManager, err := LoadRulesManager(sm.Storage, sm.Config.FlagRegex)
	if err != nil {
		log.WithError(err).Panic("failed to create a RulesManager")
	}
	sm.RulesManager = rulesManager
	sm.PcapImporter = NewPcapImporter(sm.Storage, *serverNet, sm.RulesManager, sm.NotificationController)
	sm.ServicesController = NewServicesController(sm.Storage)
	sm.SearchController = NewSearchController(sm.Storage)
	sm.ConnectionsController = NewConnectionsController(sm.Storage, sm.SearchController, sm.ServicesController)
	sm.ConnectionStreamsController = NewConnectionStreamsController(sm.Storage)
	sm.StatisticsController = NewStatisticsController(sm.Storage)
	sm.IsConfigured = true
}