This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
share.go
93 lines (82 loc) · 2.43 KB
/
share.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package handler
import (
"encoding/json"
"fmt"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/cloudradar-monitoring/plexus/api"
"github.com/cloudradar-monitoring/plexus/asset"
"github.com/cloudradar-monitoring/plexus/control"
)
// ShareSession godoc
// @Summary Start the remote control on the session via a browser.
// @Tags session
// @Produce text/html
// @Param id path string true "session id"
// @Security BasicAuth
// @Success 200 {object} string
// @Failure 400 {object} api.Error
// @Failure 401 {object} api.Error
// @Failure 500 {object} api.Error
// @Router /session/{id} [get]
func (h *Handler) ShareSession(rw http.ResponseWriter, r *http.Request) {
h.lock.Lock()
defer h.lock.Unlock()
session, ok := h.checkSessionAuthentication(rw, r, mux.Vars(r)["id"])
if !ok {
return
}
rw.Header().Add("content-type", "text/html")
rw.WriteHeader(http.StatusOK)
_ = asset.ShareTemplate.Execute(rw, map[string]string{
"ID": session.ID,
"GetURL": path.Join(h.prefix, "session", session.ID, "url"),
})
}
// ShareSessionURL godoc
// @Summary Gets the meshcentral share session for the session id.
// @Tags session
// @Produce application/json
// @Param id path string true "session id"
// @Security BasicAuth
// @Success 200 {object} api.URLResponse
// @Failure 400 {object} api.Error
// @Failure 401 {object} api.Error
// @Failure 500 {object} api.Error
// @Failure 502 {object} api.Error
// @Router /session/{id}/url [get]
func (h *Handler) ShareSessionURL(rw http.ResponseWriter, r *http.Request) {
h.lock.Lock()
defer h.lock.Unlock()
session, ok := h.checkSessionAuthentication(rw, r, mux.Vars(r)["id"])
if !ok {
return
}
if session.ShareURL == "" {
url, exit := h.tryGetURL(rw, session)
if exit {
return
}
session.ShareURL = url
}
rw.Header().Add("content-type", "application/json")
rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(&api.URLResponse{
URL: session.ShareURL,
})
}
func (h *Handler) tryGetURL(rw http.ResponseWriter, session *Session) (string, bool) {
mc, err := control.Connect(h.ccfg, h.log)
if err != nil {
api.WriteBadGatewayJSON(rw, fmt.Sprintf("could not connect: %s", err))
return "", true
}
defer mc.Close()
share, err := mc.Share(session.AgentConfig.MeshID, session.ID, session.ExpiresAt)
if err != nil && err != control.ErrAgentNotConnected {
api.WriteBadGatewayJSON(rw, fmt.Sprintf("could not create share: %s", err))
return "", true
}
return share, false
}