-
Notifications
You must be signed in to change notification settings - Fork 4
/
libchcooky.go
93 lines (77 loc) · 2.07 KB
/
libchcooky.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"log"
// "unsafe"
"net/http"
"io/ioutil"
"encoding/json"
"github.com/gorilla/websocket"
)
/*
#include <stdlib.h>
#include <string.h>
*/
import "C"
//export GetChromeCookiesFromWS
func GetChromeCookiesFromWS(wsurl *C.char) *C.char {
c, _, err := websocket.DefaultDialer.Dial(C.GoString(wsurl), nil)
if err != nil {
log.Fatal("Error in Dial: ", err)
return nil
}
defer c.Close()
c.WriteMessage(websocket.TextMessage, []byte("{\"id\":1,\"method\":\"Network.getAllCookies\"}"))
_, message, err := c.ReadMessage()
if err != nil {
log.Fatal("Error in ReadMessage: ", err)
return nil
}
var msg map[string]interface{}
err = json.Unmarshal(message, &msg)
if err != nil {
log.Fatal("Error in json.Unmarshal cookies: ", err)
return nil
}
data := msg["result"].(map[string]interface {})
data2, _ := json.Marshal(data["cookies"])
// log.Printf("Cookies: %s", data2)
// _cks := []byte(data2)
// cookies := (*C.char)C.malloc((C.int)(len(data2)) + 1)
// C.memset(cookies, (C.int)(0), (C.int)(len(data2)) + 1)
// C.memcpy(cookies, (*C.char)(unsafe.Pointer(&_cks[0])), (C.int)(string(data2)))
// return (*C.char)(unsafe.Pointer(&_cks[0]))
return C.CString(string(data2))
}
//export GetChromeDBGUrl
func GetChromeDBGUrl(url *C.char) *C.char {
resp, err := http.Get(C.GoString(url))
if err != nil {
// handle error
log.Fatal("Error in http.Get: ", err)
panic("")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// log.Printf("Resp body: %s", body)
var dat []map[string]interface{}
err = json.Unmarshal(body, &dat)
if err != nil {
log.Fatal("Error in json.Unmarshal: ", err)
return nil
}
if (len(dat) == 0){
return nil
} else {
wsurl := dat[0]["webSocketDebuggerUrl"].(string)
return C.CString(wsurl)
}
}
func main() {
log.SetFlags(0)
url := GetChromeDBGUrl(C.CString("http://localhost:43210/json"))
log.Printf("webSocketDebuggerUrl: %s", C.GoString(url))
// interrupt := make(chan os.Signal, 1)
// signal.Notify(interrupt, os.Interrupt)
cookies := GetChromeCookiesFromWS(url)
log.Printf(C.GoString(cookies))
}