Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"os/exec"
)

const ttyHost = "127.0.0.1"

// randomPort returns a random port number that is not in use.
func randomPort() int {
addr, _ := net.Listen("tcp", ":0") //nolint:gosec,noctx
Expand All @@ -27,7 +29,7 @@ func randomPort() int {
func buildTtyCmd(port int, shell Shell) *exec.Cmd {
args := []string{ //nolint:prealloc
fmt.Sprintf("--port=%d", port),
"--interface", "127.0.0.1",
"--interface", ttyHost,
"-t", "rendererType=canvas",
"-t", "disableResizeOverlay=true",
"-t", "enableSixel=true",
Expand Down
6 changes: 5 additions & 1 deletion vhs.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (vhs *VHS) Start() error {
return fmt.Errorf("could not launch browser: %w", err)
}
browser := rod.New().ControlURL(u).MustConnect()
page, err := browser.Page(proto.TargetCreateTarget{URL: fmt.Sprintf("http://localhost:%d", port)})
page, err := browser.Page(proto.TargetCreateTarget{URL: ttyURL(port)})
if err != nil {
return fmt.Errorf("could not open ttyd: %w", err)
}
Expand All @@ -155,6 +155,10 @@ func (vhs *VHS) Start() error {
return nil
}

func ttyURL(port int) string {
return fmt.Sprintf("http://%s:%d", ttyHost, port)
}

// Setup sets up the VHS instance and performs the necessary actions to reflect
// the options that are default and set by the user.
func (vhs *VHS) Setup() {
Expand Down
25 changes: 25 additions & 0 deletions vhs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "testing"

func TestTtyHostMatchesBrowserURL(t *testing.T) {
const port = 19760

cmd := buildTtyCmd(port, Shell{Command: []string{"sh"}})
iface := ""
for i, arg := range cmd.Args {
if arg == "--interface" && i+1 < len(cmd.Args) {
iface = cmd.Args[i+1]
break
}
}

if iface != ttyHost {
t.Fatalf("expected ttyd to bind to %q, got %q", ttyHost, iface)
}

wantURL := "http://" + iface + ":19760"
if gotURL := ttyURL(port); gotURL != wantURL {
t.Fatalf("expected browser URL %q, got %q", wantURL, gotURL)
}
}