forked from crabkun/switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
159 lines (139 loc) · 3.47 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
"time"
)
type configStructure struct {
LogLevel string `json:"log_level"`
Rules []*ruleStructure `json:"rules"`
}
type ruleStructure struct {
Name string `json:"name"`
Listen string `json:"listen"`
EnableRegexp bool `json:"enable_regexp"`
Targets []*struct {
Regexp string `json:"regexp"`
regexp *regexp.Regexp `json:"-"`
Address string `json:"address"`
} `json:"targets"`
FirstPacketTimeout uint64 `json:"first_packet_timeout"`
BlacklistFile string `json:"blacklist_file"`
blacklistMap map[string]bool `json:"-"`
}
var config *configStructure
var blacklistMutex = &sync.Mutex{}
func init() {
cfgPath := flag.String("config", "config.json", "config.json file path")
flag.Parse()
buf, err := ioutil.ReadFile(*cfgPath)
if err != nil {
logrus.Fatalf("failed to load config json: %s", err.Error())
}
if err := json.Unmarshal(buf, &config); err != nil {
logrus.Fatalf("failed to load config json: %s", err.Error())
}
if len(config.Rules) == 0 {
logrus.Fatalf("empty rule")
}
lvl, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
logrus.Fatalf("invalid log_level")
}
logrus.SetLevel(lvl)
for i, v := range config.Rules {
if err := v.verify(); err != nil {
logrus.Fatalf("verity rule failed at pos %d : %s", i, err.Error())
}
}
}
func (c *ruleStructure) verify() error {
if c.Name == "" {
return fmt.Errorf("empty name")
}
if c.Listen == "" {
return fmt.Errorf("invalid listen address")
}
if len(c.Targets) == 0 {
return fmt.Errorf("invalid targets")
}
if c.EnableRegexp {
if c.FirstPacketTimeout == 0 {
c.FirstPacketTimeout = 5000
}
}
for i, v := range c.Targets {
if v.Address == "" {
return fmt.Errorf("invalid address at pos %d", i)
}
if c.EnableRegexp {
r, err := regexp.Compile(v.Regexp)
if err != nil {
return fmt.Errorf("invalid regexp at pos %d : %s", i, err.Error())
}
v.regexp = r
}
}
if c.BlacklistFile != "" {
err := loadBlacklist(c.BlacklistFile, &c.blacklistMap)
if err != nil {
return fmt.Errorf("failed to load blacklist: %s", err.Error())
}
go watchBlacklist(c.BlacklistFile, &c.blacklistMap)
}
return nil
}
func loadBlacklist(path string, blacklist *map[string]bool) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
newBlacklist := make(map[string]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ip := strings.TrimSpace(scanner.Text())
newBlacklist[ip] = true
}
if err := scanner.Err(); err != nil {
return err
}
blacklistMutex.Lock()
oldBlacklist := *blacklist
*blacklist = newBlacklist
blacklistMutex.Unlock()
// 打印出被移除的IP地址
for ip := range oldBlacklist {
if !newBlacklist[ip] {
logrus.Infof("At %s, IP %s move out the Blacklist", time.Now().Format(time.RFC3339), ip)
}
}
// 打印出新添加的IP地址
for ip := range newBlacklist {
if !oldBlacklist[ip] {
logrus.Infof("At %s, IP %s add to the Blacklist", time.Now().Format(time.RFC3339), ip)
}
}
return nil
}
func watchBlacklist(path string, blacklist *map[string]bool) {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := loadBlacklist(path, blacklist)
if err != nil {
logrus.Errorf("failed to reload blacklist: %s", err.Error())
}
}
}
}