forked from deepfence/YaraHunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
88 lines (72 loc) · 1.57 KB
/
session.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
package core
import (
"context"
"math/rand"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/hillu/go-yara/v4"
)
type Session struct {
sync.Mutex
Version string
Options *Options
Config *Config
Context context.Context
Log *Logger
YaraRules *yara.Rules
}
var (
session *Session
sessionSync sync.Once
err error
)
func (s *Session) Start() {
rand.Seed(time.Now().Unix())
s.InitLogger()
s.InitThreads()
}
func (s *Session) InitLogger() {
s.Log = &Logger{}
s.Log.SetLogLevel(*s.Options.DebugLevel)
}
func (s *Session) InitThreads() {
if *s.Options.Threads == 0 {
numCPUs := runtime.NumCPU()
s.Options.Threads = &numCPUs
}
runtime.GOMAXPROCS(*s.Options.Threads + 1)
}
func GetSession() *Session {
sessionSync.Do(func() {
session = &Session{
Context: context.Background(),
}
if session.Options, err = ParseOptions(); err != nil {
session.Log.Error(err.Error())
os.Exit(1)
}
if session.Config, err = ParseConfig(session.Options); err != nil {
session.Log.Error(err.Error())
os.Exit(1)
}
pathSeparator := string(os.PathSeparator)
var excludedPaths []string
for _, excludedPath := range session.Config.ExcludedPaths {
excludedPaths = append(excludedPaths, strings.Replace(excludedPath, "{sep}", pathSeparator, -1))
}
session.Config.ExcludedPaths = excludedPaths
session.Start()
if session.YaraRules == nil {
rules, err := compile(filescan, session)
if err != nil {
session.Log.Error("compiling rules issue: %s", err)
os.Exit(1)
}
session.YaraRules = rules
}
})
return session
}