Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
coco
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
coco
Commits
3b277ec4
Commit
3b277ec4
authored
Dec 10, 2017
by
liuzheng712
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: flask test
parent
12fa847a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
208 additions
and
49 deletions
+208
-49
httpd.py
coco/httpd.py
+208
-49
No files found.
coco/httpd.py
View file @
3b277ec4
#!/usr/bin/env python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
#
import
io
import
socket
import
os
import
json
import
paramiko
import
logging
import
logging
from
flask_socketio
import
SocketIO
,
Namespace
,
emit
import
tornado.web
from
flask
import
Flask
,
send_from_directory
,
render_template
,
request
,
jsonify
import
tornado.websocket
import
tornado.httpclient
import
tornado.ioloop
import
tornado.gen
# Todo: Remove for future
# Todo: Remove for future
from
jms.models
import
User
from
jms.models
import
User
from
.models
import
Request
,
Client
,
WSProxy
from
.models
import
Request
,
Client
,
WSProxy
from
.interactive
import
InteractiveServer
from
.interactive
import
InteractiveServer
__version__
=
'0.4.0'
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
__file__
))
logger
=
logging
.
getLogger
(
__file__
)
logger
=
logging
.
getLogger
(
__file__
)
...
@@ -37,52 +36,57 @@ class BaseWebSocketHandler:
...
@@ -37,52 +36,57 @@ class BaseWebSocketHandler:
def
check_origin
(
self
,
origin
):
def
check_origin
(
self
,
origin
):
return
True
return
True
def
write_message
(
self
,
data
):
self
.
emit
(
'data'
,
data
[
'data'
])
class
InteractiveWebSocketHandler
(
BaseWebSocketHandler
,
tornado
.
websocket
.
WebSocketHandler
):
@tornado.web.authenticated
def
open
(
self
):
InteractiveServer
(
self
.
app
,
self
.
client
)
.
interact_async
()
def
on_message
(
self
,
message
):
class
SSHws
(
Namespace
,
BaseWebSocketHandler
):
try
:
def
ssh_with_password
(
self
):
message
=
json
.
loads
(
message
)
self
.
ssh
=
paramiko
.
SSHClient
()
except
json
.
JSONDecodeError
:
self
.
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
logger
.
info
(
"Loads websocket json message failed"
)
self
.
ssh
.
connect
(
"127.0.0.1"
,
22
,
"liuzheng"
,
"liuzheng"
)
return
self
.
chan
=
self
.
ssh
.
invoke_shell
(
term
=
'xterm'
,
width
=
self
.
cols
,
height
=
self
.
raws
)
self
.
socketio
.
start_background_task
(
self
.
send_data
)
# self.chan.settimeout(0.1)
if
message
.
get
(
'event'
):
def
send_data
(
self
):
self
.
evt_handle
(
message
)
while
True
:
elif
message
.
get
(
'data'
):
data
=
self
.
chan
.
recv
(
2048
)
.
decode
(
'utf-8'
,
'replace'
)
self
.
proxy
.
send
(
message
)
print
(
data
)
self
.
emit
(
'data'
,
data
)
def
on_close
(
self
):
def
on_connect
(
self
):
self
.
proxy
.
close
()
self
.
cols
=
int
(
request
.
cookies
.
get
(
'cols'
,
80
))
self
.
raws
=
int
(
request
.
cookies
.
get
(
'raws'
,
24
))
InteractiveServer
(
self
.
app
,
self
.
client
)
.
interact_async
()
def
evt_handle
(
self
,
data
):
def
on_data
(
self
,
message
):
if
data
[
'event'
]
==
'change_size'
:
# self.chan.send(message)
try
:
# while not self.chan.recv_ready():
self
.
request
.
meta
[
'width'
]
=
data
[
'meta'
][
'width'
]
self
.
proxy
.
send
(
message
)
self
.
request
.
meta
[
'height'
]
=
data
[
'meta'
][
'height'
]
# emit('data', self.chan.recv(2048).decode('utf-8', 'replace'))
self
.
request
.
change_size_event
.
set
()
except
KeyError
:
pass
# def on_event(self, message):
# self.evt_handle(message)
class
ProxyWebSocketHandler
(
BaseWebSocketHandler
):
def
on_host
(
self
,
message
):
pass
print
(
message
)
def
on_resize
(
self
,
message
):
# self.cols = message.get('cols', 80)
# self.raws = message.get('raws', 24)
# self.chan.resize_pty(width=self.cols, height=self.raws)
self
.
request
.
meta
[
'width'
]
=
message
.
get
(
'cols'
,
80
)
self
.
request
.
meta
[
'height'
]
=
message
.
get
(
'raws'
,
24
)
self
.
request
.
change_size_event
.
set
()
class
MonitorWebSocketHandler
(
BaseWebSocketHandler
):
def
on_disconnect
(
self
):
pass
self
.
proxy
.
close
()
# self.ssh.close()
pass
class
HttpServer
:
class
HttpServer
:
routers
=
[
(
r'/ws/interactive/'
,
InteractiveWebSocketHandler
),
(
r'/ws/proxy/(?P<asset_id>[0-9]+)/(?P<system_user_id>[0-9]+)/'
,
ProxyWebSocketHandler
),
(
r'/ws/session/(?P<session_id>[0-9]+)/monitor/'
,
MonitorWebSocketHandler
),
]
# prepare may be rewrite it
# prepare may be rewrite it
settings
=
{
settings
=
{
'cookie_secret'
:
''
,
'cookie_secret'
:
''
,
...
@@ -92,19 +96,174 @@ class HttpServer:
...
@@ -92,19 +96,174 @@ class HttpServer:
def
__init__
(
self
,
app
):
def
__init__
(
self
,
app
):
self
.
app
=
app
self
.
app
=
app
self
.
_prepare
()
def
_prepare
(
self
):
self
.
settings
[
'cookie_secret'
]
=
self
.
app
.
config
[
'SECRET_KEY'
]
self
.
settings
[
'cookie_secret'
]
=
self
.
app
.
config
[
'SECRET_KEY'
]
self
.
settings
[
'app'
]
=
self
.
app
self
.
settings
[
'app'
]
=
self
.
app
self
.
flask
=
Flask
(
__name__
,
template_folder
=
'dist'
)
self
.
flask
.
config
[
'SECRET_KEY'
]
=
self
.
app
.
config
[
'SECRET_KEY'
]
self
.
socketio
=
SocketIO
()
def
run
(
self
):
def
run
(
self
):
host
=
self
.
app
.
config
[
"BIND_HOST"
]
host
=
self
.
app
.
config
[
"BIND_HOST"
]
port
=
self
.
app
.
config
[
"HTTPD_PORT"
]
port
=
self
.
app
.
config
[
"HTTPD_PORT"
]
print
(
'Starting websocket server at {}:{}'
.
format
(
host
,
port
))
print
(
'Starting websocket server at {}:{}'
.
format
(
host
,
port
))
ws
=
tornado
.
web
.
Application
(
self
.
routers
,
**
self
.
settings
)
self
.
socketio
.
on_namespace
(
SSHws
(
'/ssh'
)
.
prepare
()
)
ws
.
listen
(
port
=
port
,
address
=
host
)
self
.
socketio
.
init_app
(
self
.
flask
)
tornado
.
ioloop
.
IOLoop
.
current
()
.
start
(
)
self
.
socketio
.
run
(
self
.
flask
,
port
=
port
,
address
=
host
)
def
shutdown
(
self
):
def
shutdown
(
self
):
pass
pass
if
__name__
==
"__main__"
:
app
=
Flask
(
__name__
,
template_folder
=
'/Users/liuzheng/gitproject/Jumpserver/webterminal/dist'
)
@app.route
(
'/luna/<path:path>'
)
def
send_js
(
path
):
return
send_from_directory
(
'/Users/liuzheng/gitproject/Jumpserver/webterminal/dist'
,
path
)
@app.route
(
'/'
)
@app.route
(
'/luna/'
)
def
index
():
return
render_template
(
'index.html'
)
@app.route
(
'/api/perms/v1/user/my/asset-groups-assets/'
)
def
asset_groups_assets
():
assets
=
[
{
"id"
:
0
,
"name"
:
"ungrouped"
,
"assets"
:
[]
},
{
"id"
:
1
,
"name"
:
"Default"
,
"comment"
:
"Default asset group"
,
"assets"
:
[
{
"id"
:
2
,
"hostname"
:
"192.168.1.6"
,
"ip"
:
"192.168.2.6"
,
"port"
:
22
,
"system"
:
"windows"
,
"uuid"
:
"xxxxxx"
,
"system_users"
:
[
{
"id"
:
1
,
"name"
:
"web"
,
"username"
:
"web"
,
"protocol"
:
"ssh"
,
"auth_method"
:
"P"
,
"auto_push"
:
True
}
]
},
{
"id"
:
4
,
"hostname"
:
"testserver123"
,
"ip"
:
"123.57.183.135"
,
"port"
:
8022
,
"system"
:
"linux"
,
"uuid"
:
"linux-xxlkjadf"
,
"system_users"
:
[
{
"id"
:
1
,
"name"
:
"web"
,
"username"
:
"web"
,
"protocol"
:
"ssh"
,
"auth_method"
:
"P"
,
"auto_push"
:
True
}
]
}
]
},
{
"id"
:
4
,
"name"
:
"java"
,
"comment"
:
""
,
"assets"
:
[
{
"id"
:
2
,
"hostname"
:
"192.168.1.6"
,
"ip"
:
"192.168.2.6"
,
"uuid"
:
"sadcascas"
,
"system"
:
"linux"
,
"port"
:
22
,
"system_users"
:
[
{
"id"
:
1
,
"name"
:
"web"
,
"username"
:
"web"
,
"protocol"
:
"ssh"
,
"auth_method"
:
"P"
,
"auto_push"
:
True
}
]
}
]
},
{
"id"
:
3
,
"name"
:
"数据库"
,
"comment"
:
""
,
"assets"
:
[
{
"id"
:
2
,
"hostname"
:
"192.168.1.6"
,
"ip"
:
"192.168.2.6"
,
"port"
:
22
,
"uuid"
:
"sadcascascasdcas"
,
"system"
:
"linux"
,
"system_users"
:
[
{
"id"
:
1
,
"name"
:
"web"
,
"username"
:
"web"
,
"protocol"
:
"ssh"
,
"auth_method"
:
"P"
,
"auto_push"
:
True
}
]
}
]
},
{
"id"
:
2
,
"name"
:
"运维组"
,
"comment"
:
""
,
"assets"
:
[
{
"id"
:
2
,
"hostname"
:
"192.168.1.6"
,
"ip"
:
"192.168.2.6"
,
"port"
:
22
,
"uuid"
:
"zxcasd"
,
"system"
:
"linux"
,
"system_users"
:
[
{
"id"
:
1
,
"name"
:
"web"
,
"username"
:
"web"
,
"protocol"
:
"ssh"
,
"auth_method"
:
"P"
,
"auto_push"
:
True
}
]
}
]
}
]
return
jsonify
(
assets
)
print
(
'socketio'
)
socketio
=
SocketIO
()
socketio
.
init_app
(
app
)
socketio
.
on_namespace
(
SSHws
(
'/ssh'
))
socketio
.
run
(
app
)
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