-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApi.py
116 lines (97 loc) · 3.16 KB
/
Api.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
import json
import base64
from flask import Flask, request, jsonify
import subprocess
import sys
import os
import signal
import hashlib
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
nodejs_process = None
secret = os.getenv('API_SECRET')
session_text = None
def confirm_ses(hashed):
global session_text
if session_text is not None:
real_hash = hashlib.sha512(str(session_text).encode('utf-8')+str(secret).encode('utf-8')).hexdigest()
session_text = None
if real_hash == hashed:
return True
else:
return False
else:
return False
@app.route('/', methods=['GET'])
def test_up():
status = "bot appears to be down" if nodejs_process is None else "bot appears to be up"
return jsonify({'status': status})
@app.route('/ses', methods=['GET'])
def ses():
global session_text
session_text = base64.b64encode(os.urandom(128)).decode("utf-8")
return jsonify({'data': session_text})
@app.route('/start', methods=['POST'])
def start():
global nodejs_process
req = request.form.get('data',default=None)
if req is not None:
hashed = request.form['data']
if confirm_ses(hashed):
if nodejs_process is None:
nodejs_process = subprocess.Popen("node ./bot.js",shell=True)
return jsonify({'status': 'started'})
else:
return jsonify({'status': 'already running'})
else:
return jsonify({'error': 'invalid session'})
else:
return jsonify({'error': 'invalid request'})
@app.route('/stop', methods=['POST'])
def stop():
global nodejs_process
req = request.form.get('data',default=None)
if req is not None:
hashed = request.form['data']
if confirm_ses(hashed):
if nodejs_process is not None:
nodejs_process.kill()
print("nodejs stopped")
nodejs_process = None
return jsonify({'status': 'stopped'})
else:
print("no process running")
return jsonify({'status': 'already not running'})
else:
return jsonify({'error': 'invalid session'})
else:
return jsonify({'error': 'invalid request'})
@app.route('/restart', methods=['POST'])
def restart():
req = request.form.get('data',default=None)
if req is not None:
hashed = request.form['data']
if confirm_ses(hashed):
shutdown()
return jsonify({'status': 'rebooting'})
else:
return jsonify({'error': 'invalid session'})
else:
return jsonify({'error': 'invalid request'})
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return "Shutting down..."
@app.route('/test', methods=['POST'])
def test():
#body = request.form.keys()[0]
secret = request.form.get('data',default=None)
if secret is not None:
print(request.form['data'])
else:
print("secret not found")
return jsonify({'test': 'ok'})
app.run(host='10.0.0.69', port='9877')