forked from aluzzardi/wssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_server.py
65 lines (51 loc) · 1.88 KB
/
flask_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
#!/usr/bin/env python
from gevent import monkey
monkey.patch_all()
from flask import Flask, request
from werkzeug.exceptions import BadRequest, Unauthorized
import wssh
app = Flask(__name__)
@app.route('/remote')
def index():
# Abort if this is not a websocket request
if not request.environ.get('wsgi.websocket'):
app.logger.error('Abort: Request is not WebSocket upgradable')
raise BadRequest()
# Here you can perform authentication and sanity checks
if request.args.get('key') != 'secret':
raise Unauthorized
# Initiate a WSSH Bridge and connect to a remote SSH server
bridge = wssh.WSSHBridge(request.environ['wsgi.websocket'])
try:
bridge.open(
hostname='localhost',
username='root',
password='my password'
)
except Exception as e:
app.logger.exception('Error while connecting: {0}'.format(
e.message))
request.environ['wsgi.websocket'].close()
return str()
# Launch a shell on the remote server and bridge the connection
# This won't return as long as the session is alive
bridge.shell()
# Alternatively, you can run a command on the remote server
# bridge.execute('/bin/ls -l /')
# We have to manually close the websocket and return an empty response,
# otherwise flask will complain about not returning a response and will
# throw a 500 at our websocket client
request.environ['wsgi.websocket'].close()
return str()
if __name__ == '__main__':
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
app.debug = True
http_server = WSGIServer(('localhost', 5000), app,
log=None,
handler_class=WebSocketHandler)
print 'Server running on ws://localhost:5000/remote'
try:
http_server.serve_forever()
except KeyboardInterrupt:
pass