-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
377 lines (324 loc) · 8.97 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/creack/pty"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/gorilla/websocket"
)
func NewHandler() (http.Handler, error) {
r := chi.NewRouter()
// Middleware to set the required header for private network access
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Private-Network", "true")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("Referrer-Policy", "same-origin")
w.Header().Set("Content-Security-Policy", "script-src 'self';")
next.ServeHTTP(w, r)
})
})
r.Use(cors.Handler(cors.Options{
AllowOriginFunc: func(r *http.Request, origin string) bool {
if origin == "https://www.tweety.sh" || origin == "https://tweety.sh" {
return true
}
if strings.HasPrefix(origin, "chrome-extension://") {
return true
}
return false
},
}))
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
r.Get("/config", func(w http.ResponseWriter, r *http.Request) {
config, err := LoadConfig(configPath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
config.Env = nil
for k, profile := range config.Profiles {
profile.Command = ""
profile.Args = nil
profile.Cwd = ""
profile.Env = nil
config.Profiles[k] = profile
}
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)
if err := encoder.Encode(config); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
})
currentUser, err := user.Current()
if err != nil {
return nil, fmt.Errorf("error getting current user: %w", err)
}
ttyMap := make(map[string]*os.File)
r.Get("/pty/{terminalID}", func(w http.ResponseWriter, r *http.Request) {
terminalID := chi.URLParam(r, "terminalID")
config, err := LoadConfig(configPath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
cols, err := strconv.Atoi(r.URL.Query().Get("cols"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid cols value: %s", err)))
return
}
rows, err := strconv.Atoi(r.URL.Query().Get("rows"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid rows value: %s", err)))
return
}
profileName := r.URL.Query().Get("profile")
profile, ok := config.Profiles[profileName]
if !ok {
log.Println("invalid profile name:", profileName)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid profile name: %s", profileName)))
return
}
cmd := exec.Command(profile.Command, profile.Args...)
cmd.Env = append(cmd.Env, "TERM=xterm-256color")
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", currentUser.Username))
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", currentUser.HomeDir))
for k, v := range config.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
for k, v := range profile.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("failed to get user home directory: %s", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if profile.Cwd != "" {
if profile.Cwd == "~" {
cmd.Dir = homeDir
} else if strings.HasPrefix(profile.Cwd, "~/") {
cmd.Dir = filepath.Join(homeDir, profile.Cwd[2:])
} else {
cmd.Dir = profile.Cwd
}
} else {
cmd.Dir = homeDir
}
log.Println("executing command:", cmd.String())
tty, err := pty.Start(cmd)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer func() {
log.Print("killing spawned process")
cmd.Process.Kill()
if err := tty.Close(); err != nil {
log.Printf("failed to close spawned tty gracefully: %s", err)
}
}()
if err := pty.Setsize(tty, &pty.Winsize{
Rows: uint16(rows),
Cols: uint16(cols),
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
ttyMap[terminalID] = tty
HandleWebsocket(tty)(w, r)
})
r.Post("/resize/{terminalID}", func(w http.ResponseWriter, r *http.Request) {
terminalID := chi.URLParam(r, "terminalID")
tty, ok := ttyMap[terminalID]
if !ok {
availableTerminals := make([]string, 0, len(ttyMap))
for k := range ttyMap {
availableTerminals = append(availableTerminals, k)
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid terminal ID: %s, available terminal: %v", terminalID, availableTerminals)))
return
}
var resizePayload struct {
Rows uint16 `json:"rows"`
Cols uint16 `json:"cols"`
}
if err := json.NewDecoder(r.Body).Decode(&resizePayload); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("invalid resize payload: %s", err)))
return
}
if err := pty.Setsize(tty, &pty.Winsize{
Rows: resizePayload.Rows,
Cols: resizePayload.Cols,
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte("Resized"))
})
themeHandler, err := ThemeHandler()
if err != nil {
return nil, err
}
r.Handle("/themes/*", http.StripPrefix("/themes", themeHandler))
frontendHandler, err := FrontendHandler()
if err != nil {
return nil, err
}
r.Handle("/*", frontendHandler)
return r, nil
}
//go:embed all:frontend/dist
var frontendDist embed.FS
func FrontendHandler() (http.Handler, error) {
fs, err := fs.Sub(frontendDist, "frontend/dist")
if err != nil {
return nil, err
}
return http.FileServer(http.FS(fs)), nil
}
//go:embed all:themes
var themes embed.FS
func ThemeHandler() (http.Handler, error) {
fs, err := fs.Sub(themes, "themes")
if err != nil {
return nil, err
}
return http.FileServer(http.FS(fs)), nil
}
var (
maxBufferSizeBytes = 512
keepalivePingTimeout = 20 * time.Second
)
func HandleWebsocket(tty *os.File) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Print("established connection identity")
upgrader := getConnectionUpgrader(maxBufferSizeBytes)
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("failed to upgrade connection: %s", err)
return
}
defer connection.Close()
var waiter sync.WaitGroup
waiter.Add(1)
// tty << xterm.js
go func() {
for {
// data processing
_, data, err := connection.ReadMessage()
if err != nil {
log.Printf("failed to get next reader: %s", err)
waiter.Done()
return
}
dataLength := len(data)
dataBuffer := bytes.Trim(data, "\x00")
// process
if dataLength == -1 { // invalid
log.Printf("failed to get the correct number of bytes read, ignoring message")
continue
}
// write to tty
if _, err := tty.Write(dataBuffer); err != nil {
log.Printf("failed to write %v bytes to tty: %s", len(dataBuffer), err)
continue
}
}
}()
messages := make(chan []byte)
// tty >> xterm.js
go func() {
for {
buffer := make([]byte, maxBufferSizeBytes)
readLength, err := tty.Read(buffer)
if err != nil {
connection.Close()
log.Printf("failed to read from tty: %s", err)
return
}
messages <- buffer[:readLength]
}
}()
lastPingTime := time.Now()
connection.SetPongHandler(func(appData string) error {
lastPingTime = time.Now()
return nil
})
// this is a keep-alive loop that ensures connection does not hang-up itself
go func() {
ticker := time.NewTicker(keepalivePingTimeout / 2)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := connection.WriteMessage(websocket.PingMessage, []byte("keepalive")); err != nil {
log.Printf("failed to write ping message")
}
if time.Since(lastPingTime) > keepalivePingTimeout {
log.Printf("connection timeout, closing connection")
connection.Close()
return
}
case m := <-messages:
if err := connection.WriteMessage(websocket.BinaryMessage, m); err != nil {
log.Printf("failed to send %v bytes from tty to xterm.js", len(m))
continue
}
}
}
}()
waiter.Wait()
}
}
func getConnectionUpgrader(
maxBufferSizeBytes int,
) websocket.Upgrader {
return websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
HandshakeTimeout: 0,
ReadBufferSize: maxBufferSizeBytes,
WriteBufferSize: maxBufferSizeBytes,
}
}
var WebsocketMessageType = map[int]string{
websocket.BinaryMessage: "binary",
websocket.TextMessage: "text",
websocket.CloseMessage: "close",
websocket.PingMessage: "ping",
websocket.PongMessage: "pong",
}