summaryrefslogtreecommitdiffstats
path: root/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib
diff options
context:
space:
mode:
Diffstat (limited to 'openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib')
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/dns_util.lua99
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua18
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua93
3 files changed, 183 insertions, 27 deletions
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/dns_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/dns_util.lua
new file mode 100644
index 0000000..c2ac7b1
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/dns_util.lua
@@ -0,0 +1,99 @@
+--[[
+
+ Copyright (C) 2018 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+
+local shcache = require("vendor.shcache")
+local msbConf = require('conf.msbinit')
+local resolver = require "resty.dns.resolver"
+local str_util = require('lib.utils.str_util')
+
+local dns_cache = ngx.shared.dns_cache
+local dns_servers = msbConf.dns.servers
+local dns_cache_positive_ttl = msbConf.dns.cache_positive_ttl or 60
+local dns_cache_negative_ttl = msbConf.dns.cache_negative_ttl or 2
+local dns_cache_actualize_ttl = msbConf.dns.cache_actualize_ttl or 120
+local str_split = str_util.split
+
+local nameservers = nil
+ngx.log(ngx.WARN, "environment variable UPSTREAM_DNS_SERVERS:",dns_servers)
+local ok,res = pcall(function() return str_split(dns_servers,",") end)
+if not ok then
+ ngx.log(ngx.WARN, "failed to parse the DNS Servers from the environment variable UPSTREAM_DNS_SERVERS"," Error:"..res)
+else
+ nameservers = res
+end
+
+local function query(domain)
+ -- closure to perform external lookup to redis
+ local dns_query_from_server = function ()
+ local r, err = resolver:new{
+ nameservers = nameservers,
+ retrans = 5, -- 5 retransmissions on receive timeout
+ timeout = 2000, -- 2 sec
+ }
+
+ if not r then
+ ngx.log(ngx.ERR, "failed to instantiate the resolver:",err)
+ return nil,"failed to instantiate the resolver:"..err
+ end
+
+ --local answers, err = r:query("wygtest.service.openpalette")
+ local answers, err = r:query(domain)
+ if not answers then
+ ngx.log(ngx.ERR, "failed to query the DNS server:",err)
+ return nil,"failed to query the DNS server:"..err
+ end
+
+ if answers.errcode then
+ ngx.log(ngx.ERR, "server returned error code: ", answers.errcode,
+ ": ", answers.errstr)
+ return nil,"server returned error code: "..answers.errcode..
+ ": ".. answers.errstr
+ end
+
+ for i, ans in ipairs(answers) do
+ if r.TYPE_A==ans.type and r.CLASS_IN==ans.class then
+ return ans.address
+ end
+ end
+ return nil,"dns servers return answers,but no server is TYPE_A and CLASS_IN"
+ end
+
+ local dns_cache_table = shcache:new(
+ dns_cache,
+ { external_lookup = dns_query_from_server,
+ --encode = cmsgpack.pack,
+ --encode = cjson_safe.encode,
+ --decode = cmsgpack.unpack
+ --decode = cjson_safe.decode
+ },
+ { positive_ttl = dns_cache_positive_ttl, -- default cache good data for 60s
+ negative_ttl = dns_cache_negative_ttl, -- default cache failed lookup for 1s
+ actualize_ttl = dns_cache_actualize_ttl,
+ name = 'dns_cache' -- "named" cache, useful for debug / report
+ }
+ )
+ local server, from_cache = dns_cache_table:load(domain)
+
+ return server
+end
+_M.query = query
+
+return _M
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua
index 6fa8d39..00996c4 100644
--- a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua
@@ -1,6 +1,6 @@
--[[
- Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+ Copyright (C) 2017-2018 ZTE, Inc. and others. All rights reserved. (ZTE)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -19,6 +19,10 @@
local _M = {}
_M._VERSION = '1.0.0'
+local pl_stringx = require "pl.stringx"
+local split = pl_stringx.split
+local strip = pl_stringx.strip
+
function _M.mark_empty_as_nil(t)
if t == "" then
return nil
@@ -27,4 +31,14 @@ function _M.mark_empty_as_nil(t)
end
end
-return _M \ No newline at end of file
+--- splits a string.
+-- just a placeholder to the penlight `pl.stringx.split` function
+-- @function split
+_M.split = split
+
+--- strips whitespace from a string.
+-- just a placeholder to the penlight `pl.stringx.strip` function
+-- @function strip
+_M.strip = strip
+
+return _M
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua
index 226e31f..ce65e14 100644
--- a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua
@@ -1,6 +1,6 @@
--[[
- Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+ Copyright (C) 2017-2018 ZTE, Inc. and others. All rights reserved. (ZTE)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -22,19 +22,18 @@ _M._VERSION = '1.0.0'
local msbConf= require('conf.msbinit')
local svcConf = require('conf.svcconf')
local log_util = require('lib.utils.log_util')
+local bit = require("bit")
local log = log_util.log
-local ngx_var = ngx.var
-
-local defaultport = msbConf.systemConf.defaultport
-local defaulthttpsport = msbConf.systemConf.defaulthttpsport
-local defaultprefix = msbConf.systemConf.defaultprefix
-local router_subdomain = msbConf.routerConf.subdomain
-local router_defaultprefix = msbConf.routerConf.defaultprefix
local useconsultemplate = msbConf.systemConf.useconsultemplate
local urlfieldMap = svcConf.urlfieldMap
local apiRelatedTypes = svcConf.apiRelatedTypes
+local SYS_SCENARIO_FLAG = { -- cos router
+ ["ROUTER"] = 1, -- 0 1
+ ["COS"] = 2 -- 1 0
+}
+
function _M.isactive(svcinfo)
if svcinfo["status"] == "1" then
return true
@@ -65,32 +64,76 @@ function _M.get_backend_protocol(svcinfo)
end
end
-function _M.get_key_prefix()
- --now assemble the key prefix according the svc_name and server_port
- local key_prefix = ""
- local server_port = ngx_var.server_port
- local svc_name = ngx_var.svc_name
- if (svc_name == "microservices" or svc_name == "msdiscover") then
- key_prefix = defaultprefix
- elseif (server_port == defaultport or server_port == defaulthttpsport) then
- local m, err = ngx.re.match(ngx_var.host, "(?<hostname>.+)\\."..router_subdomain,"o")
- if m then
- key_prefix = router_defaultprefix..":"..m["hostname"]
+function _M.is_api_related_types(svc_type)
+ if(apiRelatedTypes[svc_type]) then
+ return true
+ else
+ return false
+ end
+end
+
+function _M.get_connect_timeout(svcinfo)
+ local connect_timeout = svcinfo.spec["connect_timeout"]
+ if connect_timeout then
+ connect_timeout = tonumber(connect_timeout)
+ if connect_timeout and connect_timeout<=0 then
+ ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad connect timeout!Zero and negative timeout values are not allowed.Input value:"..connect_timeout)
+ return nil
else
- key_prefix = defaultprefix
+ return connect_timeout
end
else
- key_prefix = "msb:"..server_port
+ return nil
end
- return key_prefix
end
-function _M.is_api_related_types(svc_type)
- if(apiRelatedTypes[svc_type]) then
+function _M.get_send_timeout(svcinfo)
+ local send_timeout = svcinfo.spec["send_timeout"]
+ if send_timeout then
+ send_timeout = tonumber(send_timeout)
+ if send_timeout and send_timeout<=0 then
+ ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad send timeout!Zero and negative timeout values are not allowed.Input value:"..send_timeout)
+ return nil
+ else
+ return send_timeout
+ end
+ else
+ return nil
+ end
+end
+
+function _M.get_read_timeout(svcinfo)
+ local read_timeout = svcinfo.spec["read_timeout"]
+ if read_timeout then
+ read_timeout = tonumber(read_timeout)
+ if read_timeout and read_timeout <= 0 then
+ ngx.log(ngx.WARN, ngx.var.request_id.." ".."bad send timeout!Zero and negative timeout values are not allowed.Input value:"..read_timeout)
+ return nil
+ else
+ return read_timeout
+ end
+ else
+ return nil
+ end
+end
+
+function _M.enable_refer_match(svcinfo)
+ local enable_refer_match = svcinfo.spec["enable_refer_match"]
+ --Be compatible with the old service info. If the field is not filled, the refer match is enabled by default.
+ if enable_refer_match == nil or enable_refer_match then
return true
else
return false
end
end
-return _M \ No newline at end of file
+function _M.is_allow_access(system_tag,svcinfo)
+ local scenario = svcinfo.spec["scenario"] or 1
+ local ok,res = pcall(function() return bit.band(SYS_SCENARIO_FLAG[system_tag], scenario) end)
+ if ok and res==SYS_SCENARIO_FLAG[system_tag] then
+ return true
+ else
+ return false
+ end
+end
+return _M