Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
1133 lines (841 loc) · 35.9 KB
/
Copy pathserver.py
File metadata and controls
1133 lines (841 loc) · 35.9 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cherrypy
import urllib.request
import json
import base64
import nacl.encoding
import nacl.signing
from nacl.public import PrivateKey, SealedBox
import socket
from threading import Thread
import datetime
import time
import sqlite3
#import eventlet
#eventlet.monkey_patch()
#startHTML = "<html><head><title>CS302 example</title><link rel='stylesheet' href='/static/example.css' /></head><body>"
startHTML = '<!DOCTYPE html><html><body background="https://images3.alphacoders.com/189/thumb-1920-189705.jpg"></body></html>' #for the BG
startHTML += '<center><!DOCTYPE html><html><head><style>h1{font-size: 300%;color:white;}</style></head><body><h1>Ahmad Umars Prototype</h1></body></html>'
class MainApp(object):
# CherryPy Configuration
_cp_config = {'tools.encode.on': True,
'tools.encode.encoding': 'utf-8',
'tools.sessions.on': 'True',
# increase security on sessions
# 'tools.sessions.secure' : 'True',
'tools.sessions.httponly': 'True'
}
# If they try somewhere we don't know, catch it here and send them to the right place.
@cherrypy.expose
def default(self, *args, **kwargs):
"""The default page, given when we don't recognise where the request is for."""
Page = startHTML + "I don't know where you're trying to go, so have a 404 Error."
cherrypy.response.status = 404
return Page
# PAGES (which return HTML that can be viewed in browser)
@cherrypy.expose
def index(self):
Page = startHTML
Page += '<center><!DOCTYPE html><html><head><style>h1{font-size: 200%;color:black;}</style></head><body><h1>Welcome! This is a prototype for a socialising web app!</h1></body></html>'
try:
Page += "Hello " + cherrypy.session['username'] + "!<br/>"
Page += "CONGRATULATIONS!! You have successfully signed in. <a href='/signout'>Sign out</a> <br/>"
Page += "<br>Functions available:"
Page += "<br>  - To get a list of users, click: <a href='/api/list_apis'>List APIs</a> "
Page += "<br>  - To get a list of users, click: <a href='/api/list_users'>List Users</a> "
Page += "<br>  - To update list of users, click: <a href='/api/update_users'>Update Users</a> "
Page += "<br>  - To add your public key to the login server, please click: <a href='/api/add_pubkey'>Add Public key</a>."
Page += "<br>  - To <font color = 'blue'>report</font> to the login server, please click: <a href='/api/report'>Report</a>."
Page += "<br>  - To view all broadcasted messages, please click: <a href='/api/rx_broadcast'>Broadcast Messages</a>."
Page += "<br>  - To view received private messages, please click: <a href='/api/rx_privatemessage'>Private Messages</a>."
#Page += "<br> </br>"
Page += "<br>  - To view blocked users, please click: <a href='/api/print_blocked'>View</a>."
#Page += "<br> </br>"
Page += "<br>  - To view blocked word, please click: <a href='/api/print_blocked_word'>View</a>."
Page += "<br> </br>"
Page += '<form action="/api/tx_broadcast" method="post" enctype="multipart/form-data">'
Page += '    Type your post: <input name="message"/>'
Page += '<input type="submit" value="Publish"/></form>'
Page += "<br> </br>"
Page += '<form action="/api/tx_privatemessage" method="post" enctype="multipart/form-data">'
Page += 'Type your private message    : <input name="message"/> </br>'
Page += 'Type your receivers username(upi) : <input name="upi"/>'
Page += '<input type="submit" value="Send"/></form>'
Page += "<br> </br>"
Page += '<form action="/api/block" method="post" enctype="multipart/form-data">'
Page += 'Enter username to block: <input name="upi"/>'
Page += '<input type="submit" value="Block"/></form>'
#Page += "<br> </br>"
Page += '<form action="/api/unblock" method="post" enctype="multipart/form-data">'
Page += 'Enter username to unblock: <input name="upi"/>'
Page += '<input type="submit" value="Unblock"/></form>'
Page += "<br> </br>"
Page += '<form action="/api/block_words" method="post" enctype="multipart/form-data">'
Page += 'Enter word to block: <input name="word"/>'
Page += '<input type="submit" value="Block"/></form>'
Page += '<form action="/api/unblock_word" method="post" enctype="multipart/form-data">'
Page += 'Enter word to unblock: <input name="word"/>'
Page += '<input type="submit" value="Unblock"/></form>'
except KeyError: # There is no username
Page += '<center><!DOCTYPE html><html><style>.button{border: none;color: white;padding: 16px 32px;text-align: center;text-decoration: none;display:inline-block;font-family: verdana;font-size: 20px;margin: 4px 2px;-webkit-transition-duration: 0.4s;}.button{background-color: white;color: black;border-radius:12px;border: 2px solid #555555;}.button:hover{background-color:#555555;color: white;}</style><button class="button button"><a href="login">LOG IN</a></button></html>'
#Page += "Click here to <a href='login'>login</a>."
return Page
@cherrypy.expose
def login(self, bad_attempt=0):
Page = startHTML
if bad_attempt != 0:
Page += "<font color='red'>Invalid username/password!</font>"
Page += '<form action="/signin" method="post" enctype="multipart/form-data">'
Page += 'Username: <input type="text" name="username"/><br/>'
Page += '   Password : <input type="password" name="password"/>'
Page += '<input type="submit" value="Login"/></form>'
return Page
@cherrypy.expose
def sum(self, a=0, b=0): # All inputs are strings by default
output = int(a)+int(b)
return str(output)
# LOGGING IN AND OUT
@cherrypy.expose
def signin(self, username=None, password=None):
"""Check their name and password and send them either to the main page, or back to the main login screen."""
error = authoriseUserLogin(username, password)
if error == 0:
cherrypy.session['username'] = username
cherrypy.session['password'] = password
add_pubkey(self)
report(self)
ApiApp.update_users(self)
start_time = time.time()
print(start_time)
raise cherrypy.HTTPRedirect('/')
else:
raise cherrypy.HTTPRedirect('/login?bad_attempt=1')
@cherrypy.expose
def signout(self):
"""Logs the current user out, expires their session"""
username = cherrypy.session.get('username')
if username is None:
pass
else:
cherrypy.lib.sessions.expire()
raise cherrypy.HTTPRedirect('/')
##################################
############Functions#############
##################################
@cherrypy.expose
def ping(self):
url = "http://cs302.kiwi.land/api/ping"
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def list_apis(self):
url = "http://cs302.kiwi.land/api/list_apis"
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def list_users(self, username=None, password=None):
url = "http://cs302.kiwi.land/api/list_users"
username = cherrypy.session['username']
password = cherrypy.session['password']
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
try:
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def add_pubkey(self):
url = "http://cs302.kiwi.land/api/add_pubkey"
username = cherrypy.session['username']
password = cherrypy.session['password']
cherrypy.session['check'] = True
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
priv_key = nacl.signing.SigningKey.generate() # output is in bytes
priv_key_hex = priv_key.encode(encoder=nacl.encoding.HexEncoder) # not used
public_key_hex = priv_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
# readable version of public key NOT BYTES
public_key = public_key_hex.decode('utf-8')
cherrypy.session['priv_key'] = priv_key
cherrypy.session['public_key'] = public_key # making pub key global
# if you need any other version of ur pub g, add more global variables
signature_bytes = bytes(public_key + username, encoding='utf-8')
signature_hex = priv_key.sign(signature_bytes, encoder=nacl.encoding.HexEncoder)
signature = signature_hex.signature.decode('utf-8') # readable version NOT UNDERSTANDABLE
payload = {
"pubkey": public_key,
"username": username,
"signature": signature
}
data_string = json.dumps(payload)
data_send = data_string.encode()
try:
req = urllib.request.Request(url, headers=headers, data=data_send)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def report(self):
check = False
url = "http://cs302.kiwi.land/api/report"
username = cherrypy.session['username']
password = cherrypy.session['password']
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
public_key = cherrypy.session.get('public_key')
payload = {
#"connection_address": "172.24.43.158:10050",
"connection_address": str(socket.gethostbyname(socket.gethostname())) + ":10051",
"connection_location": 2,
"incoming_pubkey": public_key
}
data_string = json.dumps(payload)
data_send = data_string.encode()
try:
req = urllib.request.Request(url, headers=headers, data=data_send)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
check = True
response.close()
except:
check = False
JSON_object = json.loads(data.decode(encoding))
check = cherrypy.session['check']
return JSON_object
@cherrypy.expose
def load_new_apikey(self):
url = "http://cs302.kiwi.land/api/load_new_apikey"
username = cherrypy.session.get('username')
password = cherrypy.session.get('password')
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
try:
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def loginserver_pubkey(self):
url = "http://cs302.kiwi.land/api/loginserver_pubkey"
try:
req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def get_loginserver_record(self):
url = "http://cs302.kiwi.land/api/get_loginserver_record"
username = cherrypy.session.get('username')
password = cherrypy.session.get('password')
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
try:
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# print(data)
cherrypy.session['login_record'] = data
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
@cherrypy.expose
def check_pubkey(self):
url = "http://cs302.kiwi.land/api/check_pubkey"
username = cherrypy.session.get('username')
password = cherrypy.session.get('password')
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
try:
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
return JSON_object
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
##############################
##########Class ApiApp#######
#############################
class ApiApp(object):
@cherrypy.expose
def ping(self):
Page = startHTML
JSON_object = ping(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def block(self, upi):
Page = startHTML
check = False
#blocked_users = []
#cherrypy.session['blocked_users'] = blocked_users
#cherrypy.session.get('blocked_users').append(upi)
#print(cherrypy.session.get('blocked_users'))
with sqlite3.connect("database.db") as a:
username = str(upi)
try:
a.execute("INSERT INTO blocked VALUES('%s')" % (username))
#Page += username
check = True
#Page += " successfully added to the block list"
except sqlite3.IntegrityError:
print("Error! Line 458")
check = False
pass
Page += username
if check:
Page += " successfully added to the block list"
else:
Page += " is already blocked!"
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def unblock(self, upi):
Page = startHTML
#blocked_users = []
#cherrypy.session['blocked_users'] = blocked_users
#cherrypy.session.get('blocked_users').append(upi)
#print(cherrypy.session.get('blocked_users'))
with sqlite3.connect("database.db") as a:
username = str(upi)
try:
a.execute("DELETE FROM blocked where username = '%s' " % (username))
Page += username
Page += " successfully removed from the block list"
except:
Page += "Woops, something went wrong"
pass
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def print_blocked(self):
Page = startHTML
connection = sqlite3.connect("database.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute("SELECT * FROM blocked")
results = cursor.fetchall()
for thing in results:
#print(thing['username'])
Page += thing['username']
Page += "</br>"
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def block_words(self, word):
Page = startHTML
check = False
with sqlite3.connect("database.db") as a:
word = str(word)
try:
a.execute("INSERT INTO blocked_word VALUES('%s')" % (word))
#Page += username
check = True
#Page += " successfully added to the block list"
except sqlite3.IntegrityError:
print("Error! Line 458")
check = False
pass
Page += word
if check:
Page += " successfully added to the block list"
else:
Page += " is already blocked!"
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def unblock_word(self, word):
Page = startHTML
with sqlite3.connect("database.db") as a:
word = str(word)
try:
a.execute("DELETE FROM blocked_word where word = '%s' " % (word))
Page += word
Page += " successfully removed from the block list"
except:
Page += "Woops, something went wrong"
pass
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def print_blocked_word(self):
Page = startHTML
connection = sqlite3.connect("database.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute("SELECT * FROM blocked_word")
results = cursor.fetchall()
for thing in results:
#print(thing['username'])
Page += thing['word']
Page += "</br>"
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def list_apis(self):
Page = startHTML
JSON_object = list_apis(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def list_users(self, username=None , password= None):
Page = startHTML
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
Page += "</br>"
JSON_object = list_users(self,username, password)
#print(JSON_object)
rough = JSON_object['users']
for data in rough:
Page += json.dumps(data['username'])
Page += " at IP address: "
Page += json.dumps(data['connection_address'])
Page += "</br>"
#Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def update_users(self):
Page = startHTML
username = cherrypy.session['username']
password = cherrypy.session['password']
JSON_object = list_users(self,username, password)
#print(JSON_object)
rough = JSON_object['users']
check = False
for data in rough:
name = data['username']
with sqlite3.connect("database.db") as a:
try:
a.execute("INSERT INTO users VALUES ('%s')"% (name))
check = True
#print("added")
except sqlite3.IntegrityError:
#print("Error!")
pass
if check:
Page += "Users database successfully updated! </br>"
else:
Page += "No new users were added"
Page += "<br>  - To return, please click: <a href='/index'>Home</a> </br>"
return Page
@cherrypy.expose
def add_pubkey(self):
Page = startHTML
JSON_object = add_pubkey(self)
Page += json.dumps(JSON_object)
Page += "<br> </br>"
Page += "<br>  - To return, please click: <a href='/index'>Home</a>."
return Page
@cherrypy.expose
def report(self):
Page = startHTML
if cherrypy.session.get('check'):
JSON_object = report(self)
Page += json.dumps(JSON_object)
Page += "<br>  - To return, please click: <a href='/index'>Home</a>."
else:
Page += "Error encountered! Please register your public key first."
Page += "<br>  - To add your public key to the login server, please click: <a href='/api/add_pubkey'>Add Public key</a>."
return Page
@cherrypy.expose
def load_new_apikey(self):
Page = startHTML
JSON_object = load_new_apikey(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def loginserver_pubkey(self):
Page = startHTML
JSON_object = loginserver_pubkey(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def get_loginserver_record(self):
Page = startHTML
JSON_object = get_loginserver_record(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def check_pubkey(self):
Page = startHTML
JSON_object = check_pubkey(self)
Page += json.dumps(JSON_object)
return Page
@cherrypy.expose
def tx_broadcast(self, message):
username = cherrypy.session['username']
password = cherrypy.session['password']
priv_key = cherrypy.session.get('priv_key')
clock_started = str(time.time())
record_JSON = get_loginserver_record(self)
JSON_object = list_users(self, username, password)
rough = JSON_object['users']
IP = "demo"
Page = startHTML
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
record = record_JSON['loginserver_record']
msg = message
signature_bytes = bytes(record + msg + clock_started, encoding='utf-8')
signature_hex = priv_key.sign(signature_bytes, encoder= nacl.encoding.HexEncoder)
signature = signature_hex.signature.decode('utf-8') # readable version NOT UNDERSTANDABLE
payload = {
"loginserver_record": record,
"message": msg,
"sender_created_at": clock_started,
"signature": signature
}
data_string = json.dumps(payload)
data_send = data_string.encode()
for data in rough:
IP = data['connection_address']
#print(IP)
url = "http://" + IP + "/api/rx_broadcast"
Page += url
Page += "</br>"
#print("before the try statement")
try:
#with eventlet.Timeout(10):
req = urllib.request.Request(url, headers=headers, data=data_send)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print("Hey! Im in the error part now")
print(error.read())
exit()
print(IP)
return Page
@cherrypy.expose
def tx_privatemessage(self, message, upi):
username = cherrypy.session['username']
password = cherrypy.session['password']
JSON_object = list_users(self, username, password)
rough = JSON_object['users']
IP = "ds"
#pubkey = " "
for data in rough:
if (upi == data['username']):
pubkey = data['incoming_pubkey']
cherrypy.session['pubkey'] = pubkey
IP = data['connection_address']
print(IP)
print("found the user")
check = True
break
else:
check = False
url = "http://" + str(IP) + "/api/rx_privatemessage"
Page = startHTML
credentials = ('%s:%s' % (username, password))
b64_credentials = base64.b64encode(credentials.encode('ascii'))
headers = {
'Authorization': 'Basic %s' % b64_credentials.decode('ascii'),
'Content-Type': 'application/json; charset=utf-8',
}
record_JSON = get_loginserver_record(self)
record = record_JSON['loginserver_record']
#msg = "sup dawg"
msg = message
msg_bytes = bytes(msg, encoding='utf-8')
clock_started = str(time.time())
public_key = cherrypy.session.get('public_key')
if check == False:
Page += "ERROR!! User is not online. Please try later. </br>"
Page += "Please return to home page, "
Page += "<a href='/index'>Home</a>. </br>"
return Page
#pubkey = "2ae0385c711e11f784bd3cad956e6640ad9059d8d96a0127e07102aeb0475911"
target_usrname = upi
priv_key = cherrypy.session.get('priv_key')
public_key_hex = priv_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
cherrypy.session['publickey_hex'] = public_key_hex
#print(pubkey)
verifykey = nacl.signing.VerifyKey(pubkey, encoder=nacl.encoding.HexEncoder)
public_key = verifykey.to_curve25519_public_key()
sealed_box = nacl.public.SealedBox(public_key)
encrypted = sealed_box.encrypt(msg_bytes, encoder=nacl.encoding.HexEncoder)
encrypt_msg = encrypted.decode('utf-8')
signature_bytes = bytes(record + pubkey + target_usrname + encrypt_msg + clock_started, encoding='utf-8')
signature_hex = priv_key.sign(signature_bytes, encoder= nacl.encoding.HexEncoder)
signature = signature_hex.signature.decode('utf-8') # readable version NOT UNDERSTANDABLE
payload = {
"loginserver_record": record,
"target_pubkey": pubkey,
"target_username": target_usrname,
"encrypted_message": encrypt_msg,
"sender_created_at": clock_started,
"signature": signature
}
data_string = json.dumps(payload)
data_send = data_string.encode()
try:
req = urllib.request.Request(url, headers=headers, data=data_send)
response = urllib.request.urlopen(req)
data = response.read() # read the received bytes
# load encoding if possible (default to utf-8)
encoding = response.info().get_content_charset('utf-8')
response.close()
except urllib.error.HTTPError as error:
print(error.read())
exit()
JSON_object = json.loads(data.decode(encoding))
Page += json.dumps(JSON_object)
Page += "<br>  - To return, please click: <a href='/index'>Home</a>."
return Page
################################
######### class ################
################################
@cherrypy.expose
class rx_broadcast(object):
@cherrypy.tools.accept(media='text/plain')
def GET(self):
Page = startHTML
blocked = False
Page += "<br>  - To return, please click: <a href='/index'>Home</a>. </br>"
Page += "</br>"
connection = sqlite3.connect("database.db")
connection.row_factory = dict_factory
cursor = connection.cursor()
cursor.execute("SELECT * FROM broadcast")
results = cursor.fetchall()
results.reverse()
cursor1 = connection.cursor()
cursor1.execute("SELECT * FROM blocked")
results1 = cursor1.fetchall()
cursor2 = connection.cursor()
cursor2.execute("SELECT * FROM blocked_word")
results2 = cursor2.fetchall()
#print(results)
for thing in results:
user = thing['loginserver_record']
#user = str(user)
name = user[0:7]
msgg = str(thing['message'])
#print(name)
#print(results1)
for thing1 in results1:
#print(thing1)
blocked_username = thing1['username']
#print(blocked_username)
#print ( blocked_username + " " + name)
if name == blocked_username:
blocked = True
break
else:
blocked = False
for thing2 in results2:
#print(thing2)
blocked_word = thing2['word']
#print(blocked_username)
#print ( blocked_username + " " + name)
if blocked_word in msgg:
blocked = True
break
else:
blocked = False
#print(blocked_username + " " + name)
if blocked == False:
Page += name
Page += " says </br>"
Page += thing['message']
Page += "<br> </br>"
else:
pass
return Page
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def POST(self):
incoming_json = cherrypy.request.json
with sqlite3.connect("database.db") as a:
loginserver_rec = incoming_json['loginserver_record']
message = incoming_json['message']
sender_created_at = incoming_json['sender_created_at']
signature = incoming_json['signature']
try: