-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathremote_web_proxy.hpp
More file actions
104 lines (87 loc) · 2.97 KB
/
Copy pathremote_web_proxy.hpp
File metadata and controls
104 lines (87 loc) · 2.97 KB
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
#pragma once
#include <atomic>
#include <cstdint>
#include <iosfwd>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace acecode::web {
inline constexpr const char* kRemoteWebProxyBind = "0.0.0.0";
inline constexpr const char* kRemoteWebProxyIpv6Bind = "::";
inline constexpr const char* kRemoteWebProxyTarget = "127.0.0.1";
inline constexpr const char* kRemoteWebProxyUpstreamSource = "127.0.0.2";
struct RemoteWebProxyStatus {
bool configured = false;
bool running = false;
bool starting = false;
bool ipv6 = false;
int port = 0;
std::int64_t pid = 0;
std::string error;
};
// Interface injected into WebServer so route tests can use a deterministic
// fake without launching another executable.
class RemoteWebProxyController {
public:
virtual ~RemoteWebProxyController() = default;
virtual RemoteWebProxyStatus status() = 0;
virtual RemoteWebProxyStatus start(
int configured_port,
int target_port) = 0;
virtual RemoteWebProxyStatus stop() = 0;
};
// The byte-transparent proxy runtime used both by the hidden child command and
// native integration tests. start() only binds/listens; run() blocks.
class RemoteWebTcpProxy {
public:
RemoteWebTcpProxy();
~RemoteWebTcpProxy();
RemoteWebTcpProxy(const RemoteWebTcpProxy&) = delete;
RemoteWebTcpProxy& operator=(const RemoteWebTcpProxy&) = delete;
bool start(int listen_port, int target_port, std::string* error = nullptr);
void run();
void stop();
int port() const;
bool ipv6_enabled() const;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
// Owns the child process launched by a daemon. configured_port=0 chooses a
// deterministic adjacent port first and falls back to an OS-selected port.
class ManagedRemoteWebProxyController final : public RemoteWebProxyController {
public:
ManagedRemoteWebProxyController(
std::string executable_path,
std::string runtime_dir,
std::int64_t parent_pid);
~ManagedRemoteWebProxyController() override;
ManagedRemoteWebProxyController(
const ManagedRemoteWebProxyController&) = delete;
ManagedRemoteWebProxyController& operator=(
const ManagedRemoteWebProxyController&) = delete;
RemoteWebProxyStatus status() override;
RemoteWebProxyStatus start(
int configured_port,
int target_port) override;
RemoteWebProxyStatus stop() override;
private:
RemoteWebProxyStatus status_locked();
RemoteWebProxyStatus start_candidate_locked(
int listen_port,
int target_port);
bool stop_locked();
std::mutex mu_;
std::string executable_path_;
std::string runtime_dir_;
std::int64_t parent_pid_ = 0;
RemoteWebProxyStatus status_;
};
// Internal top-level command. tokens excludes the executable and
// `--remote-web-proxy` marker.
int run_remote_web_proxy_command(
const std::vector<std::string>& tokens,
std::ostream& out,
std::ostream& err);
} // namespace acecode::web