-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
79 lines (64 loc) · 2.59 KB
/
handler.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
package tpmproxy
import "bytes"
// RequestResponseHandler is an interface that handles request-response pairs.
type RequestResponseHandler interface {
// HandleRequest handles a request and returns a modified request.
// The request is a byte slice that contains the raw request.
HandleRequest(request []byte) []byte
// HandleResponse handles a response and returns a modified response.
// The response is a byte slice that contains the raw response.
HandleResponse(response []byte) []byte
}
// RequestResponseHandlerFactory is an interface that creates RequestResponseHandlers.
type RequestResponseHandlerFactory interface {
// NewRequestResponseHandler creates a new RequestResponseHandler.
NewRequestResponseHandler() RequestResponseHandler
}
// NopRequestResponseHandler is a RequestResponseHandler that does nothing.
type NopRequestResponseHandler struct {
}
func (h *NopRequestResponseHandler) HandleRequest(request []byte) []byte {
return request
}
func (h *NopRequestResponseHandler) HandleResponse(response []byte) []byte {
return response
}
// NopRequestResponseHandlerFactory is a RequestResponseHandlerFactory that creates NopRequestResponseHandlers.
type NopRequestResponseHandlerFactory struct {
Handler NopRequestResponseHandler
}
func (f *NopRequestResponseHandlerFactory) NewRequestResponseHandler() RequestResponseHandler {
return &f.Handler
}
// TpmRequestResponseHandlerFactory is a RequestResponseHandlerFactory that creates TpmRequestResponseHandlers.
type TpmRequestResponseHandlerFactory struct {
// Interceptor is the Interceptor that intercepts requests and responses.
Interceptor Interceptor
}
func (f *TpmRequestResponseHandlerFactory) NewRequestResponseHandler() RequestResponseHandler {
return &TpmRequestResponseHandler{
Interceptor: f.Interceptor,
}
}
// TpmRequestResponseHandler is a RequestResponseHandler that handles TPM request-response pairs.
type TpmRequestResponseHandler struct {
Interceptor Interceptor
Request Request
}
// HandleRequest handles a request and returns a Interceptor-modified request.
func (h *TpmRequestResponseHandler) HandleRequest(request []byte) []byte {
h.Request.Raw = request
if len(request) < 10 /* sizeof(TPMCmdHeader) */ {
return request
}
var err error
reqBuf := bytes.NewBuffer(request)
if h.Request.Hdr, err = ReqHeader(reqBuf); err != nil {
return request
}
return h.Interceptor.HandleRequest(&h.Request)
}
// HandleResponse handles a response and returns a Interceptor-modified response.
func (h *TpmRequestResponseHandler) HandleResponse(response []byte) []byte {
return h.Interceptor.HandleResponse(&h.Request, response)
}