Last active
August 29, 2015 14:04
-
-
Save cloudwu/41acb20f1914728e331a to your computer and use it in GitHub Desktop.
test reconnect gate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package.cpath = "luaclib/?.so" | |
local socket = require "clientsocket" | |
local crypt = require "crypt" | |
local input = {} | |
local function readpackage() | |
local line = table.remove(input, 1) | |
if line then | |
return line | |
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 | |
end | |
local fd = assert(socket.connect("127.0.0.1", 8888)) | |
--- new connection | |
-- 0\n | |
-- base64(DH_key)\n | |
local clientkey = crypt.randomkey() | |
local text = string.format("0\n%s\n",crypt.base64encode(crypt.dhexchange(clientkey))) | |
socket.send(fd, text) | |
local handshake_response = readpackage() | |
--id\n | |
--base64(DH_key)\n | |
local id,key = handshake_response:match "([^\n]*)\n([^\n]*)" | |
id = tonumber(id) | |
key = crypt.base64decode(key) | |
local secret = crypt.dhsecret(key, clientkey) | |
print("secret ===>", crypt.hexencode(secret)) | |
socket.writeline "hello world" | |
socket.close(fd) | |
local fd = assert(socket.connect("127.0.0.1", 8888)) | |
--- reconnect | |
--id\n | |
--index\n | |
--recvnumber\n | |
--base64(HMAC_CODE)\n | |
local handshake = string.format("%d\n1\n0\n",id) | |
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret) | |
socket.send(fd, string.format("%s%s\n",handshake, crypt.base64encode(hmac))) | |
local reconnect_response = readpackage() | |
--recvnumber\n | |
--CODE msg\n | |
local recv,msg = handshake_response:match "([^\n]*)\n([^\n]*)" | |
recv = tonumber(recv) | |
print(recv, msg) | |
-------------------------- | |
--- read line from server | |
-------------------------- | |
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 | |
print(readline()) | |
socket.close(fd) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment