From e6e69ae8058eb01426493d0014e9811a65ce384b Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 29 Aug 2018 19:52:41 +0800 Subject: [PATCH 1/3] add mongo config option: authdb, see issue #884 --- lualib/skynet/db/mongo.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 7522da20..04b27805 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -87,13 +87,17 @@ local function mongo_auth(mongoc) local pass = rawget(mongoc, "password") local authmod = rawget(mongoc, "authmod") or "scram_sha1" authmod = "auth_" .. authmod + local authdb = rawget(mongoc, "authdb") + if authdb then + authdb = mongo_client.getDB(mongoc, authdb) -- mongoc has not set metatable yet + end return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" local auth_func = mongoc[authmod] assert(auth_func , "Invalid authmod") - assert(auth_func(mongoc,user, pass)) + assert(auth_func(authdb or mongoc, user, pass)) end local rs_data = mongoc:runCommand("ismaster") if rs_data.ok == 1 then @@ -156,6 +160,7 @@ function mongo.client( conf ) username = first.username, password = first.password, authmod = first.authmod, + authdb = first.authdb, } obj.__id = 0 From c4334be9c3c3d840dfbc17f89284f40ed0531864 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 30 Aug 2018 09:51:55 +0800 Subject: [PATCH 2/3] add mongo_db:auth --- lualib/skynet/db/mongo.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index 04b27805..ec8063a6 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -82,6 +82,8 @@ local function __parse_addr(addr) return host, tonumber(port) end +local auth_method = {} + local function mongo_auth(mongoc) local user = rawget(mongoc, "username") local pass = rawget(mongoc, "password") @@ -95,7 +97,7 @@ local function mongo_auth(mongoc) return function() if user ~= nil and pass ~= nil then -- autmod can be "mongodb_cr" or "scram_sha1" - local auth_func = mongoc[authmod] + local auth_func = auth_method[authmod] assert(auth_func , "Invalid authmod") assert(auth_func(authdb or mongoc, user, pass)) end @@ -211,7 +213,7 @@ function mongo_client:runCommand(...) return self.admin:runCommand(...) end -function mongo_client:auth_mongodb_cr(user,password) +function auth_method:auth_mongodb_cr(user,password) local password = md5.sumhexa(string.format("%s:mongo:%s",user,password)) local result= self:runCommand "getnonce" if result.ok ~=1 then @@ -234,7 +236,7 @@ local function salt_password(password, salt, iter) return output end -function mongo_client:auth_scram_sha1(username,password) +function auth_method:auth_scram_sha1(username,password) local user = string.gsub(string.gsub(username, '=', '=3D'), ',' , '=2C') local nonce = crypt.base64encode(crypt.randomkey()) local first_bare = "n=" .. user .. ",r=" .. nonce @@ -305,6 +307,13 @@ function mongo_client:logout() return result.ok == 1 end +function mongo_db:auth(user, pass) + local authmod = rawget(self.connection, "authmod") or "scram_sha1" + local auth_func = auth_method[authmod] + assert(auth_func , "Invalid authmod") + return auth_func(self, user, pass) +end + function mongo_db:runCommand(cmd,cmd_v,...) local conn = self.connection local request_id = conn:genId() From c96e20e6c35f951a599546d53a130d147445db54 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 6 Sep 2018 18:59:33 +0800 Subject: [PATCH 3/3] add skynet.context and enhance debug console command service --- lualib/skynet.lua | 14 ++++++++++++++ lualib/skynet/debug.lua | 12 ++++++++---- service/service_mgr.lua | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index a9fc92af..68aa5d9c 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -439,6 +439,12 @@ function skynet.ret(msg, sz) return ret end +function skynet.context() + local co_session = session_coroutine_id[running_thread] + local co_address = session_coroutine_address[running_thread] + return co_session, co_address +end + function skynet.ignoreret() -- We use session for other uses session_coroutine_id[running_thread] = nil @@ -782,6 +788,14 @@ function skynet.stat(what) end function skynet.task(ret) + if type(ret) == "number" then + local co = session_id_coroutine[ret] + if co then + return debug.traceback(co) + else + return "No session" + end + end local t = 0 for session,co in pairs(session_id_coroutine) do if ret then diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index aeec0e3e..3b540fda 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -32,10 +32,14 @@ local function init(skynet, export) skynet.ret(skynet.pack(stat)) end - function dbgcmd.TASK() - local task = {} - skynet.task(task) - skynet.ret(skynet.pack(task)) + function dbgcmd.TASK(session) + if session then + skynet.ret(skynet.pack(skynet.task(session))) + else + local task = {} + skynet.task(task) + skynet.ret(skynet.pack(task)) + end end function dbgcmd.INFO(...) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index dcd901fd..e1efeb41 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -16,7 +16,7 @@ local function request(name, func, ...) end for _,v in ipairs(s) do - skynet.wakeup(v) + skynet.wakeup(v.co) end if ok then @@ -47,7 +47,12 @@ local function waitfor(name , func, ...) return request(name, func, ...) end - table.insert(s, co) + local session, source = skynet.context() + table.insert(s, { + co = co, + session = session, + source = source, + }) skynet.wait() s = service[name] if type(s) == "string" then @@ -91,7 +96,11 @@ local function list_service() if type(v) == "string" then v = "Error: " .. v elseif type(v) == "table" then - v = "Querying" + local querying = { "Querying:" } + for _, detail in ipairs(v) do + table.insert(querying, skynet.address(detail.source) .. " " .. tostring(skynet.call(detail.source, "debug", "TASK", detail.session))) + end + v = table.concat(querying, "\n") else v = skynet.address(v) end