This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mikrotik.go
140 lines (111 loc) · 3.82 KB
/
mikrotik.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
package main
import (
"fmt"
"strings"
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/rs/zerolog/log"
"github.com/go-routeros/routeros"
)
func dial() (*routeros.Client, error) {
if useTLS {
return routeros.DialTLS(mikrotikHost, username, password, nil)
}
return routeros.Dial(mikrotikHost, username, password)
}
func (mal *mikrotikAddrList) initMikrotik() {
log.Info().Msg("Connecting to mikrotik")
c, err := dial()
if err != nil {
log.Fatal().Err(err).Str("host", mikrotikHost).Str("username", username).Bool("useTLS", useTLS).Msg("Connection failed")
}
if async {
c.Async()
}
mal.c = c
mal.cache = make(map[string]string)
protos := []string{"ip", "ipv6"}
for _, proto := range protos {
log.Info().Msgf("mikrotik %s list addr", proto)
initCmd := fmt.Sprintf("/%s/firewall/address-list/print ?list=crowdsec =.proplist=.id,address", proto)
r, err := c.RunArgs(strings.Split(initCmd, " "))
if err != nil {
log.Fatal().Err(err).Msg("address-list print failed")
}
log.Info().Msgf("fill %d entry in internal addrList\n", len(r.Re))
for _, v := range r.Re {
mal.cache[v.Map["address"]] = v.Map[".id"]
}
}
}
func (mal *mikrotikAddrList) add(decision *models.Decision) {
log.Info().Msgf("new decisions from %s: IP: %s | Scenario: %s | Duration: %s | Scope : %v", *decision.Origin, *decision.Value, *decision.Scenario, *decision.Duration, *decision.Scope)
var proto string
if strings.Contains(*decision.Value, ":") {
proto = "ipv6"
} else {
proto = "ip"
}
var address string
if *decision.Scope == "Ip" && proto == "ipv6" {
address = fmt.Sprintf("%s/128", *decision.Value)
} else {
address = *decision.Value
}
addCmd := fmt.Sprintf("/%s/firewall/address-list/add#=list=crowdsec#=address=%s#=comment=%s#=timeout=%s", proto, address, *decision.Scenario, *decision.Duration)
if mal.cache[address] != "" {
log.Info().Msgf("Address %s already present", address)
} else {
r, err := mal.c.RunArgs(strings.Split(addCmd, "#"))
log.Info().Msgf("resp %s", r)
if err != nil {
log.Error().Err(err).Msgf("%s address-list add cmd failed", proto)
} else {
mal.cache[address] = r.Done.List[0].Value
log.Info().Msgf("Address %s blocked in mikrotik", address)
}
}
}
func (mal *mikrotikAddrList) remove(decision *models.Decision) {
log.Info().Msgf("removed decisions: IP: %s | Scenario: %s | Duration: %s | Scope : %v", *decision.Value, *decision.Scenario, *decision.Duration, *decision.Scope)
var proto string
if strings.Contains(*decision.Value, ":") {
proto = "ipv6"
} else {
proto = "ip"
}
var address string
if *decision.Scope == "Ip" && proto == "ipv6" {
address = fmt.Sprintf("%s/128", *decision.Value)
} else {
address = *decision.Value
}
if mal.cache[address] != "" {
log.Info().Msgf("Verify address %s in mikrotik", address)
checkCmd := fmt.Sprintf("/%s/firewall/address-list/print =.proplist=address ?.id=%s", proto, mal.cache[address])
r, err := mal.c.RunArgs(strings.Split(checkCmd, " "))
if err != nil {
log.Fatal().Err(err).Msgf("%s address-list search cmd failed", proto)
}
if len(r.Re) == 1 && r.Re[0].Map["address"] == address {
delCmd := fmt.Sprintf("/%s/firewall/address-list/remove =numbers=%s", proto, mal.cache[address])
_, err = mal.c.RunArgs(strings.Split(delCmd, " "))
if err != nil {
log.Error().Err(err).Msgf("%s address-list remove cmd failed", proto)
}
log.Info().Msgf("%s removed from mikrotik", address)
} else {
log.Info().Msgf("%s already removed from mikrotik", address)
}
delete(mal.cache, address)
} else {
log.Info().Msgf("%s not find in local cache", address)
}
}
func (mal *mikrotikAddrList) decisionProcess(streamDecision *models.DecisionsStreamResponse) {
for _, decision := range streamDecision.Deleted {
mal.remove(decision)
}
for _, decision := range streamDecision.New {
mal.add(decision)
}
}