forked from aerokube/selenoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
408 lines (377 loc) · 13.7 KB
/
main.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
402
403
404
405
406
407
408
package main
import (
"encoding/json"
"flag"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"golang.org/x/net/websocket"
"fmt"
"path/filepath"
ggr "github.com/aerokube/ggr/config"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/logger"
"github.com/aerokube/selenoid/protect"
"github.com/aerokube/selenoid/server"
"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/selenoid/upload"
"github.com/aerokube/util"
"github.com/aerokube/util/docker"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)
var (
hostname string
disableDocker bool
disableQueue bool
enableFileUpload bool
listen string
timeout time.Duration
maxTimeout time.Duration
newSessionAttemptTimeout time.Duration
sessionDeleteTimeout time.Duration
serviceStartupTimeout time.Duration
gracefulPeriod time.Duration
limit int
retryCount int
containerNetwork string
sessions = session.NewMap()
confPath string
logConfPath string
captureDriverLogs bool
disablePrivileged bool
videoOutputDir string
videoRecorderImage string
logOutputDir string
saveAllLogs bool
ggrHost *ggr.Host
conf *config.Config
queue *protect.Queue
manager service.Manager
cli *client.Client
// Added by om
testsessionID string
nerveServer string
resultID string
startTime = time.Now()
version bool
gitRevision = "HEAD"
buildStamp = "unknown"
)
func init() {
var mem service.MemLimit
var cpu service.CpuLimit
flag.StringVar(&nerveServer, "nerveServer", "", "nerve server ")
flag.StringVar(&testsessionID, "testsessionID", "", "test session id")
flag.StringVar(&resultID, "resultID", "", "result id")
flag.BoolVar(&disableDocker, "disable-docker", true, "Disable docker support")
flag.BoolVar(&disableQueue, "disable-queue", false, "Disable wait queue")
flag.BoolVar(&enableFileUpload, "enable-file-upload", false, "File upload support")
flag.StringVar(&listen, "listen", ":4444", "Network address to accept connections")
flag.StringVar(&confPath, "conf", "config/browsers.json", "Browsers configuration file")
flag.StringVar(&logConfPath, "log-conf", "", "Container logging configuration file")
flag.IntVar(&limit, "limit", 5, "Simultaneous container runs")
flag.IntVar(&retryCount, "retry-count", 1, "New session attempts retry count")
flag.DurationVar(&timeout, "timeout", 60*time.Second, "Session idle timeout in time.Duration format")
flag.DurationVar(&maxTimeout, "max-timeout", 1*time.Hour, "Maximum valid session idle timeout in time.Duration format")
flag.DurationVar(&newSessionAttemptTimeout, "session-attempt-timeout", 30*time.Second, "New session attempt timeout in time.Duration format")
flag.DurationVar(&sessionDeleteTimeout, "session-delete-timeout", 30*time.Second, "Session delete timeout in time.Duration format")
flag.DurationVar(&serviceStartupTimeout, "service-startup-timeout", 30*time.Second, "Service startup timeout in time.Duration format")
flag.BoolVar(&version, "version", false, "Show version and exit")
flag.Var(&mem, "mem", "Containers memory limit e.g. 128m or 1g")
flag.Var(&cpu, "cpu", "Containers cpu limit as float e.g. 0.2 or 1.0")
flag.StringVar(&containerNetwork, "container-network", service.DefaultContainerNetwork, "Network to be used for containers")
flag.BoolVar(&captureDriverLogs, "capture-driver-logs", false, "Whether to add driver process logs to Selenoid output")
flag.BoolVar(&disablePrivileged, "disable-privileged", false, "Whether to disable privileged container mode")
flag.StringVar(&videoOutputDir, "video-output-dir", "video", "Directory to save recorded video to")
flag.StringVar(&videoRecorderImage, "video-recorder-image", "selenoid/video-recorder:latest-release", "Image to use as video recorder")
flag.StringVar(&logOutputDir, "log-output-dir", "", "Directory to save session log to")
flag.BoolVar(&saveAllLogs, "save-all-logs", false, "Whether to save all logs without considering capabilities")
flag.DurationVar(&gracefulPeriod, "graceful-period", 300*time.Second, "graceful shutdown period in time.Duration format, e.g. 300s or 500ms")
flag.Parse()
if version {
showVersion()
os.Exit(0)
}
// fmt.Printf("saveAllLogs - %t", saveAllLogs)
var err error
hostname, err = os.Hostname()
if err != nil {
log.Fatalf("[-] [INIT] [%s: %v]", os.Args[0], err)
}
if ggrHostEnv := os.Getenv("GGR_HOST"); ggrHostEnv != "" {
ggrHost = parseGgrHost(ggrHostEnv)
}
queue = protect.New(limit, disableQueue)
conf = config.NewConfig()
err = conf.Load(confPath, logConfPath)
if err != nil {
log.Fatalf("[-] [INIT] [%s: %v]", os.Args[0], err)
}
onSIGHUP(func() {
err := conf.Load(confPath, logConfPath)
if err != nil {
log.Info("[-] [INIT] [%s: %v]", os.Args[0], err)
}
})
inDocker := false
_, err = os.Stat("/.dockerenv")
if err == nil {
inDocker = true
}
if nerveServer == "" {
log.Error("We cant start selenium without nerve server ")
os.Exit(1)
}
if testsessionID == "" {
log.Error("We cant start selenium without valid test session ID ")
os.Exit(1)
}
logger.Setup(nerveServer, testsessionID, resultID)
if !disableDocker {
videoOutputDir, err = filepath.Abs(videoOutputDir)
if err != nil {
log.Fatalf("[-] [INIT] [Invalid video output dir %s: %v]", videoOutputDir, err)
}
err = os.MkdirAll(videoOutputDir, os.FileMode(0644))
if err != nil {
log.Fatalf("[-] [INIT] [Failed to create video output dir %s: %v]", videoOutputDir, err)
}
log.Info("[-] [INIT] [Video Dir: %s]", videoOutputDir)
}
if logOutputDir != "" {
logOutputDir, err = filepath.Abs(logOutputDir)
if err != nil {
log.Fatalf("[-] [INIT] [Invalid log output dir %s: %v]", logOutputDir, err)
}
err = os.MkdirAll(logOutputDir, os.FileMode(0644))
if err != nil {
log.Fatalf("[-] [INIT] [Failed to create log output dir %s: %v]", logOutputDir, err)
}
if saveAllLogs {
log.Info("[-] [INIT] [Saving all logs]")
}
}
upload.Init()
environment := service.Environment{
InDocker: inDocker,
CPU: int64(cpu),
Memory: int64(mem),
Network: containerNetwork,
StartupTimeout: serviceStartupTimeout,
SessionDeleteTimeout: sessionDeleteTimeout,
CaptureDriverLogs: captureDriverLogs,
VideoOutputDir: videoOutputDir,
VideoContainerImage: videoRecorderImage,
LogOutputDir: logOutputDir,
SaveAllLogs: saveAllLogs,
Privileged: !disablePrivileged,
}
if disableDocker {
manager = &service.DefaultManager{Environment: &environment, Config: conf}
if logOutputDir != "" && captureDriverLogs {
log.Fatalf("[-] [INIT] [In drivers mode only one of -capture-driver-logs and -log-output-dir flags is allowed]")
}
return
}
dockerHost := os.Getenv("DOCKER_HOST")
if dockerHost == "" {
dockerHost = client.DefaultDockerHost
}
u, err := client.ParseHostURL(dockerHost)
if err != nil {
log.Fatalf("[-] [INIT] [%v]", err)
}
ip, _, _ := net.SplitHostPort(u.Host)
environment.IP = ip
cli, err = docker.CreateCompatibleDockerClient(
func(specifiedApiVersion string) {
log.Info("[-] [INIT] [Using Docker API version: %s]", specifiedApiVersion)
},
func(determinedApiVersion string) {
log.Info("[-] [INIT] [Your Docker API version is %s]", determinedApiVersion)
},
func(defaultApiVersion string) {
log.Info("[-] [INIT] [Did not manage to determine your Docker API version - using default version: %s]", defaultApiVersion)
},
)
if err != nil {
log.Fatalf("[-] [INIT] [New docker client: %v]", err)
}
manager = &service.DefaultManager{Environment: &environment, Client: cli, Config: conf}
}
func parseGgrHost(s string) *ggr.Host {
h, p, err := net.SplitHostPort(s)
if err != nil {
log.Fatalf("[-] [INIT] [Invalid Ggr host: %v]", err)
}
ggrPort, err := strconv.Atoi(p)
if err != nil {
log.Fatalf("[-] [INIT] [Invalid Ggr host: %v]", err)
}
host := &ggr.Host{
Name: h,
Port: ggrPort,
}
log.Info("[-] [INIT] [Will prefix all session IDs with a hash-sum: %s]", host.Sum())
return host
}
func onSIGHUP(fn func()) {
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGHUP)
go func() {
for {
<-sig
fn()
}
}()
}
var seleniumPaths = struct {
CreateSession, ProxySession string
}{
CreateSession: "/session",
ProxySession: "/session/",
}
func selenium() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc(seleniumPaths.CreateSession, queue.Try(queue.Check(queue.Protect(post(create)))))
mux.HandleFunc(seleniumPaths.ProxySession, proxy)
mux.HandleFunc(paths.Status, status)
mux.HandleFunc(paths.Welcome, welcome)
return mux
}
func post(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
next.ServeHTTP(w, r)
}
}
func ping(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(struct {
Uptime string `json:"uptime"`
LastReloadTime string `json:"lastReloadTime"`
NumRequests uint64 `json:"numRequests"`
Version string `json:"version"`
}{time.Since(startTime).String(), conf.LastReloadTime.Format(time.RFC3339), getSerial(), gitRevision})
}
func video(w http.ResponseWriter, r *http.Request) {
requestId := serial()
if r.Method == http.MethodDelete {
deleteFileIfExists(requestId, w, r, videoOutputDir, paths.Video, "DELETED_VIDEO_FILE")
return
}
user, remote := util.RequestInfo(r)
if _, ok := r.URL.Query()[jsonParam]; ok {
listFilesAsJson(requestId, w, videoOutputDir, "VIDEO_ERROR")
return
}
log.Info("[%d] [VIDEO_LISTING] [%s] [%s]", requestId, user, remote)
fileServer := http.StripPrefix(paths.Video, http.FileServer(http.Dir(videoOutputDir)))
fileServer.ServeHTTP(w, r)
}
func deleteFileIfExists(requestId uint64, w http.ResponseWriter, r *http.Request, dir string, prefix string, status string) {
user, remote := util.RequestInfo(r)
fileName := strings.TrimPrefix(r.URL.Path, prefix)
filePath := filepath.Join(dir, fileName)
_, err := os.Stat(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Unknown file %s", filePath), http.StatusNotFound)
return
}
err = os.Remove(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to delete file %s: %v", filePath, err), http.StatusInternalServerError)
return
}
log.Infof("[%d] [%s] [%s] [%s] [%s]", requestId, status, user, remote, fileName)
}
var paths = struct {
Video, VNC, Logs, Devtools, Download, Clipboard, File, Ping, Status, Error, WdHub, Welcome string
}{
Video: "/video/",
VNC: "/vnc/",
Logs: "/logs/",
Devtools: "/devtools/",
Download: "/download/",
Clipboard: "/clipboard/",
Status: "/status",
File: "/file",
Ping: "/ping",
Error: "/error",
WdHub: "/wd/hub",
Welcome: "/",
}
func handleSelenium(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
r.URL.Scheme = "http"
r.URL.Host = (&request{r}).localaddr()
r.URL.Path = strings.TrimPrefix(r.URL.Path, paths.WdHub)
selenium().ServeHTTP(w, r)
}
func handler() http.Handler {
root := http.NewServeMux()
loggingMiddleware := logger.LoggingMiddleware()
root.HandleFunc(paths.WdHub+"/", loggingMiddleware(handleSelenium))
root.HandleFunc(paths.Error, func(w http.ResponseWriter, r *http.Request) {
util.JsonError(w, "Session timed out or not found", http.StatusNotFound)
})
root.HandleFunc(paths.Status, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(conf.State(sessions, limit, queue.Queued(), queue.Pending()))
})
root.HandleFunc(paths.Ping, loggingMiddleware(ping))
root.Handle(paths.VNC, websocket.Handler(vnc))
root.HandleFunc(paths.Logs, logs)
root.HandleFunc(paths.Video, video)
root.HandleFunc(paths.Download, reverseProxy(func(sess *session.Session) string { return sess.HostPort.Fileserver }, "DOWNLOADING_FILE"))
root.HandleFunc(paths.Clipboard, reverseProxy(func(sess *session.Session) string { return sess.HostPort.Clipboard }, "CLIPBOARD"))
root.HandleFunc(paths.Devtools, reverseProxy(func(sess *session.Session) string { return sess.HostPort.Devtools }, "DEVTOOLS"))
if enableFileUpload {
root.HandleFunc(paths.File, fileUpload)
}
root.HandleFunc(paths.Welcome, welcome)
return root
}
func showVersion() {
log.Infof("Git Revision: %s", gitRevision)
log.Infof("UTC Build Time: %s", buildStamp)
}
func main() {
// Logfmt is a structured, key=val logging format that is easy to read and parse
// logger = logNew.NewLogfmtLogger(logNew.NewSyncWriter(os.Stderr))
// Direct any attempts to use Go's log package to our structured logger
// Log the timestamp (in UTC) and the callsite (file + line number) of the logging
// call for debugging in the future.
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
// logger.SetFormatter(&logger.JSONFormatter{})
// logger = logNew.With(logger, "ts", logNew.DefaultTimestampUTC, "loc", logNew.DefaultCaller)
// log.Printf("[-] [INIT] [Timezone: %s]", time.Local)
// log.Printf("[-] [INIT] [Listening on %s]", listen)
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
server.Server = &http.Server{
Addr: listen,
Handler: handler(),
}
e := make(chan error)
go func() {
e <- server.Server.ListenAndServe()
}()
select {
case err := <-e:
log.Fatalf("[-] [INIT] [Failed to start: %v]", err)
case <-stop:
}
server.StopServer(gracefulPeriod)
}