forked from goodrain/rainbond
/
ippool.go
175 lines (157 loc) · 3.94 KB
/
ippool.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2019 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package util
import (
"context"
"net"
"sync"
"time"
"github.com/Sirupsen/logrus"
)
//IPEVENTTYPE ip change event type
type IPEVENTTYPE int
//IPEVENT ip change event
type IPEVENT struct {
Type IPEVENTTYPE
IP net.IP
}
//IPPool ip pool
type IPPool struct {
ctx context.Context
cancel context.CancelFunc
lock sync.Mutex
HostIPs map[string]net.IP
EventCh chan IPEVENT
StopCh chan struct{}
ignoreInterfaceName []string
startReady bool
}
const (
//ADD add event
ADD IPEVENTTYPE = iota
//DEL del event
DEL
//UPDATE update event
UPDATE
)
//NewIPPool new ip pool
func NewIPPool(ignoreInterfaceName []string) *IPPool {
ctx, cancel := context.WithCancel(context.Background())
ippool := &IPPool{
ctx: ctx,
cancel: cancel,
HostIPs: map[string]net.IP{},
EventCh: make(chan IPEVENT, 20),
StopCh: make(chan struct{}),
ignoreInterfaceName: ignoreInterfaceName,
startReady: false,
}
go ippool.LoopCheckIPs()
return ippool
}
//Ready ready
func (i *IPPool) Ready() bool {
for !i.startReady {
time.Sleep(time.Second * 1)
}
return i.startReady
}
//GetHostIPs get host ips
func (i *IPPool) GetHostIPs() []net.IP {
i.lock.Lock()
defer i.lock.Unlock()
var ips []net.IP
for _, ip := range i.HostIPs {
ips = append(ips, ip)
}
return ips
}
//GetWatchIPChan watch ip change
func (i *IPPool) GetWatchIPChan() <-chan IPEVENT {
return i.EventCh
}
//Close close
func (i *IPPool) Close() {
i.cancel()
}
//LoopCheckIPs loop check ips
func (i *IPPool) LoopCheckIPs() {
Exec(i.ctx, func() error {
ips, err := i.getInterfaceIPs()
if err != nil {
logrus.Errorf("get ip address from interface failure %s, will retry", err.Error())
return nil
}
i.lock.Lock()
defer i.lock.Unlock()
var newIP = make(map[string]net.IP, 2)
for _, v := range ips {
if v.To4() == nil {
continue
}
_, ok := i.HostIPs[v.To4().String()]
if ok {
i.EventCh <- IPEVENT{Type: UPDATE, IP: v.To4()}
}
if !ok {
i.EventCh <- IPEVENT{Type: ADD, IP: v.To4()}
}
newIP[v.To4().String()] = v.To4()
}
for k, v := range i.HostIPs {
if _, ok := newIP[k]; !ok {
i.EventCh <- IPEVENT{Type: DEL, IP: v.To4()}
}
}
i.HostIPs = newIP
i.startReady = true
return nil
}, time.Second*5)
}
func (i *IPPool) getInterfaceIPs() ([]net.IP, error) {
var ips []net.IP
tables, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, table := range tables {
if StringArrayContains(i.ignoreInterfaceName, table.Name) {
logrus.Debugf("ip address from interface %s ignore", table.Name)
continue
}
addrs, err := table.Addrs()
if err != nil {
return nil, err
}
for _, address := range addrs {
if ipnet := checkIPAddress(address); ipnet != nil {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.To4())
}
}
}
}
return ips, nil
}
func checkIPAddress(addr net.Addr) *net.IPNet {
ipnet, ok := addr.(*net.IPNet)
if !ok {
return nil
}
if ipnet.IP.IsLoopback() {
return nil
}
return ipnet
}