-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_device_finder.py
308 lines (208 loc) · 9.43 KB
/
cc_device_finder.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""
Locates Chromecast devices on the local network.
version 0.4
Parts of this are adapted from code found in PyChromecast - https://github.com/balloob/pychromecast
"""
# Copyright (C) 2014-2016 Pat Carter
#
# This file is part of Stream2chromecast.
#
# Stream2chromecast is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Stream2chromecast is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Stream2chromecast. If not, see <http://www.gnu.org/licenses/>.
import os
import socket, select
import datetime
import urlparse
import httplib, urllib
from xml.etree import ElementTree
import struct
import json
CACHE_FILE = "~/.cc_device_cache"
SSDP_ENABLED = False
MDNS_ENABLED = True
def search_network(device_limit=None, time_limit=5):
""" Search network for Chromecast devices using mDNS and SSDP """
addrs = []
if MDNS_ENABLED:
addrs += search_network_mdns(device_limit=device_limit, time_limit=time_limit)
if device_limit and len(addrs) >= device_limit:
return addrs
if SSDP_ENABLED or len(addrs) == 0:
addrs += search_network_ssdp(device_limit=device_limit, time_limit=time_limit)
return addrs
def search_network_ssdp(device_limit=None, time_limit=5):
""" SSDP discovery """
addrs = []
start_time = datetime.datetime.now()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
req = "\r\n".join(['M-SEARCH * HTTP/1.1',
'HOST: 239.255.255.250:1900',
'MAN: "ssdp:discover"',
'MX: 1',
'ST: urn:dial-multiscreen-org:service:dial:1',
'',''])
sock.sendto(req, ("239.255.255.250", 1900))
while True:
time_remaining = time_limit - (datetime.datetime.now() - start_time).seconds
if time_remaining <= 0:
break
readable = select.select([sock], [], [], time_remaining)[0]
if sock in readable:
st, addr = None, None
data = sock.recv(1024)
for line in data.split("\r\n"):
line = line.replace(" ", "")
if line.upper().startswith("LOCATION:"):
addr = urlparse.urlparse(line[9:].strip()).hostname
elif line.upper().startswith("ST:"):
st = line[3:].strip()
if addr is not None and st == "urn:dial-multiscreen-org:service:dial:1":
addrs.append(addr)
if device_limit and len(addrs) == device_limit:
break
sock.close()
return addrs
def search_network_mdns(device_limit=None, time_limit=5):
""" mDNS discovery """
addrs = []
# A rough and ready quick-hack mDNS client - this should be improved
# build query
query_format = "\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00%s\x00\x00\x0c\x00\x01"
service_type = "_googlecast._tcp.local."
query_data = ""
for query_part in service_type.split("."):
if len(query_part) > 0:
query_data += struct.pack("b", len(query_part)) + query_part
query = query_format % query_data
# setup multicast socket
m_addr, m_port = ('224.0.0.251', 5353)
intf = socket.gethostbyname(socket.gethostname())
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.bind(('', m_port))
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(m_addr) + socket.inet_aton('0.0.0.0'))
sock.settimeout(time_limit)
try:
print "Sending mDNS query"
sock.sendto(query, 0, (m_addr, m_port))
while True:
try:
data, addr = sock.recvfrom(1024)
# TODO parse the response properly, but for now this should identify chromecast responses
if query_data in data and "md=Chromecast" in data and addr[0] not in addrs:
print "chromecast found:", addr[0]
addrs.append(addr[0])
if device_limit and len(addrs) == device_limit:
print "enough devices found"
break
except socket.timeout:
break
finally:
sock.close()
return addrs
def get_device_name(ip_addr):
""" get the device friendly name for an IP address """
try:
conn = httplib.HTTPConnection(ip_addr + ":8008")
conn.request("GET", "/setup/eureka_info?options=detail")
resp = conn.getresponse()
if resp.status == 200:
status_doc = resp.read()
message = json.loads(status_doc)
return message['name']
else:
if resp.status == 404:
# eureka info not found, falling back to try SSDP description
conn = httplib.HTTPConnection(ip_addr + ":8008")
conn.request("GET", "/ssdp/device-desc.xml")
resp = conn.getresponse()
if resp.status == 200:
status_doc = resp.read()
try:
xml = ElementTree.fromstring(status_doc)
device_element = xml.find("{urn:schemas-upnp-org:device-1-0}" + "device")
return device_element.find("{urn:schemas-upnp-org:device-1-0}" + "friendlyName").text
except ElementTree.ParseError:
return ""
else:
return ""
except:
# unable to get a name - this might be for many reasons
# e.g. a non chromecast device on the network that responded to the search
return ""
def check_cache(name):
""" check the search results cache file """
result = None
filepath = os.path.expanduser(CACHE_FILE)
try:
with open(filepath, "r") as f:
for line in f.readlines():
if "\t" in line:
line_split = line.strip().split("\t", 1)
if len(line_split) > 1:
hostname, host = line_split
if name == hostname:
# name is found - check that the host responds with the same name
device_name = get_device_name(host)
print "Device name response:", device_name
if name == device_name and device_name != "":
result = host
break
except IOError:
pass
return result
def save_cache(host_map):
""" save the search results for quick access later """
filepath = os.path.expanduser(CACHE_FILE)
with open(filepath, "w") as f:
for key in host_map.keys():
if len(key) > 0 and len(host_map[key]) > 0:
# file format: hostname[tab]ip_addr
f.write(key + "\t" + host_map[key] + "\n")
def find_device(name=None, time_limit=6):
""" find the first device (quick) or search by name (slower)"""
if name is None or name == "":
# no name specified so find the first device that responds
print "searching the network for a Chromecast device"
hosts = search_network(device_limit=1)
if len(hosts) > 0:
return hosts[0], get_device_name(hosts[0])
else:
return None, None
else:
# name specified, check the cached network search results file
ip_addr = check_cache(name)
if ip_addr is not None:
# address found in cache
print "found device in cache:", name
return ip_addr, name
else:
# no cached results found run a full network search
print "searching the network for:", name
result_map = {}
hosts = search_network(time_limit=time_limit)
for host in hosts:
device_name = get_device_name(host)
if device_name != "":
result_map[device_name] = host
save_cache(result_map)
if name in result_map.keys():
print "found device:", name
return result_map[name], name
else:
return None, None