forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogServer.cpp
More file actions
262 lines (218 loc) · 5.01 KB
/
DialogServer.cpp
File metadata and controls
262 lines (218 loc) · 5.01 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
//
// DialogServer.cpp
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "DialogServer.h"
#include "Poco/Net/DialogSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timespan.h"
#include <iostream>
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/Context.h"
#include "Poco/Net/SecureStreamSocketImpl.h"
using Poco::Net::Socket;
using Poco::Net::DialogSocket;
using Poco::Net::SocketAddress;
using Poco::FastMutex;
using Poco::Thread;
using Poco::Net::SecureStreamSocket;
using Poco::Net::SSLManager;
using Poco::Exception;
using Poco::Net::Context;
using Poco::Net::Session;
using Poco::Net::SecureStreamSocketImpl;
DialogServer::DialogServer(bool acceptCommands, bool ssl):
_socket(SocketAddress()),
_thread("DialogServer"),
_stop(false),
_acceptCommands(acceptCommands),
_log(false),
_ssl(ssl)
{
_thread.start(*this);
_ready.wait();
}
DialogServer::~DialogServer()
{
_stop = true;
_thread.join();
}
Poco::UInt16 DialogServer::port() const
{
return _socket.address().port();
}
void handleDataSSLrequest(DialogSocket& ds, bool ssl, Session::Ptr& sslSession)
{
if (ssl)
{
try
{
Context::Ptr cDefaultServerContext = SSLManager::instance().defaultServerContext();
SecureStreamSocket sss(SecureStreamSocket::attach(ds, cDefaultServerContext, sslSession));
sss.setLazyHandshake(true);
if (sss.completeHandshake() == 1)
{
ds = sss;
}
}
catch (Exception& exc)
{
std::cout << exc.displayText() << std::endl;
}
}
}
void handleSSLrequest(DialogSocket& ds, bool ssl, Session::Ptr& sslSession)
{
if (ssl)
{
try
{
Context::Ptr cDefaultServerContext = SSLManager::instance().defaultServerContext();
ds.sendMessage("200 OK");
SecureStreamSocket sss(SecureStreamSocket::attach(ds, cDefaultServerContext));
sss.setLazyHandshake(true);
if (sss.completeHandshake() == 1)
{
ds = sss;
SecureStreamSocketImpl* pSecure = dynamic_cast<SecureStreamSocketImpl*>(sss.impl());
if (pSecure != nullptr)
sslSession = pSecure->currentSession();
}
else
{
ds.sendMessage("501 Explicit TLS authentication not available");
}
}
catch (Exception&) {
ds.sendMessage("501 Explicit TLS authentication not available");
}
}
else
{
ds.sendMessage("501 Explicit TLS authentication not available");
}
}
void DialogServer::run()
{
_ready.set();
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
DialogSocket ds = _socket.acceptConnection();
if (!_SSLsession.isNull()) {
handleDataSSLrequest(ds, _ssl, _SSLsession);
}
{
FastMutex::ScopedLock lock(_mutex);
if (!_nextResponses.empty())
{
ds.sendMessage(_nextResponses.front());
_nextResponses.erase(_nextResponses.begin());
}
}
if (_acceptCommands)
{
try
{
std::string command;
while (ds.receiveMessage(command))
{
if ((command == "AUTH TLS") || (command == "AUTH SSL"))
{
handleSSLrequest(ds, _ssl, _SSLsession);
continue;
}
else if ((command == "PBSZ 0") || (command == "PROT P"))
{
ds.sendMessage("200 OK");
continue;
}
if (_log) std::cout << ">> " << command << std::endl;
{
FastMutex::ScopedLock lock(_mutex);
_lastCommands.push_back(command);
if (!_nextResponses.empty())
{
if (_log) std::cout << "<< " << _nextResponses.front() << std::endl;
ds.sendMessage(_nextResponses.front());
_nextResponses.erase(_nextResponses.begin());
}
}
}
}
catch (Poco::Exception& exc)
{
std::cerr << "DialogServer: " << exc.displayText() << std::endl;
}
}
}
}
}
const std::string& DialogServer::lastCommand() const
{
FastMutex::ScopedLock lock(_mutex);
static const std::string EMPTY;
if (_lastCommands.empty())
return EMPTY;
else
return _lastCommands.back();
}
const std::vector<std::string>& DialogServer::lastCommands() const
{
return _lastCommands;
}
std::string DialogServer::popCommand()
{
FastMutex::ScopedLock lock(_mutex);
std::string command;
if (!_lastCommands.empty())
{
command = _lastCommands.front();
_lastCommands.erase(_lastCommands.begin());
}
return command;
}
std::string DialogServer::popCommandWait()
{
std::string result(popCommand());
while (result.empty())
{
Thread::sleep(100);
result = popCommand();
}
return result;
}
void DialogServer::addResponse(const std::string& response)
{
FastMutex::ScopedLock lock(_mutex);
_nextResponses.push_back(response);
}
void DialogServer::clearCommands()
{
FastMutex::ScopedLock lock(_mutex);
_lastCommands.clear();
}
void DialogServer::clearResponses()
{
FastMutex::ScopedLock lock(_mutex);
_nextResponses.clear();
}
void DialogServer::log(bool flag)
{
_log = flag;
}
void DialogServer::setSslSession(Session::Ptr cSession)
{
_SSLsession = cSession;
}
Session::Ptr DialogServer::getSslSession()
{
return _SSLsession;
}