-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (68 loc) · 1.32 KB
/
main.go
File metadata and controls
77 lines (68 loc) · 1.32 KB
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
package main // import "github.com/raohwork/prpr"
import (
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
func main() {
secret := os.Getenv("SECRET")
firefox := os.Getenv("FIREFOX")
if firefox == "" {
firefox = "firefox"
}
bind := os.Getenv("BIND")
if bind == "" {
bind = ":9801"
}
ch := make(chan error)
go runFX(firefox, ch)
// wait few second for firefox to start
time.Sleep(10 * time.Second)
go runWeb(bind, secret, ch)
log.Fatal(<-ch)
}
func runFX(firefox string, ch chan error) {
// starting firefox
opts := []string{
"--marionette",
"--safe-mode",
}
if os.Getenv("DEBUG_FIREFOX") == "" {
opts = append(opts, "--headless")
}
if x := os.Getenv("FIREFOX_OPTS"); x != "" {
xarr := strings.Split(x, " ")
for _, item := range xarr {
item = strings.TrimSpace(item)
if item == "" {
continue
}
opts = append(opts, item)
}
}
cmd := exec.Command(
firefox,
opts...,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
ch <- cmd.Run()
}
func runWeb(bind, secret string, ch chan error) {
maxStr := os.Getenv("QUEUE_SIZE")
max, err := strconv.Atoi(maxStr)
if err != nil || max < 1 {
max = 1
}
h := New("", 0, max)
if secret != "" {
h.Secret = secret
}
http.HandleFunc("/grab", h.Accept)
log.Print("Starting prpr...")
ch <- http.ListenAndServe(bind, nil)
}