-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdcnow.py
More file actions
131 lines (108 loc) · 4.34 KB
/
dcnow.py
File metadata and controls
131 lines (108 loc) · 4.34 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
#!/usr/bin/env python
#dcnow.py_version=202512152004
import threading
import os
import json
import time
import logging
import urllib
import urllib2
import sh
from hashlib import sha256
from uuid import getnode as get_mac
logger = logging.getLogger('dcnow')
API_ROOT = "https://dcnow-2016.appspot.com"
UPDATE_END_POINT = "/api/update/{mac_address}/"
UPDATE_INTERVAL = 15
CONFIGURATION_FILE = os.path.expanduser("~/.dreampi.json")
gameloft = False
def scan_mac_address():
mac = get_mac()
return sha256(':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))).hexdigest()
class DreamcastNowThread(threading.Thread):
def __init__(self, service):
self._service = service
self._running = True
super(DreamcastNowThread, self).__init__()
def run(self):
def post_update():
if not self._service._enabled:
return
global gameloft
lines = [ x for x in sh.tail("/var/log/syslog", "-n", "15", _iter=True) ]
dns_query = None
for line in lines[::-1]:
if "query[A]" in line:
# We did a DNS lookup, what was it?
remainder = line[line.find("query[A]") + len("query[A]"):].strip()
domain = remainder.split(" ", 1)[0].strip()
dns_query = sha256(domain).hexdigest()
#Send monaco/pod/speed just once - Begin
if gameloft and "gameloft" in domain: ## already sent, do not send again.
dns_query = None
break
if "gameloft" in domain: ## first read, send.
gameloft = True
logger.info("Domain sent to DCNow API: " + domain)
break
#Send monaco/pod/speed just once - End
if "appspot" in domain:
pass
else:
logger.info("Domain sent to DCNow API: " + domain)
break
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT), Dreamcast Now'
header = { 'User-Agent' : user_agent }
mac_address = self._service._mac_address
data = {}
if dns_query:
data["dns_query"] = dns_query
data = urllib.urlencode(data)
req = urllib2.Request(API_ROOT + UPDATE_END_POINT.format(mac_address=mac_address), data, header)
urllib2.urlopen(req) # Send POST update
while self._running:
try:
post_update()
except:
logger.exception("Couldn't update Dreamcast Now!")
dcnow_run.wait(UPDATE_INTERVAL)
def stop(self):
self._running = False
self.join()
class DreamcastNowService(object):
def __init__(self):
self._thread = None
self._mac_address = None
self._enabled = True
self.reload_settings()
logger.setLevel(logging.INFO)
handler = logging.handlers.SysLogHandler(address='/dev/log')
logger.addHandler(handler)
formatter = logging.Formatter('%(name)s[%(process)d]: %(message)s')
handler.setFormatter(formatter)
def update_mac_address(self, dreamcast_ip):
self._mac_address = scan_mac_address()
logger.info("MAC address: {}".format(self._mac_address))
def reload_settings(self):
settings_file = CONFIGURATION_FILE
if os.path.exists(settings_file):
with open(settings_file, "r") as settings:
content = json.loads(settings.read())
self._enabled = content["enabled"]
def go_online(self, dreamcast_ip):
logger.propagate = False
if not self._enabled:
return
global dcnow_run
dcnow_run = threading.Event()
self.update_mac_address(dreamcast_ip)
self._thread = DreamcastNowThread(self)
self._thread.start()
logger.info("DC Now Session Started")
def go_offline(self):
global dcnow_run, gameloft
gameloft = False
dcnow_run.set()
self._thread.stop()
self._thread = None
logger.info("DC Now Session Ended")