-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathdiscovery.go
More file actions
205 lines (178 loc) · 5.11 KB
/
Copy pathdiscovery.go
File metadata and controls
205 lines (178 loc) · 5.11 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cluster
import (
"context"
"net"
"runtime"
"github.com/grandcat/zeroconf"
"github.com/oleksandr/bonjour"
)
type ServiceRecord struct {
Instance string `json:"name"`
Service string `json:"type"`
Domain string `json:"domain"`
}
type ServiceEntry struct {
ServiceRecord
HostName string `json:"hostname"`
Port int `json:"port"`
Text []string `json:"text"`
TTL uint32 `json:"ttl"`
AddrIPv4 []net.IP `json:"-"`
AddrIPv6 []net.IP `json:"-"`
}
type Resolver interface {
Browse(ctx context.Context, service string, domain string, entries chan<- *ServiceEntry) error
Lookup(ctx context.Context, instance string, service string, domain string, entries chan<- *ServiceEntry) error
}
const (
darwin = "darwin"
)
func NewResolver(ifs ...net.Interface) (Resolver, error) {
switch runtime.GOOS {
case darwin:
var iface *net.Interface
if len(ifs) > 0 {
iface = &ifs[0]
}
r, err := bonjour.NewResolver(iface)
if err != nil {
return nil, err
}
return &BonjourResolver{
Resolver: r,
}, nil
default:
var opt []zeroconf.ClientOption
if len(ifs) > 0 {
opt = append(opt, zeroconf.SelectIfaces(ifs))
}
r, err := zeroconf.NewResolver(opt...)
if err != nil {
return nil, err
}
return &ZeroconfResolver{
Resolver: r,
}, nil
}
}
type ZeroconfResolver struct {
*zeroconf.Resolver
}
func (r *ZeroconfResolver) Browse(ctx context.Context, service string, domain string, entries chan<- *ServiceEntry) error {
ch := make(chan *zeroconf.ServiceEntry, cap(entries))
go func(zeroconfEntries <-chan *zeroconf.ServiceEntry) {
defer close(entries)
for e := range zeroconfEntries {
entries <- &ServiceEntry{
ServiceRecord: ServiceRecord{
Instance: e.Instance,
Service: e.Service,
Domain: e.Domain,
},
HostName: e.HostName,
Port: e.Port,
Text: e.Text,
TTL: e.TTL,
AddrIPv4: e.AddrIPv4,
AddrIPv6: e.AddrIPv6,
}
}
}(ch)
return r.Resolver.Browse(ctx, service, domain, ch)
}
func (r *ZeroconfResolver) Lookup(ctx context.Context, instance string, service string, domain string, entries chan<- *ServiceEntry) error {
ch := make(chan *zeroconf.ServiceEntry, cap(entries))
go func(zeroconfEntries <-chan *zeroconf.ServiceEntry) {
defer close(entries)
for e := range zeroconfEntries {
entries <- &ServiceEntry{
ServiceRecord: ServiceRecord{
Instance: e.Instance,
Service: e.Service,
Domain: e.Domain,
},
HostName: e.HostName,
Port: e.Port,
Text: e.Text,
TTL: e.TTL,
AddrIPv4: e.AddrIPv4,
AddrIPv6: e.AddrIPv6,
}
}
}(ch)
return r.Resolver.Lookup(ctx, instance, service, domain, ch)
}
type BonjourResolver struct {
*bonjour.Resolver
}
func (r *BonjourResolver) Browse(_ context.Context, service string, domain string, entries chan<- *ServiceEntry) error {
ch := make(chan *bonjour.ServiceEntry, cap(entries))
go func(bonjourEntries chan *bonjour.ServiceEntry) {
defer close(entries)
for e := range bonjourEntries {
entries <- &ServiceEntry{
ServiceRecord: ServiceRecord{
Instance: e.Instance,
Service: e.Service,
Domain: e.Domain,
},
HostName: e.HostName,
Port: e.Port,
Text: e.Text,
TTL: e.TTL,
AddrIPv4: []net.IP{e.AddrIPv4},
AddrIPv6: []net.IP{e.AddrIPv6},
}
}
}(ch)
return r.Resolver.Browse(service, domain, ch)
}
func (r *BonjourResolver) Lookup(_ context.Context, instance string, service string, domain string, entries chan<- *ServiceEntry) error {
ch := make(chan *bonjour.ServiceEntry, cap(entries))
go func(bonjourEntries chan *bonjour.ServiceEntry) {
defer close(entries)
for e := range bonjourEntries {
entries <- &ServiceEntry{
ServiceRecord: ServiceRecord{
Instance: e.Instance,
Service: e.Service,
Domain: e.Domain,
},
HostName: e.HostName,
Port: e.Port,
Text: e.Text,
TTL: e.TTL,
AddrIPv4: []net.IP{e.AddrIPv4},
AddrIPv6: []net.IP{e.AddrIPv6},
}
}
}(ch)
return r.Resolver.Lookup(instance, service, domain, ch)
}
type Discovery interface {
RegisterProxy(instance string, service string, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface) (Shutdownable, error)
}
type ZeroconfDiscovery struct {
}
func NewDiscovery() Discovery {
switch runtime.GOOS {
case darwin:
return new(BonjourDiscovery)
default:
return new(ZeroconfDiscovery)
}
}
func (z *ZeroconfDiscovery) RegisterProxy(instance string, service string, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface) (Shutdownable, error) {
server, err := zeroconf.RegisterProxy(instance, service, domain, port, host, ips, text, ifaces)
return server, err
}
type BonjourDiscovery struct {
}
func (z *BonjourDiscovery) RegisterProxy(instance string, service string, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface) (Shutdownable, error) {
var iface *net.Interface
if len(ifaces) > 0 {
iface = &ifaces[0]
}
server, err := bonjour.RegisterProxy(instance, service, domain, port, host, append(ips, "")[0], text, iface)
return server, err
}