aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/proxy/httptrafficrecorder.cpp
blob: 0279555a0758f9f0b765599523b58c09194f924a (plain) (blame)
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
187
188
189
190
191
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httptrafficrecorder.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/logging.h>

#include <chrono>
#include <filesystem>

namespace zen {

HttpTrafficRecorder::HttpTrafficRecorder(const std::filesystem::path& OutputDir, std::string_view ClientLabel, std::string_view TargetLabel)
: m_Log(logging::Get("proxy.record"))
, m_Dir(OutputDir)
, m_ClientLabel(ClientLabel)
, m_TargetLabel(TargetLabel)
{
	auto Now	  = std::chrono::system_clock::now();
	m_StartTimeMs = uint64_t(std::chrono::duration_cast<std::chrono::milliseconds>(Now.time_since_epoch()).count());

	std::error_code Ec;
	std::filesystem::create_directories(m_Dir, Ec);
	if (Ec)
	{
		ZEN_WARN("failed to create recording directory {} - {}", m_Dir.string(), Ec.message());
		return;
	}

	std::error_code ReqEc;
	m_RequestFile.Open(m_Dir / "request.bin", BasicFile::Mode::kTruncate, ReqEc);
	if (ReqEc)
	{
		ZEN_WARN("failed to open request.bin in {} - {}", m_Dir.string(), ReqEc.message());
		return;
	}

	std::error_code RespEc;
	m_ResponseFile.Open(m_Dir / "response.bin", BasicFile::Mode::kTruncate, RespEc);
	if (RespEc)
	{
		ZEN_WARN("failed to open response.bin in {} - {}", m_Dir.string(), RespEc.message());
		m_RequestFile.Close();
		return;
	}

	m_Valid = true;
	ZEN_DEBUG("recording started in {}", m_Dir.string());
}

HttpTrafficRecorder::~HttpTrafficRecorder()
{
	if (m_Valid && !m_Finalized)
	{
		Finalize(false, Oid::Zero);
	}
}

void
HttpTrafficRecorder::WriteRequest(const char* Data, size_t Length)
{
	if (!m_Valid)
	{
		return;
	}
	m_RequestFile.Write(Data, Length, m_RequestOffset);
	m_RequestOffset += Length;
}

void
HttpTrafficRecorder::WriteResponse(const char* Data, size_t Length)
{
	if (!m_Valid)
	{
		return;
	}
	m_ResponseFile.Write(Data, Length, m_ResponseOffset);
	m_ResponseOffset += Length;
}

void
HttpTrafficRecorder::OnMessageComplete(HttpTrafficInspector::Direction Dir,
									   std::string_view				   Method,
									   std::string_view				   Url,
									   uint16_t						   StatusCode,
									   int64_t /*ContentLength*/)
{
	if (!m_Valid)
	{
		return;
	}

	if (Dir == HttpTrafficInspector::Direction::Request)
	{
		// Record the request boundary. The request spans from m_CurrentRequestStart to m_RequestOffset.
		m_PendingReqOffset	= m_CurrentRequestStart;
		m_PendingReqSize	= m_RequestOffset - m_CurrentRequestStart;
		m_PendingMethod		= Method;
		m_PendingUrl		= Url;
		m_HasPendingRequest = true;

		// Advance start to current offset for the next request.
		m_CurrentRequestStart = m_RequestOffset;
	}
	else
	{
		// Response complete -- pair with pending request.
		RecordedEntry Entry;
		if (m_HasPendingRequest)
		{
			Entry.ReqOffset		= m_PendingReqOffset;
			Entry.ReqSize		= m_PendingReqSize;
			Entry.Method		= std::move(m_PendingMethod);
			Entry.Url			= std::move(m_PendingUrl);
			m_HasPendingRequest = false;
		}

		Entry.RespOffset = m_CurrentResponseStart;
		Entry.RespSize	 = m_ResponseOffset - m_CurrentResponseStart;
		Entry.Status	 = StatusCode;

		m_Entries.push_back(std::move(Entry));

		// Advance start to current offset for the next response.
		m_CurrentResponseStart = m_ResponseOffset;
	}
}

void
HttpTrafficRecorder::Finalize(bool WebSocket, const Oid& SessionId)
{
	if (!m_Valid || m_Finalized)
	{
		return;
	}
	m_Finalized = true;

	m_RequestFile.Close();
	m_ResponseFile.Close();

	// Write index.ucb as a CbObject.
	CbObjectWriter Cbo;

	Cbo << "client" << std::string_view(m_ClientLabel);
	Cbo << "target" << std::string_view(m_TargetLabel);
	Cbo << "startTime" << m_StartTimeMs;
	Cbo << "websocket" << WebSocket;
	if (SessionId != Oid::Zero)
	{
		std::string SessionIdStr = SessionId.ToString();
		Cbo << "sessionId" << std::string_view(SessionIdStr);
	}

	Cbo.BeginArray("entries");
	for (const RecordedEntry& Entry : m_Entries)
	{
		Cbo.BeginObject();
		Cbo << "reqOffset" << Entry.ReqOffset;
		Cbo << "reqSize" << Entry.ReqSize;
		Cbo << "respOffset" << Entry.RespOffset;
		Cbo << "respSize" << Entry.RespSize;
		Cbo << "method" << std::string_view(Entry.Method);
		Cbo << "url" << std::string_view(Entry.Url);
		Cbo << "status" << Entry.Status;
		Cbo.EndObject();
	}
	Cbo.EndArray();

	CbObject IndexObj = Cbo.Save();

	MemoryView		View = IndexObj.GetView();
	std::error_code Ec;
	BasicFile		IndexFile(m_Dir / "index.ucb", BasicFile::Mode::kTruncate, Ec);
	if (!Ec)
	{
		IndexFile.Write(View, 0, Ec);
		if (Ec)
		{
			ZEN_WARN("failed to write index.ucb in {} - {}", m_Dir.string(), Ec.message());
		}
		IndexFile.Close();
	}
	else
	{
		ZEN_WARN("failed to create index.ucb in {} - {}", m_Dir.string(), Ec.message());
	}

	ZEN_DEBUG("recording finalized in {} ({} entries, websocket: {})", m_Dir.string(), m_Entries.size(), WebSocket);
}

}  // namespace zen