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
12 changes: 7 additions & 5 deletions chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -127,17 +128,16 @@ func headlessFlag(config Config) ([]chromedp.ExecAllocatorOption, func() error,

if config.Headless {
// Create virtual display
frameBuffer, err := newFrameBuffer("1920x1080x24")
frameBuffer, err := newFrameBuffer("1280x1024x16")
if err != nil {
return nil, nil, err
}

cleanup = frameBuffer.Stop

opts = append(opts,
// chromedp.Flag("headless", true),
chromedp.Flag("window-size", "1920,1080"),
chromedp.Flag("start-maximized", true),
// chromedp.Flag("window-size", "1920,1080"),
// chromedp.Flag("start-maximized", true),
chromedp.Flag("no-sandbox", true),
chromedp.ModifyCmdFunc(func(cmd *exec.Cmd) {
cmd.Env = append(cmd.Env, "DISPLAY=:"+frameBuffer.Display)
Expand All @@ -154,7 +154,9 @@ func headlessFlag(config Config) ([]chromedp.ExecAllocatorOption, func() error,
}

// When the parent process dies (Go), kill the child as well.
cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL
if runtime.GOOS == "linux" {
cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL
}
}),
)
}
Expand Down
47 changes: 25 additions & 22 deletions display.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"runtime"
"syscall"
"time"

"golang.org/x/exp/slog"
)
Expand Down Expand Up @@ -60,15 +58,17 @@ func newFrameBuffer(screenSize string) (*frameBuffer, error) { //nolint:funlen

// Xvfb will print the display on which it is listening to file descriptor 3,
// for which we provide a pipe.
arguments := []string{"-displayfd", "3", "-nolisten", "tcp"}
// arguments := []string{"-displayfd", "3", "-nolisten", "tcp"}
arguments := []string{":3"}

if screenSize != "" {
screenSizeExpression := regexp.MustCompile(`^\d+x\d+(?:x\d+)$`)
if !screenSizeExpression.MatchString(screenSize) {
return nil, fmt.Errorf("invalid screen size: expected 'WxH[xD]', got %q", screenSize)
}

arguments = append(arguments, "-screen", "0", screenSize)
// arguments = append(arguments, "-screen", "0", screenSize)
arguments = append(arguments, "-screen", ":3", screenSize)
}

xvfb := exec.Command("Xvfb", arguments...)
Expand All @@ -79,7 +79,9 @@ func newFrameBuffer(screenSize string) (*frameBuffer, error) { //nolint:funlen
xvfb.SysProcAttr = new(syscall.SysProcAttr)
}

xvfb.SysProcAttr.Pdeathsig = syscall.SIGKILL
if runtime.GOOS == "linux" {
xvfb.SysProcAttr.Pdeathsig = syscall.SIGKILL
}

if err := xvfb.Start(); err != nil {
return nil, err
Expand All @@ -102,22 +104,23 @@ func newFrameBuffer(screenSize string) (*frameBuffer, error) { //nolint:funlen
ch <- resp{s, err}
}()

var display string
select {
case resp := <-ch:
if resp.err != nil {
return nil, resp.err
}

display = strings.TrimSpace(resp.display)
if _, err := strconv.Atoi(display); err != nil {
return nil, errors.New("xvfb did not print the display number")
}

case <-time.After(10 * time.Second):
return nil, errors.New("timeout waiting for Xvfb")
}

// var display string
// select {
// case resp := <-ch:
// if resp.err != nil {
// return nil, resp.err
// }
//
// display = strings.TrimSpace(resp.display)
// if _, err := strconv.Atoi(display); err != nil {
// return nil, errors.New("xvfb did not print the display number")
// }
//
// case <-time.After(10 * time.Second):
// return nil, errors.New("timeout waiting for Xvfb")
// }

display := "3"
xauth := exec.Command("xauth", "generate", ":"+display, ".", "trusted") //nolint:gosec
xauth.Env = append(xauth.Env, "XAUTHORITY="+authPath)
// Make make this conditional?
Expand Down