-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
65 lines (50 loc) · 1.61 KB
/
api.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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package daemon
import (
"fmt"
"github.com/pb33f/wiretap/shared"
"net/http"
)
type wiretapTransport struct {
capturedCookieHeaders []string
originalTransport http.RoundTripper
}
func newWiretapTransport() *wiretapTransport {
return &wiretapTransport{
originalTransport: http.DefaultTransport,
}
}
func (c *wiretapTransport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := c.originalTransport.RoundTrip(r)
if resp != nil {
cookie := resp.Header.Get("Set-Cookie")
if cookie != "" {
c.capturedCookieHeaders = append(c.capturedCookieHeaders, cookie)
}
}
return resp, err
}
func (ws *WiretapService) callAPI(req *http.Request, responseChan chan *http.Response, errorChan chan error) {
tr := newWiretapTransport()
client := &http.Client{Transport: tr}
configStore, _ := ws.controlsStore.Get(shared.ConfigKey)
// create a new request from the original request, but replace the path
config := configStore.(*shared.WiretapConfiguration)
resp, err := client.Do(cloneRequest(req,
config.RedirectProtocol,
config.RedirectHost,
config.RedirectPort))
if err != nil {
errorChan <- err
close(errorChan)
}
fmt.Print(tr.capturedCookieHeaders)
if len(tr.capturedCookieHeaders) > 0 {
if resp.Header.Get("Set-Cookie") == "" {
resp.Header.Set("Set-Cookie", tr.capturedCookieHeaders[0])
}
}
responseChan <- resp
close(responseChan)
}