From 5464e850167415727ce5313777828f5374a71ce4 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Thu, 28 Jan 2016 18:09:21 +0100 Subject: [PATCH 01/11] Kill the browser and restart it in linux --- killbrowser.go | 42 ++++++++++++++++++++++++++++++++++++++++++ killbrowser_linux.go | 25 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 killbrowser.go create mode 100644 killbrowser_linux.go diff --git a/killbrowser.go b/killbrowser.go new file mode 100644 index 000000000..f3a777dd3 --- /dev/null +++ b/killbrowser.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "net/http" + + "github.com/gin-gonic/gin" +) + +func killBrowserHandler(c *gin.Context) { + + var data struct { + Action string `json:"action"` + Process string `json:"process"` + URL string `json:"url"` + } + + c.BindJSON(&data) + + command, err := findBrowser(data.Process) + + log.Println(command) + + if err != nil { + c.JSON(http.StatusInternalServerError, err) + } + + if data.Action == "kill" || data.Action == "restart" { + _, err := killBrowser(data.Process) + if err != nil { + c.JSON(http.StatusInternalServerError, err) + } + } + + if data.Action == "restart" { + _, err := startBrowser(command, data.URL) + if err != nil { + c.JSON(http.StatusInternalServerError, err) + } + } + +} diff --git a/killbrowser_linux.go b/killbrowser_linux.go new file mode 100644 index 000000000..b7b12f13c --- /dev/null +++ b/killbrowser_linux.go @@ -0,0 +1,25 @@ +package main + +import ( + "os/exec" + "strings" +) + +func findBrowser(process string) ([]byte, error) { + ps := exec.Command("ps", "-A", "-o", "command") + grep := exec.Command("grep", process) + head := exec.Command("head", "-n", "1") + + return pipe_commands(ps, grep, head) +} + +func killBrowser(process string) ([]byte, error) { + cmd := exec.Command("pkill", "-9", process) + return cmd.Output() +} + +func startBrowser(command []byte, url string) ([]byte, error) { + parts := strings.Split(string(command), " ") + cmd := exec.Command(parts[0], url) + return cmd.Output() +} From b5116ec231fd8e4cff43c34684198aa487650297 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 09:30:50 +0100 Subject: [PATCH 02/11] Add kill options for windows --- killbrowser_windows.go | 17 +++++++++++++++++ main.go | 2 ++ 2 files changed, 19 insertions(+) create mode 100644 killbrowser_windows.go diff --git a/killbrowser_windows.go b/killbrowser_windows.go new file mode 100644 index 000000000..469ca9825 --- /dev/null +++ b/killbrowser_windows.go @@ -0,0 +1,17 @@ +package main + +import "os/exec" + +func findBrowser(process string) ([]byte, error) { + return []byte(process), nil +} + +func killBrowser(process string) ([]byte, error) { + cmd := exec.Command("pskill", process) + return cmd.Output() +} + +func startBrowser(command []byte, url string) ([]byte, error) { + cmd := exec.Command("start", string(command), url) + return cmd.Output() +} diff --git a/main.go b/main.go index e04ba5477..160319a3e 100755 --- a/main.go +++ b/main.go @@ -241,6 +241,8 @@ func main() { r.Handle("WS", "/socket.io/", socketHandler) r.Handle("WSS", "/socket.io/", socketHandler) r.GET("/info", infoHandler) + r.POST("/killbrowser", killBrowserHandler) + go func() { // check if certificates exist; if not, use plain http if _, err := os.Stat(filepath.Join(dest, "cert.pem")); os.IsNotExist(err) { From cfe32ca02d93b464ac0194dc7a4dfb1bb2c141f0 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 09:41:07 +0100 Subject: [PATCH 03/11] Stubs for darwin browserkiller --- killbrowser_darwin.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 killbrowser_darwin.go diff --git a/killbrowser_darwin.go b/killbrowser_darwin.go new file mode 100644 index 000000000..b0f1ec939 --- /dev/null +++ b/killbrowser_darwin.go @@ -0,0 +1,13 @@ +package main + +func findBrowser(process string) ([]byte, error) { + return nil, nil +} + +func killBrowser(process string) ([]byte, error) { + return nil, nil +} + +func startBrowser(command []byte, url string) ([]byte, error) { + return nil, nil +} From 0cd95ca9c7c08ff175eb8e773f81f3a03ab9f794 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 09:46:48 +0100 Subject: [PATCH 04/11] Use a different strategy on windows --- killbrowser_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/killbrowser_windows.go b/killbrowser_windows.go index 469ca9825..3f8c8cd73 100644 --- a/killbrowser_windows.go +++ b/killbrowser_windows.go @@ -7,7 +7,7 @@ func findBrowser(process string) ([]byte, error) { } func killBrowser(process string) ([]byte, error) { - cmd := exec.Command("pskill", process) + cmd := exec.Command("Taskkill", "/F", "/IM", process+".exe") return cmd.Output() } From 2b05c7d85861e846b106e631b0dee3e614e888cb Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 10:05:01 +0100 Subject: [PATCH 05/11] Test without killing to see what's going wrong on windows --- killbrowser.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index f3a777dd3..380be54fe 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -26,10 +26,10 @@ func killBrowserHandler(c *gin.Context) { } if data.Action == "kill" || data.Action == "restart" { - _, err := killBrowser(data.Process) - if err != nil { - c.JSON(http.StatusInternalServerError, err) - } + // _, err := killBrowser(data.Process) + // if err != nil { + // c.JSON(http.StatusInternalServerError, err) + // } } if data.Action == "restart" { From fcaa31773e9b1bfd38fbf309cb3d3bb162519cdc Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 10:25:45 +0100 Subject: [PATCH 06/11] Better handle of errors --- killbrowser.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index 380be54fe..c11151f5f 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -22,20 +22,20 @@ func killBrowserHandler(c *gin.Context) { log.Println(command) if err != nil { - c.JSON(http.StatusInternalServerError, err) + c.JSON(http.StatusInternalServerError, err.Error()) } if data.Action == "kill" || data.Action == "restart" { // _, err := killBrowser(data.Process) // if err != nil { - // c.JSON(http.StatusInternalServerError, err) + // c.JSON(http.StatusInternalServerError, err.Error()) // } } if data.Action == "restart" { _, err := startBrowser(command, data.URL) if err != nil { - c.JSON(http.StatusInternalServerError, err) + c.JSON(http.StatusInternalServerError, err.Error()) } } From 1e7b2a3bc3fab229eb3ee6fb7743d7add5e08f21 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 10:40:37 +0100 Subject: [PATCH 07/11] Launch cmd instead of directly start --- killbrowser_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/killbrowser_windows.go b/killbrowser_windows.go index 3f8c8cd73..f1e4e833c 100644 --- a/killbrowser_windows.go +++ b/killbrowser_windows.go @@ -12,6 +12,6 @@ func killBrowser(process string) ([]byte, error) { } func startBrowser(command []byte, url string) ([]byte, error) { - cmd := exec.Command("start", string(command), url) + cmd := exec.Command("cmd", "/C", "start", string(command), url) return cmd.Output() } From f7a5eabd76374406354d8a91e105dcfca35f52aa Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 10:47:30 +0100 Subject: [PATCH 08/11] Start killing the browser again --- killbrowser.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index c11151f5f..f482e2137 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -26,10 +26,10 @@ func killBrowserHandler(c *gin.Context) { } if data.Action == "kill" || data.Action == "restart" { - // _, err := killBrowser(data.Process) - // if err != nil { - // c.JSON(http.StatusInternalServerError, err.Error()) - // } + _, err := killBrowser(data.Process) + if err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + } } if data.Action == "restart" { From 2ba1652653e620671e75694ac3dbcf3d36315f5e Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 10:58:24 +0100 Subject: [PATCH 09/11] Add a bit of safeguard against malicious attacks --- killbrowser.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index f482e2137..c54625de0 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -1,7 +1,7 @@ package main import ( - "log" + "errors" "net/http" "github.com/gin-gonic/gin" @@ -17,18 +17,23 @@ func killBrowserHandler(c *gin.Context) { c.BindJSON(&data) - command, err := findBrowser(data.Process) + if data.Process != "chrome" && data.Process != "chrom" { + c.JSON(http.StatusBadRequest, errors.New("You can't kill the process"+data.Process)) + return + } - log.Println(command) + command, err := findBrowser(data.Process) if err != nil { c.JSON(http.StatusInternalServerError, err.Error()) + return } if data.Action == "kill" || data.Action == "restart" { _, err := killBrowser(data.Process) if err != nil { c.JSON(http.StatusInternalServerError, err.Error()) + return } } @@ -36,6 +41,7 @@ func killBrowserHandler(c *gin.Context) { _, err := startBrowser(command, data.URL) if err != nil { c.JSON(http.StatusInternalServerError, err.Error()) + return } } From 797caa84f90420a4aaa055c6f1eb651621db2e80 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 16:07:00 +0100 Subject: [PATCH 10/11] Debug informations --- killbrowser.go | 6 +++--- killbrowser_linux.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index c54625de0..8673627e9 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -25,14 +25,14 @@ func killBrowserHandler(c *gin.Context) { command, err := findBrowser(data.Process) if err != nil { - c.JSON(http.StatusInternalServerError, err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"when": "find", "err": err.Error()}) return } if data.Action == "kill" || data.Action == "restart" { _, err := killBrowser(data.Process) if err != nil { - c.JSON(http.StatusInternalServerError, err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"when": "kill", "err": err.Error()}) return } } @@ -40,7 +40,7 @@ func killBrowserHandler(c *gin.Context) { if data.Action == "restart" { _, err := startBrowser(command, data.URL) if err != nil { - c.JSON(http.StatusInternalServerError, err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"when": "start", "err": err.Error()}) return } } diff --git a/killbrowser_linux.go b/killbrowser_linux.go index b7b12f13c..12f2cd850 100644 --- a/killbrowser_linux.go +++ b/killbrowser_linux.go @@ -1,6 +1,7 @@ package main import ( + "log" "os/exec" "strings" ) @@ -10,16 +11,33 @@ func findBrowser(process string) ([]byte, error) { grep := exec.Command("grep", process) head := exec.Command("head", "-n", "1") + log.Println("ps command:") + log.Printf("%+v", ps) + + log.Println("grep command:") + log.Printf("%+v", grep) + + log.Println("head command:") + log.Printf("%+v", head) + return pipe_commands(ps, grep, head) } func killBrowser(process string) ([]byte, error) { cmd := exec.Command("pkill", "-9", process) + + log.Println("kill command:") + log.Printf("%+v", cmd) + return cmd.Output() } func startBrowser(command []byte, url string) ([]byte, error) { parts := strings.Split(string(command), " ") cmd := exec.Command(parts[0], url) + + log.Println("start command:") + log.Printf("%+v", cmd) + return cmd.Output() } From 58f468d7fe96cb8a2b5bbda16b960b7fae31dc7a Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Jan 2016 16:34:20 +0100 Subject: [PATCH 11/11] Revert "Debug informations" This reverts commit 797caa84f90420a4aaa055c6f1eb651621db2e80. --- killbrowser.go | 6 +++--- killbrowser_linux.go | 18 ------------------ 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/killbrowser.go b/killbrowser.go index 8673627e9..c54625de0 100644 --- a/killbrowser.go +++ b/killbrowser.go @@ -25,14 +25,14 @@ func killBrowserHandler(c *gin.Context) { command, err := findBrowser(data.Process) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"when": "find", "err": err.Error()}) + c.JSON(http.StatusInternalServerError, err.Error()) return } if data.Action == "kill" || data.Action == "restart" { _, err := killBrowser(data.Process) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"when": "kill", "err": err.Error()}) + c.JSON(http.StatusInternalServerError, err.Error()) return } } @@ -40,7 +40,7 @@ func killBrowserHandler(c *gin.Context) { if data.Action == "restart" { _, err := startBrowser(command, data.URL) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"when": "start", "err": err.Error()}) + c.JSON(http.StatusInternalServerError, err.Error()) return } } diff --git a/killbrowser_linux.go b/killbrowser_linux.go index 12f2cd850..b7b12f13c 100644 --- a/killbrowser_linux.go +++ b/killbrowser_linux.go @@ -1,7 +1,6 @@ package main import ( - "log" "os/exec" "strings" ) @@ -11,33 +10,16 @@ func findBrowser(process string) ([]byte, error) { grep := exec.Command("grep", process) head := exec.Command("head", "-n", "1") - log.Println("ps command:") - log.Printf("%+v", ps) - - log.Println("grep command:") - log.Printf("%+v", grep) - - log.Println("head command:") - log.Printf("%+v", head) - return pipe_commands(ps, grep, head) } func killBrowser(process string) ([]byte, error) { cmd := exec.Command("pkill", "-9", process) - - log.Println("kill command:") - log.Printf("%+v", cmd) - return cmd.Output() } func startBrowser(command []byte, url string) ([]byte, error) { parts := strings.Split(string(command), " ") cmd := exec.Command(parts[0], url) - - log.Println("start command:") - log.Printf("%+v", cmd) - return cmd.Output() }