-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.go
96 lines (82 loc) · 1.94 KB
/
config.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
package sgfw
import (
"bufio"
"io/ioutil"
"os"
"path"
"github.com/naoina/toml"
"github.com/op/go-logging"
)
const (
configDefaultPath string = "/etc/sgfw/sgfw.conf"
)
type FirewallConfigs struct {
LogLevel string
LoggingLevel logging.Level `toml:"-"`
LogRedact bool
PromptExpanded bool
PromptExpert bool
DefaultAction string
DefaultActionID FilterScope `toml:"-"`
}
var FirewallConfig FirewallConfigs
func _readConfig(file string) []byte {
f, err := os.Open(configDefaultPath)
if err != nil {
log.Warning(err.Error())
return []byte{}
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
log.Warning(err.Error())
return []byte{}
}
return buf
}
func readConfig() {
buf := _readConfig(configDefaultPath)
FirewallConfig = FirewallConfigs{
LogLevel: "NOTICE",
LoggingLevel: logging.NOTICE,
LogRedact: false,
PromptExpanded: false,
PromptExpert: false,
DefaultAction: "SESSION",
DefaultActionID: 1,
}
if len(buf) > 0 {
if err := toml.Unmarshal(buf, &FirewallConfig); err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
FirewallConfig.LoggingLevel, _ = logging.LogLevel(FirewallConfig.LogLevel)
FirewallConfig.DefaultActionID = GetFilterScopeValue(FirewallConfig.DefaultAction)
}
func writeConfig() {
FirewallConfig.LogLevel = FirewallConfig.LoggingLevel.String()
FirewallConfig.DefaultAction = GetFilterScopeString(FirewallConfig.DefaultActionID)
if _, err := os.Stat(path.Dir(configDefaultPath)); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configDefaultPath), 0755); err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
}
f, err := os.Create(configDefaultPath)
if err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
defer f.Close()
w := bufio.NewWriter(f)
cw := toml.NewEncoder(w)
if err := cw.Encode(FirewallConfig); err != nil {
log.Error(err.Error())
//os.Exit(1)
return
}
w.Flush()
}