-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
92 lines (76 loc) · 2 KB
/
proxy.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
package k6proxy
import (
"errors"
"net/http"
"net/url"
"go.k6.io/k6/js/modules"
)
// init is called by the Go runtime at application startup.
func init() {
modules.Register("k6/x/proxy", New())
}
type (
// RootModule is the global module instance that will create module
// instances for each VU.
RootModule struct{}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
// vu provides methods for accessing internal k6 objects for a VU
vu modules.VU
// proxier is the exported type
proxier *Proxy
}
)
// Ensure the interfaces are implemented correctly.
var (
_ modules.Instance = &ModuleInstance{}
_ modules.Module = &RootModule{}
)
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the modules.Module interface returning a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
proxier: &Proxy{
vu: vu,
},
}
}
// Compare is the type for our custom API.
type Proxy struct {
vu modules.VU
oldProxyFunc func(*http.Request) (*url.URL, error)
}
func (p *Proxy) SetProxy(proxyURL string) error {
if p.vu.State() == nil || p.vu.State().Transport == nil {
return errors.New("unable to set proxy: unexpected nil transport")
}
tp, ok := p.vu.State().Transport.(*http.Transport)
if !ok {
return errors.New("unable to set proxy: other extensions might highjack the http already")
}
u, err := url.Parse(proxyURL)
if err != nil {
return err
}
if p.oldProxyFunc == nil {
p.oldProxyFunc = tp.Proxy
}
tp.Proxy = http.ProxyURL(u)
return nil
}
func (p *Proxy) ClearProxy() {
if p.oldProxyFunc == nil {
return
}
p.vu.State().Transport.(*http.Transport).Proxy = p.oldProxyFunc
}
// Exports implements the modules.Instance interface and returns the exported types for the JS module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Default: mi.proxier,
}
}