-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
313 lines (263 loc) · 9.28 KB
/
Copy pathmain.cpp
File metadata and controls
313 lines (263 loc) · 9.28 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <boost/asio.hpp>
#include "debug.hpp"
#include "parser.hpp"
#include "logger.hpp"
namespace net = boost::asio;
namespace db_proxy
{
enum { max_data_length = 8192 }; // 8KB
class session : public std::enable_shared_from_this<session>
{
public:
using ptr_type = std::shared_ptr<session>;
session(net::io_context& ios)
: client_socket_(ios), server_socket_(ios)
{
}
net::ip::tcp::socket& client_socket()
{
return client_socket_;
}
net::ip::tcp::socket& server_socket()
{
return server_socket_;
}
void start(const std::string& server_host, unsigned short server_port)
{
server_socket_.async_connect(
net::ip::tcp::endpoint(
net::ip::make_address(server_host),
server_port),
std::bind(&session::handle_server_connect,
shared_from_this(),
std::placeholders::_1));
}
void handle_server_connect(const boost::system::error_code& error)
{
if (!error)
{
std::cout << "Client connected from " << client_socket_.local_endpoint().address() << '\n';
server_socket_.async_read_some(
net::buffer(server_data_, max_data_length),
std::bind(&session::handle_server_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
client_socket_.async_read_some(
net::buffer(client_data_, max_data_length),
std::bind(&session::handle_client_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
else
close();
}
private:
void handle_server_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
{
if (!error)
{
parser_.parse(server_data_, bytes_transferred);
async_write(client_socket_,
net::buffer(server_data_, bytes_transferred),
std::bind(&session::handle_client_write,
shared_from_this(),
std::placeholders::_1));
}
else
close();
}
void handle_client_write(const boost::system::error_code& error)
{
if (!error)
{
server_socket_.async_read_some(
net::buffer(server_data_, max_data_length),
std::bind(&session::handle_server_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
else
close();
}
void handle_client_read(const boost::system::error_code& error,
const size_t& bytes_transferred)
{
if (!error)
{
parser_.parse(client_data_, bytes_transferred);
async_write(server_socket_,
net::buffer(client_data_,bytes_transferred),
std::bind(&session::handle_server_write,
shared_from_this(),
std::placeholders::_1));
}
else
close();
}
void handle_server_write(const boost::system::error_code& error)
{
if (!error)
{
client_socket_.async_read_some(
net::buffer(client_data_,max_data_length),
std::bind(&session::handle_client_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
else
close();
}
void close()
{
std::lock_guard<std::mutex> lock(mutex_);
if (client_socket_.is_open())
client_socket_.close();
if (server_socket_.is_open())
server_socket_.close();
}
net::ip::tcp::socket client_socket_;
net::ip::tcp::socket server_socket_;
uint8_t client_data_[max_data_length] = {0};
uint8_t server_data_[max_data_length] = {0};
std::mutex mutex_;
My::Parser parser_;
};
class server
{
public:
server(net::io_context& io_service,
const std::string& local_host, unsigned short local_port,
const std::string& server_host, unsigned short server_port)
: io_service_(io_service),
localhost_address(net::ip::make_address_v4(local_host)),
server_(io_service_,net::ip::tcp::endpoint(localhost_address,local_port)),
server_port_(server_port),
server_host_(server_host)
{}
bool accept_connections()
{
try
{
session_ = std::make_shared<session>(io_service_);
server_.async_accept(session_->client_socket(),
std::bind(&server::handle_accept,
this,
std::placeholders::_1));
}
catch(std::exception& e)
{
std::cerr << "server exception: " << e.what() << std::endl;
return false;
}
return true;
}
private:
void handle_accept(const boost::system::error_code& error)
{
if (!error)
{
session_->start(server_host_,server_port_);
if (!accept_connections())
{
std::cerr << "Failure during call to accept." << std::endl;
}
}
else
{
std::cerr << "Error: " << error.message() << std::endl;
}
}
net::io_context& io_service_;
net::ip::address_v4 localhost_address;
net::ip::tcp::acceptor server_;
session::ptr_type session_;
unsigned short server_port_;
std::string server_host_;
};
}
class CmdOptions {
private:
int argc_;
char **argv_;
public:
unsigned short bind_port = 8080;
unsigned short remote_port = 0;
std::string bind_host = "127.0.0.1";
std::string remote_host = "";
CmdOptions(int argc, char **argv) : argc_(argc), argv_(argv) {
}
bool parse() {
for(int i = 1; i < argc_; i++) {
std::string arg = argv_[i];
if(arg == "--remote-port")
remote_port = static_cast<unsigned short>(std::stoi(argv_[++i]));
if(arg == "--bind-port")
remote_port = static_cast<unsigned short>(std::stoi(argv_[++i]));
if(arg == "--bind-host")
bind_host = argv_[++i];
if(arg == "--remote-host")
remote_host = argv_[++i];
if(arg == "--help") {
help();
return true;
}
}
return required_missing();
}
bool required_missing() {
return remote_host.empty() || remote_port == 0;
}
void help() {
std::cout << "Usage: " << argv_[0] << " [options]" << '\n';
std::cout << " Options:\n";
std::cout << " --remote-port arg" << "\t Remote DB port\n";
std::cout << " --remote-host arg" << "\t Remote DB host\n";
std::cout << " --bind-port [arg]" << "\t Local port to listen. Default: " << bind_port << '\n';
std::cout << " --bind-host [arg]" << "\t Local adress to listen. Default: " << bind_host << '\n';
}
};
static net::io_context ios;
BOOL WINAPI CtrlHandler(DWORD dwCtrlType) {
switch (dwCtrlType)
{
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_C_EVENT:
if(!ios.stopped())
ios.stop();
return TRUE;
default:
return FALSE;
}
}
int main(int argc, char** argv)
{
SetConsoleCtrlHandler(CtrlHandler, TRUE);
auto logger = LoggerRegistry::instance().create_file("logger", "db-proxy.log");
CmdOptions options(argc, argv);
bool res = options.parse();
if (res) {
std::cout << "Missed required options\n\n";
options.help();
return EXIT_FAILURE;
}
try
{
db_proxy::server server(ios,
options.bind_host, options.bind_port,
options.remote_host, options.remote_port);
server.accept_connections();
ios.run();
}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return EXIT_SUCCESS;
}