Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
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
koko
Commits
334fc5b1
Commit
334fc5b1
authored
Aug 19, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update]add ssh keep-alive
parent
79402cfe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
3 deletions
+38
-3
connmanager.go
pkg/srvconn/connmanager.go
+38
-3
No files found.
pkg/srvconn/connmanager.go
View file @
334fc5b1
...
@@ -37,6 +37,8 @@ type SSHClient struct {
...
@@ -37,6 +37,8 @@ type SSHClient struct {
ref
int
ref
int
key
string
key
string
mu
*
sync
.
RWMutex
mu
*
sync
.
RWMutex
closed
chan
struct
{}
}
}
func
(
s
*
SSHClient
)
refCount
()
int
{
func
(
s
*
SSHClient
)
refCount
()
int
{
...
@@ -67,9 +69,35 @@ func (s *SSHClient) Close() error {
...
@@ -67,9 +69,35 @@ func (s *SSHClient) Close() error {
if
s
.
ref
>
1
{
if
s
.
ref
>
1
{
return
nil
return
nil
}
}
select
{
case
<-
s
.
closed
:
return
nil
default
:
close
(
s
.
closed
)
}
return
s
.
client
.
Close
()
return
s
.
client
.
Close
()
}
}
func
KeepAlive
(
c
*
gossh
.
Client
,
closed
<-
chan
struct
{},
keepInterval
time
.
Duration
)
{
t
:=
time
.
NewTicker
(
keepInterval
*
time
.
Second
)
defer
t
.
Stop
()
logger
.
Debugf
(
"SSH client %p keep alive start"
,
c
)
defer
logger
.
Debugf
(
"SSH client %p keep alive stop"
,
c
)
for
{
select
{
case
<-
closed
:
return
case
<-
t
.
C
:
_
,
_
,
err
:=
c
.
SendRequest
(
"keepalive@jumpserver.org"
,
true
,
nil
)
if
err
!=
nil
{
logger
.
Error
(
"SSH client %p keep alive err: "
,
c
,
err
.
Error
())
return
}
}
}
}
type
SSHClientConfig
struct
{
type
SSHClientConfig
struct
{
Host
string
`json:"host"`
Host
string
`json:"host"`
Port
string
`json:"port"`
Port
string
`json:"port"`
...
@@ -207,7 +235,13 @@ func newClient(asset *model.Asset, systemUser *model.SystemUser, timeout time.Du
...
@@ -207,7 +235,13 @@ func newClient(asset *model.Asset, systemUser *model.SystemUser, timeout time.Du
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
return
&
SSHClient
{
client
:
conn
,
username
:
systemUser
.
Username
,
mu
:
new
(
sync
.
RWMutex
)},
err
closed
:=
make
(
chan
struct
{})
go
KeepAlive
(
conn
,
closed
,
60
)
return
&
SSHClient
{
client
:
conn
,
username
:
systemUser
.
Username
,
mu
:
new
(
sync
.
RWMutex
),
closed
:
closed
,},
nil
}
}
func
NewClient
(
user
*
model
.
User
,
asset
*
model
.
Asset
,
systemUser
*
model
.
SystemUser
,
timeout
time
.
Duration
,
func
NewClient
(
user
*
model
.
User
,
asset
*
model
.
Asset
,
systemUser
*
model
.
SystemUser
,
timeout
time
.
Duration
,
...
@@ -265,11 +299,12 @@ func RecycleClient(client *SSHClient) {
...
@@ -265,11 +299,12 @@ func RecycleClient(client *SSHClient) {
clientLock
.
Unlock
()
clientLock
.
Unlock
()
err
:=
client
.
Close
()
err
:=
client
.
Close
()
if
err
!=
nil
{
if
err
!=
nil
{
logger
.
Info
(
"Failed to close client err: "
,
err
.
Error
())
logger
.
Error
(
"Failed to close client err: "
,
err
.
Error
())
}
else
{
}
else
{
logger
.
Debug
(
"Success to close client"
)
logger
.
Debug
(
"Success to close client"
)
}
}
default
:
default
:
client
.
decreaseRef
()
client
.
decreaseRef
()
logger
.
Debugf
(
"Reuse client %p Current ref: %d"
,
client
,
client
.
refCount
())
}
}
}
}
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