aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/proxy/zenproxyserver.h
blob: 329ca52355c9c657b8fb400d82b662bba78fc670 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "zenserver.h"

#include "proxy/tcpproxy.h"

#include <memory>
#include <optional>
#include <thread>
#include <vector>

namespace zen {
class HttpApiService;
class HttpFrontendService;
class HttpProxyStatsService;
}  // namespace zen

namespace cxxopts {
class Options;
}
namespace zen::LuaConfig {
struct Options;
}

namespace zen {

struct ZenProxyServerConfig : public ZenServerConfig
{
	static constexpr int kDefaultProxyPort = 8118;

	std::vector<ProxyMapping> ProxyMappings;
};

struct ZenProxyServerConfigurator : public ZenServerConfiguratorBase
{
	ZenProxyServerConfigurator(ZenProxyServerConfig& ServerOptions)
	: ZenServerConfiguratorBase(ServerOptions)
	, m_ServerOptions(ServerOptions)
	{
	}

	~ZenProxyServerConfigurator() = default;

private:
	virtual void AddCliOptions(cxxopts::Options& Options) override;
	virtual void AddConfigOptions(LuaConfig::Options& Options) override;
	virtual void ApplyOptions(cxxopts::Options& Options) override;
	virtual void OnConfigFileParsed(LuaConfig::Options& LuaOptions) override;
	virtual void ValidateOptions() override;

	ZenProxyServerConfig& m_ServerOptions;

	std::vector<std::string> m_RawProxyMappings;
};

class ZenProxyServerMain : public ZenServerMain
{
public:
	ZenProxyServerMain(ZenProxyServerConfig& ServerOptions);
	virtual void DoRun(ZenServerState::ZenServerEntry* Entry) override;

	ZenProxyServerMain(const ZenProxyServerMain&) = delete;
	ZenProxyServerMain& operator=(const ZenProxyServerMain&) = delete;

	typedef ZenProxyServerConfig	   Config;
	typedef ZenProxyServerConfigurator Configurator;

private:
	ZenProxyServerConfig& m_ServerOptions;
};

class ZenProxyServer : public ZenServerBase
{
	ZenProxyServer& operator=(ZenProxyServer&&) = delete;
	ZenProxyServer(ZenProxyServer&&)			= delete;

public:
	ZenProxyServer();
	~ZenProxyServer();

	int	 Initialize(const ZenProxyServerConfig& ServerConfig, ZenServerState::ZenServerEntry* ServerEntry);
	void Run();
	void Cleanup();

private:
	asio::io_context														  m_ProxyIoContext;
	std::optional<asio::executor_work_guard<asio::io_context::executor_type>> m_ProxyIoWorkGuard;
	std::vector<std::thread>												  m_ProxyIoThreads;
	std::vector<std::unique_ptr<TcpProxyService>>							  m_ProxyServices;
	std::unique_ptr<HttpApiService>											  m_ApiService;
	std::unique_ptr<HttpFrontendService>									  m_FrontendService;
	std::unique_ptr<HttpProxyStatsService>									  m_ProxyStatsService;
};

}  // namespace zen