forked from TunSafe/TunSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_win32.h
223 lines (171 loc) · 7.05 KB
/
service_win32.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: AGPL-1.0-only
// Copyright (C) 2018 Ludvig Strigeus <[email protected]>. All Rights Reserved.
#pragma once
#include "service_win32_api.h"
#include "service_pipe_win32.h"
#include "network_win32_api.h"
#include "tunsafe_threading.h"
// Takes care of multiple TunsafeServiceBackend
class TunsafeServiceManager : public PipeManager::Delegate {
friend class TunsafeServiceBackend;
friend class TunsafeServiceServer;
public:
TunsafeServiceManager();
virtual ~TunsafeServiceManager();
// -- from PipeManager::Delegate
virtual void HandleNotify() override;
virtual PipeConnection::Delegate *HandleNewConnection(PipeConnection *connection) override;
// Called by the service control code to bring the service up or down
unsigned OnStart(int argc, wchar_t **argv);
void OnStop();
void OnShutdown();
TunsafeServiceBackend *main_backend() { return main_backend_; }
TunsafeServiceBackend *CreateBackend(const char *guid);
void DestroyBackend(TunsafeServiceBackend *backend);
bool SwitchInterface(TunsafeServiceServer *server, const char *interfac, bool want_create);
private:
// Points at the Tunsafe hklm reg key
HKEY hkey_;
uint32 server_unique_id_;
PipeManager pipe_manager_;
TunsafeServiceBackend *main_backend_;
std::vector<TunsafeServiceBackend *> backends_;
};
// One of these exist for each TunsafeBackend
class TunsafeServiceBackend : public TunsafeBackend::Delegate {
friend class TunsafeServiceServer;
public:
explicit TunsafeServiceBackend(TunsafeServiceManager *manager);
virtual ~TunsafeServiceBackend();
// -- from TunsafeBackend::Delegate
virtual void OnGetStats(const WgProcessorStats &stats) override;
virtual void OnClearLog() override;
virtual void OnLogLine(const char **s) override;
virtual void OnStateChanged() override;
virtual void OnStatusCode(TunsafeBackend::StatusCode status) override;
virtual void OnGraphAvailable() override;
virtual void OnConfigurationProtocolReply(uint32 ident, const std::string &&reply) override;
TunsafeBackend *backend() { return backend_; }
TunsafeBackend::Delegate *delegate() { return thread_delegate_; }
void Start(const char *filename);
void RememberLastUsedConfigFile(const char *filename);
void Stop();
// Trigger backend stats updates whenever a connected pipe client needs it
void UpdateRequestStats();
// Called by TunsafeServiceManager::HandleNotify to process events
// on each backend.
void HandleNotify();
// Send a state update to all connected pipes unless filter is set, then it
// sends only to that.
void SendStateUpdate(TunsafeServiceServer *filter);
// Called whenever a pipe server disconnects
void RemovePipeServer(TunsafeServiceServer *pipe_server);
// Called to register a pipe server with this backend
void AddPipeServer(TunsafeServiceServer *pipe_server);
private:
// toggled every time a token submit is processed
uint8 token_request_flag_;
// Points at the service manager
TunsafeServiceManager *manager_;
// Points at the actual TunsafeBackend
TunsafeBackend *backend_;
// Points at all |TunsafeServiceServer| currently associated with this
// backend.
std::vector<TunsafeServiceServer*> pipe_servers_;
// Points at the thing that transmits TunsafeBackend events to
// the main thread
TunsafeBackend::Delegate *thread_delegate_;
// The config filename that is loaded
std::string current_filename_;
// Positions into |historical_log_lines_|
uint32 historical_log_lines_pos_;
uint32 historical_log_lines_count_;
enum { LOGLINE_COUNT = 256 };
char *historical_log_lines_[LOGLINE_COUNT];
};
// The server side of the client<->server pipe connection
class TunsafeServiceServer : public PipeConnection::Delegate {
public:
TunsafeServiceServer(PipeConnection *pipe, TunsafeServiceBackend *backend, uint32 unique_id);
virtual ~TunsafeServiceServer();
void WritePacket(int type, const uint8 *data, size_t data_size);
// -- from PipeConnection::Delegate
virtual bool HandleMessage(int type, uint8 *data, size_t size) override;
virtual void HandleDisconnect() override;
// Called by TunsafeServiceBackend to push a graph to the client
void OnGraphAvailable();
// Called by TunsafeServiceBackend to push more log lines to the client
void SendQueuedLogLines();
bool want_stats() const { return want_stats_; }
bool want_state_updates() const { return want_state_updates_; }
uint32 unique_id() const { return unique_id_; }
TunsafeServiceBackend *service_backend() { return service_backend_; }
void set_service_backend(TunsafeServiceBackend *sb) { service_backend_ = sb; }
private:
bool AuthenticateUser();
// Whether the client wants state updates
bool want_state_updates_;
// Whether the client has authenticated
bool did_authenticate_user_;
// Whether we want stats
bool want_stats_;
// Whether the currently connected user wants a graph
uint32 want_graph_type_;
// The last log line sent to the currently connected user
uint32 last_line_sent_;
uint32 unique_id_;
// The pipe used to communicate
PipeConnection *connection_;
// The backend we're currently associated with
TunsafeServiceBackend *service_backend_;
};
struct ServiceState {
uint8 is_started : 1;
uint8 token_request_flag : 1; // toggled each time token_request changes
uint8 reserved1;
uint16 internet_block_state;
uint8 reserved[20 + 64];
uint32 token_request;
uint32 ipv4_ip;
uint8 public_key[32];
};
STATIC_ASSERT(sizeof(ServiceState) == 128, ServiceState_wrong_size);
class TunsafeServiceClient : public TunsafeBackend, public PipeConnection::Delegate, public PipeManager::Delegate {
public:
TunsafeServiceClient(TunsafeBackend::Delegate *delegate);
virtual ~TunsafeServiceClient();
// -- from TunsafeBackend
virtual bool Configure();
virtual void Teardown();
virtual bool SetTunAdapterName(const char *name);
virtual void Start(const char *config_file);
virtual void Stop();
virtual void RequestStats(bool enable);
virtual void ResetStats();
virtual InternetBlockState GetInternetBlockState();
virtual void SetInternetBlockState(InternetBlockState s);
virtual std::string GetConfigFileName();
virtual void SetServiceStartupFlags(uint32 flags);
virtual LinearizedGraph *GetGraph(int type);
virtual void SendConfigurationProtocolPacket(uint32 identifier, const std::string &&message) override;
virtual uint32 GetTokenRequest() override;
virtual void SubmitToken(const std::string &&token) override;
// -- from PipeConnection::Delegate
virtual bool HandleMessage(int type, uint8 *data, size_t size) override;
virtual void HandleDisconnect() override;
// -- from PipeManager::Delegate
virtual void HandleNotify() override;
virtual PipeConnection::Delegate *HandleNewConnection(PipeConnection *connection) override;
protected:
TunsafeBackend::Delegate *delegate_;
uint8 want_stats_;
uint8 token_request_flag_;
bool got_state_from_control_;
ServiceState service_state_;
std::string config_file_;
PipeManager pipe_manager_;
PipeConnection *connection_;
LinearizedGraph *cached_graph_;
uint32 last_graph_type_;
Mutex mutex_;
};