-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
71 lines (61 loc) · 1.61 KB
/
watch.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
package sshHostsDeny
import (
"github.com/caryxiao/go-zlog"
"github.com/hpcloud/tail"
"regexp"
"strings"
)
func Watch(config CmdConfig) (err error) {
err = config.validate()
if err != nil {
return
}
// whence
// 0 = 文件开始位置, 1 = 当前位置, 2 = 文件结尾处
t, err := tail.TailFile(config.SecureFile, tail.Config{
Follow: true,
Location: &tail.SeekInfo{Whence: 2},
Logger: zlog.Logger, //使用我们自己的logger
})
if err != nil {
zlog.Logger.Error(err.Error())
return
}
denyFile := config.DenyFile
hs, err := getSystemHostsDeny(denyFile)
if err != nil {
zlog.Logger.Error(err)
}
zlog.Logger.Debugf("hosts list: %+v", hs)
if err != nil {
zlog.Logger.Error(err.Error())
}
pattern := `^.*pam_unix\(sshd:auth\)\:.*rhost=([^\s]*).*$`
reg := regexp.MustCompile(pattern)
var ip string
for line := range t.Lines {
lineText := strings.TrimSpace(line.Text)
matched := reg.FindStringSubmatch(lineText)
if len(matched) > 0 {
ip = matched[1]
if !hs.FindKey(ip) {
rdhs := hs.GetRecordHost(ip)
if rdhs.Cnt < config.SshLoginFailCnt {
hs.AddRecordHost(host{HType: "sshd", Ip: ip})
zlog.Logger.Debugf("add, type: %s, ip: %s, cnt: %d", "sshd", ip, rdhs.Cnt)
} else {
zlog.Logger.Debugf("write, type: %s, ip: %s, cnt: %d", rdhs.HType, rdhs.Ip, rdhs.Cnt)
err = WriteFile(denyFile, rdhs.HType+":"+rdhs.Ip)
if err != nil {
zlog.Logger.Errorf("write error: %s", err)
}
hs.add(rdhs.host)
hs.DelRecordHost(rdhs.host)
zlog.Logger.Debugf("write %+v", hs)
}
}
zlog.Logger.Debugf("matched %+v", hs)
}
}
return
}