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
e932076f
Commit
e932076f
authored
May 18, 2018
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 优化coco
parent
b5221652
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
40 deletions
+46
-40
interactive.py
coco/interactive.py
+38
-35
proxy.py
coco/proxy.py
+7
-4
session.py
coco/session.py
+1
-1
No files found.
coco/interactive.py
View file @
e932076f
...
...
@@ -6,14 +6,11 @@ import socket
import
threading
import
os
from
jms.models
import
Asset
,
AssetGroup
from
.
import
char
from
.utils
import
wrap_with_line_feed
as
wr
,
wrap_with_title
as
title
,
\
wrap_with_warning
as
warning
,
is_obj_attr_has
,
\
is_obj_attr_eq
,
sort_assets
,
TtyIOParser
,
\
ugettext
as
_
,
get_logger
,
net_input
,
format_with_zh
,
item_max_length
,
\
size_of_str_with_zh
wrap_with_warning
as
warning
,
is_obj_attr_has
,
is_obj_attr_eq
,
\
sort_assets
,
ugettext
as
_
,
get_logger
,
net_input
,
format_with_zh
,
\
item_max_length
,
size_of_str_with_zh
from
.ctx
import
current_app
,
app_service
from
.proxy
import
ProxyServer
...
...
@@ -28,7 +25,7 @@ class InteractiveServer:
self
.
request
=
client
.
request
self
.
assets
=
None
self
.
_search_result
=
None
self
.
asset_group
s
=
None
self
.
node
s
=
None
self
.
get_user_assets_async
()
self
.
get_user_nodes_async
()
...
...
@@ -58,8 +55,8 @@ class InteractiveServer:
1) 输入 {green}ID{end} 直接登录 或 输入{green}部分 IP,主机名,备注{end} 进行搜索登录(如果唯一).
\r
2) 输入 {green}/{end} + {green}IP, 主机名{end} or {green}备注 {end}搜索. 如: /ip
\r
3) 输入 {green}p{end} 显示您有权限的主机.
\r
4) 输入 {green}g{end} 显示您有权限的
主机组.
\r
5) 输入 {green}g{end} + {green}组ID{end} 显示
该
节点下主机. 如: g1
\r
4) 输入 {green}g{end} 显示您有权限的
节点
\r
5) 输入 {green}g{end} + {green}组ID{end} 显示节点下主机. 如: g1
\r
6) 输入 {green}h{end} 帮助.
\r
0) 输入 {green}q{end} 退出.
\r\n
"""
)
.
format
(
title
=
"
\033
[1;32m"
,
green
=
"
\033
[32m"
,
...
...
@@ -75,9 +72,9 @@ class InteractiveServer:
elif
opt
in
[
'p'
,
'P'
,
''
]:
self
.
display_assets
()
elif
opt
in
[
'g'
,
'G'
]:
self
.
display_
asset_group
s
()
self
.
display_
node
s
()
elif
opt
.
startswith
(
"g"
)
and
opt
.
lstrip
(
"g"
)
.
isdigit
():
self
.
display_
group
_assets
(
int
(
opt
.
lstrip
(
"g"
)))
self
.
display_
node
_assets
(
int
(
opt
.
lstrip
(
"g"
)))
elif
opt
in
[
'q'
,
'Q'
,
'exit'
,
'quit'
]:
return
self
.
_sentinel
elif
opt
in
[
'h'
,
'H'
]:
...
...
@@ -94,18 +91,21 @@ class InteractiveServer:
if
q
==
''
:
result
=
self
.
assets
# 用户输入的是数字,可能想使用id唯一键搜索
elif
q
.
isdigit
()
and
self
.
search_result
and
len
(
self
.
search_result
)
>=
int
(
q
):
elif
q
.
isdigit
()
and
self
.
search_result
and
\
len
(
self
.
search_result
)
>=
int
(
q
):
result
=
[
self
.
search_result
[
int
(
q
)
-
1
]]
# 全匹配到则直接返回全匹配的
if
len
(
result
)
==
0
:
_result
=
[
asset
for
asset
in
self
.
assets
if
is_obj_attr_eq
(
asset
,
q
)]
_result
=
[
asset
for
asset
in
self
.
assets
if
is_obj_attr_eq
(
asset
,
q
)]
if
len
(
_result
)
==
1
:
result
=
_result
# 最后模糊匹配
if
len
(
result
)
==
0
:
result
=
[
asset
for
asset
in
self
.
assets
if
is_obj_attr_has
(
asset
,
q
)]
result
=
[
asset
for
asset
in
self
.
assets
if
is_obj_attr_has
(
asset
,
q
)]
self
.
search_result
=
result
...
...
@@ -116,17 +116,17 @@ class InteractiveServer:
"""
self
.
search_and_display
(
''
)
def
display_
asset_group
s
(
self
):
if
self
.
asset_group
s
is
None
:
def
display_
node
s
(
self
):
if
self
.
node
s
is
None
:
self
.
get_user_nodes
()
if
len
(
self
.
asset_group
s
)
==
0
:
if
len
(
self
.
node
s
)
==
0
:
self
.
client
.
send
(
warning
(
_
(
"无"
)))
return
id_length
=
max
(
len
(
str
(
len
(
self
.
asset_group
s
))),
5
)
name_length
=
item_max_length
(
self
.
asset_group
s
,
15
,
key
=
lambda
x
:
x
.
name
)
amount_length
=
item_max_length
(
self
.
asset_group
s
,
10
,
id_length
=
max
(
len
(
str
(
len
(
self
.
node
s
))),
5
)
name_length
=
item_max_length
(
self
.
node
s
,
15
,
key
=
lambda
x
:
x
.
name
)
amount_length
=
item_max_length
(
self
.
node
s
,
10
,
key
=
lambda
x
:
x
.
assets_amount
)
size_list
=
[
id_length
,
name_length
,
amount_length
]
fake_data
=
[
'ID'
,
_
(
"Name"
),
_
(
"Assets"
)]
...
...
@@ -140,18 +140,18 @@ class InteractiveServer:
fake_data
.
append
(
_
(
"Comment"
))
self
.
client
.
send
(
title
(
format_with_zh
(
size_list
,
*
fake_data
)))
for
index
,
group
in
enumerate
(
self
.
asset_group
s
,
1
):
for
index
,
group
in
enumerate
(
self
.
node
s
,
1
):
data
=
[
index
,
group
.
name
,
group
.
assets_amount
,
group
.
comment
]
self
.
client
.
send
(
wr
(
format_with_zh
(
size_list
,
*
data
)))
self
.
client
.
send
(
wr
(
_
(
"总共: {}"
)
.
format
(
len
(
self
.
asset_group
s
)),
before
=
1
))
self
.
client
.
send
(
wr
(
_
(
"总共: {}"
)
.
format
(
len
(
self
.
node
s
)),
before
=
1
))
def
display_
group
_assets
(
self
,
_id
):
if
_id
>
len
(
self
.
asset_group
s
)
or
_id
<=
0
:
def
display_
node
_assets
(
self
,
_id
):
if
_id
>
len
(
self
.
node
s
)
or
_id
<=
0
:
self
.
client
.
send
(
wr
(
warning
(
"没有匹配分组,请重新输入"
)))
self
.
display_
asset_group
s
()
self
.
display_
node
s
()
return
self
.
search_result
=
self
.
asset_group
s
[
_id
-
1
]
.
assets_granted
self
.
search_result
=
self
.
node
s
[
_id
-
1
]
.
assets_granted
self
.
display_search_result
()
def
display_search_result
(
self
):
...
...
@@ -188,7 +188,7 @@ class InteractiveServer:
self
.
display_search_result
()
def
get_user_nodes
(
self
):
self
.
asset_group
s
=
app_service
.
get_user_asset_groups
(
self
.
client
.
user
)
self
.
node
s
=
app_service
.
get_user_asset_groups
(
self
.
client
.
user
)
def
get_user_nodes_async
(
self
):
thread
=
threading
.
Thread
(
target
=
self
.
get_user_nodes
)
...
...
@@ -198,30 +198,31 @@ class InteractiveServer:
def
filter_system_users
(
assets
):
for
asset
in
assets
:
system_users_granted
=
asset
.
system_users_granted
high_priority
=
max
([
s
.
priority
for
s
in
system_users_granted
])
if
system_users_granted
else
1
system_users_cleaned
=
[
s
for
s
in
system_users_granted
if
s
.
priority
==
high_priority
]
high_priority
=
max
([
s
.
priority
for
s
in
system_users_granted
])
\
if
system_users_granted
else
1
system_users_cleaned
=
[
s
for
s
in
system_users_granted
if
s
.
priority
==
high_priority
]
asset
.
system_users_granted
=
system_users_cleaned
return
assets
def
get_user_assets
(
self
):
self
.
assets
=
app_service
.
get_user_assets
(
self
.
client
.
user
)
logger
.
debug
(
"Get user {} assets total: {}"
.
format
(
self
.
client
.
user
,
len
(
self
.
assets
)))
logger
.
debug
(
"Get user {} assets total: {}"
.
format
(
self
.
client
.
user
,
len
(
self
.
assets
))
)
def
get_user_assets_async
(
self
):
thread
=
threading
.
Thread
(
target
=
self
.
get_user_assets
)
thread
.
start
()
def
choose_system_user
(
self
,
system_users
):
# highest_priority = max([s.priority for s in system_users])
# system_users = [s for s in system_users if s == highest_priority]
if
len
(
system_users
)
==
1
:
return
system_users
[
0
]
elif
len
(
system_users
)
==
0
:
return
None
while
True
:
self
.
client
.
send
(
wr
(
_
(
"选择一个登
陆
: "
),
after
=
1
))
self
.
client
.
send
(
wr
(
_
(
"选择一个登
录
: "
),
after
=
1
))
self
.
display_system_users
(
system_users
)
opt
=
net_input
(
self
.
client
,
prompt
=
"ID> "
)
if
opt
.
isdigit
()
and
len
(
system_users
)
>
int
(
opt
):
...
...
@@ -242,7 +243,9 @@ class InteractiveServer:
if
self
.
search_result
and
len
(
self
.
search_result
)
==
1
:
asset
=
self
.
search_result
[
0
]
if
asset
.
platform
==
"Windows"
:
self
.
client
.
send
(
warning
(
_
(
"终端不支持登录windows, 请使用web terminal访问"
)))
self
.
client
.
send
(
warning
(
_
(
"终端不支持登录windows, 请使用web terminal访问"
))
)
return
self
.
proxy
(
asset
)
else
:
...
...
coco/proxy.py
View file @
e932076f
...
...
@@ -90,8 +90,9 @@ class ProxyServer:
width
=
request
.
meta
.
get
(
'width'
,
80
)
height
=
request
.
meta
.
get
(
'height'
,
24
)
ssh
=
SSHConnection
()
chan
,
msg
=
ssh
.
get_channel
(
asset
,
system_user
,
term
=
term
,
width
=
width
,
height
=
height
)
chan
,
msg
=
ssh
.
get_channel
(
asset
,
system_user
,
term
=
term
,
width
=
width
,
height
=
height
)
if
not
chan
:
self
.
client
.
send
(
warning
(
wr
(
msg
,
before
=
1
,
after
=
0
)))
server
=
None
...
...
@@ -125,9 +126,11 @@ class ProxyServer:
def
send_connecting_message
(
self
,
asset
,
system_user
):
def
func
():
delay
=
0.0
self
.
client
.
send
(
'Connecting to {}@{} {:.1f}'
.
format
(
system_user
,
asset
,
delay
))
self
.
client
.
send
(
'Connecting to {}@{} {:.1f}'
.
format
(
system_user
,
asset
,
delay
)
)
while
self
.
connecting
and
delay
<
TIMEOUT
:
self
.
client
.
send
(
'
\x08\x08\x08
{:.1f}'
.
format
(
delay
)
.
encode
(
'utf-8'
))
self
.
client
.
send
(
'
\x08\x08\x08
{:.1f}'
.
format
(
delay
)
.
encode
())
time
.
sleep
(
0.1
)
delay
+=
0.1
thread
=
threading
.
Thread
(
target
=
func
)
...
...
coco/session.py
View file @
e932076f
...
...
@@ -40,7 +40,7 @@ class Session:
"""
logger
.
info
(
"Session add watcher: {} -> {} "
.
format
(
self
.
id
,
watcher
))
if
not
silent
:
watcher
.
send
(
"Welcome to watch session {}
\r\n
"
.
format
(
self
.
id
)
.
encode
(
"utf-8"
))
watcher
.
send
(
"Welcome to watch session {}
\r\n
"
.
format
(
self
.
id
)
.
encode
())
self
.
sel
.
register
(
watcher
,
selectors
.
EVENT_READ
)
self
.
_watchers
.
append
(
watcher
)
...
...
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