1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package httpd
import (
"context"
"net"
"net/http"
"path/filepath"
"strings"
"time"
"net/http/pprof"
"github.com/gorilla/mux"
gorillaws "github.com/gorilla/websocket"
"github.com/jumpserver/koko/pkg/config"
"github.com/jumpserver/koko/pkg/logger"
"github.com/kataras/neffos"
"github.com/kataras/neffos/gorilla"
)
var (
httpServer *http.Server
Timeout = time.Duration(60)
)
var upgrader = gorilla.Upgrader(gorillaws.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
})
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,
},
"elfinder": neffos.Events{
neffos.OnNamespaceConnected: OnELFinderConnect,
neffos.OnNamespaceDisconnect: OnELFinderDisconnect,
"ping": OnPingHandler,
},
},
}
func StartHTTPServer() {
conf := config.GetConf()
sshWs := neffos.New(upgrader, wsEvents)
sshWs.IDGenerator = func(w http.ResponseWriter, r *http.Request) string {
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() {
namespace := c.Socket().Request().Header.Get("X-Namespace")
if namespace != "" {
_, _ = c.Connect(context.TODO(), "ssh")
}
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()
fs := http.FileServer(http.Dir(filepath.Join(conf.RootPath, "static")))
subRouter := router.PathPrefix("/koko/").Subrouter()
subRouter.PathPrefix("/static/").Handler(http.StripPrefix("/koko/static/", fs))
subRouter.Handle("/ws/", sshWs)
elfinderRouter := subRouter.PathPrefix("/elfinder/").Subrouter()
elfinderRouter.HandleFunc("/sftp/{host}/", AuthDecorator(sftpHostFinder))
elfinderRouter.HandleFunc("/sftp/", AuthDecorator(sftpFinder))
elfinderRouter.HandleFunc("/sftp/connector/{host}/",
AuthDecorator(sftpHostConnectorView),
).Methods("GET", "POST")
//router.PathPrefix("/coco/static/").Handler(http.StripPrefix("/coco/static/", fs))
//router.Handle("/socket.io/", sshWs)
//router.HandleFunc("/coco/elfinder/sftp/{host}/", AuthDecorator(sftpHostFinder))
//router.HandleFunc("/coco/elfinder/sftp/", AuthDecorator(sftpFinder))
//router.HandleFunc("/coco/elfinder/sftp/connector/{host}/",
// AuthDecorator(sftpHostConnectorView)).Methods("GET", "POST")
if strings.ToUpper(conf.LogLevel) == "DEBUG" {
router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
}
addr := net.JoinHostPort(conf.BindHost, conf.HTTPPort)
logger.Info("Start HTTP server at ", addr)
httpServer = &http.Server{Addr: addr, Handler: router}
logger.Fatal(httpServer.ListenAndServe())
}
func StopHTTPServer() {
_ = httpServer.Close()
}