forked from Davincible/chromedp-undetected
-
Notifications
You must be signed in to change notification settings - Fork 1
/
display_linux.go
165 lines (129 loc) · 3.62 KB
/
display_linux.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
//go:build linux
package chromedpundetected
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"golang.org/x/exp/slog"
)
// Errors.
var (
ErrXvfbNotFound = errors.New("xvfb not found. Please install (Linux only)")
)
// frameBuffer controls an X virtual frame buffer running as a background
// process.
type frameBuffer struct {
// Display is the X11 display number that the Xvfb process is hosting
// (without the preceding colon).
Display string
// AuthPath is the path to the X11 authorization file that permits X clients
// to use the X server. This is typically provided to the client via the
// XAUTHORITY environment variable.
AuthPath string
cmd *exec.Cmd
}
// newFrameBuffer starts an X virtual frame buffer running in the background.
// FrameBufferOptions may be populated to change the behavior of the frame buffer.
func newFrameBuffer(screenSize string) (*frameBuffer, error) { //nolint:funlen
if err := exec.Command("which", "Xvfb").Run(); err != nil {
return nil, ErrXvfbNotFound
}
pipeReader, pipeWriter, err := os.Pipe()
if err != nil {
return nil, err
}
defer func() {
if err = pipeReader.Close(); err != nil {
slog.Error("failed to close pipe reader", err)
}
}()
authPath, err := tempFile("chromedp-xvfb")
if err != nil {
return nil, err
}
// 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"}
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)
}
xvfb := exec.Command("Xvfb", arguments...)
xvfb.ExtraFiles = []*os.File{pipeWriter}
xvfb.Env = append(xvfb.Env, "XAUTHORITY="+authPath)
if xvfb.SysProcAttr == nil {
xvfb.SysProcAttr = new(syscall.SysProcAttr)
}
xvfb.SysProcAttr.Pdeathsig = syscall.SIGKILL
if err := xvfb.Start(); err != nil {
return nil, err
}
if err := pipeWriter.Close(); err != nil {
return nil, err
}
type resp struct {
display string
err error
}
ch := make(chan resp)
go func() {
bufr := bufio.NewReader(pipeReader)
s, err := bufr.ReadString('\n')
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")
}
xauth := exec.Command("xauth", "generate", ":"+display, ".", "trusted") //nolint:gosec
xauth.Env = append(xauth.Env, "XAUTHORITY="+authPath)
// Make make this conditional?
xauth.Stderr = os.Stderr
xauth.Stdout = os.Stdout
if err := xauth.Run(); err != nil {
return nil, err
}
return &frameBuffer{display, authPath, xvfb}, nil
}
// Stop kills the background frame buffer process and removes the X
// authorization file.
func (f frameBuffer) Stop() error {
if err := f.cmd.Process.Kill(); err != nil {
return err
}
_ = os.Remove(f.AuthPath) //nolint:errcheck
if err := f.cmd.Wait(); err != nil && err.Error() != "signal: killed" {
return err
}
return nil
}
func tempFile(pattern string) (string, error) {
tempFile, err := os.CreateTemp("", pattern)
if err != nil {
return "", err
}
fileName := tempFile.Name()
if err := tempFile.Close(); err != nil {
return "", err
}
return fileName, nil
}