Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
L
luna
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
luna
Commits
86cf253e
Commit
86cf253e
authored
Feb 23, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Fixture] 使用gevent, 使用proxy server
parent
73a012f8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
130 additions
and
21 deletions
+130
-21
__init__.py
luna/__init__.py
+2
-0
app.py
luna/app.py
+6
-3
authentication.py
luna/authentication.py
+3
-5
channel.py
luna/channel.py
+33
-0
proxy.py
luna/proxy.py
+0
-0
websocket.py
luna/views/websocket.py
+81
-11
run_server.py
run_server.py
+5
-2
No files found.
luna/__init__.py
View file @
86cf253e
...
@@ -7,3 +7,5 @@ BASE_DIR = os.path.abspath(os.path.dirname(__file__))
...
@@ -7,3 +7,5 @@ BASE_DIR = os.path.abspath(os.path.dirname(__file__))
from
.app
import
app
,
socket_io
from
.app
import
app
,
socket_io
from
.
import
authentication
,
views
from
.
import
authentication
,
views
import
logger
import
logger
from
flask
import
request
luna/app.py
View file @
86cf253e
...
@@ -5,6 +5,7 @@ import time
...
@@ -5,6 +5,7 @@ import time
from
flask
import
Flask
from
flask
import
Flask
import
socketio
import
socketio
from
flask_socketio
import
SocketIO
from
.conf
import
config
from
.conf
import
config
from
.service
import
service
from
.service
import
service
...
@@ -16,8 +17,9 @@ __version__ = '0.4.0'
...
@@ -16,8 +17,9 @@ __version__ = '0.4.0'
class
Luna
(
Flask
):
class
Luna
(
Flask
):
app_
service
=
service
service
=
service
clients
=
{}
clients
=
{}
proxy_list
=
{}
active
=
True
active
=
True
def
run
(
self
,
host
=
None
,
port
=
None
,
debug
=
None
,
**
options
):
def
run
(
self
,
host
=
None
,
port
=
None
,
debug
=
None
,
**
options
):
...
@@ -38,6 +40,7 @@ class Luna(Flask):
...
@@ -38,6 +40,7 @@ class Luna(Flask):
async_mode
=
'threading'
async_mode
=
'threading'
app
=
Luna
(
__name__
,
template_folder
=
'dist'
)
app
=
Luna
(
__name__
,
template_folder
=
'dist'
)
socket_io
=
SocketIO
(
app
)
app
.
config
.
update
(
**
config
)
app
.
config
.
update
(
**
config
)
socket_io
=
socketio
.
Server
(
logger
=
False
,
async_mode
=
async_mode
)
#
socket_io = socketio.Server(logger=False, async_mode=async_mode)
app
.
wsgi_app
=
socketio
.
Middleware
(
socket_io
,
app
.
wsgi_app
)
#
app.wsgi_app = socketio.Middleware(socket_io, app.wsgi_app)
luna/authentication.py
View file @
86cf253e
...
@@ -9,10 +9,6 @@ from jms import UserService
...
@@ -9,10 +9,6 @@ from jms import UserService
from
.
import
app
from
.
import
app
def
is_authenticate
(
user_service
):
pass
def
login_required
(
func
=
None
,
login_url
=
None
):
def
login_required
(
func
=
None
,
login_url
=
None
):
if
func
is
None
:
if
func
is
None
:
return
partial
(
login_required
,
login_url
=
login_url
)
return
partial
(
login_required
,
login_url
=
login_url
)
...
@@ -31,7 +27,9 @@ def login_required(func=None, login_url=None):
...
@@ -31,7 +27,9 @@ def login_required(func=None, login_url=None):
g
.
user_service
=
UserService
(
endpoint
=
app
.
config
[
'JUMPSERVER_ENDPOINT'
])
g
.
user_service
=
UserService
(
endpoint
=
app
.
config
[
'JUMPSERVER_ENDPOINT'
])
g
.
user_service
.
auth_from_session
(
session_id
,
csrf_token
)
g
.
user_service
.
auth_from_session
(
session_id
,
csrf_token
)
if
g
.
user_service
.
is_authenticated
():
user
=
g
.
user_service
.
is_authenticated
()
if
user
:
g
.
user
=
user
return
func
(
*
args
,
**
kwargs
)
return
func
(
*
args
,
**
kwargs
)
else
:
else
:
return
redirect
(
login_url
)
return
redirect
(
login_url
)
...
...
luna/channel.py
0 → 100644
View file @
86cf253e
# ~*~ coding: utf-8 ~*~
import
socket
from
.app
import
socket_io
class
ProxyChannel
(
object
):
def
__init__
(
self
,
sid
):
self
.
sid
=
sid
self
.
srv
,
self
.
cli
=
socket
.
socketpair
()
self
.
win_width
=
80
self
.
win_height
=
24
def
fileno
(
self
):
return
self
.
srv
.
fileno
()
def
send
(
self
,
s
):
"""Proxy server中使用select, 通过socketio发送给用户"""
return
socket_io
.
emit
(
'data'
,
s
,
root
=
self
.
sid
)
def
recv
(
self
,
size
):
"""Proxy server使用select, 接受socketio客户端数据发送来的数据"""
return
self
.
srv
.
recv
(
size
)
def
write
(
self
,
s
):
"""socket io写数据,proxy可以通过recv收到"""
print
(
'Client write message:
%
s'
%
s
)
self
.
cli
.
send
(
s
)
def
set_win_size
(
self
,
size
):
self
.
win_width
,
self
.
win_height
=
size
luna/proxy.py
View file @
86cf253e
This diff is collapsed.
Click to expand it.
luna/views/websocket.py
View file @
86cf253e
# ~*~ coding: utf-8 ~*~
# ~*~ coding: utf-8 ~*~
import
re
import
select
import
select
import
threading
import
threading
import
socket
import
collections
import
collections
import
json
import
json
import
logging
import
logging
import
paramiko
import
paramiko
import
time
from
flask
import
request
,
g
from
flask
import
request
,
g
from
jms.utils
import
TtyIOParser
from
jms.utils
import
to_dotmap
from
..
import
app
,
socket_io
from
..
import
app
,
socket_io
from
..nav
import
nav
from
..nav
import
nav
from
..tasks
import
command_queue
,
record_queue
from
..proxy
import
ProxyServer
from
..channel
import
ProxyChannel
clients
=
app
.
clients
clients
=
app
.
clients
logger
=
logging
.
getLogger
(
__file__
)
logger
=
logging
.
getLogger
(
__file__
)
...
@@ -26,6 +24,58 @@ __all__ = [
...
@@ -26,6 +24,58 @@ __all__ = [
]
]
@socket_io.on
(
'nav'
)
def
handle_api
():
socket_io
.
emit
(
'nav'
,
json
.
dumps
(
nav
))
@socket_io.on
(
'connect'
,
namespace
=
'/'
)
def
handle_term_connect
():
clients
[
request
.
sid
]
=
collections
.
defaultdict
(
dict
)
@socket_io.on
(
'machine'
)
def
handle_machine
(
message
):
sid
=
request
.
sid
clients
[
sid
][
'host'
]
=
host
=
'120.25.240.109'
clients
[
sid
][
'port'
]
=
port
=
8022
user
=
to_dotmap
({
'username'
:
'root'
,
'name'
:
'redhat'
})
asset
=
to_dotmap
({
'hostname'
:
host
,
'ip'
:
host
,
'port'
:
8022
})
# win_width = request.cookies.get('col')
# win_height = request.cookies.get('row')
system_user
=
to_dotmap
({
'name'
:
'jms'
,
'username'
:
'jms'
,
'id'
:
102
})
clients
[
sid
][
'proxy_chan'
]
=
proxy_chan
=
ProxyChannel
(
sid
)
# proxy_chan.set_win_size((win_width, win_height))
proxy_server
=
ProxyServer
(
app
,
user
,
asset
,
system_user
,
proxy_chan
)
socket_io
.
start_background_task
(
proxy_server
.
proxy
)
# t = threading.Thread(target=proxy_server.proxy, args=())
# t.daemon = True
# t.start()
@socket_io.on
(
'data'
)
def
handle_data
(
message
):
sid
=
request
.
sid
logger
.
debug
(
'Receive data:
%
s'
%
message
)
if
clients
[
sid
][
'proxy_chan'
]:
print
(
'Sending to client channel'
)
clients
[
sid
][
'proxy_chan'
]
.
write
(
message
)
@socket_io.on
(
'disconnect'
)
def
handle_term_disconnect
():
sid
=
request
.
sid
del
clients
[
sid
]
print
(
'term disconnect'
)
@socket_io.on
(
'resize'
)
def
handle_term_resize
(
json
):
sid
=
request
.
sid
logger
.
debug
(
'Resize term:
%
s'
%
json
)
"""
@socket_io.on('nav')
@socket_io.on('nav')
def handle_api(sid):
def handle_api(sid):
socket_io.emit('nav', json.dumps(nav), room=sid)
socket_io.emit('nav', json.dumps(nav), room=sid)
...
@@ -40,17 +90,25 @@ def handle_term_connect(sid, environ):
...
@@ -40,17 +90,25 @@ def handle_term_connect(sid, environ):
def handle_machine(sid, message):
def handle_machine(sid, message):
clients[sid]['host'] = host = '120.25.240.109'
clients[sid]['host'] = host = '120.25.240.109'
clients[sid]['port'] = port = 8022
clients[sid]['port'] = port = 8022
t
=
threading
.
Thread
(
target
=
forward
,
args
=
(
sid
,))
user = to_dotmap({'username': 'root', 'name': 'redhat'})
asset = to_dotmap({'hostname': host, 'ip': host, 'port': 8022})
# win_width = request.cookies.get('col')
# win_height = request.cookies.get('row')
system_user = to_dotmap({'name': 'jms', 'username': 'jms', 'id': 102})
clients[sid]['proxy_chan'] = proxy_chan = ProxyChannel(sid)
# proxy_chan.set_win_size((win_width, win_height))
proxy_server = ProxyServer(app, user, asset, system_user, proxy_chan)
t = threading.Thread(target=proxy_server.proxy, args=())
t.daemon = True
t.daemon = True
t.start()
t.start()
socket_io
.
emit
(
'data'
,
'Connect to
%
s:
%
s
\r\n
'
%
(
host
,
port
),
room
=
sid
)
@socket_io.on('data')
@socket_io.on('data')
def handle_data(sid, message):
def handle_data(sid, message):
logger.debug('Receive data:
%
s'
%
message)
logger.debug('Receive data:
%
s'
%
message)
if
clients
[
sid
][
'chan'
]:
if clients[sid]['proxy_chan']:
clients
[
sid
][
'chan'
]
.
send
(
message
)
print('Sending to client channel')
clients[sid]['proxy_chan'].write(message)
@socket_io.on('disconnect')
@socket_io.on('disconnect')
...
@@ -62,11 +120,12 @@ def handle_term_disconnect(sid):
...
@@ -62,11 +120,12 @@ def handle_term_disconnect(sid):
@socket_io.on('resize')
@socket_io.on('resize')
def handle_term_resize(sid, json):
def handle_term_resize(sid, json):
logger.debug('Resize term:
%
s'
%
json)
logger.debug('Resize term:
%
s'
%
json)
"""
### Only for test ###
def
forward
(
sid
):
def
forward
(
sid
):
print
(
request
)
try
:
try
:
host
=
clients
[
sid
][
'host'
]
host
=
clients
[
sid
][
'host'
]
port
=
clients
[
sid
][
'port'
]
port
=
clients
[
sid
][
'port'
]
...
@@ -86,3 +145,14 @@ def forward(sid):
...
@@ -86,3 +145,14 @@ def forward(sid):
break
break
socket_io
.
emit
(
'data'
,
data
,
room
=
sid
)
socket_io
.
emit
(
'data'
,
data
,
room
=
sid
)
del
clients
[
sid
]
del
clients
[
sid
]
def
handle
(
p
):
while
True
:
r
,
w
,
x
=
select
.
select
([
p
],
[],
[])
if
p
in
r
:
data
=
p
.
recv
(
1024
)
if
len
(
data
)
==
0
:
break
print
(
"Recieve client:
%
s"
%
data
)
run_server.py
View file @
86cf253e
#!/usr/bin/env python
#!/usr/bin/env python
# ~*~ coding: utf-8 ~*~
# ~*~ coding: utf-8 ~*~
from
luna
import
app
import
sys
import
os
import
os
from
luna
import
app
,
socket_io
host
=
app
.
config
[
'BIND_HOST'
]
host
=
app
.
config
[
'BIND_HOST'
]
port
=
app
.
config
[
'LISTEN_PORT'
]
port
=
app
.
config
[
'LISTEN_PORT'
]
...
@@ -17,7 +18,9 @@ if __name__ == '__main__':
...
@@ -17,7 +18,9 @@ if __name__ == '__main__':
pass
pass
try
:
try
:
app
.
run
(
threaded
=
True
,
host
=
host
,
port
=
port
)
socket_io
.
run
(
app
)
# app.run(threaded=True, host=host, port=port)
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
app
.
stop
()
app
.
stop
()
sys
.
exit
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment