forked from heyvito/eswim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
109 lines (94 loc) · 2.94 KB
/
server.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 eswim
import (
"fmt"
"github.com/heyvito/eswim/internal/core"
"github.com/heyvito/eswim/internal/iputil"
"go.uber.org/zap"
"net/netip"
)
type Server interface {
Start()
Shutdown()
GetKnownNodes() []netip.Addr
}
type server struct {
opts *Options
v4Server, v6Server *swimServer
}
func NewServer(opts *Options) (Server, error) {
var err error
if err = opts.normalize(); err != nil {
return nil, err
}
logger := opts.LogHandler.With(zap.String("facility", "server"))
addrs := opts.HostAddresses
if addrs.IPv6Address == nil {
logger.Warn("IPv6 is set to autoconfigure, but no usable address could be found. Disabling.")
addrs.IPv6Address = IgnoreFamilyAddr
} else if addrs.IPv6Address == nil && opts.IPv6MulticastAddress != "" {
logger.Warn("IPv6 is set to autoconfigure, but no usable address could be found. Disabling.")
addrs.IPv6Address = IgnoreFamilyAddr
} else if addrs.IPv6Address != nil && opts.IPv6MulticastAddress == "" {
logger.Warn("IPv6 is either set to autoconfigure or has been provided manually, but IPv6MulticastAddress is empty. Disabling.")
addrs.IPv6Address = IgnoreFamilyAddr
}
if addrs.IPv4Address == nil {
logger.Warn("IPv4 is set to autoconfigure, but no usable address could be found. Disabling.")
addrs.IPv4Address = IgnoreFamilyAddr
} else if addrs.IPv4Address == nil && opts.IPv4MulticastAddress != "" {
logger.Warn("IPv4 is set to autoconfigure, but no usable address could be found. Disabling.")
addrs.IPv4Address = IgnoreFamilyAddr
} else if addrs.IPv4Address != nil && opts.IPv4MulticastAddress == "" {
logger.Warn("IPv4 is either set to autoconfigure or has been provided manually, but IPv4MulticastAddress is empty. Disabling.")
addrs.IPv4Address = IgnoreFamilyAddr
}
allIPs, err := iputil.LocalAddresses()
if err != nil {
return nil, err
}
var v4Server, v6Server *swimServer
if addrs.IPv4Address != IgnoreFamilyAddr {
v4Server, err = newSwimServer(*addrs.IPv4Address, allIPs, opts)
}
if err != nil {
return nil, fmt.Errorf("failed initializing IPv4 server: %w", err)
}
if addrs.IPv6Address != IgnoreFamilyAddr {
v6Server, err = newSwimServer(*addrs.IPv6Address, allIPs, opts)
}
if err != nil {
return nil, fmt.Errorf("failed initializing IPv6 server: %w", err)
}
return &server{opts, v4Server, v6Server}, nil
}
func (s *server) Start() {
if s.v4Server != nil {
s.v4Server.Start()
}
if s.v6Server != nil {
s.v6Server.Start()
}
}
func (s *server) Shutdown() {
if s.v4Server != nil {
s.v4Server.Shutdown()
}
if s.v6Server != nil {
s.v6Server.Shutdown()
}
}
func (s *server) GetKnownNodes() []netip.Addr {
var list []netip.Addr
filter := core.NodeTypeStable | core.NodeTypeStable
if s.v4Server != nil {
for _, v := range s.v4Server.nodes.All(filter) {
list = append(list, v.Address.IntoManaged())
}
}
if s.v6Server != nil {
for _, v := range s.v6Server.nodes.All(filter) {
list = append(list, v.Address.IntoManaged())
}
}
return list
}