-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebind.go
88 lines (68 loc) · 1.36 KB
/
rebind.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
package fakedns
import (
"fmt"
"net"
"strings"
"sync"
)
type Rebind struct {
rebindV4 net.IP
rebindV6 net.IP
threshold int
counter map[string]int
mu sync.RWMutex
}
func NewRebind(rebindV4, rebindV6 string, threshold int) (*Rebind, error) {
var ipv4, ipv6 net.IP
if rebindV4 != "" {
ipv4 = net.ParseIP(rebindV4)
if strings.Contains(rebindV4, ":") || ipv4 == nil {
return nil, fmt.Errorf("invalid IPV4 address: %s", rebindV4)
}
}
if rebindV6 != "" {
ipv6 = net.ParseIP(rebindV6)
if strings.Contains(rebindV6, ".") || ipv6 == nil {
return nil, fmt.Errorf("invalid IPV6 address: %s", rebindV6)
}
}
return &Rebind{
rebindV4: ipv4,
rebindV6: ipv6,
threshold: threshold,
counter: make(map[string]int),
}, nil
}
func (r *Rebind) Inc(domain string) {
r.mu.Lock()
defer r.mu.Unlock()
r.counter[domain]++
}
func (r *Rebind) IsV4Activ(domain string) bool {
r.mu.Lock()
defer r.mu.Unlock()
if r.rebindV4 == nil {
return false
}
if c, ok := r.counter[domain]; ok {
return c > r.threshold
}
return false
}
func (r *Rebind) IsV6Activ(domain string) bool {
r.mu.Lock()
defer r.mu.Unlock()
if r.rebindV6 == nil {
return false
}
if c, ok := r.counter[domain]; ok {
return c > r.threshold
}
return false
}
func (r *Rebind) IPV4() net.IP {
return r.rebindV4
}
func (r *Rebind) IPV6() net.IP {
return r.rebindV6
}