-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdesktop.go
202 lines (174 loc) · 5.38 KB
/
desktop.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
func (sfui *SfUI) handleDesktopWS(w http.ResponseWriter, r *http.Request) {
//Get Secret
queryVals := r.URL.Query()
clientSecret := queryVals.Get("secret")
desktopType := queryVals.Get("type")
if !sfui.ValidSecret(clientSecret) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`unacceptable secret`))
return
}
defer sfui.RemoveClientIfInactive(clientSecret)
// Get the associated client or create a new one
// client variable below will get stale
client, cerr := sfui.GetExistingClientOrMakeNew(clientSecret, sfui.getClientAddr(r))
if cerr != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(cerr.Error()))
return
}
if client.DesktopActive.Load() {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte(`can only have one desktop connection active at a time`))
return
}
client.ActivateDesktop()
defer client.DeActivateDesktop()
defer client.DeactivateDesktopSharing() // Remove all shares when master VNC connection exits
sfui.startDesktopService(&client, desktopType, time.Second*3)
conn, err := client.SSHConnection.ForwardRemotePort(sfui.VNCPort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer (*conn).Close()
vncWebSockify(
conn,
false, // not view only
false, // not shared
client.SharedDesktopConn,
time.Minute*time.Duration(sfui.WSTimeout),
).ServeHTTP(w, r)
}
// Issue appropriate desktop start command(Type) using Pty and Wait for a certain duration
func (sfui *SfUI) startDesktopService(client *Client, desktoptype string, wait time.Duration) {
startCmd := ""
switch desktoptype {
case "xpra":
startCmd = sfui.StartXpraCommand
default:
startCmd = sfui.StartVNCCommand
}
client.SSHConnection.RunControlCommand(startCmd)
time.Sleep(wait)
}
func (sfui *SfUI) handleSharedDesktopWS(w http.ResponseWriter, r *http.Request) {
// Secret in this case will be the client Id and not the actual secret,
// this is to prevent the leak of secret to third party.
queryVals := r.URL.Query()
clientId := queryVals.Get("client_id")
shareSecret := queryVals.Get("secret")
if !sfui.ValidSecret(clientId) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`unacceptable secret`))
return
}
// Get the associated client
// client variable below will get stale
client, cerr := sfui.GetClientById(clientId)
if cerr != nil {
w.WriteHeader(http.StatusGone)
w.Write([]byte(`{"status":"desktop is not active"}`))
return
}
if client.SharedDesktopSecret != shareSecret {
w.WriteHeader(http.StatusNotAcceptable)
w.Write([]byte(`unacceptable secret`))
return
}
serr := client.IncSharedDesktopConnCount()
if serr != nil {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{"status":"maximum shares active"}`))
return
}
defer client.DecSharedDesktopConnCount()
conn, err := client.SSHConnection.ForwardRemotePort(sfui.VNCPort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer (*conn).Close()
vncWebSockify(
conn,
client.SharedDesktopIsViewOnly.Load(),
true, // is a shared connection
client.SharedDesktopConn,
time.Minute*time.Duration(sfui.WSTimeout),
).ServeHTTP(w, r)
}
type DesktopShareRequest struct {
Secret string `json:"secret"`
ClientId string `json:"client_id"`
Action string `json:"action"`
ViewOnly bool `json:"view_only"`
}
func (sfui *SfUI) handleSetupDesktopSharing(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
data, err := io.ReadAll(io.LimitReader(r.Body, 2048))
if err == nil {
desktopShareReq := DesktopShareRequest{}
if json.Unmarshal(data, &desktopShareReq) == nil {
var client Client
var cerr error
if desktopShareReq.Action == "verify" {
client, cerr = sfui.GetClientById(desktopShareReq.ClientId)
} else {
client, cerr = sfui.GetClient(desktopShareReq.Secret)
}
if cerr != nil {
w.WriteHeader(http.StatusGone)
w.Write([]byte(`{"status":"desktop is not active"}`))
return
}
if !client.DesktopActive.Load() {
w.WriteHeader(http.StatusGone)
w.Write([]byte(`{"status":"desktop is not active"}`))
return
}
if client.SharedDesktopConnCount.Load() >= client.MaxSharedDesktopConn {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{"status":"maximum shares active"}`))
return
}
switch desktopShareReq.Action {
case "activate":
sharedSecret := RandomStr(24)
alreadyShared := client.ActivateDesktopSharing(desktopShareReq.ViewOnly, sharedSecret)
if alreadyShared {
sharedSecret = client.SharedDesktopSecret
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{"status":"OK","client_id":"%s","share_secret":"%s"}`,
client.ClientId, sharedSecret)))
return
case "deactivate":
client.DeactivateDesktopSharing()
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"OK"}`))
return
case "verify":
if client.ShareDesktop.Load() && (client.SharedDesktopSecret == desktopShareReq.Secret) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"OK"}`))
} else {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`{"status":"Desktop Not Shared"}`))
}
return
}
}
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"status":"Internal Server Error"}`))
}