-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathaddress_group.go
109 lines (97 loc) · 2.5 KB
/
address_group.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
package usocksd
import (
"fmt"
"net"
"sync"
"time"
"github.com/cybozu-go/log"
)
const (
// invalidCheckInterval is the interval between checking DNSBL for
// black-listed IP addresses.
invalidCheckInterval = 15 * time.Second
)
// AddressGroup is a group of external IP addresses to be used
// for outgoing connections. With the help of associated goroutines,
// IP addresses listed on DNSBL will be checked and excluded.
type AddressGroup struct {
addresses []net.IP // immutable
dnsblDomain string
lock *sync.Mutex
valids []net.IP
invalids []net.IP
}
func makeDNSBLDomain(domain string, ip net.IP) string {
if len(domain) == 0 {
return ""
}
ip = ip.To4()
if ip == nil {
return ""
}
return fmt.Sprintf("%d.%d.%d.%d.%s", ip[3], ip[2], ip[1], ip[0], domain)
}
// isBadIP returns true if IP is registered on DNSBL.
func (a *AddressGroup) isBadIP(ip net.IP) bool {
d := makeDNSBLDomain(a.dnsblDomain, ip)
if len(d) == 0 {
return false
}
_, err := net.LookupIP(d)
return err == nil
}
func toStringList(ips []net.IP) []string {
sips := make([]string, 0, len(ips))
for _, ip := range ips {
sips = append(sips, ip.String())
}
return sips
}
// detectInvalid is a non-returning method, thus should be
// called as a goroutine, to detect black-listed IP addresses.
func (a *AddressGroup) detectInvalid() {
for {
var valids, invalids []net.IP
for _, ip := range a.addresses {
if a.isBadIP(ip) {
invalids = append(invalids, ip)
} else {
valids = append(valids, ip)
}
}
a.lock.Lock()
if len(invalids) > 0 && len(a.invalids) != len(invalids) {
_ = log.Warn("detect black-listed IP", map[string]interface{}{
"_bad_ips": toStringList(invalids),
})
}
if len(valids) < len(invalids) {
// Too few valid IPs
valids = a.addresses
}
a.valids = valids
a.invalids = invalids
a.lock.Unlock()
time.Sleep(invalidCheckInterval)
}
}
// PickAddress returns a local IP address for outgoing connection.
// hint should be an integer calculated from client and/or target IP addresses.
func (a *AddressGroup) PickAddress(hint uint32) net.IP {
a.lock.Lock()
defer a.lock.Unlock()
return a.valids[int(hint)%len(a.valids)]
}
// NewAddressGroup initializes a new AddressGroup and starts
// helper goroutines.
func NewAddressGroup(addresses []net.IP, dnsblDomain string) *AddressGroup {
a := &AddressGroup{
addresses: addresses,
dnsblDomain: dnsblDomain,
lock: new(sync.Mutex),
valids: addresses,
invalids: nil,
}
go a.detectInvalid()
return a
}