/
firewall.go
57 lines (45 loc) · 845 Bytes
/
firewall.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
package firewall
import (
"net"
ttlmap "github.com/leprosus/golang-ttl-map"
)
var ttlMap ttlmap.Heap
func init() {
ttlMap = ttlmap.New("firewall.tsv")
}
func SetAllow(ip string, ttl int64) {
Set(ip, "allow", ttl)
}
func DelAllow(ip string) {
Del(ip, "allow")
}
func SetDeny(ip string, ttl int64) {
Set(ip, "deny", ttl)
}
func DelDeny(ip string) {
Del(ip, "deny")
}
func Set(ip, value string, ttl int64) {
ttlMap.Set(ip, value, ttl)
}
func Del(ip, match string) {
value := ttlMap.Get(ip)
if value == match {
ttlMap.Del(ip)
}
}
func Allows(ip string) bool {
value := ttlMap.Get(ip)
if value == "deny" {
return false
}
return true
}
func IsAllowClient(client net.Conn) bool {
ip, _, _ := net.SplitHostPort(client.RemoteAddr().String())
value := ttlMap.Get(ip)
if value == "deny" {
return false
}
return true
}