-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
196 lines (169 loc) · 3.97 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2024 The Funeypot Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package config
import (
"fmt"
"os"
"time"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
)
type Config struct {
Log Log `yaml:"log"`
Ssh Ssh `yaml:"ssh"`
Http Http `yaml:"http"`
Ftp Ftp `yaml:"ftp"`
Database Database `yaml:"database"`
Dashboard Dashboard `yaml:"dashboard"`
Abuseipdb Abuseipdb `yaml:"abuseipdb"`
}
type Log struct {
Level string `yaml:"level"`
}
func (l Log) Validate() error {
if l.Level == "" {
return fmt.Errorf("level is required")
}
if _, err := zapcore.ParseLevel(l.Level); err != nil {
return fmt.Errorf("invalid level %q: %w", l.Level, err)
}
return nil
}
type Ssh struct {
Address string `yaml:"address"`
Delay time.Duration `yaml:"delay"`
KeySeed string `yaml:"key_seed"`
}
func (s Ssh) Validate() error {
if s.Address == "" {
return fmt.Errorf("address is required")
}
if s.Delay < 0 {
return fmt.Errorf("delay cannot be negative")
}
return nil
}
type Http struct {
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
}
func (h Http) Validate() error {
if !h.Enabled {
return nil
}
if h.Address == "" {
return fmt.Errorf("address is required")
}
return nil
}
type Ftp struct {
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
}
func (f Ftp) Validate() error {
if !f.Enabled {
return nil
}
if f.Address == "" {
return fmt.Errorf("address is required")
}
return nil
}
type Database struct {
Driver string `yaml:"driver"`
Dsn string `yaml:"dsn"`
}
func (d Database) Validate() error {
if d.Driver == "" {
return fmt.Errorf("driver is required")
}
if d.Dsn == "" {
return fmt.Errorf("dsn is required")
}
return nil
}
type Dashboard struct {
Enabled bool `yaml:"enabled"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
func (d Dashboard) Validate() error {
if !d.Enabled {
return nil
}
if d.Username == "" {
return fmt.Errorf("username is required")
}
if len(d.Password) < 8 {
return fmt.Errorf("password is required and must be at least 8 characters")
}
return nil
}
type Abuseipdb struct {
Enabled bool `yaml:"enabled"`
Key string `yaml:"key"`
Interval time.Duration `yaml:"interval"`
}
func (a Abuseipdb) Validate() error {
if !a.Enabled {
return nil
}
if a.Key == "" {
return fmt.Errorf("key is required when enabled")
}
if a.Interval < 15*time.Minute {
return fmt.Errorf("interval must be at least 15 minutes")
}
return nil
}
func Load(file string, generate bool) (*Config, error) {
if _, err := os.Stat(file); os.IsNotExist(err) {
if !generate {
return nil, fmt.Errorf("config file %q does not exist", file)
}
if err := Generate(file); err != nil {
return nil, fmt.Errorf("generate config: %w", err)
}
}
f, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("open file %q: %w", file, err)
}
defer f.Close()
encoder := yaml.NewDecoder(f)
ret := &Config{}
if err := encoder.Decode(ret); err != nil {
return nil, fmt.Errorf("decode yaml: %w", err)
}
if err := ret.Validate(); err != nil {
return nil, fmt.Errorf("validate config: %w", err)
}
return ret, nil
}
func (c *Config) Validate() error {
if err := c.Log.Validate(); err != nil {
return fmt.Errorf("log: %w", err)
}
if err := c.Ssh.Validate(); err != nil {
return fmt.Errorf("ssh: %w", err)
}
if err := c.Http.Validate(); err != nil {
return fmt.Errorf("http: %w", err)
}
if err := c.Ftp.Validate(); err != nil {
return fmt.Errorf("ftp: %w", err)
}
if err := c.Database.Validate(); err != nil {
return fmt.Errorf("database: %w", err)
}
if err := c.Dashboard.Validate(); err != nil {
return fmt.Errorf("dashboard: %w", err)
}
if err := c.Abuseipdb.Validate(); err != nil {
return fmt.Errorf("abuse ipdb: %w", err)
}
if !c.Http.Enabled && c.Dashboard.Enabled {
return fmt.Errorf("http.enabled must be true when dashboard.enabled is true")
}
return nil
}