This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
forked from aerokube/selenoid
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
415 lines (380 loc) · 13.1 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
409
410
411
412
413
414
415
package main
import (
"encoding/json"
"flag"
"log"
"net"
"net/http"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"github.com/docker/go-units"
"golang.org/x/net/websocket"
"fmt"
"path/filepath"
ggr "github.com/aerokube/ggr/config"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/protect"
"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"
)
type memLimit int64
func (limit *memLimit) String() string {
return units.HumanSize(float64(*limit))
}
func (limit *memLimit) Set(s string) error {
v, err := units.RAMInBytes(s)
if err != nil {
return fmt.Errorf("set memory limit: %v", err)
}
*limit = memLimit(v)
return nil
}
type cpuLimit int64
func (limit *cpuLimit) String() string {
return strconv.FormatFloat(float64(*limit/1000000000), 'f', -1, 64)
}
func (limit *cpuLimit) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("set cpu limit: %v", err)
}
*limit = cpuLimit(v * 1000000000)
return nil
}
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
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
nameSpace string
enableKubernetesSupport bool
startTime = time.Now()
version bool
gitRevision = "HEAD"
buildStamp = "unknown"
)
func init() {
var mem memLimit
var cpu cpuLimit
flag.BoolVar(&disableDocker, "disable-docker", false, "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", 60*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.BoolVar(&enableKubernetesSupport, "enable-kubernetes-support", false, "Enable kubernetes support")
flag.StringVar(&nameSpace, "namespace", "default", "Kubernetes namespace for browsers")
flag.Parse()
if version {
showVersion()
os.Exit(0)
}
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.Printf("[-] [INIT] [%s: %v]", os.Args[0], err)
}
})
cancelOnSignal()
inDocker := false
_, err = os.Stat("/.dockerenv")
if err == nil {
inDocker = true
}
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.Printf("[-] [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)
}
log.Printf("[-] [INIT] [Logs Dir: %s]", logOutputDir)
if saveAllLogs {
log.Printf("[-] [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,
NameSpace: nameSpace,
InKubernetes: enableKubernetesSupport,
}
if enableKubernetesSupport {
disableDocker = true
manager = &service.DefaultManager{Environment: &environment, Config: conf}
log.Println("[-] [INIT] [Selenoid is running with kubernetes support]")
return
}
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.Printf("[-] [INIT] [Using Docker API version: %s]", specifiedApiVersion)
},
func(determinedApiVersion string) {
log.Printf("[-] [INIT] [Your Docker API version is %s]", determinedApiVersion)
},
func(defaultApiVersion string) {
log.Printf("[-] [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.Printf("[-] [INIT] [Will prefix all session IDs with a hash-sum: %s]", host.Sum())
return host
}
func cancelOnSignal() {
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
log.Println("[-] [-] [SHUTTING_DOWN] [-] [-] [-] [-] [-] [-] [-]")
sessions.Each(func(k string, s *session.Session) {
if enableFileUpload {
os.RemoveAll(path.Join(os.TempDir(), k))
}
s.Cancel()
})
if !disableDocker {
err := cli.Close()
if err != nil {
log.Fatalf("[-] [SHUTTING_DOWN] [Error closing docker client: %v]", err)
}
}
os.Exit(0)
}()
}
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)
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.String(), getSerial(), gitRevision})
}
func video(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodDelete {
deleteFileIfExists(w, r, videoOutputDir, paths.Video, "DELETED_VIDEO_FILE")
return
}
fileServer := http.StripPrefix(paths.Video, http.FileServer(http.Dir(videoOutputDir)))
fileServer.ServeHTTP(w, r)
}
func deleteFileIfExists(w http.ResponseWriter, r *http.Request, dir string, prefix string, status string) {
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.Printf("[%d] [%s] [%s]", serial(), status, fileName)
}
var paths = struct {
Video, VNC, Logs, Devtools, Download, Clipboard, File, Ping, Status, Error, WdHub string
}{
Video: "/video/",
VNC: "/vnc/",
Logs: "/logs/",
Devtools: "/devtools/",
Download: "/download/",
Clipboard: "/clipboard/",
Status: "/status",
File: "/file",
Ping: "/ping",
Error: "/error",
WdHub: "/wd/hub",
}
func handler() http.Handler {
root := http.NewServeMux()
root.HandleFunc(paths.WdHub+"/", func(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)
})
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, ping)
root.Handle(paths.VNC, websocket.Handler(vnc))
root.Handle(paths.Devtools, websocket.Server{Handler: devtools})
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"))
if enableFileUpload {
root.HandleFunc(paths.File, fileUpload)
}
return root
}
func showVersion() {
fmt.Printf("Git Revision: %s\n", gitRevision)
fmt.Printf("UTC Build Time: %s\n", buildStamp)
}
func main() {
log.Printf("[-] [INIT] [Timezone: %s]", time.Local)
log.Printf("[-] [INIT] [Listening on %s]", listen)
log.Fatal(http.ListenAndServe(listen, handler()))
}