From 16c9d76eb7ac5756c61071746baf02fd3e50e409 Mon Sep 17 00:00:00 2001 From: BaerMitUmlaut Date: Mon, 8 Oct 2018 20:42:49 +0200 Subject: [PATCH 1/3] Replace firefoxheadless.sh with soft kill --- interfacer/src/browsh/firefox.go | 25 +++++++++++------------- interfacer/src/browsh/raw_text_server.go | 4 ++++ interfacer/test/http-server/setup.go | 2 +- interfacer/test/tty/setup.go | 2 +- webext/contrib/firefoxheadless.sh | 8 -------- 5 files changed, 17 insertions(+), 24 deletions(-) delete mode 100755 webext/contrib/firefoxheadless.sh diff --git a/interfacer/src/browsh/firefox.go b/interfacer/src/browsh/firefox.go index da6d6c1e..c14f3733 100644 --- a/interfacer/src/browsh/firefox.go +++ b/interfacer/src/browsh/firefox.go @@ -8,7 +8,6 @@ import ( "net" "os" "os/exec" - "regexp" "runtime" "strings" "time" @@ -21,6 +20,7 @@ import ( var ( marionette net.Conn ffCommandCount = 0 + pid = -1 defaultFFPrefs = map[string]string{ "startup.homepage_welcome_url.additional": "''", "devtools.errorconsole.enabled": "true", @@ -52,7 +52,9 @@ var ( ) func startHeadlessFirefox() { - checkIfFirefoxIsAlreadyRunning() + if pid != -1 { + Shutdown(errors.New("A headless Firefox is already running")) + } Log("Starting Firefox in headless mode") firefoxPath := ensureFirefoxBinary() ensureFirefoxVersion(firefoxPath) @@ -78,23 +80,13 @@ func startHeadlessFirefox() { if err := firefoxProcess.Start(); err != nil { Shutdown(err) } + pid = firefoxProcess.Process.Pid in := bufio.NewScanner(stdout) for in.Scan() { Log("FF-CONSOLE: " + in.Text()) } } -func checkIfFirefoxIsAlreadyRunning() { - if runtime.GOOS == "windows" { - return - } - processes := Shell("ps aux") - r, _ := regexp.Compile("firefox.*--headless") - if r.MatchString(processes) { - Shutdown(errors.New("A headless Firefox is already running")) - } -} - func ensureFirefoxBinary() string { path := viper.GetString("firefox.path") if path == "firefox" { @@ -150,9 +142,13 @@ func versionOrdinal(version string) string { func startWERFirefox() { Log("Attempting to start headless Firefox with `web-ext`") var rootDir = Shell("git rev-parse --show-toplevel") + firefoxBinary, isSet := os.LookupEnv("FIREFOX") + if !isSet { + firefoxBinary = "firefox" + } args := []string{ "run", - "--firefox=" + rootDir + "/webext/contrib/firefoxheadless.sh", + "--firefox=" + firefoxBinary, "--verbose", "--no-reload", } @@ -166,6 +162,7 @@ func startWERFirefox() { if err := firefoxProcess.Start(); err != nil { Shutdown(err) } + pid = firefoxProcess.Process.Pid in := bufio.NewScanner(stdout) for in.Scan() { if strings.Contains(in.Text(), "JavaScript strict") || diff --git a/interfacer/src/browsh/raw_text_server.go b/interfacer/src/browsh/raw_text_server.go index 684bd08e..b48327c2 100644 --- a/interfacer/src/browsh/raw_text_server.go +++ b/interfacer/src/browsh/raw_text_server.go @@ -80,6 +80,10 @@ func HTTPServerStart() { } } +func HTTPServerStop() { + quitFirefox() +} + func setupRateLimiter() *stdlib.Middleware { rate, err := limiter.NewRateFromFormatted(viper.GetString("http-server.rate-limit")) if err != nil { diff --git a/interfacer/test/http-server/setup.go b/interfacer/test/http-server/setup.go index e3d3d919..6e2ea17c 100644 --- a/interfacer/test/http-server/setup.go +++ b/interfacer/test/http-server/setup.go @@ -72,5 +72,5 @@ var _ = ginkgo.BeforeSuite(func() { }) var _ = ginkgo.AfterSuite(func() { - browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill") + browsh.HTTPServerStop() }) diff --git a/interfacer/test/tty/setup.go b/interfacer/test/tty/setup.go index 71ec0797..5a089638 100644 --- a/interfacer/test/tty/setup.go +++ b/interfacer/test/tty/setup.go @@ -225,5 +225,5 @@ var _ = ginkgo.BeforeSuite(func() { }) var _ = ginkgo.AfterSuite(func() { - browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill") + browsh.HTTPServerStop() }) diff --git a/webext/contrib/firefoxheadless.sh b/webext/contrib/firefoxheadless.sh deleted file mode 100755 index 82f3fdfd..00000000 --- a/webext/contrib/firefoxheadless.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -if [[ "$1" = "kill" ]]; then - kill $(ps aux|grep headless|grep 'profile /tmp'| tr -s ' ' | cut -d ' ' -f2) -else - FIREFOX_BIN=${FIREFOX:-firefox} - $FIREFOX_BIN --headless "$@" -fi From 89e0d78e06f4c49ab9f73822fc9c700f167b83da Mon Sep 17 00:00:00 2001 From: BaerMitUmlaut Date: Wed, 10 Oct 2018 12:50:50 +0200 Subject: [PATCH 2/3] Kill process instead of sending command --- interfacer/src/browsh/firefox.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/interfacer/src/browsh/firefox.go b/interfacer/src/browsh/firefox.go index c14f3733..03d14f7a 100644 --- a/interfacer/src/browsh/firefox.go +++ b/interfacer/src/browsh/firefox.go @@ -20,7 +20,7 @@ import ( var ( marionette net.Conn ffCommandCount = 0 - pid = -1 + pid = -1 defaultFFPrefs = map[string]string{ "startup.homepage_welcome_url.additional": "''", "devtools.errorconsole.enabled": "true", @@ -306,5 +306,8 @@ func startFirefox() { } func quitFirefox() { - sendFirefoxCommand("quitApplication", map[string]interface{}{}) + process, err := os.FindProcess(pid) + if err == nil { + process.Kill() + } } From aaccca3c2cbe65f7203518df32c5277f3695e819 Mon Sep 17 00:00:00 2001 From: BaerMitUmlaut Date: Wed, 10 Oct 2018 13:09:41 +0200 Subject: [PATCH 3/3] Fix missing headless flag --- interfacer/src/browsh/firefox.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/interfacer/src/browsh/firefox.go b/interfacer/src/browsh/firefox.go index 03d14f7a..906caa22 100644 --- a/interfacer/src/browsh/firefox.go +++ b/interfacer/src/browsh/firefox.go @@ -142,13 +142,9 @@ func versionOrdinal(version string) string { func startWERFirefox() { Log("Attempting to start headless Firefox with `web-ext`") var rootDir = Shell("git rev-parse --show-toplevel") - firefoxBinary, isSet := os.LookupEnv("FIREFOX") - if !isSet { - firefoxBinary = "firefox" - } args := []string{ "run", - "--firefox=" + firefoxBinary, + "--firefox=" + rootDir + "/webext/contrib/firefoxheadless.sh", "--verbose", "--no-reload", }