mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
simplify clientsocket lib
This commit is contained in:
@@ -2,44 +2,65 @@ package.cpath = "luaclib/?.so"
|
||||
|
||||
local socket = require "clientsocket"
|
||||
local crypt = require "crypt"
|
||||
local bit32 = require "bit32"
|
||||
|
||||
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
|
||||
local function writeline(fd, text)
|
||||
socket.send(fd, text .. "\n")
|
||||
end
|
||||
|
||||
local function unpack_line(text)
|
||||
local from = text:find("\n", 1, true)
|
||||
if from then
|
||||
return text:sub(1, from-1), text:sub(from+1)
|
||||
end
|
||||
return nil, text
|
||||
end
|
||||
|
||||
while true do
|
||||
local status
|
||||
status, last = socket.readline(fd, last, input)
|
||||
if status == nil then
|
||||
local last = ""
|
||||
|
||||
local function unpack_f(f)
|
||||
local function try_recv(fd, last)
|
||||
local result
|
||||
result, last = f(last)
|
||||
if result then
|
||||
return result, last
|
||||
end
|
||||
local r = socket.recv(fd)
|
||||
if not r then
|
||||
return nil, last
|
||||
end
|
||||
if r == "" then
|
||||
error "Server closed"
|
||||
end
|
||||
if not status then
|
||||
socket.usleep(100)
|
||||
else
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
return f(last .. r)
|
||||
end
|
||||
|
||||
return function()
|
||||
while true do
|
||||
local result
|
||||
result, last = try_recv(fd, last)
|
||||
if result then
|
||||
return result
|
||||
end
|
||||
socket.usleep(100)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local readline = unpack_f(unpack_line)
|
||||
|
||||
local challenge = crypt.base64decode(readline())
|
||||
|
||||
local clientkey = crypt.randomkey()
|
||||
socket.writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey)))
|
||||
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))
|
||||
writeline(fd, crypt.base64encode(hmac))
|
||||
|
||||
local token = {
|
||||
server = "sample",
|
||||
@@ -56,7 +77,7 @@ end
|
||||
|
||||
local etoken = crypt.desencode(secret, encode_token(token))
|
||||
local b = crypt.base64encode(etoken)
|
||||
socket.writeline(fd, crypt.base64encode(etoken))
|
||||
writeline(fd, crypt.base64encode(etoken))
|
||||
|
||||
local result = readline()
|
||||
print(result)
|
||||
@@ -71,8 +92,18 @@ print("login ok, subid=", subid)
|
||||
----- connect to game server
|
||||
|
||||
local function send_request(v, session)
|
||||
local s = string.char(bit32.extract(session,24,8), bit32.extract(session,16,8), bit32.extract(session,8,8), bit32.extract(session,0,8))
|
||||
socket.send(fd , v..s)
|
||||
local size = #v + 4
|
||||
local package = string.format("%c%c%s%c%c%c%c",
|
||||
bit32.extract(size,8,8),
|
||||
bit32.extract(size,0,8),
|
||||
v,
|
||||
bit32.extract(session,24,8),
|
||||
bit32.extract(session,16,8),
|
||||
bit32.extract(session,8,8),
|
||||
bit32.extract(session,0,8)
|
||||
)
|
||||
|
||||
socket.send(fd, package)
|
||||
return v, session
|
||||
end
|
||||
|
||||
@@ -87,29 +118,29 @@ local function recv_response(v)
|
||||
return ok ~=0 , content, session
|
||||
end
|
||||
|
||||
local input = {}
|
||||
|
||||
local function readpackage()
|
||||
local line = table.remove(input, 1)
|
||||
if line then
|
||||
return line
|
||||
local function unpack_package(text)
|
||||
local size = #text
|
||||
if size < 2 then
|
||||
return nil, text
|
||||
end
|
||||
local s = text:byte(1) * 256 + text:byte(2)
|
||||
if size < s+2 then
|
||||
return nil, text
|
||||
end
|
||||
|
||||
while true do
|
||||
local status
|
||||
status, last = socket.recv(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
|
||||
return text:sub(3,2+s), text:sub(3+s)
|
||||
end
|
||||
|
||||
local readpackage = unpack_f(unpack_package)
|
||||
|
||||
local function send_package(fd, pack)
|
||||
local size = #pack
|
||||
local package = string.format("%c%c%s",
|
||||
bit32.extract(size,8,8),
|
||||
bit32.extract(size,0,8),
|
||||
pack)
|
||||
|
||||
socket.send(fd, package)
|
||||
end
|
||||
|
||||
local text = "echo"
|
||||
@@ -117,12 +148,13 @@ local index = 1
|
||||
|
||||
print("connect")
|
||||
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||
input = {}
|
||||
last = ""
|
||||
|
||||
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
|
||||
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
|
||||
|
||||
socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||
|
||||
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||
|
||||
print(readpackage())
|
||||
print("===>",send_request(text,0))
|
||||
@@ -136,12 +168,12 @@ index = index + 1
|
||||
|
||||
print("connect again")
|
||||
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||
input = {}
|
||||
last = ""
|
||||
|
||||
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
|
||||
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
|
||||
|
||||
socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||
|
||||
print(readpackage())
|
||||
print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake)
|
||||
|
||||
Reference in New Issue
Block a user