/
httpacct.cxx
257 lines (227 loc) · 8.13 KB
/
httpacct.cxx
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
/*
* httpacct.cxx
*
* accounting module for GNU Gatekeeper that sends it's messages over HTTP
*
* Copyright (c) 2018, Jan Willamowius
*
* This work is published under the GNU Public License version 2 (GPLv2)
* see file COPYING for details.
* We also explicitly grant the right to link this code
* with the OpenH323/H323Plus and OpenSSL library.
*
*/
#include "config.h"
#if defined(P_HTTP) || defined (HAS_LIBCURL)
#include "httpacct.h"
#include "Toolkit.h"
#include <ptclib/http.h>
#ifdef HAS_LIBCURL
#include <curl/curl.h>
#endif // HAS_LIBCURL
HttpAcct::HttpAcct(const char* moduleName, const char* cfgSecName)
: GkAcctLogger(moduleName, cfgSecName)
{
// it is very important to set what type of accounting events
// are supported for each accounting module, otherwise the Log method
// will no get called
SetSupportedEvents(HttpAcctEvents);
PConfig* cfg = GetConfig();
const PString & cfgSec = GetConfigSectionName();
m_timestampFormat = cfg->GetString(cfgSec, "TimestampFormat", "");
m_method = GkConfig()->GetString(cfgSec, "Method", "POST");
m_startURL = cfg->GetString(cfgSec, "StartURL", "");
m_startBody = cfg->GetString(cfgSec, "StartBody", "");
m_stopURL = cfg->GetString(cfgSec, "StopURL", "");
m_stopBody = cfg->GetString(cfgSec, "StopBody", "");
m_updateURL = cfg->GetString(cfgSec, "UpdateURL", "");
m_updateBody = cfg->GetString(cfgSec, "UpdateBody", "");
m_connectURL = cfg->GetString(cfgSec, "ConnectURL", "");
m_connectBody = cfg->GetString(cfgSec, "ConnectBody", "");
m_alertURL = cfg->GetString(cfgSec, "AlertURL", "");
m_alertBody = cfg->GetString(cfgSec, "AlertBody", "");
m_registerURL = cfg->GetString(cfgSec, "RegisterURL", "");
m_registerBody = cfg->GetString(cfgSec, "RegisterBody", "");
m_unregisterURL = cfg->GetString(cfgSec, "UnregisterURL", "");
m_unregisterBody = cfg->GetString(cfgSec, "UnregisterBody", "");
m_onURL = cfg->GetString(cfgSec, "OnURL", "");
m_onBody = cfg->GetString(cfgSec, "OnBody", "");
m_offURL = cfg->GetString(cfgSec, "OffURL", "");
m_offBody = cfg->GetString(cfgSec, "OffBody", "");
m_rejectURL = cfg->GetString(cfgSec, "RejectURL", "");
m_rejectBody = cfg->GetString(cfgSec, "RejectBody", "");
}
HttpAcct::~HttpAcct()
{
}
GkAcctLogger::Status HttpAcct::Log(GkAcctLogger::AcctEvent evt, const callptr & call)
{
// a workaround to prevent processing end on "sufficient" module
// if it is not interested in this event type
if ((evt & GetEnabledEvents() & GetSupportedEvents()) == 0)
return Next;
if (!call && evt != AcctOn && evt != AcctOff) {
PTRACE(1, "HttpAcct\t" << GetName() << " - missing call info for event " << evt);
return Fail;
}
PString eventURL;
PString eventBody;
if (evt == AcctStart) {
eventURL = m_startURL;
eventBody = m_startBody;
} else if (evt == AcctConnect) {
eventURL = m_connectURL;
eventBody = m_connectBody;
} else if (evt == AcctUpdate) {
eventURL = m_updateURL;
eventBody = m_updateBody;
} else if (evt == AcctStop) {
eventURL = m_stopURL;
eventBody = m_stopBody;
} else if (evt == AcctAlert) {
eventURL = m_alertURL;
eventBody = m_alertBody;
} else if (evt == AcctOn) {
eventURL = m_onURL;
eventBody = m_onBody;
} else if (evt == AcctOff) {
eventURL = m_offURL;
eventBody = m_offBody;
} else if (evt == AcctReject) {
eventURL = m_rejectURL;
eventBody = m_rejectBody;
}
if (eventURL.IsEmpty()) {
PTRACE(1, "HttpAcct\t" << GetName() << "Error: No URL configured for event " << evt);
return Fail;
}
std::map<PString, PString> params;
SetupAcctParams(params, call, m_timestampFormat);
PString url = ReplaceAcctParams(eventURL, params);
url = Toolkit::Instance()->ReplaceGlobalParams(url);
PString body = ReplaceAcctParams(eventBody, params);
body = Toolkit::Instance()->ReplaceGlobalParams(body);
return HttpLog(url, body);
}
GkAcctLogger::Status HttpAcct::Log(GkAcctLogger::AcctEvent evt, const endptr & ep)
{
// a workaround to prevent processing end on "sufficient" module
// if it is not interested in this event type
if ((evt & GetEnabledEvents() & GetSupportedEvents()) == 0)
return Next;
if (!ep) {
PTRACE(1, "HttpAcct\t" << GetName() << " - missing endpoint info for event " << evt);
return Fail;
}
PString eventURL;
PString eventBody;
if (evt == AcctRegister) {
eventURL = m_registerURL;
eventBody = m_registerBody;
} else if (evt == AcctUnregister) {
eventURL = m_unregisterURL;
eventBody = m_unregisterBody;
}
if (eventURL.IsEmpty()) {
PTRACE(1, "HttpAcct\t" << GetName() << "Error: No URL configured for event " << evt);
return Fail;
}
std::map<PString, PString> params;
SetupAcctEndpointParams(params, ep, m_timestampFormat);
PString url = ReplaceAcctParams(eventURL, params);
url = Toolkit::Instance()->ReplaceGlobalParams(url);
PString body = ReplaceAcctParams(eventBody, params);
body = Toolkit::Instance()->ReplaceGlobalParams(body);
return HttpLog(url, body);
}
// TODO: refactor (copied from gkauth.cxx)
#ifdef HAS_LIBCURL
// receives the document data
static size_t CurlWriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
*((PString*)userp) = PString((const char*)contents, size * nmemb);
return size * nmemb;
}
// receives debug output
static int DebugToTrace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
{
PTRACE (6, "CURL\t" << PString((const char *)data, size).Trim());
return 0;
}
#endif // HAS_LIBCURL
GkAcctLogger::Status HttpAcct::HttpLog(PString url, PString body)
{
url.Replace(" ", "%20", true); // TODO: better URL escaping ?
PString host = PURL(url).GetHostName();
PString result; // we have to capture the response, but we ignore it for now
// TODO: check CURL concurrency
#ifdef HAS_LIBCURL
CURL * curl = NULL;
CURLcode curl_res = CURLE_FAILED_INIT;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
if (m_method == "GET") {
// nothing special to do
} else if (m_method == "POST") {
PStringArray parts = url.Tokenise("?");
if (body.IsEmpty() && parts.GetSize() == 2) {
url = parts[0];
body = parts[1];
}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (const char *)body);
} else {
PTRACE(2, "HttpAcct\tUnsupported method " << m_method);
return Fail;
}
curl_easy_setopt(curl, CURLOPT_URL, (const char *)url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
if (PTrace::CanTrace(6)) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, DebugToTrace);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
curl_res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if (curl_res != CURLE_OK) {
PTRACE(2, "HttpAcct\tCould not GET password from " << host << " : " << curl_easy_strerror(curl_res));
curl_global_cleanup();
return Fail;
}
#else
if (url.Left(5) == "https") {
PTRACE(2, "HttpPasswordAuth\tPlease compile GnuGk with libcurl for https support");
return Fail;
}
PHTTPClient http;
if (m_method == "GET") {
if (!http.GetTextDocument(url, result)) {
PTRACE(2, "HttpPasswordAuth\tCould not GET password from " << host);
return Fail;
}
} else if (m_method == "POST") {
PStringArray parts = url.Tokenise("?");
if (body.IsEmpty() && parts.GetSize() == 2) {
url = parts[0];
body = parts[1];
}
PMIMEInfo outMIME;
outMIME.SetAt(PMIMEInfo::ContentTypeTag(), "text/plain");
PMIMEInfo replyMIME;
if (!http.PostData(url, outMIME, body, replyMIME, result)) {
PTRACE(2, "HttpPasswordAuth\tCould not POST to " << host);
return Fail;
}
} else {
PTRACE(2, "HttpPasswordAuth\tUnsupported method " << m_method);
return Fail;
}
#endif // HAS_LIBCURL
return Ok;
}
namespace {
// append HTTP accounting logger to the global list of loggers
GkAcctLoggerCreator<HttpAcct> StatusAcctCreator("HttpAcct");
}
#endif // defined(P_HTTP) || defined (HAS_LIBCURL)