Unverified Commit 04b83c9f authored by 老广's avatar 老广 Committed by GitHub

Merge pull request #48 from jumpserver/dev

Dev
parents 29a0e0d3 56ca009c
......@@ -11,10 +11,16 @@
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
var port = document.location.port ? ":" + document.location.port : "";
var wsURL = scheme + "://" + document.location.hostname + port + "/socket.io/";
let interval;
dial(wsURL, {
"elfinder": {
_OnNamespaceConnected: function (nsConn, msg) {
console.log("Connect websocket done")
interval = setInterval(() => nsConn.emit('ping', ''), 10000);
},
_OnNamespaceDisconnect: function (ns, msg) {
if (interval) {
clearInterval(interval);
}
},
data: function (nsConn, msg) {
var data = msg.unmarshal();
......
package httpd
import (
"fmt"
"sync"
"github.com/jumpserver/koko/pkg/logger"
......@@ -37,14 +36,12 @@ func (c *Clients) DeleteClient(cID string) {
}
func (c *Clients) AddClient(cID string, conn *Client) {
fmt.Println("Add Client id: ", cID)
c.mu.Lock()
defer c.mu.Unlock()
c.container[cID] = conn
logger.Debug("Now clients count: ", len(c.container))
}
type Connections struct {
container map[string][]string
mu *sync.RWMutex
......@@ -69,7 +66,7 @@ func (c *Connections) GetClients(cID string) (clients []string) {
}
func (c *Connections) DeleteClients(cID string) {
if clientIDs := c.GetClients(cID); clientIDs != nil{
if clientIDs := c.GetClients(cID); clientIDs != nil {
for _, clientID := range clientIDs {
clients.DeleteClient(clientID)
}
......@@ -78,4 +75,3 @@ func (c *Connections) DeleteClients(cID string) {
defer c.mu.Unlock()
delete(c.container, cID)
}
......@@ -4,6 +4,7 @@ import (
"net"
"net/http"
"path/filepath"
"time"
"github.com/gorilla/mux"
"github.com/kataras/neffos"
......@@ -15,28 +16,35 @@ import (
var (
httpServer *http.Server
Timeout = time.Duration(60)
)
var wsEvents = neffos.Namespaces{
"ssh": neffos.Events{
neffos.OnNamespaceConnected: OnNamespaceConnected,
neffos.OnNamespaceDisconnect: OnNamespaceDisconnect,
neffos.OnRoomJoined: func(c *neffos.NSConn, msg neffos.Message) error {
return nil
var wsEvents = neffos.WithTimeout{
ReadTimeout: Timeout * time.Second,
WriteTimeout: Timeout * time.Second,
Namespaces: neffos.Namespaces{
"ssh": neffos.Events{
neffos.OnNamespaceConnected: OnNamespaceConnected,
neffos.OnNamespaceDisconnect: OnNamespaceDisconnect,
neffos.OnRoomJoined: func(c *neffos.NSConn, msg neffos.Message) error {
return nil
},
neffos.OnRoomLeft: func(c *neffos.NSConn, msg neffos.Message) error {
return nil
},
"data": OnDataHandler,
"resize": OnResizeHandler,
"host": OnHostHandler,
"logout": OnLogoutHandler,
"token": OnTokenHandler,
"ping": OnPingHandler,
},
neffos.OnRoomLeft: func(c *neffos.NSConn, msg neffos.Message) error {
return nil
"elfinder": neffos.Events{
neffos.OnNamespaceConnected: OnELFinderConnect,
neffos.OnNamespaceDisconnect: OnELFinderDisconnect,
"ping": OnPingHandler,
},
"data": OnDataHandler,
"resize": OnResizeHandler,
"host": OnHostHandler,
"logout": OnLogoutHandler,
"token": OnTokenHandler,
},
"elfinder": neffos.Events{
neffos.OnNamespaceConnected: OnELFinderConnect,
neffos.OnNamespaceDisconnect: OnELFinderDisconnect,
},
}
......@@ -47,6 +55,22 @@ func StartHTTPServer() {
return neffos.DefaultIDGenerator(w, r)
}
sshWs.OnUpgradeError = func(err error) {
if ok := neffos.IsTryingToReconnect(err); ok {
logger.Debug("A client was tried to reconnect")
return
}
logger.Error("ERROR: ", err)
}
sshWs.OnConnect = func(c *neffos.Conn) error {
if c.WasReconnected() {
logger.Debugf("Connection %s reconnected, with tries: %d", c.ID(), c.ReconnectTries)
} else {
logger.Debug("A new ws connection arrive")
}
return nil
}
sshWs.OnDisconnect = func(c *neffos.Conn) {
logger.Debug("Ws connection disconnect")
}
router := mux.NewRouter()
......
......@@ -172,7 +172,7 @@ func (u *UserVolume) UploadChunk(cid int, dirPath, chunkName string, reader io.R
func (u *UserVolume) MergeChunk(cid, total int, dirPath, filename string) (elfinder.FileDir, error) {
path := filepath.Join(dirPath, filename)
logger.Debug("merge chunk path: ",path)
logger.Debug("merge chunk path: ", path)
var rest elfinder.FileDir
fd, err := u.UserSftp.Create(path)
if err != nil {
......@@ -249,6 +249,15 @@ func (u *UserVolume) Rename(oldNamePath, newName string) (elfinder.FileDir, erro
}
func (u *UserVolume) Remove(path string) error {
var res os.FileInfo
var err error
res, err = u.UserSftp.Stat(path)
if err != nil {
return err
}
if res.IsDir() {
return u.UserSftp.RemoveDirectory(path)
}
return u.UserSftp.Remove(path)
}
......
......@@ -4,31 +4,32 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/jumpserver/koko/pkg/model"
"io"
"net"
"strings"
"sync"
"time"
"github.com/gliderlabs/ssh"
"github.com/kataras/neffos"
"github.com/satori/go.uuid"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/model"
"github.com/jumpserver/koko/pkg/proxy"
"github.com/jumpserver/koko/pkg/service"
)
func OnPingHandler(c *neffos.NSConn, msg neffos.Message) error {
c.Emit("pong", []byte(""))
return nil
}
// OnConnectHandler 当websocket连接后触发
func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
// 首次连接 1.获取当前用户的信息
cc := c.Conn
if cc.WasReconnected() {
logger.Debugf("Web terminal redirected, with tries: %d", cc.ID(), cc.ReconnectTries)
} else {
logger.Debug("Web terminal on connect event trigger")
}
logger.Debug("Web terminal on connect event trigger")
request := cc.Socket().Request()
header := request.Header
cookies := strings.Split(header.Get("Cookie"), ";")
......@@ -54,6 +55,12 @@ func OnNamespaceConnected(c *neffos.NSConn, msg neffos.Message) error {
}
remoteIP = strings.Split(remoteAddr, ",")[0]
logger.Infof("Accepted %s connect websocket from %s", user.Username, remoteIP)
go func() {
for {
<-time.After(30 * time.Second)
c.Emit("ping", []byte(""))
}
}()
return nil
}
......@@ -130,7 +137,7 @@ func OnHostHandler(c *neffos.NSConn, msg neffos.Message) (err error) {
}
go func() {
defer logger.Debug("Web proxy process end")
logger.Debug("Start proxy to host")
logger.Debug("Web ssh start proxy to host")
proxySrv.Proxy()
logoutMsg, _ := json.Marshal(RoomMsg{Room: roomID})
// 服务器主动退出
......
......@@ -141,6 +141,7 @@ func (s *SwitchSession) Bridge(userConn UserConnection, srvConn srvconn.ServerCo
// 检测是否超过最大空闲时间
case <-time.After(s.MaxIdleTime * time.Minute):
msg := fmt.Sprintf(i18n.T("Connect idle more than %d minutes, disconnect"), s.MaxIdleTime)
logger.Debugf("Session idle more than %d minutes, disconnect: %s", s.MaxIdleTime, s.ID)
msg = utils.WrapperWarn(msg)
utils.IgnoreErrWriteString(userConn, "\n\r"+msg)
return
......
......@@ -172,7 +172,7 @@ func (u *UserSftp) RemoveDirectory(path string) error {
if conn == nil {
return sftp.ErrSshFxPermissionDenied
}
err := conn.client.RemoveDirectory(realPath)
err := u.removeDirectoryAll(conn.client, realPath)
filename := realPath
isSucess := false
operate := model.OperateRemoveDir
......@@ -183,6 +183,31 @@ func (u *UserSftp) RemoveDirectory(path string) error {
return err
}
func (u *UserSftp) removeDirectoryAll(conn *sftp.Client, path string) error {
var err error
var files []os.FileInfo
files, err = conn.ReadDir(path)
if err != nil {
return err
}
for _, item := range files {
realPath := filepath.Join(path, item.Name())
if item.IsDir() {
err = u.removeDirectoryAll(conn, realPath)
if err != nil {
return err
}
continue
}
err = conn.Remove(realPath)
if err != nil {
return err
}
}
return conn.RemoveDirectory(path)
}
func (u *UserSftp) Remove(path string) error {
req := u.ParsePath(path)
if req.host == "" {
......@@ -496,9 +521,17 @@ func (u *UserSftp) LoopPushFTPLog() {
dataChan := make(chan *model.FTPLog)
go u.SendFTPLog(dataChan)
defer close(dataChan)
var timeoutSecond time.Duration
for {
switch len(ftpLogList) {
case 0:
timeoutSecond = time.Second * 60
default:
timeoutSecond = time.Second * 1
}
select {
case <-time.After(time.Second * 5):
case <-time.After(timeoutSecond):
case logData, ok := <-u.LogChan:
if !ok {
return
......@@ -613,7 +646,7 @@ func NewFakeFile(name string, isDir bool) *FakeFileInfo {
}
}
func NewFakeSymFile(name string) *FakeFileInfo{
func NewFakeSymFile(name string) *FakeFileInfo {
return &FakeFileInfo{
name: name,
modtime: time.Now().UTC(),
......
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