This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
poc.py
executable file
·149 lines (120 loc) · 4.2 KB
/
poc.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
#!/usr/bin/env python
#Copyright 2014 Fred Clift. All Rights reserved.
#Inspired by PHK
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE":
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return - Fred Clift
# (except that I dont drink....)
# ----------------------------------------------------------------------------
#
import cast_channel_pb2
import socket, ssl, select, time, re
from struct import pack,unpack
import sys, os
import json
from hexdump import hexdump
request_id = os.getpid()
chromecast_server = "192.168.0.138"
#chromecast_server = "127.0.0.1"
namespace = { 'con': 'urn:x-cast:com.google.cast.tp.connection',
'receiver': 'urn:x-cast:com.google.cast.receiver',
'cast': 'urn:x-cast:com.google.cast.media',
'heartbeat': 'urn:x-cast:com.google.cast.tp.heartbeat',
'message': 'urn:x-cast:com.google.cast.player.message',
'media': "urn:x-cast:com.google.cast.media",
}
def make_msg(nspace = None):
msg = cast_channel_pb2.CastMessage()
msg.protocol_version = msg.CASTV2_1_0
msg.source_id = "sender-0"
msg.destination_id = "receiver-0"
msg.payload_type = cast_channel_pb2.CastMessage.STRING;
if nspace:
msg.namespace = namespace[nspace]
return msg
#def hexdump(msg):
# f = os.popen("/usr/bin/hexdump -C", "w")
# f.write(msg)
# f.close()
def format_msg(msg):
#prepend message with Big-Endian 4 byte payload size
be_size = pack(">I",msg.ByteSize())
return be_size + msg.SerializeToString()
def read_message(soc):
#first 4 bytes is Big-Endian payload length
data=""
while len(data) < 4:
frag = soc.recv(1)
data += frag
read_len = unpack(">I", data)[0]
#now read the payload
data=""
while len(data) < read_len:
frag = soc.recv(2048)
data += frag
print "."
return data
def get_response(soc):
print "\nReading ..."
#get the data
payload=read_message(soc)
response = make_msg()
response.ParseFromString(payload)
resp = json.loads(response.payload_utf8)
print json.dumps(resp, indent=4)
return resp
def send_pong(soc):
print "Sending PONG"
msg.namespace=namespace['heartbeat']
msg.payload_utf8 = """{ "type": "PONG" }"""
message = format_msg(msg)
soc.sendall(message)
##################################################
soc = socket.socket()
soc = ssl.wrap_socket(soc)
soc.connect((chromecast_server,8009))
msg = make_msg()
print "Connecting ..."
msg.namespace=namespace['con']
msg.payload_utf8 = """{"type":"CONNECT","origin":{}}"""
message = format_msg(msg)
soc.sendall(message)
#hexdump(message)
print "Sending get_status..."
msg.namespace = namespace['receiver']
msg.payload_utf8 = """{"type":"GET_STATUS","requestId":%s}""" % (request_id)
message = format_msg(msg)
soc.sendall(message)
#hexdump(message)
resp = get_response(soc)
session = resp['status']['applications'][0]['sessionId']
print "Sending Launch App"
msg.namespace = namespace['receiver']
msg.payload_utf8 = """{"type":"LAUNCH","requestId":%s,"appId":"CC1AD845"}""" % (request_id)
message = format_msg(msg)
soc.sendall(message)
#hexdump(message)
resp = get_response(soc)
print "Sending Launch App"
msg.namespace = namespace['con']
msg.payload_utf8 = """{"type":"CONNECT","origin":{}}"""
message = format_msg(msg)
soc.sendall(message)
#hexdump(message)
resp = get_response(soc)
print "Sending load URL"
msg.namespace = namespace['media']
data = """{"type":"LOAD","requestId":%s,"sessionId":"%s","media":{"contentId":"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4","streamType":"buffered","contentType":"video/mp4"},"autoplay":true,"currentTime":0,"customData":{"payload":{"title:":"Big Buck Bunny","thumb":"images/BigBuckBunny.jpg"}}}""" % (request_id, session)
msg.payload_utf8 =data
message = format_msg(msg)
soc.sendall(message)
try:
while True:
resp = get_response(soc)
if resp['type'] == 'PING':
send_pong(soc)
finally:
soc.close