-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (130 loc) · 2.94 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
"encoding/json"
"strings"
"time"
"github.com/mafredri/cdp"
"github.com/mafredri/cdp/devtool"
"github.com/mafredri/cdp/protocol/network"
"github.com/mafredri/cdp/rpcc"
)
var requests []request
var requestsUpdated chan bool
type request struct {
time time.Time
id network.RequestID
initiator network.Initiator
req network.Request
res *network.Response
}
func main() {
requestsUpdated = make(chan bool)
connectChrome()
setupTUI()
}
func connectChrome() {
// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).
devt := devtool.New("http://127.0.0.1:9222")
pt, err := devt.Get(context.Background(), devtool.Page)
if err != nil {
pt, err = devt.Create(context.Background())
if err != nil {
panic(err)
}
}
// Initiate a new RPC connection to the Chrome DevTools Protocol target.
conn, err := rpcc.DialContext(context.Background(), pt.WebSocketDebuggerURL)
if err != nil {
panic(err)
}
//defer conn.Close() // Leaving connections open will leak memory.
c := cdp.NewClient(conn)
c.Network.Enable(context.Background(), &network.EnableArgs{})
reqWillBeSent, err := c.Network.RequestWillBeSent(context.Background())
if err != nil {
panic(err)
}
go func() {
for {
reply, err := reqWillBeSent.Recv()
if err != nil {
panic(err)
}
if !strings.HasPrefix(reply.Request.URL, "http") {
continue
}
requests = append(
requests,
request{
id: reply.RequestID,
time: reply.WallTime.Time(),
initiator: reply.Initiator,
req: reply.Request,
},
)
requestsUpdated <- true
}
}()
resReceived, err := c.Network.ResponseReceived(context.Background())
if err != nil {
panic(err)
}
go func() {
for {
reply, err := resReceived.Recv()
if err != nil {
panic(err)
}
for i, req := range requests {
if req.id == reply.RequestID {
requests[i].res = &reply.Response
requestsUpdated <- true
break
}
}
}
}()
reqWillBeSentExtraInfo, err := c.Network.RequestWillBeSentExtraInfo(context.Background())
if err != nil {
panic(err)
}
go func() {
for {
reply, err := reqWillBeSentExtraInfo.Recv()
if err != nil {
panic(err)
}
newHeaders, err := reply.Headers.Map()
if err != nil {
panic(err)
}
for i, req := range requests {
if req.id == reply.RequestID {
headers, err := req.req.Headers.Map()
if err != nil {
panic(err)
}
outer:
for key, value := range newHeaders {
for existingKey := range headers {
existingKeyLower := strings.ToLower(existingKey)
if existingKeyLower == key {
headers[existingKey] = value
continue outer
}
}
headers[key] = value
}
newJson, err := json.Marshal(headers)
if err != nil {
panic(err)
}
requests[i].req.Headers = newJson
requestsUpdated <- true
break
}
}
}
}()
}