-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsqltcpproxy.cpp
executable file
·50 lines (39 loc) · 1.32 KB
/
pgsqltcpproxy.cpp
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
#include "pgsqltcpproxy.h"
#include "stream.h"
#include "connection.h"
#include <iostream>
PgsqlTcpProxy::PgsqlTcpProxy(std::shared_ptr<Connection> clientSock, std::shared_ptr<Connection> dbSocket)
: _clientSocket{std::move(clientSock)}
, _dbSocket{std::move(dbSocket)}
{
}
void PgsqlTcpProxy::run()
{
auto self = shared_from_this();
std::shared_ptr<Stream> clientStream(new Stream(_clientSocket, Pgparser::Stage::HeaderStartup,
[self](const Pgparser::Packet packet)
{
self->_printQuery(packet);
self->_dbSocket->async_send(packet.data, packet.data_len, nullptr);
})
, [self = shared_from_this()](Stream *ptr) { self->_dbSocket->close(); delete ptr;});
std::shared_ptr<Stream> dbStream(new Stream(_dbSocket, Pgparser::Stage::HeaderRegular,
[self](const Pgparser::Packet packet)
{
self->_clientSocket->async_send(packet.data, packet.data_len, nullptr);
})
, [self = shared_from_this()](Stream *ptr) { self->_clientSocket->close(); delete ptr; });
clientStream->run();
dbStream->run();
}
void PgsqlTcpProxy::stop()
{
}
void PgsqlTcpProxy::_printQuery(Pgparser::Packet packet)
{
if(packet.query && packet.query_len != 0) {
std::cout << "\nquery. ";
std::cout.write(packet.query, packet.query_len);
std::cout << std::endl;
}
}