mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
loginserver and example
This commit is contained in:
8
examples/config.login
Normal file
8
examples/config.login
Normal file
@@ -0,0 +1,8 @@
|
||||
thread = 8
|
||||
logger = nil
|
||||
harbor = 0
|
||||
start = "main"
|
||||
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||
luaservice = "./service/?.lua;./examples/login/?.lua"
|
||||
lualoader = "lualib/loader.lua"
|
||||
cpath = "./cservice/?.so"
|
||||
59
examples/login/client.lua
Normal file
59
examples/login/client.lua
Normal file
@@ -0,0 +1,59 @@
|
||||
package.cpath = "luaclib/?.so"
|
||||
|
||||
local socket = require "clientsocket"
|
||||
local cjson = require "cjson"
|
||||
local crypt = require "crypt"
|
||||
|
||||
local last
|
||||
local fd = assert(socket.connect("127.0.0.1", 8001))
|
||||
local input = {}
|
||||
|
||||
local function readline()
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
end
|
||||
|
||||
while true do
|
||||
local status
|
||||
status, last = socket.readline(fd, last, input)
|
||||
if status == nil then
|
||||
error "Server closed"
|
||||
end
|
||||
if not status then
|
||||
socket.usleep(100)
|
||||
else
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local challenge = crypt.base64decode(readline())
|
||||
|
||||
local clientkey = crypt.randomkey()
|
||||
socket.writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey)))
|
||||
local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey)
|
||||
|
||||
print("sceret is ", crypt.hexencode(secret))
|
||||
|
||||
local hmac = crypt.hmac64(challenge, secret)
|
||||
socket.writeline(fd, crypt.base64encode(hmac))
|
||||
|
||||
local token = {
|
||||
user = "hello",
|
||||
pass = "password",
|
||||
}
|
||||
|
||||
local etoken = crypt.desencode(secret, cjson.encode(token))
|
||||
local b = crypt.base64encode(etoken)
|
||||
socket.writeline(fd, crypt.base64encode(etoken))
|
||||
|
||||
print(readline())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
25
examples/login/logind.lua
Normal file
25
examples/login/logind.lua
Normal file
@@ -0,0 +1,25 @@
|
||||
local login = require "loginserver"
|
||||
local json = require "cjson"
|
||||
local crypt = require "crypt"
|
||||
|
||||
local server = {
|
||||
host = "127.0.0.1",
|
||||
port = 8001,
|
||||
}
|
||||
|
||||
function server.auth_handler(token)
|
||||
token = json.decode(token)
|
||||
assert(token.user)
|
||||
assert(token.pass == "password")
|
||||
return "sample", token.user
|
||||
end
|
||||
|
||||
function server.login_handler(server, uid, secret)
|
||||
print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret)))
|
||||
end
|
||||
|
||||
function server.logout_handler(server, uid)
|
||||
print(string.format("%s@%s is logout", uid, server))
|
||||
end
|
||||
|
||||
login(server)
|
||||
5
examples/login/main.lua
Normal file
5
examples/login/main.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
skynet.newservice "logind"
|
||||
end)
|
||||
Reference in New Issue
Block a user