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
2de9b63c
Commit
2de9b63c
authored
Jul 18, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 优化ws
parent
bd3d0119
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
64 deletions
+48
-64
Dockerfile
Dockerfile
+1
-0
client.go
pkg/httpd/client.go
+7
-14
data.go
pkg/httpd/data.go
+3
-7
websshws.go
pkg/httpd/websshws.go
+37
-43
No files found.
Dockerfile
View file @
2de9b63c
...
@@ -5,6 +5,7 @@ ARG GOPROXY
...
@@ -5,6 +5,7 @@ ARG GOPROXY
ENV
GOPROXY=$GOPROXY
ENV
GOPROXY=$GOPROXY
ENV
GO111MODULE=on
ENV
GO111MODULE=on
COPY
. .
COPY
. .
RUN
cd
cmd
&&
go build koko.go
RUN
cd
cmd
&&
go build koko.go
FROM
alpine
FROM
alpine
...
...
pkg/httpd/client.go
View file @
2de9b63c
...
@@ -7,21 +7,16 @@ import (
...
@@ -7,21 +7,16 @@ import (
"github.com/gliderlabs/ssh"
"github.com/gliderlabs/ssh"
"github.com/kataras/neffos"
"github.com/kataras/neffos"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/model"
)
)
type
Client
struct
{
type
Client
struct
{
Uuid
string
Uuid
string
Cid
string
Cid
string
user
*
model
.
User
addr
string
addr
string
WinChan
chan
ssh
.
Window
WinChan
chan
ssh
.
Window
UserRead
io
.
Reader
UserRead
io
.
Reader
UserWrite
io
.
WriteCloser
UserWrite
io
.
WriteCloser
Conn
*
neffos
.
NSConn
Conn
*
neffos
.
NSConn
Closed
bool
Closed
bool
pty
ssh
.
Pty
pty
ssh
.
Pty
mu
*
sync
.
RWMutex
mu
*
sync
.
RWMutex
...
@@ -49,18 +44,13 @@ func (c *Client) Write(p []byte) (n int, err error) {
...
@@ -49,18 +44,13 @@ func (c *Client) Write(p []byte) (n int, err error) {
if
c
.
Closed
{
if
c
.
Closed
{
return
return
}
}
data
:=
DataMsg
{
Data
:
string
(
p
)}
data
:=
DataMsg
{
Data
:
string
(
p
)
,
Room
:
c
.
Uuid
}
msg
,
err
:=
json
.
Marshal
(
data
)
msg
,
err
:=
json
.
Marshal
(
data
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
n
=
len
(
p
)
n
=
len
(
p
)
room
:=
c
.
Conn
.
Room
(
c
.
Uuid
)
c
.
Conn
.
Emit
(
"data"
,
msg
)
if
room
==
nil
{
logger
.
Error
(
"room not found: "
,
c
.
Uuid
)
return
}
room
.
Emit
(
"data"
,
msg
)
return
return
}
}
...
@@ -81,5 +71,8 @@ func (c *Client) Close() (err error) {
...
@@ -81,5 +71,8 @@ func (c *Client) Close() (err error) {
func
(
c
*
Client
)
SetWinSize
(
size
ssh
.
Window
)
{
func
(
c
*
Client
)
SetWinSize
(
size
ssh
.
Window
)
{
c
.
mu
.
RLock
()
c
.
mu
.
RLock
()
defer
c
.
mu
.
RUnlock
()
defer
c
.
mu
.
RUnlock
()
c
.
WinChan
<-
size
select
{
case
c
.
WinChan
<-
size
:
default
:
}
}
}
pkg/httpd/data.go
View file @
2de9b63c
...
@@ -20,6 +20,7 @@ type TokenMsg struct {
...
@@ -20,6 +20,7 @@ type TokenMsg struct {
type
DataMsg
struct
{
type
DataMsg
struct
{
Data
string
`json:"data"`
Data
string
`json:"data"`
Room
string
`json:"room"`
}
}
type
RoomMsg
struct
{
type
RoomMsg
struct
{
...
@@ -27,16 +28,11 @@ type RoomMsg struct {
...
@@ -27,16 +28,11 @@ type RoomMsg struct {
Secret
string
`json:"secret"`
Secret
string
`json:"secret"`
}
}
type
EmitDataMsg
struct
{
type
LogoutMsg
struct
{
Room
string
`json:"room"`
Data
string
`json:"data"`
}
type
EmitLogoutMsg
struct
{
Room
string
`json:"room"`
Room
string
`json:"room"`
}
}
type
Emit
DisconnectMsg
struct
{
type
DisconnectMsg
struct
{
}
}
type
EmitSidMsg
struct
{
type
EmitSidMsg
struct
{
...
...
pkg/httpd/websshws.go
View file @
2de9b63c
...
@@ -20,10 +20,6 @@ import (
...
@@ -20,10 +20,6 @@ import (
"github.com/jumpserver/koko/pkg/service"
"github.com/jumpserver/koko/pkg/service"
)
)
// OnConnectHandler 当websocket连接后触发
// OnConnectHandler 当websocket连接后触发
func
OnNamespaceConnected
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
error
{
func
OnNamespaceConnected
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
error
{
// 首次连接 1.获取当前用户的信息
// 首次连接 1.获取当前用户的信息
...
@@ -61,9 +57,8 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
...
@@ -61,9 +57,8 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
return
nil
return
nil
}
}
// OnDisconnect websocket断开后触发
// OnDisconnect websocket断开后触发
func
OnNamespaceDisconnect
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
){
func
OnNamespaceDisconnect
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
)
{
logger
.
Debug
(
"On disconnect event trigger"
)
logger
.
Debug
(
"On disconnect event trigger"
)
conns
.
DeleteClients
(
c
.
Conn
.
ID
())
conns
.
DeleteClients
(
c
.
Conn
.
ID
())
return
nil
return
nil
...
@@ -83,7 +78,6 @@ func OnHostHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -83,7 +78,6 @@ func OnHostHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
fmt
.
Println
(
"Host msg: "
,
message
)
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
win
:=
ssh
.
Window
{
Height
:
24
,
Width
:
80
}
assetID
:=
message
.
Uuid
assetID
:=
message
.
Uuid
systemUserID
:=
message
.
UserID
systemUserID
:=
message
.
UserID
...
@@ -97,46 +91,49 @@ func OnHostHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -97,46 +91,49 @@ func OnHostHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
}
}
roomID
:=
uuid
.
NewV4
()
.
String
()
roomID
:=
uuid
.
NewV4
()
.
String
()
emitMsg
:=
RoomMsg
{
roomID
,
secret
}
emitMsg
:=
RoomMsg
{
roomID
,
secret
}
joinRoomMsg
,
_
:=
json
.
Marshal
(
emitMsg
)
roomMsg
,
_
:=
json
.
Marshal
(
emitMsg
)
c
.
Emit
(
"room"
,
joinRoomMsg
)
c
.
Emit
(
"room"
,
roomMsg
)
if
err
!=
nil
{
logger
.
Debug
(
"Join room error occur: "
,
err
)
return
}
asset
:=
service
.
GetAsset
(
assetID
)
asset
:=
service
.
GetAsset
(
assetID
)
systemUser
:=
service
.
GetSystemUser
(
systemUserID
)
systemUser
:=
service
.
GetSystemUser
(
systemUserID
)
if
asset
.
ID
==
""
||
systemUser
.
ID
==
""
{
if
asset
.
ID
==
""
||
systemUser
.
ID
==
""
{
logger
.
Debug
(
"No asset id or system user id found, exit"
)
msg
:=
"No asset id or system user id found, exit"
logger
.
Debug
(
msg
)
dataMsg
:=
DataMsg
{
Room
:
roomID
,
Data
:
msg
}
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
return
return
}
}
logger
.
Debug
(
"Web terminal want to connect host: "
,
asset
.
Hostname
)
logger
.
Debug
(
"Web terminal want to connect host: "
,
asset
.
Hostname
)
currentUser
,
ok
:=
cc
.
Get
(
"currentUser"
)
.
(
*
model
.
User
)
currentUser
,
ok
:=
cc
.
Get
(
"currentUser"
)
.
(
*
model
.
User
)
if
!
ok
{
if
!
ok
{
return
errors
.
New
(
"not found current user"
)
err
=
errors
.
New
(
"not found current user"
)
dataMsg
:=
DataMsg
{
Room
:
roomID
,
Data
:
err
.
Error
()}
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
return
}
}
userR
,
userW
:=
io
.
Pipe
()
userR
,
userW
:=
io
.
Pipe
()
addr
,
_
,
_
:=
net
.
SplitHostPort
(
cc
.
Socket
()
.
Request
()
.
RemoteAddr
)
addr
,
_
,
_
:=
net
.
SplitHostPort
(
cc
.
Socket
()
.
Request
()
.
RemoteAddr
)
client
:=
&
Client
{
client
:=
&
Client
{
Uuid
:
roomID
,
user
:
currentUser
,
addr
:
addr
,
Uuid
:
roomID
,
addr
:
addr
,
WinChan
:
make
(
chan
ssh
.
Window
,
100
),
Conn
:
c
,
WinChan
:
make
(
chan
ssh
.
Window
,
100
),
Conn
:
c
,
UserRead
:
userR
,
UserWrite
:
userW
,
mu
:
new
(
sync
.
RWMutex
),
UserRead
:
userR
,
UserWrite
:
userW
,
mu
:
new
(
sync
.
RWMutex
),
pty
:
ssh
.
Pty
{
Term
:
"xterm"
,
Window
:
win
},
pty
:
ssh
.
Pty
{
Term
:
"xterm"
,
Window
:
win
},
}
}
user
:=
cc
.
Get
(
"currentUser"
)
.
(
*
model
.
User
)
client
.
WinChan
<-
win
client
.
WinChan
<-
win
clients
.
AddClient
(
roomID
,
client
)
clients
.
AddClient
(
roomID
,
client
)
conns
.
AddClient
(
cc
.
ID
(),
roomID
)
conns
.
AddClient
(
cc
.
ID
(),
roomID
)
proxySrv
:=
proxy
.
ProxyServer
{
proxySrv
:=
proxy
.
ProxyServer
{
UserConn
:
client
,
User
:
u
ser
,
UserConn
:
client
,
User
:
currentU
ser
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
Asset
:
&
asset
,
SystemUser
:
&
systemUser
,
}
}
go
func
()
{
go
func
()
{
defer
logger
.
Debug
(
"
web proxy
end"
)
defer
logger
.
Debug
(
"
Web proxy process
end"
)
logger
.
Debug
(
"Start proxy"
)
logger
.
Debug
(
"Start proxy
to host
"
)
proxySrv
.
Proxy
()
proxySrv
.
Proxy
()
logoutMsg
,
_
:=
json
.
Marshal
(
RoomMsg
{
Room
:
roomID
})
logoutMsg
,
_
:=
json
.
Marshal
(
RoomMsg
{
Room
:
roomID
})
// 服务器主动退出
c
.
Emit
(
"logout"
,
logoutMsg
)
c
.
Emit
(
"logout"
,
logoutMsg
)
clients
.
DeleteClient
(
roomID
)
clients
.
DeleteClient
(
roomID
)
}()
}()
...
@@ -154,21 +151,21 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -154,21 +151,21 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
}
}
token
:=
message
.
Token
token
:=
message
.
Token
secret
:=
message
.
Secret
secret
:=
message
.
Secret
client
ID
:=
uuid
.
NewV4
()
.
String
()
room
ID
:=
uuid
.
NewV4
()
.
String
()
roomMsg
:=
RoomMsg
{
client
ID
,
secret
}
roomMsg
:=
RoomMsg
{
room
ID
,
secret
}
c
.
Emit
(
"room"
,
neffos
.
Marshal
(
roomMsg
))
c
.
Emit
(
"room"
,
neffos
.
Marshal
(
roomMsg
))
// check token
// check token
if
token
==
""
||
secret
==
""
{
if
token
==
""
||
secret
==
""
{
msg
:=
fmt
.
Sprintf
(
"Token or secret is None: %s %s"
,
token
,
secret
)
msg
:=
fmt
.
Sprintf
(
"Token or secret is None: %s %s"
,
token
,
secret
)
dataMsg
:=
EmitDataMsg
{
Data
:
msg
,
Room
:
client
ID
}
dataMsg
:=
DataMsg
{
Data
:
msg
,
Room
:
room
ID
}
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"disconnect"
,
nil
)
c
.
Emit
(
"disconnect"
,
nil
)
}
}
tokenUser
:=
service
.
GetTokenAsset
(
token
)
tokenUser
:=
service
.
GetTokenAsset
(
token
)
if
tokenUser
.
UserID
==
""
{
if
tokenUser
.
UserID
==
""
{
msg
:=
"Token info is none, maybe token expired"
msg
:=
"Token info is none, maybe token expired"
dataMsg
:=
EmitDataMsg
{
Data
:
msg
,
Room
:
client
ID
}
dataMsg
:=
DataMsg
{
Data
:
msg
,
Room
:
room
ID
}
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"disconnect"
,
nil
)
c
.
Emit
(
"disconnect"
,
nil
)
}
}
...
@@ -177,7 +174,7 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -177,7 +174,7 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
if
currentUser
==
nil
{
if
currentUser
==
nil
{
msg
:=
"User id error"
msg
:=
"User id error"
dataMsg
:=
EmitDataMsg
{
Data
:
msg
,
Room
:
client
ID
}
dataMsg
:=
DataMsg
{
Data
:
msg
,
Room
:
room
ID
}
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"data"
,
neffos
.
Marshal
(
dataMsg
))
c
.
Emit
(
"disconnect"
,
nil
)
c
.
Emit
(
"disconnect"
,
nil
)
}
}
...
@@ -185,28 +182,27 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -185,28 +182,27 @@ func OnTokenHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
cc
.
Set
(
"currentUser"
,
currentUser
)
cc
.
Set
(
"currentUser"
,
currentUser
)
hostMsg
:=
HostMsg
{
hostMsg
:=
HostMsg
{
Uuid
:
tokenUser
.
AssetID
,
UserID
:
tokenUser
.
SystemUserID
,
Uuid
:
tokenUser
.
AssetID
,
UserID
:
tokenUser
.
SystemUserID
,
Size
:
message
.
Size
,
Secret
:
secret
,
Size
:
message
.
Size
,
Secret
:
secret
,
}
}
fmt
.
Println
(
"Host msg: "
,
hostMsg
)
hostWsMsg
:=
neffos
.
Message
{
hostWsMsg
:=
neffos
.
Message
{
Body
:
neffos
.
Marshal
(
hostMsg
),
Body
:
neffos
.
Marshal
(
hostMsg
),
}
}
return
OnHostHandler
(
c
,
hostWsMsg
)
return
OnHostHandler
(
c
,
hostWsMsg
)
}
}
// OnDataHandler 收发数据时触发
// OnDataHandler 收发数据时触发
func
OnDataHandler
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
)
{
func
OnDataHandler
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
)
{
roomID
:=
msg
.
Room
client
:=
clients
.
GetClient
(
roomID
)
if
client
==
nil
{
return
}
var
message
DataMsg
var
message
DataMsg
err
=
msg
.
Unmarshal
(
&
message
)
err
=
msg
.
Unmarshal
(
&
message
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
clientID
:=
message
.
Room
client
:=
clients
.
GetClient
(
clientID
)
if
client
==
nil
{
return
}
_
,
err
=
client
.
UserWrite
.
Write
([]
byte
(
message
.
Data
))
_
,
err
=
client
.
UserWrite
.
Write
([]
byte
(
message
.
Data
))
return
err
return
err
}
}
...
@@ -220,9 +216,8 @@ func OnResizeHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -220,9 +216,8 @@ func OnResizeHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
}
}
logger
.
Debugf
(
"Web terminal on resize event trigger: %d*%d"
,
message
.
Width
,
message
.
Height
)
logger
.
Debugf
(
"Web terminal on resize event trigger: %d*%d"
,
message
.
Width
,
message
.
Height
)
winSize
:=
ssh
.
Window
{
Height
:
message
.
Height
,
Width
:
message
.
Width
}
winSize
:=
ssh
.
Window
{
Height
:
message
.
Height
,
Width
:
message
.
Width
}
for
_
,
room
:=
range
c
.
Rooms
()
{
for
_
,
clientID
:=
range
conns
.
GetClients
(
c
.
Conn
.
ID
())
{
roomID
:=
room
.
Name
client
:=
clients
.
GetClient
(
clientID
)
client
:=
clients
.
GetClient
(
roomID
)
if
client
!=
nil
{
if
client
!=
nil
{
client
.
SetWinSize
(
winSize
)
client
.
SetWinSize
(
winSize
)
}
}
...
@@ -230,16 +225,15 @@ func OnResizeHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
...
@@ -230,16 +225,15 @@ func OnResizeHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
return
nil
return
nil
}
}
// OnLogoutHandler 用户登出一个会话时触发
// OnLogoutHandler 用户登出一个会话时触发
, 用户主动退出
func
OnLogoutHandler
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
){
func
OnLogoutHandler
(
c
*
neffos
.
NSConn
,
msg
neffos
.
Message
)
(
err
error
)
{
logger
.
Debug
(
"Web terminal on logout event trigger: "
,
msg
.
Room
)
logger
.
Debug
(
"Web terminal on logout event trigger: "
,
msg
.
Room
)
var
message
Room
Msg
var
message
Logout
Msg
err
=
msg
.
Unmarshal
(
&
message
)
err
=
msg
.
Unmarshal
(
&
message
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
room
ID
:=
message
.
Room
client
ID
:=
message
.
Room
clients
.
DeleteClient
(
room
ID
)
clients
.
DeleteClient
(
client
ID
)
return
return
}
}
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