-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
201 lines (170 loc) · 4.09 KB
/
service.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
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
// Package discovery provides a discovery service for chromecast devices
package discovery
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/micro/mdns"
"github.com/sirupsen/logrus"
"github.com/stampzilla/gocast"
)
type Service struct {
found chan *gocast.Device
entriesCh chan *mdns.ServiceEntry
foundDevices map[string]*gocast.Device
periodicRunning uint32
stop context.CancelFunc
}
func NewService() *Service {
return &Service{
found: make(chan *gocast.Device),
entriesCh: make(chan *mdns.ServiceEntry),
foundDevices: make(map[string]*gocast.Device),
}
}
func (d *Service) periodic(ctx context.Context, interval time.Duration) error {
if i := atomic.LoadUint32(&d.periodicRunning); i != 0 {
return fmt.Errorf("Periodic discovery is already running")
}
c, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
err := mdns.Query(&mdns.QueryParam{
Service: "_googlecast._tcp",
Domain: "local",
Timeout: time.Second * 1,
Entries: d.entriesCh,
Context: c,
})
if err != nil {
logrus.Error("error doing mdns query: ", err)
}
ticker := time.NewTicker(interval)
atomic.AddUint32(&d.periodicRunning, 1)
go func() {
for {
err := mdns.Query(&mdns.QueryParam{
Service: "_googlecast._tcp",
Domain: "local",
Timeout: time.Second * 1,
Entries: d.entriesCh,
Context: c,
})
if err != nil {
logrus.Error("error doing mdns query: ", err)
}
select {
case <-ticker.C:
case <-ctx.Done():
ticker.Stop()
logrus.Debug("stopping periodic goroutine")
return
}
}
}()
return nil
}
func (d *Service) Start(pCtx context.Context, interval time.Duration) {
ctx, cancel := context.WithCancel(pCtx)
d.stop = cancel
go d.listner(ctx)
err := d.periodic(ctx, interval)
if err != nil {
logrus.Error("error starting periodic mdns query: ", err)
}
}
func (d *Service) Stop() {
if d.stop != nil {
d.stop()
}
}
func (d *Service) Found() chan *gocast.Device {
return d.found
}
func (d *Service) listner(ctx context.Context) {
for {
select {
case <-ctx.Done():
logrus.Debug("stopping listner goroutine")
d.foundDevices = make(map[string]*gocast.Device)
return
case entry := <-d.entriesCh:
// fmt.Printf("Got new entry: %#v\n", entry)
name := strings.Split(entry.Name, "._googlecast")
// Skip everything that dont have googlecast in the fdqn
if len(name) < 2 {
continue
}
info := decodeTxtRecord(entry.Info)
key := info["id"] // Use device ID as key, allowes the device to change IP
if dev, ok := d.foundDevices[key]; ok {
changed := false
if !entry.AddrV4.Equal(dev.Ip()) {
dev.SetIp(entry.AddrV4)
changed = true
}
if entry.Port != dev.Port() {
dev.SetPort(entry.Port)
changed = true
}
if changed {
dev.SetLogger(logrus.WithFields(logrus.Fields{
"ip": entry.AddrV4.String(),
"port": entry.Port,
"name": info["fn"],
}))
}
continue
}
device := gocast.NewDevice(logrus.WithFields(logrus.Fields{
"ip": entry.AddrV4.String(),
"port": entry.Port,
"name": info["fn"],
}))
device.SetIp(entry.AddrV4)
device.SetPort(entry.Port)
device.SetUuid(key)
device.SetName(info["fn"])
d.foundDevices[key] = device
delay := time.NewTimer(time.Second)
select {
case d.found <- device:
if !delay.Stop() {
<-delay.C
}
case <-delay.C:
}
}
}
}
func decodeDnsEntry(text string) string {
text = strings.ReplaceAll(text, `\.`, ".")
text = strings.ReplaceAll(text, `\ `, " ")
re := regexp.MustCompile(`([\\][0-9][0-9][0-9])`)
text = re.ReplaceAllStringFunc(text, func(source string) string {
i, err := strconv.Atoi(source[1:])
if err != nil {
return ""
}
return string([]byte{byte(i)})
})
return text
}
func decodeTxtRecord(txt string) map[string]string {
m := make(map[string]string)
s := strings.Split(txt, "|")
for _, v := range s {
s := strings.Split(v, "=")
if len(s) == 2 {
if s[0] == "fn" { // Friendly name
m[s[0]] = decodeDnsEntry(s[1])
} else {
m[s[0]] = s[1]
}
}
}
return m
}