Repository navigation
Expand file tree
/
Copy pathhijack_response.go
More file actions
155 lines (137 loc) · 4.02 KB
/
Copy pathhijack_response.go
File metadata and controls
155 lines (137 loc) · 4.02 KB
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
package rod
import (
"encoding/base64"
"io"
"net/http"
"regexp"
"github.com/projectdiscovery/rod/lib/proto"
)
type hijackResponseHandler struct {
pattern string
regexp *regexp.Regexp
handler func(*HijackResponseStage)
resource proto.NetworkResourceType
}
// HijackResponseStage is a request paused after the browser received the
// response headers. Unlike [Hijack.LoadResponse] the browser itself made the
// request, so cookies, proxy, TLS, and HTTP/2 behave exactly as they would
// without interception.
type HijackResponseStage struct {
Event *proto.FetchRequestPaused
client proto.Client
maxBody int
done bool
}
// URL of the request.
func (s *HijackResponseStage) URL() string { return s.Event.Request.URL }
// StatusCode of the response. Zero when the request failed.
func (s *HijackResponseStage) StatusCode() int {
if s.Event.ResponseStatusCode == nil {
return 0
}
return *s.Event.ResponseStatusCode
}
// Header returns the response headers.
func (s *HijackResponseStage) Header() http.Header {
h := http.Header{}
for _, e := range s.Event.ResponseHeaders {
h.Add(e.Name, e.Value)
}
return h
}
// Body fetches the response body from the browser. It fails when the body
// is larger than the router's MaxBody.
func (s *HijackResponseStage) Body() ([]byte, error) {
res, err := proto.FetchGetResponseBody{RequestID: s.Event.RequestID}.Call(s.c())
if err != nil {
return nil, err
}
limit := s.maxBody
if limit <= 0 {
limit = DefaultHijackBodyLimit
}
if !res.Base64Encoded {
if len(res.Body) > limit {
return nil, io.ErrShortBuffer
}
return []byte(res.Body), nil
}
if base64.StdEncoding.DecodedLen(len(res.Body)) > limit+3 {
return nil, io.ErrShortBuffer
}
return base64.StdEncoding.DecodeString(res.Body)
}
// Continue sends the original response to the page.
func (s *HijackResponseStage) Continue() error {
s.done = true
return proto.FetchContinueResponse{RequestID: s.Event.RequestID}.Call(s.c())
}
// Fulfill replaces the response. A nil header keeps the original headers.
func (s *HijackResponseStage) Fulfill(code int, header http.Header, body []byte) error {
s.done = true
entries := s.Event.ResponseHeaders
if header != nil {
entries = make([]*proto.FetchHeaderEntry, 0, len(header))
for k, vs := range header {
for _, v := range vs {
entries = append(entries, &proto.FetchHeaderEntry{Name: k, Value: v})
}
}
}
return proto.FetchFulfillRequest{
RequestID: s.Event.RequestID,
ResponseCode: code,
ResponseHeaders: entries,
Body: body,
}.Call(s.c())
}
// Fail aborts the request with reason.
func (s *HijackResponseStage) Fail(reason proto.NetworkErrorReason) error {
s.done = true
return proto.FetchFailRequest{RequestID: s.Event.RequestID, ErrorReason: reason}.Call(s.c())
}
func (s *HijackResponseStage) c() proto.Client {
return s.client
}
// AddResponse adds a handler that runs after the browser received the
// response headers for requests matching pattern. The handler should call
// one of Continue, Fulfill, or Fail; otherwise the original response is sent.
func (r *HijackRouter) AddResponse(
pattern string, resourceType proto.NetworkResourceType, handler func(*HijackResponseStage),
) error {
reg, err := regexp.Compile(proto.PatternToReg(pattern))
if err != nil {
return err
}
r.mu.Lock()
list := make([]*hijackResponseHandler, 0, len(r.response)+1)
list = append(list, r.response...)
list = append(list, &hijackResponseHandler{
pattern: pattern,
regexp: reg,
handler: handler,
resource: resourceType,
})
r.response = list
r.mu.Unlock()
return r.apply()
}
func (r *HijackRouter) handleResponse(e *proto.FetchRequestPaused) {
r.mu.RLock()
handlers := r.response
r.mu.RUnlock()
s := &HijackResponseStage{Event: e, client: r.client, maxBody: r.MaxBody}
for _, h := range handlers {
if !h.regexp.MatchString(e.Request.URL) {
continue
}
if h.resource != "" && h.resource != e.ResourceType {
continue
}
h.handler(s)
break
}
if !s.done {
_ = proto.FetchContinueResponse{RequestID: e.RequestID}.Call(r.client)
}
}