forked from gaulliath/operative-framework
/
engine.go
119 lines (110 loc) · 2.78 KB
/
engine.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
package engine
import (
"net/http"
"os"
"time"
"github.com/Oni-kuki/operative-framework/config"
"github.com/Oni-kuki/operative-framework/filters"
"github.com/Oni-kuki/operative-framework/modules"
"github.com/Oni-kuki/operative-framework/session"
"github.com/jinzhu/gorm"
)
const VERSION = "1.20 (perception)"
// Generate New Session
func New() *session.Session {
conf, err := config.ParseConfig()
if err != nil {
panic(err.Error())
}
db, err := gorm.Open(conf.Database.Driver, conf.Database.Name)
if err != nil {
panic(err.Error())
}
t := time.Now()
timeText := t.Format("2006-01-02 15:04:05")
s := session.Session{
SessionName: "opf_" + timeText,
Version: VERSION,
Information: session.Information{
ApiStatus: false,
ModuleLaunched: 0,
Event: 0,
},
Stream: session.Stream{
Verbose: true,
},
Connection: session.Connection{
ORM: db,
Migrations: make(map[string]interface{}),
},
Client: session.GetOpfClient(),
Config: conf,
Alias: make(map[string]string),
}
s.Tracker.Server = &http.Server{
Addr: os.Getenv("TRACKING_HOST") + ":" + os.Getenv("TRACKING_PORT"),
Handler: s.GetTrackerRouter(),
}
s.Stream.Sess = &s
s.Connection.Migrate()
modules.LoadModules(&s)
filters.LoadFilters(&s)
db.Create(&s)
return &s
}
// Load Session With ID
func Load(id int) *session.Session {
conf, err := config.ParseConfig()
if err != nil {
panic(err.Error())
}
db, err := gorm.Open(conf.Database.Driver, conf.Database.Name)
if err != nil {
panic(err.Error())
}
s := session.Session{
Version: VERSION,
Stream: session.Stream{
Verbose: true,
},
Information: session.Information{
ApiStatus: false,
ModuleLaunched: 0,
Event: 0,
},
Connection: session.Connection{
ORM: db,
Migrations: make(map[string]interface{}),
},
Client: session.GetOpfClient(),
Config: conf,
}
s.Stream.Sess = &s
s.Connection.Migrate()
modules.LoadModules(&s)
filters.LoadFilters(&s)
// Load targets
var targets []*session.Target
s.Connection.ORM.Where("session_id = ?", id).Find(&targets)
s.Targets = targets
// Load target results
if len(s.Targets) > 0 {
for k, target := range s.Targets {
var linked []session.Linking
target.Results = make(map[string][]*session.OpfResults)
s.Connection.ORM.Where("session_id = ?", id).Where("target_base = ?", target.GetId()).Find(&linked)
s.Targets[k].TargetLinked = linked
s.Targets[k].Sess = &s
if len(s.Modules) > 0 {
for _, module := range s.Modules {
var results []*session.OpfResults
s.Connection.ORM.Where("session_id = ?", id).Where("module_name = ?", module.Name()).Where("target_id = ?", target.GetId()).Find(&results)
if len(results) > 0 {
target.Results[module.Name()] = results
}
}
}
}
}
return &s
}