forked from jumpserver/koko
/
server.go
70 lines (59 loc) · 1.98 KB
/
server.go
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
package httpd
import (
"encoding/json"
"net"
"net/http"
"net/http/pprof"
"path/filepath"
"strings"
"time"
"github.com/eeeeethan2333/koko/pkg/config"
"github.com/eeeeethan2333/koko/pkg/logger"
"github.com/gorilla/mux"
"github.com/kataras/neffos"
)
var (
httpServer *http.Server
Timeout = time.Duration(60)
)
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 = neffosOnUpgradeError
sshWs.OnConnect = neffosOnConnect
sshWs.OnDisconnect = neffosOnDisconnect
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)
subRouter.Handle("/room/{roomID}/", AuthDecorator(roomHandler))
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.HandleFunc("/status/", statusHandler)
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()
}
func statusHandler(wr http.ResponseWriter, req *http.Request) {
status := make(map[string]interface{})
data := websocketManager.GetWebsocketData()
status["websocket"] = data
wr.Header().Set("Content-Type", "application/json")
jsonData, _ := json.Marshal(status)
_, _ = wr.Write(jsonData)
}