-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
62 lines (55 loc) · 1.52 KB
/
config.go
File metadata and controls
62 lines (55 loc) · 1.52 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
package main
import (
"encoding/json"
"fmt"
"os"
)
type S3Config struct {
Endpoint string `json:"endpoint"`
Region string `json:"region"`
Bucket string `json:"bucket"`
Prefix string `json:"prefix"`
AccessKeyID string `json:"access_key_id"`
SecretAccessKey string `json:"secret_access_key"`
}
type Config struct {
Interfaces []string `json:"interfaces"`
CaptureDir string `json:"capture_dir"`
MaxFileSizeMB int `json:"max_file_size_mb"`
BPFFilter string `json:"bpf_filter"`
SnapLen int `json:"snap_len"`
S3 S3Config `json:"s3"`
DeleteAfterUpload bool `json:"delete_after_upload"`
}
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config file: %w", err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse config: %w", err)
}
if len(cfg.Interfaces) == 0 {
return nil, fmt.Errorf("no interfaces configured")
}
if cfg.CaptureDir == "" {
cfg.CaptureDir = "/pcaps"
}
if cfg.MaxFileSizeMB <= 0 {
cfg.MaxFileSizeMB = 1024
}
if cfg.S3.Endpoint == "" {
return nil, fmt.Errorf("s3.endpoint is required")
}
if cfg.S3.Bucket == "" {
return nil, fmt.Errorf("s3.bucket is required")
}
if cfg.S3.AccessKeyID == "" || cfg.S3.SecretAccessKey == "" {
return nil, fmt.Errorf("s3 credentials are required")
}
if cfg.S3.Region == "" {
cfg.S3.Region = "us-east-1"
}
return &cfg, nil
}