Skip to content

Kill and restart the browser #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 29, 2016
48 changes: 48 additions & 0 deletions killbrowser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"errors"
"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)

if data.Process != "chrome" && data.Process != "chrom" {
c.JSON(http.StatusBadRequest, errors.New("You can't kill the process"+data.Process))
return
}

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
}
}

if data.Action == "restart" {
_, err := startBrowser(command, data.URL)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
}

}
13 changes: 13 additions & 0 deletions killbrowser_darwin.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions killbrowser_linux.go
Original file line number Diff line number Diff line change
@@ -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()
}
17 changes: 17 additions & 0 deletions killbrowser_windows.go
Original file line number Diff line number Diff line change
@@ -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("Taskkill", "/F", "/IM", process+".exe")
return cmd.Output()
}

func startBrowser(command []byte, url string) ([]byte, error) {
cmd := exec.Command("cmd", "/C", "start", string(command), url)
return cmd.Output()
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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) {