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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
-- Gemeinschaft 5 module: gateway class
-- (c) AMOOMA GmbH 2013
--
module(...,package.seeall)
Gateway = {}
-- Create Gateway object
function Gateway.new(self, arg)
arg = arg or {}
object = arg.object or {}
setmetatable(object, self);
self.__index = self;
self.class = 'gateway';
self.log = arg.log;
self.database = arg.database;
self.record = arg.record;
self.GATEWAY_PREFIX = 'gateway';
return object;
end
function Gateway.list(self, technology)
technology = technology or 'sip';
local sql_query = 'SELECT * FROM `gateways` WHERE (`outbound` IS TRUE OR `inbound` IS TRUE) AND `technology` = "' .. technology .. '"';
local gateways = {};
self.database:query(sql_query, function(entry)
table.insert(gateways, entry);
end)
return gateways;
end
function Gateway.find_by_id(self, id)
if not tonumber(id) then
return nil;
end
local sql_query = 'SELECT `a`.*, `c`.`sip_host` AS `domain`, `c`.`contact` AS `contact_full`, `c`.`network_ip`, `c`.`network_port` \
FROM `gateways` `a` \
LEFT JOIN `gateway_settings` `b` ON `a`.`id` = `b`.`gateway_id` AND `b`.`name` = "inbound_username" \
LEFT JOIN `sip_registrations` `c` ON `b`.`value` = `c`.`sip_user` \
WHERE `a`.`id`= ' .. tonumber(id) .. ' LIMIT 1';
local gateway = nil;
self.database:query(sql_query, function(entry)
require 'common.str';
gateway = Gateway:new(self);
gateway.record = entry;
gateway.id = tonumber(entry.id);
gateway.name = entry.name;
gateway.technology = entry.technology;
gateway.outbound = common.str.to_b(entry.outbound);
gateway.inbound = common.str.to_b(entry.inbound);
gateway.domain = entry.domain;
gateway.network_ip = entry.network_ip;
gateway.network_port = tonumber(entry.network_port) or 5060;
end)
if gateway then
gateway.settings = self:config_table_get('gateway_settings', gateway.id);
if common.str.blank(gateway.domain) then
gateway.domain = gateway.settings.domain;
end
end
return gateway;
end
function Gateway.find_by_name(self, name)
local gateway_name = name:gsub('([^%a%d%._%+])', '');
local sql_query = 'SELECT `a`.*, `c`.`sip_host` `domain`, `c`.`contact` AS `contact_full`, `c`.`network_ip`, `c`.`network_port`\
FROM `gateways` `a` \
LEFT JOIN `gateway_settings` `b` ON `a`.`id` = `b`.`gateway_id` AND `b`.`name` = "inbound_username" \
LEFT JOIN `sip_registrations` `c` ON `b`.`value` = `c`.`sip_user` \
WHERE `a`.`name`= ' .. self.database:escape(gateway_name, '"') .. ' LIMIT 1';
local gateway = nil;
self.database:query(sql_query, function(entry)
require 'common.str';
gateway = Gateway:new(self);
gateway.record = entry;
gateway.id = tonumber(entry.id);
gateway.name = entry.name;
gateway.technology = entry.technology;
gateway.outbound = common.str.to_b(entry.outbound);
gateway.inbound = common.str.to_b(entry.inbound);
gateway.domain = entry.domain;
gateway.network_ip = entry.network_ip;
gateway.network_port = tonumber(entry.network_port) or 5060;
end)
if gateway then
gateway.settings = self:config_table_get('gateway_settings', gateway.id);
end
return gateway;
end
function Gateway.find_by_auth_name(self, name)
local auth_name = name:gsub('([^%a%d%._%+])', '');
local sql_query = 'SELECT `c`.*, `a`.`value` `password`, `b`.`value` `username` \
FROM `gateway_settings` `a` \
INNER JOIN `gateway_settings` `b` \
ON (`a`.`gateway_id` = `b`.`gateway_id` AND `a`.`name` = "inbound_password" AND `b`.`name` = "inbound_username" AND `b`.`value` = ' .. self.database:escape(auth_name, '"') .. ') \
LEFT JOIN `gateways` `c` \
ON (`a`.`gateway_id` = `c`.`id`) \
WHERE `c`.`inbound` IS TRUE OR `c`.`outbound` IS TRUE LIMIT 1';
local gateway = nil;
self.database:query(sql_query, function(entry)
require 'common.str';
gateway = Gateway:new(self);
gateway.record = entry;
gateway.id = tonumber(entry.id);
gateway.name = entry.name;
gateway.technology = entry.technology;
gateway.outbound = common.str.to_b(entry.outbound);
gateway.inbound = common.str.to_b(entry.inbound);
end)
if gateway then
gateway.settings = self:config_table_get('gateway_settings', gateway.id);
end
return gateway;
end
function Gateway.call_url(self, destination_number)
require 'common.str';
if common.str.blank(self.settings.dial_string) then
if self.technology == 'sip' then
if self.settings.inbound_username and self.settings.inbound_password and not common.str.blank(self.record.domain) then
return 'sofia/' .. (self.settings.profile or 'gemeinschaft') .. '/' .. self.settings.inbound_username .. '%' .. self.record.domain;
else
return 'sofia/gateway/' .. self.GATEWAY_PREFIX .. self.id .. '/' .. tostring(destination_number);
end
elseif self.technology == 'xmpp' then
local destination_str = tostring(destination_number);
if self.settings.destination_domain then
destination_str = destination_str .. '@' .. self.settings.destination_domain;
end
return 'dingaling/' .. self.GATEWAY_PREFIX .. self.id .. '/' .. destination_str;
end
else
require 'common.array';
return tostring(common.array.expand_variables(self.settings.dial_string, self, { destination_number = destination_number }));
end
return '';
end
function Gateway.authenticate(self, caller, technology)
local sql_query = 'SELECT `c`.`name`, `c`.`id`, `a`.`value` `auth_source`, `b`.`value` `auth_pattern` \
FROM `gateway_settings` `a` \
INNER JOIN `gateway_settings` `b` \
ON (`a`.`gateway_id` = `b`.`gateway_id` AND `a`.`name` = "auth_source" AND `b`.`name` = "auth_pattern" ) \
LEFT JOIN `gateways` `c` \
ON (`a`.`gateway_id` = `c`.`id`) \
WHERE `c`.`inbound` IS TRUE';
if technology then
sql_query = sql_query .. ' AND `c`.`technology` = "' .. tostring(technology) .. '"';
end
local gateway = false;
self.database:query(sql_query, function(entry)
if caller:to_s(entry.auth_source):match(entry.auth_pattern) then
gateway = entry;
return;
end
end)
return gateway;
end
function Gateway.profile_get(self, gateway_id)
local sql_query = 'SELECT `value` FROM `gateway_settings` WHERE `gateway_id` = ' .. tonumber(gateway_id) .. ' AND `name` = "profile" LIMIT 1';
return self.database:query_return_value(sql_query);
end
function Gateway.config_table_get(self, config_table, gateway_id)
require 'common.str'
local sql_query = 'SELECT * FROM `'.. config_table ..'` WHERE `gateway_id` = ' .. tonumber(gateway_id);
local settings = {};
self.database:query(sql_query, function(entry)
local p_class_type = common.str.strip(entry.class_type):lower();
local p_name = common.str.strip(entry.name):lower();
if p_class_type == 'boolean' then
settings[p_name] = common.str.to_b(entry.value);
elseif p_class_type == 'integer' then
settings[p_name] = common.str.to_i(entry.value);
else
settings[p_name] = tostring(entry.value);
end
end)
return settings
end
function Gateway.parameters_build(self, gateway_id, technology)
local settings = self:config_table_get('gateway_settings', gateway_id);
require 'common.str'
local parameters = {};
if technology == 'sip' then
parameters.realm = settings.domain;
parameters.extension = 'auto_to_user';
if common.str.blank(settings.username) then
parameters.username = 'gateway' .. gateway_id;
parameters.register = false;
else
parameters.username = settings.username;
parameters.register = true;
end
if not common.str.blank(settings.register) then
parameters.register = common.str.to_b(settings.register);
end
if not common.str.blank(settings.password) then
parameters.password = settings.password;
end
parameters['extension-in-contact'] = true;
if common.str.blank(settings.contact) then
parameters['extension'] = 'gateway' .. gateway_id;
else
parameters['extension'] = settings.contact;
end
elseif technology == 'xmpp' then
parameters.message = 'Gemeinschaft 5 by AMOOMA';
parameters.dialplan = 'XML';
parameters.context = 'default';
parameters['rtp-ip'] = 'auto';
parameters['auto-login'] = 'true';
parameters.sasl = 'plain';
parameters.tls = 'true';
parameters['use-rtp-timer'] = 'true';
parameters.vad = 'both';
parameters.use_jingle = 'true';
parameters['candidate-acl'] = 'wan.auto';
parameters.name = self.GATEWAY_PREFIX .. gateway_id;
parameters.server = settings.server;
parameters.login = settings.login;
parameters.password = settings.password;
parameters.exten = settings.inbound_number or parameters.name;
end
for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do
parameters[key] = value;
end
return parameters;
end
function Gateway.headers_get(self, header_type, gateway_id)
gateway_id = gateway_id or self.id;
local sql_query = 'SELECT * FROM `gateway_headers` WHERE `gateway_id` = ' .. tonumber(gateway_id) .. ' AND `header_type` = ' .. self.database:escape(header_type, '"');
local headers = {};
self.database:query(sql_query, function(entry)
table.insert(headers, entry);
end)
return headers;
end
function Gateway.constraint_match(self, constraints_str, variable_sets)
if common.str.blank(constraints_str) then
entry_match = true;
else
local constraints = common.str.strip_to_a(constraints_str, ',')
for constraint_index=1, #constraints do
local variable_name, pattern = common.str.partition(constraints[constraint_index], '!=')
local invert = variable_name ~= nil;
if not variable_name then
variable_name, pattern = common.str.partition(constraints[constraint_index], '=')
end
if not common.str.blank(variable_name) and not common.str.blank(pattern) then
local search_string = common.array.expand_variable(variable_name, variable_sets);
if search_string ~= nil then
local success, result = pcall(string.find, tostring(search_string), pattern);
entry_match = common.str.to_b(result);
if invert then
entry_match = not entry_match;
end
if entry_match == false then
break;
end
end
end
end
end
return entry_match;
end
function Gateway.origination_variables(self, header_type, origination_variables, ...)
local dtmf = tostring(self.settings.dtmf_type):lower();
if dtmf == 'inband' then
table.insert(origination_variables, "dtmf_type=none");
elseif dtmf == 'none' or dtmf == 'info' then
table.insert(origination_variables, "dtmf_type=" .. dtmf);
else
table.insert(origination_variables, "dtmf_type=rfc2833");
end
local header_to_variable = {
default = {
default = 'sip_h_',
from = 'sip_full_from',
to = 'sip_full_to',
invite = 'sip_req_uri',
},
invite = {
default = 'sip_h_',
from = 'sip_invite_full_from',
to = 'sip_invite_full_to',
invite = 'sip_invite_req_uri',
route = 'sip_invite_route_uri',
['Record-Route'] = 'sip_invite_record_route',
}
}
local variable_sets = {...};
local headers = self:headers_get('default');
for index, header in ipairs(self:headers_get(header_type)) do
table.insert(headers, header);
end
for index, header in ipairs(headers) do
local search_string = common.array.expand_variable(header.constraint_source, variable_sets);
if common.str.blank(header.constraint_value) or self:constraint_match(header.constraint_value, variable_sets) then
if header_to_variable[header.header_type] then
local origination_variable = header_to_variable[header.header_type][header.name:lower()] or header_to_variable[header.header_type].default .. header.name;
table.insert(origination_variables, origination_variable .. "='" .. common.array.expand_variables(header.value, unpack(variable_sets)) .. "'");
end
end
end
end
|