-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
129 lines (111 loc) · 3.16 KB
/
config.go
File metadata and controls
129 lines (111 loc) · 3.16 KB
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
125
126
127
128
129
// config.go
package miner
import (
"fmt"
"path/filepath"
"time"
"github.com/asiffer/netspot/config"
"github.com/asiffer/netspot/miner/counters"
)
//----------------------------------------------------------------------------//
//---------------------------- EXPORTED FUNCTIONS ----------------------------//
//----------------------------------------------------------------------------//
// InitConfig initializes the miner package from the config module
func InitConfig() error {
if err := Zero(); err != nil {
return err
}
key := "miner.device"
s, err := config.GetString(key)
if err != nil {
minerLogger.Error().Msgf("Error while retrieving key %s: %v", key, err)
return err
}
if err := SetDevice(s); err != nil {
return err
}
key = "miner.snapshot_len"
l, err := config.GetStrictlyPositiveInt(key)
if err != nil {
minerLogger.Error().Msgf("Error while retrieving key %s: %v", key, err)
return err
}
SetSnapshotLen(int32(l))
key = "miner.promiscuous"
p, err := config.GetBool(key)
if err != nil {
minerLogger.Error().Msgf("Error while retrieving key %s: %v", key, err)
return err
}
SetPromiscuous(p)
key = "miner.timeout"
t, err := config.GetDuration(key)
if err != nil {
minerLogger.Error().Msgf("Error while retrieving key %s: %v", key, err)
return err
}
SetTimeout(t)
// log
minerLogger.Debug().Msg(fmt.Sprint("Available counters: ", counters.GetAvailableCounters()))
minerLogger.Info().Msg("Miner package configured")
return nil
}
// GetNumberOfDevices returns the number of available devices (interfaces)
func GetNumberOfDevices() int {
return len(GetAvailableDevices())
}
// IsDeviceInterface check if the current device is an interface
func IsDeviceInterface() bool {
return iface
}
// IsPromiscuous returns the current status of the interface
// (not relevant for pcap file)
func IsPromiscuous() bool {
return promiscuous
}
// SetPromiscuous set the promiscuous mode. If true, it means that the interface
// will receives packets that are not intended for it.
func SetPromiscuous(b bool) error {
promiscuous = b
minerLogger.Debug().Msgf("Promiscuous set to %v", b)
return nil
}
// SetSnapshotLen sets the maximum size of packets which are captured
func SetSnapshotLen(sl int32) error {
if sl <= 0 {
return fmt.Errorf("snapshot length must be strictly positive")
}
snapshotLen = sl
minerLogger.Debug().Msgf("Snapshot length set to %d", sl)
return nil
}
// SetTimeout set the timeout to the desired duration
func SetTimeout(d time.Duration) error {
timeout = d
minerLogger.Debug().Msgf("Timeout set to %s", d)
return nil
}
// GetDevice returns the current device (interface name or capture file)
func GetDevice() string {
return device
}
// SetDevice sets the device to listen. It can be either an interface or
// a capture file (ex: .pcap)
func SetDevice(dev string) error {
if contains(availableDevices, dev) {
device = dev
iface = true
} else {
dev, err := filepath.Abs(dev)
if err == nil && fileExists(dev) {
device = dev
iface = false
} else {
err := fmt.Errorf("unknown device %s", dev)
minerLogger.Error().Msg(err.Error())
return err
}
}
minerLogger.Info().Msgf(`Set device to "%s"`, dev)
return nil
}