-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (95 loc) · 2.32 KB
/
main.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
package main
import (
"fmt"
"github.com/akamensky/argparse"
"github.com/hpcloud/tail"
"log"
"os"
"regexp"
iptools "softether-bot-blocker/utils"
"time"
)
var ip string
var count int
var blockedIPS []iptools.BlockedIP
var regEXP = `(?P<time>^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d).+\((?P<ipaddr>[0-9]+(?:\.[0-9]+){3}).+channel is created`
var timeFormat = "2006-01-02 15:04:05"
var filename string
func main() {
parser := argparse.NewParser("print", "Binare that runs after TLS release")
// Create string flag
configPath := parser.String(
"c",
"config",
&argparse.Options{
Required: true,
Help: "command",
Default: "soft_reset",
})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
cnf := iptools.Config(*configPath)
maxCNT := cnf.MaxCount
bullShitBingo := make([]iptools.BlockedIP, maxCNT)
l, err := os.OpenFile("/var/log/blocked.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
log.SetOutput(l)
filenameChan := make(chan string)
for {
filename = fmt.Sprintf("%v/%v", cnf.LogPath, iptools.LogNameFormat(time.Now()))
t, err := tail.TailFile(filename, tail.Config{Follow: true, ReOpen: true, MustExist: true})
if err != nil {
log.Println("No log file", filename, ". Waiting for 10 minute")
time.Sleep(time.Minute * 10)
continue
}
go iptools.CheckFileName(filename, cnf, filenameChan)
go func() {
for {
select {
case name := <-filenameChan:
log.Println("Received a new name for log:", name)
t.Stop()
return
}
}
}()
if err != nil {
log.Fatalln(err)
}
for line := range t.Lines {
re := regexp.MustCompile(regEXP)
match := re.FindStringSubmatch(line.Text)
if len(match) > 2 {
ip = match[2]
timeString := match[1]
timeSeen, err := time.Parse(timeFormat, timeString)
if err != nil {
log.Fatalln(err)
}
if iptools.CountIP(maxCNT-1, ip, &bullShitBingo, cnf.Interval) {
if !iptools.CheckInBlockList(ip, blockedIPS) {
go iptools.BlockIP(ip)
blockedIPS = append(blockedIPS, iptools.BlockedIP{
IpAddress: ip,
LastSeen: timeSeen,
})
}
}
bullShitBingo[count] = iptools.BlockedIP{
IpAddress: ip,
LastSeen: timeSeen,
}
count++
if count >= len(bullShitBingo)-1 {
count = 0
}
}
}
}
}