mirror of
https://github.com/slackhq/nebula.git
synced 2024-11-10 17:24:37 +08:00
e1af37e46d
* add calculated_remotes This setting allows us to "guess" what the remote might be for a host while we wait for the lighthouse response. For networks that hard designed with in mind, it can help speed up handshake performance, as well as improve resiliency in the case that all lighthouses are down. Example: lighthouse: # ... calculated_remotes: # For any Nebula IPs in 10.0.10.0/24, this will apply the mask and add # the calculated IP as an initial remote (while we wait for the response # from the lighthouse). Both CIDRs must have the same mask size. # For example, Nebula IP 10.0.10.123 will have a calculated remote of # 192.168.1.123 10.0.10.0/24: - mask: 192.168.1.0/24 port: 4242 * figure out what is up with this test * add test * better logic for sending handshakes Keep track of the last light of hosts we sent handshakes to. Only log handshake sent messages if the list has changed. Remove the test Test_NewHandshakeManagerTrigger because it is faulty and makes no sense. It relys on the fact that no handshake packets actually get sent, but with these changes we would send packets now (which it should!) * use atomic.Pointer * cleanup to make it clearer * fix typo in example
100 lines
1.5 KiB
Go
100 lines
1.5 KiB
Go
package udp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
)
|
|
|
|
type m map[string]interface{}
|
|
|
|
type Addr struct {
|
|
IP net.IP
|
|
Port uint16
|
|
}
|
|
|
|
func NewAddr(ip net.IP, port uint16) *Addr {
|
|
addr := Addr{IP: make([]byte, net.IPv6len), Port: port}
|
|
copy(addr.IP, ip.To16())
|
|
return &addr
|
|
}
|
|
|
|
func NewAddrFromString(s string) *Addr {
|
|
ip, port, err := ParseIPAndPort(s)
|
|
//TODO: handle err
|
|
_ = err
|
|
return &Addr{IP: ip.To16(), Port: port}
|
|
}
|
|
|
|
func (ua *Addr) Equals(t *Addr) bool {
|
|
if t == nil || ua == nil {
|
|
return t == nil && ua == nil
|
|
}
|
|
return ua.IP.Equal(t.IP) && ua.Port == t.Port
|
|
}
|
|
|
|
func (ua *Addr) String() string {
|
|
if ua == nil {
|
|
return "<nil>"
|
|
}
|
|
|
|
return net.JoinHostPort(ua.IP.String(), fmt.Sprintf("%v", ua.Port))
|
|
}
|
|
|
|
func (ua *Addr) MarshalJSON() ([]byte, error) {
|
|
if ua == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return json.Marshal(m{"ip": ua.IP, "port": ua.Port})
|
|
}
|
|
|
|
func (ua *Addr) Copy() *Addr {
|
|
if ua == nil {
|
|
return nil
|
|
}
|
|
|
|
nu := Addr{
|
|
Port: ua.Port,
|
|
IP: make(net.IP, len(ua.IP)),
|
|
}
|
|
|
|
copy(nu.IP, ua.IP)
|
|
return &nu
|
|
}
|
|
|
|
type AddrSlice []*Addr
|
|
|
|
func (a AddrSlice) Equal(b AddrSlice) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
|
|
for i := range a {
|
|
if !a[i].Equals(b[i]) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func ParseIPAndPort(s string) (net.IP, uint16, error) {
|
|
rIp, sPort, err := net.SplitHostPort(s)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
addr, err := net.ResolveIPAddr("ip", rIp)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
iPort, err := strconv.Atoi(sPort)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return addr.IP, uint16(iPort), nil
|
|
}
|