-
Notifications
You must be signed in to change notification settings - Fork 2
/
start_1
executable file
·190 lines (137 loc) · 4.05 KB
/
start_1
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
#!/usr/bin/env python
from flask import Flask, request, jsonify, copy_current_request_context
from flask_socketio import SocketIO, send, emit
from tools.keysim import Sim
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
socketio = SocketIO(app)
webpage = open("Index_1.html", "r").read()
s = Sim()
import pyscreeze
import PIL
import os
import base64
from io import BytesIO
import time
def screenshot(store=False):
if not store:
buffered = BytesIO()
t1 = time.time()
#shot = pyscreeze.screenshot()
shot = pyscreeze.screenshot(region=(0,0, 300, 400))
print("T: screenshot")
print( (time.time() - t1 ) *1000)
t1 = time.time()
shot.save(buffered, format="JPEG")
print("T: save")
print( (time.time() - t1 ) *1000)
t1 = time.time()
b = base64.b64encode(buffered.getvalue())
print("T: encode")
print( (time.time() - t1 ) *1000)
return b
else:
shot = pyscreeze.screenshot()
shot.save(os.path.join("static","shot.jpeg"))
return shot
@app.route("/")
def index():
return webpage.encode()
@app.route("/shot")
def handle_screenshot():
print("[screenshot] stored")
screenshot(store=True)
return jsonify(msg= "received")
@app.route("/click", methods=["POST"])
def click():
data = (request.json)
posx = data["posx"]
posy = data["posy"]
s.m.click(posx, posy, 1)
return "received"
@app.route("/simulate", methods=["POST"])
def simulate():
data = (request.json)
val = data["key"]
print(val)
command = {
"left": s.pressLeft,
"right": s.pressRight,
"up": lambda: s.tap(s.k.up_key),
"down": lambda: s.tap(s.k.down_key),
}
selected = command.get(val, lambda: s.k.tap_key(val))
print(selected())
return "received"
@app.route("/text", methods=["POST"])
def type():
data = (request.json)
val = data["text"]
print(val)
print(s.k.type_string(val))
print(s.k.tap_key("Return"))
return "received"
#################################
intervals = dict()
@socketio.on('connect')
def handle_connect():
sid = request.sid
print(f'[socket][/connect] [{sid}]')
@socketio.on('message')
def handle_message(message):
print('received message: ' + message)
send ( "what", broadcast=True )
import threading
stream_timer = None
@socketio.on('stream')
def handle_stream(interval):
@copy_current_request_context
def stream_loop():
global stream_timer
global intervals
if len(intervals) == 0:
return
interval_time = min(intervals.values())/1000
if interval_time == 0:
return
import time
t1 = time.time()
img_str = screenshot()
emit ( "image", img_str)
t2 = time.time()
elapsed_ms = (t2-t1) * 1000
print(f"elapsed_ms {elapsed_ms}")
time_left = (min(intervals.values()) - elapsed_ms)
stream_timer = threading.Timer( (time_left if time_left >= 0 else 0)/1000, stream_loop)
stream_timer.start()
sid = request.sid
print(f'[socket][/stream] [{sid}] {str(interval)}')
global intervals
intervals[sid] = interval
if not isinstance (interval, int) or interval == 0:
del intervals[sid]
return
stream_loop()
#################################
def get_ip():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP
if __name__ == "__main__":
import qrcode
port = 1337
path = "http://"+(get_ip() + ":" + str(port))+"/"
img = qrcode.make(path)
print(f"Remote on {path}")
img.show()
img.save(os.path.join("static", "qr.jpeg"), "JPEG")
#app.run(host= '0.0.0.0', port=port)
socketio.run(app, host= '0.0.0.0', port=port)