Unverified Commit 29a0e0d3 authored by 老广's avatar 老广 Committed by GitHub

Merge pull request #46 from jumpserver/dev

Dev
parents e9cc1fb0 7d115107
...@@ -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:
}
} }
...@@ -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 EmitDisconnectMsg struct { type DisconnectMsg struct {
} }
type EmitSidMsg struct { type EmitSidMsg struct {
......
...@@ -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: user, UserConn: client, User: currentUser,
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
clientID := uuid.NewV4().String() roomID := uuid.NewV4().String()
roomMsg := RoomMsg{clientID, secret} roomMsg := RoomMsg{roomID, 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: clientID} dataMsg := DataMsg{Data: msg, Room: roomID}
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: clientID} dataMsg := DataMsg{Data: msg, Room: roomID}
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: clientID} dataMsg := DataMsg{Data: msg, Room: roomID}
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 RoomMsg var message LogoutMsg
err = msg.Unmarshal(&message) err = msg.Unmarshal(&message)
if err != nil { if err != nil {
return return
} }
roomID := message.Room clientID := message.Room
clients.DeleteClient(roomID) clients.DeleteClient(clientID)
return return
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment