-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
131 lines (112 loc) · 3.54 KB
/
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
from flask import Flask, render_template, jsonify, Response, make_response, redirect, request
from src.recorder.Recorder import VideoStream
from src.utils.utils import timed_call, genVideoFeed
from flask_socketio import SocketIO, emit
from engineio.payload import Payload
from src.controls.Controller import Controller
from src.authentication.Auth import Auth
from config import MAX_DECODE_PACKETS , COOKIE_MAX_AGE , FPS
from src.audio.Audio import Audio
Payload.max_decode_packets = MAX_DECODE_PACKETS
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
auth = Auth()
stream = VideoStream()
controller = Controller()
authenticated = False
@app.before_request
def hook():
global authenticated
print('endpoint: %s, url: %s, path: %s' % (
request.endpoint,
request.url,
request.path))
cookie = request.cookies.get('auth')
print('cookie recieved',cookie)
if cookie == None:
authenticated = False
else:
authenticated = auth.isAuthenticated(cookie)
@app.route("/", methods=['GET'])
def home():
global authenticated
if not authenticated:
return redirect('/login')
return render_template('index.html')
@app.route("/wrong", methods=['GET'])
def wrong():
return render_template('unauth.html')
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
@app.route('/video_feed')
def video_feed():
if not authenticated:
return redirect('/login')
return Response(timed_call(genVideoFeed,FPS,stream),mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/res',methods=['GET'])
def get_res():
if not authenticated:
return redirect('/login')
res = stream.getRes()
return jsonify({'x' : res[0], 'y' : res[1] })
@app.route('/login',methods=['POST'])
def login():
global authenticated
if authenticated:
auth.generateRandomPasscode()
authenticated = False
data = request.get_json()
key = data['key']
match = auth.matchesPasscode(key)
if match:
resp = make_response(redirect('/'))
resp.set_cookie('auth',auth.gen_cookie(),max_age=COOKIE_MAX_AGE)
return resp
else:
resp = make_response(redirect('/wrong'))
resp.delete_cookie('auth')
return resp
@app.route('/login',methods=['GET'])
def loginView():
global authenticated
resp = make_response(render_template('login.html'))
resp.delete_cookie("auth")
if authenticated:
auth.generateRandomPasscode()
authenticated = False
return resp
@app.route('/logout',methods=['GET'])
def logout():
global authenticated
resp = make_response(redirect('/login'))
resp.delete_cookie("auth")
authenticated = False
auth.generateRandomPasscode()
return resp
@socketio.on('mouse_move')
def handle_mouse_move(data):
if not authenticated:
return redirect('/login')
r = stream.getRes()
controller.mouse_move(r,data)
@socketio.on('mouse_click')
def handle_mouse_click(data):
if(data['press'] == 'd'):
controller.mouse_click_down(data)
else:
controller.mouse_click_up(data)
@app.route('/audio')
def stream_audio():
audio_stream = Audio()
return Response(audio_stream.getStream(),mimetype='audio/wav')
@socketio.on('keyboard')
def handle_key_press(data):
if(data['press'] == 'd'):
controller.press_key_down(data)
else:
controller.press_key_up(data)
if __name__ == "__main__":
socketio.run(app, debug=True, host='0.0.0.0')