@@ -2,44 +2,65 @@ package.cpath = "luaclib/?.so"
2
2
3
3
local socket = require " clientsocket"
4
4
local crypt = require " crypt"
5
+ local bit32 = require " bit32"
5
6
6
- local last
7
7
local fd = assert (socket .connect (" 127.0.0.1" , 8001 ))
8
- local input = {}
9
8
10
- local function readline ()
11
- local line = table.remove (input , 1 )
12
- if line then
13
- return line
9
+ local function writeline (fd , text )
10
+ socket .send (fd , text .. " \n " )
11
+ end
12
+
13
+ local function unpack_line (text )
14
+ local from = text :find (" \n " , 1 , true )
15
+ if from then
16
+ return text :sub (1 , from - 1 ), text :sub (from + 1 )
14
17
end
18
+ return nil , text
19
+ end
15
20
16
- while true do
17
- local status
18
- status , last = socket .readline (fd , last , input )
19
- if status == nil then
21
+ local last = " "
22
+
23
+ local function unpack_f (f )
24
+ local function try_recv (fd , last )
25
+ local result
26
+ result , last = f (last )
27
+ if result then
28
+ return result , last
29
+ end
30
+ local r = socket .recv (fd )
31
+ if not r then
32
+ return nil , last
33
+ end
34
+ if r == " " then
20
35
error " Server closed"
21
36
end
22
- if not status then
23
- socket .usleep (100 )
24
- else
25
- local line = table.remove (input , 1 )
26
- if line then
27
- return line
37
+ return f (last .. r )
38
+ end
39
+
40
+ return function ()
41
+ while true do
42
+ local result
43
+ result , last = try_recv (fd , last )
44
+ if result then
45
+ return result
28
46
end
47
+ socket .usleep (100 )
29
48
end
30
49
end
31
50
end
32
51
52
+ local readline = unpack_f (unpack_line )
53
+
33
54
local challenge = crypt .base64decode (readline ())
34
55
35
56
local clientkey = crypt .randomkey ()
36
- socket . writeline (fd , crypt .base64encode (crypt .dhexchange (clientkey )))
57
+ writeline (fd , crypt .base64encode (crypt .dhexchange (clientkey )))
37
58
local secret = crypt .dhsecret (crypt .base64decode (readline ()), clientkey )
38
59
39
60
print (" sceret is " , crypt .hexencode (secret ))
40
61
41
62
local hmac = crypt .hmac64 (challenge , secret )
42
- socket . writeline (fd , crypt .base64encode (hmac ))
63
+ writeline (fd , crypt .base64encode (hmac ))
43
64
44
65
local token = {
45
66
server = " sample" ,
56
77
57
78
local etoken = crypt .desencode (secret , encode_token (token ))
58
79
local b = crypt .base64encode (etoken )
59
- socket . writeline (fd , crypt .base64encode (etoken ))
80
+ writeline (fd , crypt .base64encode (etoken ))
60
81
61
82
local result = readline ()
62
83
print (result )
@@ -71,8 +92,18 @@ print("login ok, subid=", subid)
71
92
---- - connect to game server
72
93
73
94
local function send_request (v , session )
74
- local s = string.char (bit32.extract (session ,24 ,8 ), bit32.extract (session ,16 ,8 ), bit32.extract (session ,8 ,8 ), bit32.extract (session ,0 ,8 ))
75
- socket .send (fd , v .. s )
95
+ local size = # v + 4
96
+ local package = string.format (" %c%c%s%c%c%c%c" ,
97
+ bit32.extract (size ,8 ,8 ),
98
+ bit32.extract (size ,0 ,8 ),
99
+ v ,
100
+ bit32.extract (session ,24 ,8 ),
101
+ bit32.extract (session ,16 ,8 ),
102
+ bit32.extract (session ,8 ,8 ),
103
+ bit32.extract (session ,0 ,8 )
104
+ )
105
+
106
+ socket .send (fd , package )
76
107
return v , session
77
108
end
78
109
@@ -87,42 +118,43 @@ local function recv_response(v)
87
118
return ok ~= 0 , content , session
88
119
end
89
120
90
- local input = {}
91
-
92
- local function readpackage ()
93
- local line = table.remove (input , 1 )
94
- if line then
95
- return line
121
+ local function unpack_package (text )
122
+ local size = # text
123
+ if size < 2 then
124
+ return nil , text
96
125
end
97
-
98
- while true do
99
- local status
100
- status , last = socket .recv (fd , last , input )
101
- if status == nil then
102
- error " Server closed"
103
- end
104
- if not status then
105
- socket .usleep (100 )
106
- else
107
- local line = table.remove (input , 1 )
108
- if line then
109
- return line
110
- end
111
- end
126
+ local s = text :byte (1 ) * 256 + text :byte (2 )
127
+ if size < s + 2 then
128
+ return nil , text
112
129
end
130
+
131
+ return text :sub (3 ,2 + s ), text :sub (3 + s )
132
+ end
133
+
134
+ local readpackage = unpack_f (unpack_package )
135
+
136
+ local function send_package (fd , pack )
137
+ local size = # pack
138
+ local package = string.format (" %c%c%s" ,
139
+ bit32.extract (size ,8 ,8 ),
140
+ bit32.extract (size ,0 ,8 ),
141
+ pack )
142
+
143
+ socket .send (fd , package )
113
144
end
114
145
115
146
local text = " echo"
116
147
local index = 1
117
148
118
149
print (" connect" )
119
150
local fd = assert (socket .connect (" 127.0.0.1" , 8888 ))
120
- input = {}
151
+ last = " "
121
152
122
153
local handshake = string.format (" %s@%s#%s:%d" , crypt .base64encode (token .user ), crypt .base64encode (token .server ),crypt .base64encode (subid ) , index )
123
154
local hmac = crypt .hmac64 (crypt .hashkey (handshake ), secret )
124
155
125
- socket .send (fd , handshake .. " :" .. crypt .base64encode (hmac ))
156
+
157
+ send_package (fd , handshake .. " :" .. crypt .base64encode (hmac ))
126
158
127
159
print (readpackage ())
128
160
print (" ===>" ,send_request (text ,0 ))
@@ -136,12 +168,12 @@ index = index + 1
136
168
137
169
print (" connect again" )
138
170
local fd = assert (socket .connect (" 127.0.0.1" , 8888 ))
139
- input = {}
171
+ last = " "
140
172
141
173
local handshake = string.format (" %s@%s#%s:%d" , crypt .base64encode (token .user ), crypt .base64encode (token .server ),crypt .base64encode (subid ) , index )
142
174
local hmac = crypt .hmac64 (crypt .hashkey (handshake ), secret )
143
175
144
- socket . send (fd , handshake .. " :" .. crypt .base64encode (hmac ))
176
+ send_package (fd , handshake .. " :" .. crypt .base64encode (hmac ))
145
177
146
178
print (readpackage ())
147
179
print (" ===>" ,send_request (" fake" ,0 )) -- request again (use last session 0, so the request message is fake)
0 commit comments