-
Notifications
You must be signed in to change notification settings - Fork 4
/
discover.go
148 lines (138 loc) · 3.3 KB
/
discover.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
package hue
import (
"bufio"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/textproto"
"strings"
"time"
)
// ErrNotFound is returned when no bridge was discovered.
var ErrNotFound = errors.New("no bridge was found")
// Discover returns the (first) bridge that it finds on the local network.
func Discover() (*Bridge, error) {
if b := fromCache(); b != nil {
return b, nil
}
bid, err := discover()
if err != nil {
return nil, err
}
return &Bridge{bridgeID: bid}, err
}
// bridgeID stores discovered bridges.
type bridgeID struct {
ID string `json:"id"`
IP string `json:"internalipaddress"`
}
// discover runs UPNP discovery and falls back to the remote API on failure.
func discover() (bridgeID, error) {
var (
b bridgeID
err error
)
b, err = discoverLocal()
if err != nil {
log.Println("Didn't find any bridges via UPNP, attempting remote API...")
b, err = discoverRemote()
if err != nil {
return b, ErrNotFound
}
}
return b, err
}
var (
mcastAddr = &net.UDPAddr{IP: []byte{239, 255, 255, 250}, Port: 1900}
connDeadline = 5 * time.Second
)
// discoverLocal attempts to discover any Hue bridges available via UPNP.
func discoverLocal() (bridgeID, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return bridgeID{}, err
}
defer conn.Close()
conn.WriteToUDP([]byte("M-SEARCH * HTTP/1.1\r\n"+
"HOST: 239.255.255.250:1900\r\n"+
"MAN: ssdp:discover\r\n"+
"MX: 10\r\n"+
"ST: ssdp:all\r\n"), mcastAddr)
conn.SetDeadline(time.Now().Add(connDeadline))
r := bufio.NewReader(conn)
for {
_, err := r.ReadString('\n') // HTTP/1.1 200 OK\r\n
if err != nil {
break
}
tp := textproto.NewReader(r)
h, err := tp.ReadMIMEHeader()
if err != nil {
continue
}
v, ok := h["Location"]
if !ok || len(v) == 0 {
continue
}
bid, err := tryLocation(v[0])
if err != nil {
continue
}
return bid, err
}
return bridgeID{}, ErrNotFound
}
// tryLocation queries the passed url to check if it is the description of a Hue
// bridge, in which case it returns information about it. Any other outcome will
// result in an error.
func tryLocation(url string) (bridgeID, error) {
resp, err := http.Get(url)
if err != nil {
return bridgeID{}, err
}
var body struct {
URL string `xml:"URLBase"`
Device struct {
Description string `xml:"modelDescription"`
Name string `xml:"modelName"`
ID string `xml:"serialNumber"`
} `xml:"device"`
}
err = xml.NewDecoder(resp.Body).Decode(&body)
defer resp.Body.Close()
if err != nil {
return bridgeID{}, err
}
if body.URL == "" ||
!(strings.Contains(body.Device.Description, "Philips hue") ||
strings.Contains(body.Device.Name, "Philips hue")) {
return bridgeID{}, ErrNotFound
}
return bridgeID{
ID: body.Device.ID,
IP: body.URL,
}, nil
}
var remoteAddr = "https://www.meethue.com/api/nupnp"
// discoverRemote uses the meethue.com API to discover local bridges.
func discoverRemote() (bridgeID, error) {
resp, err := http.Get(remoteAddr)
defer resp.Body.Close()
if err != nil {
return bridgeID{}, err
}
var b []bridgeID
err = json.NewDecoder(resp.Body).Decode(&b)
if err != nil {
return bridgeID{}, err
}
if len(b) == 0 {
return bridgeID{}, ErrNotFound
}
b[0].IP = fmt.Sprintf("http://%s/", b[0].IP) // sanitize
return b[0], nil
}