mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
better error log, see pr #260
This commit is contained in:
@@ -33,17 +33,17 @@ Success:
|
|||||||
]]
|
]]
|
||||||
|
|
||||||
local socket_error = {}
|
local socket_error = {}
|
||||||
local function assert_socket(v, fd)
|
local function assert_socket(service, v, fd)
|
||||||
if v then
|
if v then
|
||||||
return v
|
return v
|
||||||
else
|
else
|
||||||
skynet.error(string.format("auth failed: socket (fd = %d) closed", fd))
|
skynet.error(string.format("%s failed: socket (fd = %d) closed", service, fd))
|
||||||
error(socket_error)
|
error(socket_error)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function write(fd, text)
|
local function write(service, fd, text)
|
||||||
assert_socket(socket.write(fd, text), fd)
|
assert_socket(service, socket.write(fd, text), fd)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function launch_slave(auth_handler)
|
local function launch_slave(auth_handler)
|
||||||
@@ -57,27 +57,27 @@ local function launch_slave(auth_handler)
|
|||||||
socket.limit(fd, 8192)
|
socket.limit(fd, 8192)
|
||||||
|
|
||||||
local challenge = crypt.randomkey()
|
local challenge = crypt.randomkey()
|
||||||
write(fd, crypt.base64encode(challenge).."\n")
|
write("auth", fd, crypt.base64encode(challenge).."\n")
|
||||||
|
|
||||||
local handshake = assert_socket(socket.readline(fd), fd)
|
local handshake = assert_socket("auth", socket.readline(fd), fd)
|
||||||
local clientkey = crypt.base64decode(handshake)
|
local clientkey = crypt.base64decode(handshake)
|
||||||
if #clientkey ~= 8 then
|
if #clientkey ~= 8 then
|
||||||
error "Invalid client key"
|
error "Invalid client key"
|
||||||
end
|
end
|
||||||
local serverkey = crypt.randomkey()
|
local serverkey = crypt.randomkey()
|
||||||
write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n")
|
write("auth", fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n")
|
||||||
|
|
||||||
local secret = crypt.dhsecret(clientkey, serverkey)
|
local secret = crypt.dhsecret(clientkey, serverkey)
|
||||||
|
|
||||||
local response = assert_socket(socket.readline(fd), fd)
|
local response = assert_socket("auth", socket.readline(fd), fd)
|
||||||
local hmac = crypt.hmac64(challenge, secret)
|
local hmac = crypt.hmac64(challenge, secret)
|
||||||
|
|
||||||
if hmac ~= crypt.base64decode(response) then
|
if hmac ~= crypt.base64decode(response) then
|
||||||
write(fd, "400 Bad Request\n")
|
write("auth", fd, "400 Bad Request\n")
|
||||||
error "challenge failed"
|
error "challenge failed"
|
||||||
end
|
end
|
||||||
|
|
||||||
local etoken = assert_socket(socket.readline(fd),fd)
|
local etoken = assert_socket("auth", socket.readline(fd),fd)
|
||||||
|
|
||||||
local token = crypt.desdecode(secret, crypt.base64decode(etoken))
|
local token = crypt.desdecode(secret, crypt.base64decode(etoken))
|
||||||
|
|
||||||
@@ -91,7 +91,11 @@ local function launch_slave(auth_handler)
|
|||||||
if ok then
|
if ok then
|
||||||
skynet.ret(skynet.pack(err, ...))
|
skynet.ret(skynet.pack(err, ...))
|
||||||
else
|
else
|
||||||
error(err)
|
if err == socket_error then
|
||||||
|
skynet.ret(skynet.pack(nil, "socket error"))
|
||||||
|
else
|
||||||
|
skynet.ret(skynet.pack(false, err))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -108,13 +112,15 @@ local function accept(conf, s, fd, addr)
|
|||||||
socket.start(fd)
|
socket.start(fd)
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
write(fd, "401 Unauthorized\n")
|
if ok ~= nil then
|
||||||
|
write("response 401", fd, "401 Unauthorized\n")
|
||||||
|
end
|
||||||
error(server)
|
error(server)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not conf.multilogin then
|
if not conf.multilogin then
|
||||||
if user_login[uid] then
|
if user_login[uid] then
|
||||||
write(fd, "406 Not Acceptable\n")
|
write("response 406", fd, "406 Not Acceptable\n")
|
||||||
error(string.format("User %s is already login", uid))
|
error(string.format("User %s is already login", uid))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -127,9 +133,9 @@ local function accept(conf, s, fd, addr)
|
|||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
err = err or ""
|
err = err or ""
|
||||||
write(fd, "200 "..crypt.base64encode(err).."\n")
|
write("response 200",fd, "200 "..crypt.base64encode(err).."\n")
|
||||||
else
|
else
|
||||||
write(fd, "403 Forbidden\n")
|
write("response 403",fd, "403 Forbidden\n")
|
||||||
error(err)
|
error(err)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user