forked from mkenney/go-chrome
/
cdtp.security.go
186 lines (158 loc) · 5.5 KB
/
cdtp.security.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package socket
import (
"encoding/json"
"github.com/mkenney/go-chrome/tot/security"
)
/*
SecurityProtocol provides a namespace for the Chrome Security protocol methods.
https://chromedevtools.github.io/devtools-protocol/tot/Security/
*/
type SecurityProtocol struct {
Socket Socketer
}
/*
Disable disables tracking security state changes.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#method-disable
*/
func (protocol *SecurityProtocol) Disable() <-chan *security.DisableResult {
resultChan := make(chan *security.DisableResult)
command := NewCommand(protocol.Socket, "Security.disable", nil)
result := &security.DisableResult{}
go func() {
response := <-protocol.Socket.SendCommand(command)
if nil != response.Error && 0 != response.Error.Code {
result.Err = response.Error
}
resultChan <- result
close(resultChan)
}()
return resultChan
}
/*
Enable tracking security state changes.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#method-enable
*/
func (protocol *SecurityProtocol) Enable() <-chan *security.EnableResult {
resultChan := make(chan *security.EnableResult)
command := NewCommand(protocol.Socket, "Security.enable", nil)
result := &security.EnableResult{}
go func() {
response := <-protocol.Socket.SendCommand(command)
if nil != response.Error && 0 != response.Error.Code {
result.Err = response.Error
}
resultChan <- result
close(resultChan)
}()
return resultChan
}
/*
HandleCertificateError handles a certificate error that fired a certificateError
event.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#method-handleCertificateError
*/
func (protocol *SecurityProtocol) HandleCertificateError(
params *security.HandleCertificateErrorParams,
) <-chan *security.HandleCertificateErrorResult {
resultChan := make(chan *security.HandleCertificateErrorResult)
command := NewCommand(protocol.Socket, "Security.handleCertificateError", params)
result := &security.HandleCertificateErrorResult{}
go func() {
response := <-protocol.Socket.SendCommand(command)
if nil != response.Error && 0 != response.Error.Code {
result.Err = response.Error
}
resultChan <- result
close(resultChan)
}()
return resultChan
}
/*
SetIgnoreCertificateErrors enables/disables whether all certificate errors
should be ignored.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#method-setIgnoreCertificateErrors
EXPERIMENTAL.
*/
func (protocol *SecurityProtocol) SetIgnoreCertificateErrors(
params *security.SetIgnoreCertificateErrorsParams,
) <-chan *security.SetIgnoreCertificateErrorsResult {
resultChan := make(chan *security.SetIgnoreCertificateErrorsResult)
command := NewCommand(protocol.Socket, "Security.setIgnoreCertificateErrors", params)
result := &security.SetIgnoreCertificateErrorsResult{}
go func() {
response := <-protocol.Socket.SendCommand(command)
if nil != response.Error && 0 != response.Error.Code {
result.Err = response.Error
}
resultChan <- result
close(resultChan)
}()
return resultChan
}
/*
SetOverrideCertificateErrors enables/disables overriding certificate errors. If
enabled, all certificate error events need to be handled by the DevTools client
and should be answered with HandleCertificateError commands.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#method-setOverrideCertificateErrors
*/
func (protocol *SecurityProtocol) SetOverrideCertificateErrors(
params *security.SetOverrideCertificateErrorsParams,
) <-chan *security.SetOverrideCertificateErrorsResult {
resultChan := make(chan *security.SetOverrideCertificateErrorsResult)
command := NewCommand(protocol.Socket, "Security.setOverrideCertificateErrors", params)
result := &security.SetOverrideCertificateErrorsResult{}
go func() {
response := <-protocol.Socket.SendCommand(command)
if nil != response.Error && 0 != response.Error.Code {
result.Err = response.Error
}
resultChan <- result
close(resultChan)
}()
return resultChan
}
/*
OnCertificateError adds a handler to the Security.certificateError event.
Security.certificateError fires when there is a certificate error. If overriding
certificate errors is enabled, then it should be handled with the
HandleCertificateError command. Note: this event does not fire if the
certificate error has been allowed internally.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#event-certificateError
*/
func (protocol *SecurityProtocol) OnCertificateError(
callback func(event *security.CertificateErrorEvent),
) {
handler := NewEventHandler(
"Security.certificateError",
func(response *Response) {
event := &security.CertificateErrorEvent{}
json.Unmarshal([]byte(response.Params), event)
if nil != response.Error && 0 != response.Error.Code {
event.Err = response.Error
}
callback(event)
},
)
protocol.Socket.AddEventHandler(handler)
}
/*
OnSecurityStateChanged adds a handler to the Security.StateChanged event.
Security.StateChanged fires when the security state of the page changed.
https://chromedevtools.github.io/devtools-protocol/tot/Security/#event-securityStateChanged
*/
func (protocol *SecurityProtocol) OnSecurityStateChanged(
callback func(event *security.StateChangedEvent),
) {
handler := NewEventHandler(
"Security.securityStateChanged",
func(response *Response) {
event := &security.StateChangedEvent{}
json.Unmarshal([]byte(response.Params), event)
if nil != response.Error && 0 != response.Error.Code {
event.Err = response.Error
}
callback(event)
},
)
protocol.Socket.AddEventHandler(handler)
}