-
Notifications
You must be signed in to change notification settings - Fork 7
/
listener.py
93 lines (65 loc) · 2.11 KB
/
listener.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
import os, json, base64
from flask import Flask
from flask_cors import CORS
#
# Global Variables
#
received_messages = []
#
# FLASK
#
app = Flask(__name__)
CORS(app)
def append_to( filename, data ):
contents = b""
if os.path.exists( filename ):
with open( filename, "rb+" ) as file:
contents = file.read()
with open( filename, "wb+" ) as file:
file.write( contents )
if data not in contents:
file.write( data )
return
def refinery( msg ) -> bool:
try:
data = base64.b64decode( msg ).decode("utf-8").replace("'", '"')
# Check if a dict
try:
data = json.loads( data )
except Exception as e:
print( f"(refienry) Ran into an exception, e:", e )
exit( 2 )
return False
filedata = data[ "data" ].replace( "%2B", "+" ).replace( "%99", "/" )
filename = data[ "filename" ]
full_path = "./output/" + filename
filedata = base64.b64decode( filedata )
append_to( full_path, filedata )
except Exception as e:
print( f"[refinery] Ran into an exception, e:", e )
return False
return True
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/listener/<file>')
@app.route('/listener/<file>/')
def message( file ):
global received_messages
file = file.replace( "%99", "/" )
file = file.replace( "%2B", "+" )
try:
print( f"(listener) RECEIVED: " )
print( f"(listener)\t RAW:", file )
print( f"(listener)\t DECODED:", base64.b64decode( file ).decode("utf-8") )
print( f"(listener)\t Sending to refinery.." )
print()
refinery( file )
received_messages.append( file )
except Exception as e:
print( f"(listener) Ran into an exception, e:", e )
print( f"(listener) Flask response: ")
return 'Hello, World!'
if __name__ == "__main__":
app.run("0.0.0.0", "5500")
print( f"(listener) started to listen.. " )