-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathlrr_webserver.py
More file actions
192 lines (172 loc) · 6.02 KB
/
lrr_webserver.py
File metadata and controls
192 lines (172 loc) · 6.02 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
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
#!/usr/bin/python
#############################################################################
# Long Range Reader Web/WebSocket Server
# By: Dennis Linuz <dennismald@gmail.com>
#
#############################################################################
import tornado.websocket
import tornado.httpserver
import tornado.ioloop
import tornado.web
import os
import csv
CSV_FILE = "cards.csv"
PORT = 80
Listeners = []
HTML = """
<!DOCTYPE>
<html>
<head>
<title>Credentials Scanned</title>
<style>
#credentials {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%%;
}
#credentials td, #credentials th {
border: 1px solid #ddd;
padding: 8px;
}
#credentials tr:nth-child(even){background-color: #f2f2f2;}
#credentials tr:hover {background-color: #ddd;}
#credentials th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div id="links">
<a href="./cards.csv">Download CSV</a>
<a href="#" onclick="confirm('Are you sure you want to clear cards.csv?')?document.location.href='./clearcsv':null;">Clear CSV</a>
</div>
<div id="file">
<table id=credentials>
<tr>
<th>ID</th>
<th>Bit Length</th>
<th>Wiegand Binary</th>
<th>Wiegand Hex Data</th>
<th>iCLASS Standard Encrypted Hex Data</th>
<th>Facility Code</th>
<th>Card Number</th>
<th>Card Number without Facility Code</th>
</tr>
</table>
</div>
<script type="text/javascript" charset="utf-8">
var credential;
function parseCredential(line) {
credential = line.split(",");
write_data(credential)
}
function write_data(credential) {
var id = credential[0];
var bits = credential[1];
var wiegand_binary = credential[2];
var wiegand_hex = credential[3];
var iclass_std_enc_hex = credential[4];
var fac_code = credential[5];
var card_num = credential[6];
var card_num_no_fac = credential[7];
var table = document.getElementById("credentials");
var row = table.insertRow(1);
var cell01 = row.insertCell(-1);
var cell02 = row.insertCell(-1);
var cell03 = row.insertCell(-1);
var cell04 = row.insertCell(-1);
var cell05 = row.insertCell(-1);
var cell06 = row.insertCell(-1);
var cell07 = row.insertCell(-1);
var cell08 = row.insertCell(-1);
cell01.innerHTML = id;
cell02.innerHTML = bits;
cell03.innerHTML = wiegand_binary;
cell04.innerHTML = wiegand_hex;
cell05.innerHTML = iclass_std_enc_hex;
cell06.innerHTML = fac_code;
cell07.innerHTML = card_num;
cell08.innerHTML = card_num_no_fac;
}
if ("MozWebSocket" in window) {
WebSocket = MozWebSocket;
}
if (WebSocket) {
var ws = new WebSocket("ws://%s/ws");
ws.onopen = function() {};
ws.onmessage = function (evt) {
parseCredential(evt.data);
};
ws.onclose = function() {};
} else {
alert("WebSocket not supported");
}
</script>
</table>
</body>
</html>
"""
cards_file = open(CSV_FILE)
cards_file.seek(os.path.getsize(CSV_FILE))
def check_file():
position = cards_file.tell()
line = cards_file.readline()
if not line:
cards_file.seek(position)
else:
print "File changed detected!"
for l in Listeners:
l.write_message(line)
class CredentialHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print 'new connection'
Listeners.append(self)
#self.write_message("Connected!")
# Initially print out contents of file
with open(CSV_FILE) as cards_file:
next(cards_file)
for line in cards_file:
#print "Sending message!"
self.write_message(line)
def on_message(self, message):
pass
def on_close(self):
print 'connection closed'
Listeners.remove(self)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write(HTML % (self.request.host))
class CSVDownloadHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=' + CSV_FILE)
# Print out contents of file
with open(CSV_FILE) as cards_file:
for line in cards_file:
self.write(line)
self.finish()
class ClearCSV(tornado.web.RequestHandler):
def get(self):
with open(CSV_FILE, 'w') as csv_file:
fieldnames = ['id', 'bit_length', 'wiegand_binary', 'wiegand_hex', 'iclass_std_enc_hex', 'fac_code', 'card_num',
'card_num_no_fac']
csvwriter = csv.DictWriter(csv_file, lineterminator='\n', fieldnames=fieldnames)
csvwriter.writeheader()
cards_file.seek(os.path.getsize(CSV_FILE))
self.redirect('/')
application = tornado.web.Application([
(r'/ws', CredentialHandler),
(r'/', IndexHandler),
(r'/clearcsv', ClearCSV),
(r'/cards.csv', CSVDownloadHandler)
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(PORT)
tornado.ioloop.PeriodicCallback(check_file, 500).start()
tornado.ioloop.IOLoop.instance().start()