From 9012171763a7f1155e39bfb7f39fea34d437feef Mon Sep 17 00:00:00 2001 From: spag Date: Sun, 13 Jan 2013 17:22:00 +0100 Subject: gateway class added --- misc/freeswitch/scripts/common/gateway.lua | 122 +++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 misc/freeswitch/scripts/common/gateway.lua (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua new file mode 100644 index 0000000..2c50c0f --- /dev/null +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -0,0 +1,122 @@ +-- 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; + 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_sql(self, where) + local sql_query = 'SELECT * FROM `gateways` WHERE ' .. where .. ' LIMIT 1'; + + local gateway = nil; + self.database:query(sql_query, function(entry) + gateway = Gateway:new(self); + gateway.record = entry; + gateway.id = tonumber(entry.id); + gateway.uuid = entry.uuid; + end) + + return gateway; +end + + +-- find gateway by id +function Gateway.find_by_id(self, id) + local sql_query = '`id`= ' .. tonumber(id); + return self:find_by_sql(sql_query); +end + +-- find gateway name +function Gateway.find_by_name(self, name) + local sql_query = '`name`= "' .. name .. '"'; + + return self:find_by_sql(sql_query); +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) + local settings = self:config_table_get('gateway_settings', gateway_id); + local parameters = { + realm = settings.domain, + extension = 'auto_to_user', + }; + + require 'common.str' + + 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 common.str.blank(settings.password) then + parameters.password = 'gateway' .. gateway_id; + else + parameters.password = settings.password; + end + + for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do + parameters[key] = value; + end + + return parameters; +end -- cgit v1.2.3 From 24a60c41586dbfa64da30acea1749376bebbef46 Mon Sep 17 00:00:00 2001 From: spag Date: Mon, 14 Jan 2013 09:47:22 +0100 Subject: register setting --- misc/freeswitch/scripts/common/gateway.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 2c50c0f..9c09a22 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -108,6 +108,10 @@ function Gateway.parameters_build(self, gateway_id) parameters.register = true; end + if not common.str.blank(settings.register) then + parameters.register = common.str.to_b(settings.register); + end + if common.str.blank(settings.password) then parameters.password = 'gateway' .. gateway_id; else -- cgit v1.2.3 From e0064941bc73303e83f1fbd9374c3a731b1d3c0b Mon Sep 17 00:00:00 2001 From: spag Date: Mon, 14 Jan 2013 17:15:51 +0100 Subject: allow header based authentication --- misc/freeswitch/scripts/common/gateway.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 9c09a22..9cddd7c 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -61,6 +61,28 @@ function Gateway.find_by_name(self, name) end +function Gateway.authenticate(self, technology, caller) + 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 AND `c`.`technology` = "' .. tostring(technology) .. '"'; + + 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'; -- cgit v1.2.3 From 82ab2c07fbc494bad3ffdde30dbb3ce0d98c4e19 Mon Sep 17 00:00:00 2001 From: spag Date: Tue, 15 Jan 2013 12:29:12 +0100 Subject: gateway settings --- misc/freeswitch/scripts/common/gateway.lua | 32 ++++++++++++++---------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 9cddd7c..6e9fbfb 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -32,32 +32,22 @@ function Gateway.list(self, technology) end -function Gateway.find_by_sql(self, where) - local sql_query = 'SELECT * FROM `gateways` WHERE ' .. where .. ' LIMIT 1'; +function Gateway.find_by_id(self, id) + local sql_query = 'SELECT * FROM `gateways` WHERE `id`= ' .. tonumber(id) .. ' LIMIT 1'; local gateway = nil; self.database:query(sql_query, function(entry) gateway = Gateway:new(self); gateway.record = entry; gateway.id = tonumber(entry.id); - gateway.uuid = entry.uuid; + gateway.name = entry.name; end) - return gateway; -end - - --- find gateway by id -function Gateway.find_by_id(self, id) - local sql_query = '`id`= ' .. tonumber(id); - return self:find_by_sql(sql_query); -end - --- find gateway name -function Gateway.find_by_name(self, name) - local sql_query = '`name`= "' .. name .. '"'; + if gateway then + gateway.settings = self:config_table_get('gateway_settings', gateway.id); + end - return self:find_by_sql(sql_query); + return gateway; end @@ -140,6 +130,14 @@ function Gateway.parameters_build(self, gateway_id) 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 + for key, value in pairs(self:config_table_get('gateway_parameters', gateway_id)) do parameters[key] = value; end -- cgit v1.2.3 From d95917ecbc5d0efbc1b4b67c1974d7f0421eebf9 Mon Sep 17 00:00:00 2001 From: spag Date: Wed, 16 Jan 2013 16:07:40 +0100 Subject: set gateway prefix --- misc/freeswitch/scripts/common/gateway.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 6e9fbfb..5c76aba 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -16,6 +16,7 @@ function Gateway.new(self, arg) self.log = arg.log; self.database = arg.database; self.record = arg.record; + self.GATEWAY_PREFIX = 'gateway'; return object; end -- cgit v1.2.3 From 3a91509c6c668cc634d667a2e1c7144873a39861 Mon Sep 17 00:00:00 2001 From: spag Date: Fri, 18 Jan 2013 12:10:48 +0100 Subject: gateway find_by_name method added --- misc/freeswitch/scripts/common/gateway.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'misc/freeswitch/scripts/common/gateway.lua') diff --git a/misc/freeswitch/scripts/common/gateway.lua b/misc/freeswitch/scripts/common/gateway.lua index 5c76aba..e50c763 100644 --- a/misc/freeswitch/scripts/common/gateway.lua +++ b/misc/freeswitch/scripts/common/gateway.lua @@ -52,6 +52,27 @@ function Gateway.find_by_id(self, id) end +function Gateway.find_by_name(self, name) + local gateway_name = name:gsub('([^%a%d%._%+])', ''); + + local sql_query = 'SELECT * FROM `gateways` WHERE `name`= "' .. gateway_name .. '" LIMIT 1'; + + local gateway = nil; + self.database:query(sql_query, function(entry) + gateway = Gateway:new(self); + gateway.record = entry; + gateway.id = tonumber(entry.id); + gateway.name = entry.name; + end) + + if gateway then + gateway.settings = self:config_table_get('gateway_settings', gateway.id); + end + + return gateway; +end + + function Gateway.authenticate(self, technology, caller) local sql_query = 'SELECT `c`.`name`, `c`.`id`, `a`.`value` `auth_source`, `b`.`value` `auth_pattern` \ FROM `gateway_settings` `a` \ -- cgit v1.2.3