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
e2c18bb8
Unverified
Commit
e2c18bb8
authored
May 30, 2019
by
BaiJiangJie
Committed by
GitHub
May 30, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #218 from jumpserver/dev
Dev
parents
1b7446ca
a38b9c1f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
17 deletions
+34
-17
connection.py
coco/connection.py
+33
-16
proxy.py
coco/proxy.py
+1
-1
No files found.
coco/connection.py
View file @
e2c18bb8
...
@@ -39,6 +39,9 @@ class SSHConnection:
...
@@ -39,6 +39,9 @@ class SSHConnection:
connection
=
cls
.
connections
.
get
(
key
)
connection
=
cls
.
connections
.
get
(
key
)
if
not
connection
:
if
not
connection
:
return
None
return
None
if
not
connection
.
is_active
:
cls
.
connections
.
pop
(
key
,
None
)
return
None
connection
.
ref
+=
1
connection
.
ref
+=
1
return
connection
return
connection
...
@@ -59,6 +62,8 @@ class SSHConnection:
...
@@ -59,6 +62,8 @@ class SSHConnection:
)
)
return
connection
return
connection
connection
=
cls
(
user
,
asset
,
system_user
)
connection
=
cls
(
user
,
asset
,
system_user
)
connection
.
connect
()
if
connection
.
is_active
:
cls
.
set_connection_to_cache
(
connection
)
cls
.
set_connection_to_cache
(
connection
)
return
connection
return
connection
...
@@ -72,6 +77,7 @@ class SSHConnection:
...
@@ -72,6 +77,7 @@ class SSHConnection:
self
.
asset
=
asset
self
.
asset
=
asset
self
.
system_user
=
system_user
self
.
system_user
=
system_user
self
.
client
=
None
self
.
client
=
None
self
.
transport
=
None
self
.
sock
=
None
self
.
sock
=
None
self
.
error
=
""
self
.
error
=
""
self
.
ref
=
1
self
.
ref
=
1
...
@@ -86,7 +92,7 @@ class SSHConnection:
...
@@ -86,7 +92,7 @@ class SSHConnection:
self
.
system_user
.
password
=
password
self
.
system_user
.
password
=
password
self
.
system_user
.
private_key
=
private_key
self
.
system_user
.
private_key
=
private_key
def
get_ssh_clien
t
(
self
):
def
connec
t
(
self
):
ssh
=
paramiko
.
SSHClient
()
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
sock
=
None
sock
=
None
...
@@ -122,6 +128,7 @@ class SSHConnection:
...
@@ -122,6 +128,7 @@ class SSHConnection:
)
)
transport
=
ssh
.
get_transport
()
transport
=
ssh
.
get_transport
()
transport
.
set_keepalive
(
20
)
transport
.
set_keepalive
(
20
)
self
.
transport
=
transport
except
Exception
as
e
:
except
Exception
as
e
:
password_short
=
"None"
password_short
=
"None"
key_fingerprint
=
"None"
key_fingerprint
=
"None"
...
@@ -144,37 +151,44 @@ class SSHConnection:
...
@@ -144,37 +151,44 @@ class SSHConnection:
self
.
sock
=
ssh
self
.
sock
=
ssh
self
.
error
=
error
self
.
error
=
error
def
reconnect_if_need
(
self
):
if
not
self
.
is_active
:
self
.
connect
()
if
self
.
is_active
:
return
True
return
False
def
get_transport
(
self
):
def
get_transport
(
self
):
if
not
self
.
client
:
if
self
.
reconnect_if_need
():
self
.
get_ssh_client
()
return
self
.
transport
if
not
self
.
client
:
return
self
.
client
.
get_transport
()
else
:
return
None
return
None
def
get_channel
(
self
,
term
=
"xterm"
,
width
=
80
,
height
=
24
):
def
get_channel
(
self
,
term
=
"xterm"
,
width
=
80
,
height
=
24
):
if
not
self
.
client
:
if
self
.
reconnect_if_need
():
self
.
get_ssh_client
()
if
self
.
client
:
chan
=
self
.
client
.
invoke_shell
(
term
,
width
=
width
,
height
=
height
)
chan
=
self
.
client
.
invoke_shell
(
term
,
width
=
width
,
height
=
height
)
return
chan
return
chan
else
:
else
:
return
None
return
None
def
get_sftp
(
self
):
def
get_sftp
(
self
):
if
not
self
.
client
:
if
self
.
reconnect_if_need
():
self
.
get_ssh_client
()
if
self
.
client
:
return
self
.
client
.
open_sftp
()
return
self
.
client
.
open_sftp
()
else
:
else
:
return
None
return
None
@property
def
is_active
(
self
):
return
self
.
transport
and
self
.
transport
.
is_active
()
def
close
(
self
):
def
close
(
self
):
if
self
.
ref
>
1
:
if
self
.
ref
>
1
:
self
.
ref
-=
1
self
.
ref
-=
1
logger
.
debug
(
"Connection ref -1: {}->{}@{}"
.
format
(
msg
=
"Connection ref -1: {}->{}@{}. {}"
.
format
(
self
.
user
.
username
,
self
.
asset
.
hostname
,
self
.
system_user
.
username
)
self
.
user
.
username
,
self
.
asset
.
hostname
,
self
.
system_user
.
username
,
self
.
ref
)
)
logger
.
debug
(
msg
)
return
return
self
.
__class__
.
remove_ssh_connection
(
self
)
self
.
__class__
.
remove_ssh_connection
(
self
)
try
:
try
:
...
@@ -183,9 +197,12 @@ class SSHConnection:
...
@@ -183,9 +197,12 @@ class SSHConnection:
self
.
sock
.
close
()
self
.
sock
.
close
()
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
error
(
"Close connection error: "
,
e
)
logger
.
error
(
"Close connection error: "
,
e
)
logger
.
debug
(
"Close connection: {}->{}@{}"
.
format
(
self
.
user
.
username
,
self
.
asset
.
ip
,
self
.
system_user
.
username
)
msg
=
"Close connection: {}->{}@{}. Total connections live: {}"
.
format
(
self
.
user
.
username
,
self
.
asset
.
ip
,
self
.
system_user
.
username
,
len
(
self
.
connections
)
)
)
logger
.
debug
(
msg
)
@staticmethod
@staticmethod
def
get_proxy_sock_v2
(
asset
):
def
get_proxy_sock_v2
(
asset
):
...
...
coco/proxy.py
View file @
e2c18bb8
...
@@ -148,7 +148,7 @@ class ProxyServer:
...
@@ -148,7 +148,7 @@ class ProxyServer:
conn
=
SSHConnection
.
new_connection_from_cache
(
conn
=
SSHConnection
.
new_connection_from_cache
(
self
.
client
.
user
,
self
.
asset
,
self
.
system_user
self
.
client
.
user
,
self
.
asset
,
self
.
system_user
)
)
if
not
conn
:
if
not
conn
or
not
conn
.
is_active
:
return
None
return
None
else
:
else
:
conn
=
SSHConnection
.
new_connection
(
conn
=
SSHConnection
.
new_connection
(
...
...
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