-
Notifications
You must be signed in to change notification settings - Fork 16
/
har_service.go
78 lines (68 loc) · 1.97 KB
/
har_service.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
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley
// https://pb33f.io
//
// SPDX-License-Identifier: AGPL
package har
import (
"github.com/google/uuid"
"github.com/pb33f/harhar"
"github.com/pb33f/ranch/bus"
"github.com/pb33f/ranch/model"
"github.com/pb33f/ranch/service"
"github.com/pb33f/wiretap/daemon"
"github.com/pb33f/wiretap/shared"
"log/slog"
"time"
)
const (
HARServiceChan = "har-service"
StartTheHARRequest = "start-the-har"
)
type HARService struct {
harStore bus.BusStore
logger *slog.Logger
wiretapService *daemon.WiretapService
}
type ControlResponse struct {
Config *shared.WiretapConfiguration `json:"config,omitempty"`
}
func NewHARService(wiretapService *daemon.WiretapService, logger *slog.Logger) *HARService {
harStore := bus.GetBus().GetStoreManager().CreateStore(HARServiceChan)
return &HARService{
harStore: harStore,
logger: logger,
wiretapService: wiretapService,
}
}
func (hs *HARService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore) {
switch request.RequestCommand {
case StartTheHARRequest:
hs.startTheHAR(request)
default:
core.HandleUnknownRequest(request)
}
}
func (hs *HARService) startTheHAR(request *model.Request) {
if hs.harStore != nil {
har := hs.harStore.GetValue(shared.HARKey)
if har != nil {
harFile := har.(*harhar.HAR)
if harFile != nil {
for _, entry := range harFile.Log.Entries {
httpRequest, err := harhar.ConvertRequestIntoHttpRequest(entry.Request)
if err != nil {
hs.logger.Error("error converting request", "error", err.Error())
continue
}
id, _ := uuid.NewUUID()
request.HttpRequest = httpRequest
request.Id = &id
hs.wiretapService.ValidateRequest(request, httpRequest)
time.Sleep(10 * time.Millisecond)
httpResponse := harhar.ConvertResponseIntoHttpResponse(entry.Response)
hs.wiretapService.ValidateResponse(request, httpResponse)
}
}
}
}
}