-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
81 lines (67 loc) · 1.58 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const VERSION = `0.4`
var (
ErrorLog = log.New(os.Stderr, `error#`, log.Lshortfile)
DebugLog = log.New(os.Stdout, `debug#`, log.Lshortfile)
)
func helpText() {
fmt.Println(`bla-bla-bla`)
flag.PrintDefaults()
}
func main() {
help := flag.Bool("h", false, "print this help")
ver := flag.Bool("v", false, "Show version")
listen := flag.String("l", "127.0.0.1:8080", "Listen HTTP on address")
ipsetName := flag.String("ipset-name", "asterisk_ban", "List name of ipset")
flag.Parse()
if *help {
helpText()
os.Exit(0)
}
if *ver {
fmt.Println(VERSION)
os.Exit(0)
}
DebugLog.Printf("Started version %s", VERSION)
firewall := NewFirewall(*ipsetName)
if !firewall.IsIpsetListExist() {
if err := firewall.AddIpsetList(); err != nil {
os.Exit(1)
}
}
if !firewall.IsIptablesRuleExist() {
if err := firewall.AddIptablesRule(); err != nil {
os.Exit(1)
}
}
cmd := exec.Command(`journalctl`, `-u`, `asterisk`, `-f`, `-n`, `0`)
log, err := cmd.StdoutPipe()
if err != nil {
ErrorLog.Fatalln(err)
}
if err := cmd.Start(); err != nil {
ErrorLog.Fatalln(err)
}
metrics, err := NewMetrics(firewall.IpsetEntries)
if err != nil {
os.Exit(1)
}
http.Handle("/metrics", promhttp.Handler())
core := NewCore(firewall, metrics)
go core.UnbanTemporaryBannedRoutine(time.Minute, time.Hour)
go core.ReadLog(log)
server := http.Server{ Addr: *listen }
if err := server.ListenAndServe(); err != nil {
ErrorLog.Fatalln(err.Error())
}
}