Skip to content

Commit 986d3e3

Browse files
committed
Merge pull request #63 from arduino/killbrowser
Kill and restart the browser
2 parents 1b8d7eb + 58f468d commit 986d3e3

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

killbrowser.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func killBrowserHandler(c *gin.Context) {
11+
12+
var data struct {
13+
Action string `json:"action"`
14+
Process string `json:"process"`
15+
URL string `json:"url"`
16+
}
17+
18+
c.BindJSON(&data)
19+
20+
if data.Process != "chrome" && data.Process != "chrom" {
21+
c.JSON(http.StatusBadRequest, errors.New("You can't kill the process"+data.Process))
22+
return
23+
}
24+
25+
command, err := findBrowser(data.Process)
26+
27+
if err != nil {
28+
c.JSON(http.StatusInternalServerError, err.Error())
29+
return
30+
}
31+
32+
if data.Action == "kill" || data.Action == "restart" {
33+
_, err := killBrowser(data.Process)
34+
if err != nil {
35+
c.JSON(http.StatusInternalServerError, err.Error())
36+
return
37+
}
38+
}
39+
40+
if data.Action == "restart" {
41+
_, err := startBrowser(command, data.URL)
42+
if err != nil {
43+
c.JSON(http.StatusInternalServerError, err.Error())
44+
return
45+
}
46+
}
47+
48+
}

killbrowser_darwin.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func findBrowser(process string) ([]byte, error) {
4+
return nil, nil
5+
}
6+
7+
func killBrowser(process string) ([]byte, error) {
8+
return nil, nil
9+
}
10+
11+
func startBrowser(command []byte, url string) ([]byte, error) {
12+
return nil, nil
13+
}

killbrowser_linux.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
)
7+
8+
func findBrowser(process string) ([]byte, error) {
9+
ps := exec.Command("ps", "-A", "-o", "command")
10+
grep := exec.Command("grep", process)
11+
head := exec.Command("head", "-n", "1")
12+
13+
return pipe_commands(ps, grep, head)
14+
}
15+
16+
func killBrowser(process string) ([]byte, error) {
17+
cmd := exec.Command("pkill", "-9", process)
18+
return cmd.Output()
19+
}
20+
21+
func startBrowser(command []byte, url string) ([]byte, error) {
22+
parts := strings.Split(string(command), " ")
23+
cmd := exec.Command(parts[0], url)
24+
return cmd.Output()
25+
}

killbrowser_windows.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "os/exec"
4+
5+
func findBrowser(process string) ([]byte, error) {
6+
return []byte(process), nil
7+
}
8+
9+
func killBrowser(process string) ([]byte, error) {
10+
cmd := exec.Command("Taskkill", "/F", "/IM", process+".exe")
11+
return cmd.Output()
12+
}
13+
14+
func startBrowser(command []byte, url string) ([]byte, error) {
15+
cmd := exec.Command("cmd", "/C", "start", string(command), url)
16+
return cmd.Output()
17+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ func main() {
241241
r.Handle("WS", "/socket.io/", socketHandler)
242242
r.Handle("WSS", "/socket.io/", socketHandler)
243243
r.GET("/info", infoHandler)
244+
r.POST("/killbrowser", killBrowserHandler)
245+
244246
go func() {
245247
// check if certificates exist; if not, use plain http
246248
if _, err := os.Stat(filepath.Join(dest, "cert.pem")); os.IsNotExist(err) {

0 commit comments

Comments
 (0)