-
Notifications
You must be signed in to change notification settings - Fork 21
/
control_server.py
145 lines (103 loc) · 3.65 KB
/
control_server.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
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
from flask import Flask
from flask import request
import uuid
import json
import time
SERVER_PASSWORD = "py_remote_adb"
app = Flask(__name__, static_folder='static')
ALLOWED_EXTENSIONS = [
"jpg",
"png",
"jpeg",
"bmp",
]
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_commands(token, img_file):
""" analyze the screenshot and send
commands back to android device """
global token_dict, command_dict
img_file.save(f"static/{token}.png")
token_dict[token] = time.time() # record time
cmds = json.loads(json.dumps(command_dict[token])) # your adb shell commands
command_dict[token] = []
return cmds
token_dict = {}
command_dict = {}
@app.route('/login')
def login():
global token_dict, command_dict
token = request.args.get('token')
print(token)
if token not in token_dict.keys():
token_dict[token] = time.time() # record time
command_dict[token] = []
return json.dumps({
"code": 1,
"interval": 100
})
@app.route('/upload_screenshot', methods=['POST'])
def screenshot_upload():
if request.method == 'POST':
if 'file' not in request.files:
return json.dumps({'code': -1, 'msg': 'No file part'})
file = request.files['file']
if file.filename == '':
return json.dumps({'code': -1, 'msg': 'No selected file'})
else:
try:
if file and allowed_file(file.filename):
token = request.args.get("token")
cmds = get_commands(token, file)
return json.dumps({'code': 0, 'msg': 'success', 'commands': cmds})
else:
return json.dumps({'code': -1, 'msg': 'File not allowed'})
except Exception:
return json.dumps({'code': -1, 'msg': 'Error occurred'})
@app.route('/remote_adb/get_devices')
def remote_adb_get_devices():
global token_dict, command_dict
password = request.args.get('password')
if SERVER_PASSWORD != password:
return json.dumps({'code': -1, 'msg': 'error password'})
now_time = time.time()
key2del = []
for token in token_dict.keys():
if (now_time - token_dict[token]) > 20:
key2del.append(token)
for k in key2del:
del token_dict[k]
devices = [k for k in token_dict.keys()]
return json.dumps({
"code": 1,
"devices": devices
})
@app.route('/remote_adb/get_screenshot')
def remote_adb_get_screenshot():
token = request.args.get('token')
password = request.args.get('password')
if SERVER_PASSWORD != password:
return json.dumps({'code': -1, 'msg': 'error password'})
return app.send_static_file(f'{token}.png')
@app.route('/remote_adb/get_screenshot_unix')
def remote_adb_get_screenshot_unix():
global token_dict
token = request.args.get('token')
password = request.args.get('password')
if SERVER_PASSWORD != password:
return json.dumps({'code': -1, 'msg': 'error password'})
return json.dumps({"unix": token_dict[token]})
@app.route('/remote_adb/send_command')
def remote_adb_send_command():
global token_dict, command_dict
token = request.args.get('token')
password = request.args.get('password')
commands = request.args.get('commands')
if SERVER_PASSWORD != password:
return json.dumps({'code': -1, 'msg': 'error password'})
commands = json.loads(commands)
command_dict[token] = commands
return "{}"
if __name__ == '__main__':
app.run(host="0.0.0.0", port=6777, debug=True, threaded=False, processes=1)