-
Notifications
You must be signed in to change notification settings - Fork 260
/
tty.go
47 lines (42 loc) · 1.16 KB
/
tty.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
// Package vhs tty.go spawns the ttyd process.
// It runs on the specified port and is generally meant to run in the background
// so that other processes (go-rod) can connect to the tty.
//
// xterm.js is used for rendering the terminal and can be adjusted using the Set command.
//
// Set FontFamily "DejaVu Sans Mono"
// Set FontSize 12
// Set Padding 50
package main
import (
"fmt"
"net"
"os"
"os/exec"
)
// randomPort returns a random port number that is not in use.
func randomPort() int {
addr, _ := net.Listen("tcp", ":0") //nolint:gosec
_ = addr.Close()
return addr.Addr().(*net.TCPAddr).Port
}
// buildTtyCmd builds the ttyd exec.Command on the given port.
func buildTtyCmd(port int, shell Shell) *exec.Cmd {
args := []string{
fmt.Sprintf("--port=%d", port),
"--interface", "127.0.0.1",
"-t", "rendererType=canvas",
"-t", "disableResizeOverlay=true",
"-t", "enableSixel=true",
"-t", "customGlyphs=true",
"--once", // will allow one connection and exit
"--writable",
}
args = append(args, shell.Command...)
//nolint:gosec
cmd := exec.Command("ttyd", args...)
if shell.Env != nil {
cmd.Env = append(shell.Env, os.Environ()...)
}
return cmd
}