-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
401 lines (353 loc) · 8.91 KB
/
screen.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"encoding/binary"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"os/user"
"runtime"
"syscall"
"time"
"github.com/gorilla/websocket"
"github.com/kr/pty"
)
var devURL = "ws://localhost:8080/tty?id="
var prodURL = "wss://screen.run/tty?id="
var viewDevURL = "http://localhost:8080/view?id="
var viewProdURL = "https://screen.run/view?id="
var protocol = "uploadtty"
var endian = binary.LittleEndian
const term = "screen"
// these are platform-dependent :/
// these seem to be constant across many operating systems?
const maxLoginLen = 256
// ubuntu 16.04 x86-64
// MAXPATHLEN 4096
// MAXLOGINLEN 256
// MAXTERMLEN 32
// message 12588
// create 8248
// attach 348
// detach 264
// command 8484
// message 8192
// msgVersion = 0
func FindMaxPathLen(os string) int {
if os == "" {
os = runtime.GOOS
}
// we have to hardcode because of reasons http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
// we could try to read in syslimits.h or another file, but who knows if they are there
// and we don't want to parse C
switch os {
case "darwin":
return 1024
case "linux":
return 4096
// TODO: confirm everything below here
case "windows":
return 260
case "freebsd", "openbsd", "netbsd", "plan9", "solaris", "nacl", "dragonfly", "android":
return 1024
default:
panic("Unknown operating system for max path length")
}
}
const msgCreate = 0
const msgError = 1
const msgAttach = 2
const msgCont = 3
const msgDetach = 4
const msgPowDetach = 5
const msgWinch = 6
const msgHangup = 7
const msgCommand = 8
const msgQuery = 9
const sigLock = syscall.SIGUSR2
const sigBye = syscall.SIGHUP
const sigPowerBye = syscall.SIGUSR1
func min(a, b int) int {
if a < b {
return a
}
return b
}
func chop(b []byte, max int) []byte {
return b[:min(len(b), max)]
}
func pad(n int) int {
switch n % 4 {
case 0:
return n
case 1:
return n + 3
case 2:
return n + 2
case 3:
return n + 1
}
return 0 // unreachable
}
func max(args ...int) int {
m := 0
for _, arg := range args {
if arg > m {
m = arg
}
}
return m
}
var maxTermLen = map[int]int{
0: 20,
1: 20,
2: 20,
3: 32,
4: 32,
5: 32,
}
func messageSize(version int, pathLen int) int {
if pathLen == 0 {
pathLen = FindMaxPathLen("")
}
header := 4 + 4 + pad(pathLen)
createSize := 4 + 4 + 4 + 4 + 4 + pad(pathLen)*2 + pad(maxTermLen[version])
attachSize0 := pad(maxLoginLen+1) + 4 + 4 + 4 + 4 + pad(20) + 4 + 4 + pad(maxTermLen[version]+1) + 4
detachSize := pad(maxLoginLen+1) + 4
commandSize := pad(maxLoginLen+1) + 4 + pad(pathLen+1) + 4 + pad(20) + pad(pathLen)
messageSize := pad(pathLen * 2)
switch version {
case 0:
return header + max(createSize, attachSize0, detachSize, commandSize, messageSize)
case 1, 2, 3, 4:
attachSize := attachSize0 + 4
return header + max(createSize, attachSize, detachSize, commandSize, messageSize)
case 5:
createSize5 := 4 + 4 + 4 + 4 + 4 + pad(pathLen)*2 + pad(maxTermLen[version]+1)
attachSize := attachSize0 + 4
return header + max(createSize5, attachSize, detachSize, commandSize, messageSize)
}
panic(fmt.Errorf("Unknown screen protocol version %d", version))
}
func makeAttachMessage(version int, ttyName string, lines int, columns int) []byte {
u, err := user.Current()
if err != nil {
panic(err)
}
pathLen := FindMaxPathLen("")
uname := chop([]byte(u.Username), maxLoginLen)
w := bytes.NewBuffer(nil)
tty := chop([]byte(ttyName), pathLen)
binary.Write(w, endian, uint32(('m'<<24)|('s'<<16)|('g'<<8)|version))
binary.Write(w, endian, uint32(msgAttach))
w.Write(tty)
w.Write(bytes.Repeat([]byte{0}, pathLen-len(tty)))
w.Write(bytes.Repeat([]byte{0}, pad(pathLen)-pathLen))
w.Write(uname)
w.Write(bytes.Repeat([]byte{0}, pad(maxLoginLen+1)-len(uname)))
binary.Write(w, endian, int32(os.Getpid()))
binary.Write(w, endian, uint32(0))
binary.Write(w, endian, uint32(lines))
binary.Write(w, endian, uint32(columns))
w.Write(bytes.Repeat([]byte{0}, 20))
binary.Write(w, endian, int32(0))
binary.Write(w, endian, int32(0))
w.WriteString("screen")
w.Write(bytes.Repeat([]byte{0}, pad(maxTermLen[version]+1)-6))
binary.Write(w, endian, uint32(0))
switch version {
case 0:
case 1, 2, 3, 4, 5: // for attach messages these are identical except for maxTermLen identical
binary.Write(w, endian, uint32(0)) // version one added detachfirst
default:
panic(fmt.Sprintf("Unknown screen protocol version %d", version))
}
w.Write(bytes.Repeat([]byte{0}, messageSize(version, 0)-len(w.Bytes())))
return w.Bytes()
}
// write a message to the screen socket
func screenSocketWrite(data []byte) {
info, err := os.Stat(os.Args[1])
if os.IsNotExist(err) {
fmt.Printf("File not found: %s\n", os.Args[1])
os.Exit(1)
} else if err != nil {
panic(err)
}
var c io.WriteCloser
if info.Mode()&os.ModeNamedPipe != 0 {
c, err = os.OpenFile(os.Args[1], os.O_RDWR, info.Mode())
if err != nil {
panic(err)
}
} else if info.Mode()&os.ModeSocket != 0 {
c, err = net.Dial("unix", os.Args[1])
if err != nil {
panic(err)
}
}
defer c.Close()
_, err = c.Write(data)
if err != nil {
panic(err)
}
}
func forwardTty(pt *os.File, conn *websocket.Conn) {
last := time.Now().UnixNano()
buffer := make([]byte, 1024)
for {
n, err := pt.Read(buffer[12:])
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if n == 0 {
time.Sleep(1 * time.Millisecond)
}
now := time.Now().UnixNano()
diff := now - last
diffSeconds := int32(diff / 1e9)
diffMicros := int32((diff / 100) % 1e6)
// unlilkey, but time is weird
if diffMicros < 0 {
diffMicros += 1e6
}
binary.Write(bytes.NewBuffer(buffer[:0]), binary.LittleEndian, diffSeconds)
binary.Write(bytes.NewBuffer(buffer[:4]), binary.LittleEndian, diffMicros)
binary.Write(bytes.NewBuffer(buffer[:8]), binary.LittleEndian, int32(n))
w, err := conn.NextWriter(websocket.BinaryMessage)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
w.Write(buffer[:n+12])
w.Close()
last = now
}
}
func signalHandler(ch chan os.Signal, termFile *os.File, cont chan bool, closed chan bool) {
for {
sig := <-ch
if sig == syscall.SIGINT {
fmt.Println("Caught SIGINT, shutting down")
// close everything gracefully
termFile.Close()
closed <- true
continue
}
if sig == syscall.SIGCONT {
signal.Reset(syscall.SIGCONT)
cont <- true
continue
}
if sig == syscall.SIGHUP {
closed <- true
os.Exit(0)
}
fmt.Printf("Unknown signal %v\n", sig)
}
}
// process close messages and discard others
func processWebsocketIncoming(conn *websocket.Conn, closed chan bool) {
for {
if _, _, err := conn.NextReader(); err != nil {
fmt.Printf("WebSocket returned %v\n", err)
conn.Close()
break
}
}
closed <- true
}
func attach(termFile *os.File, version int) {
m := makeAttachMessage(version, termFile.Name(), 132, 50)
screenSocketWrite(m)
}
func url() string {
if os.Getenv("ENV") == "dev" {
return devURL
}
return prodURL
}
func viewURL() string {
if os.Getenv("ENV") == "dev" {
return viewDevURL
}
return viewProdURL
}
func newID() string {
buffer := make([]byte, 15)
rand.Read(buffer)
id := base32.StdEncoding.EncodeToString(buffer)
return id
}
func newWebSocket(id string) *websocket.Conn {
headers := http.Header{}
hostname, _ := os.Hostname()
headers.Add("Origin", hostname)
headers.Add("Sec-WebSocket-Protocol", protocol)
conn, _, err := websocket.DefaultDialer.Dial(url()+id, headers)
if err != nil {
panic(err)
}
return conn
}
func main() {
if len(os.Args) != 2 {
fmt.Println("screenrun plays GNU screen sessions on a remote web server")
fmt.Println("usage: ./screenrun screenfile")
fmt.Println("\nExample: ./screenrun $HOME/.screen/* # picks up the first screen")
os.Exit(1)
}
pt, termFile, err := pty.Open()
if err != nil {
panic(err)
}
defer termFile.Close()
defer pt.Close()
ch := make(chan os.Signal)
signal.Notify(ch, sigBye, sigPowerBye, sigLock, syscall.SIGINT, syscall.SIGWINCH, syscall.SIGSTOP, syscall.SIGALRM, syscall.SIGCONT)
cont := make(chan bool)
closed := make(chan bool)
go signalHandler(ch, termFile, cont, closed)
fmt.Printf("Attaching to screen %s...\n", os.Args[1])
// try every version in order
version := 5
found := false
for !found {
if version < 0 {
fmt.Printf("Could not negotiate a version\n")
os.Exit(1)
}
fmt.Printf("Trying version %d\n", version)
attach(termFile, version)
timer := time.NewTimer(100 * time.Millisecond)
select {
case <-cont:
// okay, we're attached
found = true
break
case <-timer.C:
// timeout, try the next version
version--
}
}
fmt.Printf("Attached with version %d\n", version)
id := newID()
conn := newWebSocket(id)
// wait for a message from the server to know that it is setup
_, _, err = conn.ReadMessage()
if err != nil {
panic(err)
}
go forwardTty(pt, conn)
fmt.Printf("View at %s\n", viewURL()+id)
go processWebsocketIncoming(conn, closed)
// wait for the connection to close
<-closed
os.Exit(0)
}