-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-https-client.bas
More file actions
352 lines (282 loc) · 7.88 KB
/
test-https-client.bas
File metadata and controls
352 lines (282 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
call OpenLBNetDLL
input "press ENTER to begin.";a
connectServer$ = "localhost"
hSock = Connect(connectServer$, "27016", 0)
if IsSocketInvalid(hSock) then
print "Connect() failed. - ";GetError()
goto [doEnd]
end if
print "Connect() successful."
[beginTLS]
print "Creating TLS context..."
hTLS = CreateTLSContext()
print "Acquiring TLS credentials..."
ret = BeginTLSClient(hTLS)
if ret <> 0 then
print "BeginTLSClient() failed. ret - ";ret;" -- Error - ";GetError()
Print dechex$( (abs(ret) XOR hexdec("FFFFFFFF")) + 1)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
Print "Finishing connection..."
[handshakeLoop]
a = SetTLSSocket(hTLS, hSock)
ret = PerformClientHandshake(hTLS, connectServer$)
if ret <> 0 then
print "PerformClientHandshake() failed. - ";ret; " - Error: ";dechex$(GetError())
Print dechex$( (abs(ret) XOR hexdec("FFFFFFFF")) + 1)
a = CloseSocket(hSock)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
crlf$ = chr$(13) + chr$(10)
request$ = "GET /easy/http/ HTTP/1.1" + crlf$
request$ = request$ + "Host: " + connectServer$ + crlf$
request$ = request$ + crlf$
print
print "REQUEST"
print request$
print "======="
print
print "Sending request to server..."
lenRequest = len(request$)
ret = EncryptSend(hTLS, request$, lenRequest)
Print "Awaiting server response..."
[bufLoop]
timer 0
if TLSActive then
ret = IsTLSReadAvailable(hTLS, 0)
end if
if ret = 0 then
'No data waiting. Stop and wait.
timer 1, [bufLoop]
wait
end if
bufLen = 1024
buf$ = space$(bufLen)
num = DecryptReceive(hTLS, buf$, bufLen)
If num = -1 then
theError = GetError()
if theError = 10101 then
print "Connection closed by server."
if HeadersComplete then goto [contentComplete]
else
Print "Socket error occurred. - ";dechex$(GetError())
end if
a = CloseSocket(hSock)
a = DestroyTLSContext(hTLS)
goto [doEnd]
End if
If HeadersComplete then
buf$ = left$(buf$, num)
goto [processContent]
end if
[firstInputSkip]
lf$ = chr$(10)
cmdBuf$ = leftOver$ + left$(buf$, num)
[lineLoop]
lineComplete = instr(cmdBuf$, crlf$)
if lineComplete = 0 then
lineComplete = instr(cmdBuf$, lf$)
if lineComplete = 0 then
leftOver$ = cmdBuf$
goto [bufLoop]
end if
CR = 0
else
CR = 1
end if
cmd$ = trim$(left$(cmdBuf$, lineComplete - 1))
Print "< ";cmd$;" - ";len(cmd$)
if instr(cmd$, "Content-Length:") then
ContentLength = val(word$(cmd$, 2))
print "Content length found - ";ContentLength;" bytes"
end if
if cmd$ <> "" then
cmdBuf$ = right$(cmdBuf$, len(cmdBuf$) - lineComplete - CR)
goto [lineLoop]
else
cmdBuf$ = right$(cmdBuf$, len(cmdBuf$) - lineComplete - CR)
Print "Headers complete, receiving content"
open "output.html" for output as #outfile
HeadersComplete = 1
buf$ = cmdBuf$
end if
[processContent]
if len(buf$) = 0 then
goto [bufLoop]
end if
bytes = len(buf$)
totalBytesReceived = totalBytesReceived + bytes
print "Received ";bytes;" bytes - ";totalBytesReceived;" out of ";ContentLength;" bytes"
print #outfile, buf$;
buf$ = ""
if totalBytesReceived < ContentLength then goto [bufLoop]
[contentComplete]
close #outfile
a = DestroyTLSContext(hTLS)
[doSockEnd]
a = CloseSocket(hServSock)
[doEnd]
call CloseLBNetDLL
Function randNum(min, max)
randNum = int(rnd(1) * max) + min
End Function
'====================
'==Helper Functions==
'====================
Sub OpenLBNetDLL
open "LBNet.dll" for DLL as #LBNet
a = InitLBNet()
End Sub
Sub CloseLBNetDLL
a = EndLBNet()
close #LBNet
End Sub
Function InitLBNet()
CallDLL #LBNet, "InitLBNet",_
InitLBNet as long
End Function
Function EndLBNet()
CallDLL #LBNet, "EndLBNet",_
EndLBNet as long
End Function
Function CreateTLSContext()
CallDLL #LBNet, "CreateTLSContext",_
CreateTLSContext as ulong
End Function
Function DestroyTLSContext(hTLS)
CallDLL #LBNet, "DestroyTLSContext",_
hTLS as ulong,_
DestroyTLSContext as long
End Function
Function BeginTLSClientNoValidation(hTLS)
CallDLL #LBNet, "BeginTLSClientNoValidation",_
hTLS as ulong,_
BeginTLSClientNoValidation as long
End Function
Function BeginTLSClient(hTLS)
CallDLL #LBNet, "BeginTLSClient",_
hTLS as ulong,_
BeginTLSClient as long
End Function
Function IsSocketInvalid(sock)
CallDLL #LBNet, "IsSocketInvalid",_
sock as ulong,_
IsSocketInvalid as long
End Function
Function BeginTLSServer(hTLS, serverName$)
CallDLL #LBNet, "BeginTLSServer",_
hTLS as ulong,_
serverName$ as ptr,_
BeginTLSServer as long
End Function
Function SetTLSSocket(hTLS, sock)
CallDLL #LBNet, "SetTLSSocket",_
hTLS as ulong,_
sock as long,_
SetTLSSock as long
End Function
Function PerformClientHandshake(hTLS, serverName$)
CallDLL #LBNet, "PerformClientHandshake",_
hTLS as ulong,_
serverName$ as ptr,_
PerformClientHandshake as long
End Function
Function PerformServerHandshake(hTLS, doInitialRead, initBuf$, initBufSize)
CallDLL #LBNet, "PerformServerHandshake",_
hTLS as ulong,_
doInitialRead as long,_
initBuf$ as ptr,_
initBufSize as long,_
PerformServerHandshake as long
End Function
Function CreateListenSocket(pService$)
CallDLL #LBNet, "CreateListenSocket",_
pService$ as ptr,_
CreateListenSocket as ulong
End Function
Function AcceptConnection(ServerSocket)
CallDLL #LBNet, "AcceptConnection",_
ServerSocket as ulong,_
AcceptConnection as ulong
End Function
Function IsReadAvailable(socket, msTimeout)
CallDLL #LBNet, "IsReadAvailable",_
socket as ulong,_
msTimeout as long,_
IsReadAvailable as long
End Function
Function IsTLSReadAvailable(hTLS, msTimeout)
CallDLL #LBNet, "IsTLSReadAvailable",_
hTLS as ulong,_
msTimeout as long,_
IsTLSReadAvailable as long
End Function
Function PingHost(host$, packetSize, byref status, byref msResponse, msTimeout)
struct a, b as long
struct c, d as long
a.b.struct = status
c.d.struct = msResponse
CallDLL #LBNet, "PingHost",_
host$ as ptr,_
packetSize as long,_
a as struct,_
c as struct,_
msTimeout as long,_
PingHost as long
status = a.b.struct
msResponse = c.d.struct
End Function
Function Connect(host$, srv$, msTimeout)
CallDLL #LBNet, "Connect",_
host$ as ptr,_
srv$ as ptr,_
msTimeout as long,_
Connect as long
End Function
Function CloseSocket(sock)
CallDLL #LBNet, "CloseSocket",_
sock as long,_
CloseSocket as long
End Function
Function GetError()
CallDLL #LBNet, "GetError",_
GetError as long
if GetError < 0 then
GetError = (abs(GetError) XOR hexdec("FFFFFFFF")) + 1
end if
End Function
Function Send(sock, msg$, msgLen)
CallDLL #LBNet, "Send",_
sock as long,_
msg$ as ptr,_
msgLen as long,_
Send as long
End Function
Function EncryptSend(hTLS, msg$, msgLen)
CallDLL #LBNet, "EncryptSend",_
hTLS as ulong,_
msg$ as ptr,_
msgLen as long,_
EncryptSend as long
End Function
Function Receive(sock, byref buf$, bufLen)
CallDLL #LBNet, "Receive",_
sock as long,_
buf$ as ptr,_
bufLen as long,_
Receive as long
End Function
Function DecryptReceive(hTLS, byref buf$, bufLen)
CallDLL #LBNet, "DecryptReceive",_
hTLS as ulong,_
buf$ as ptr,_
bufLen as long,_
DecryptReceive as long
End Function
Function EndTLSClientSession(hTLS)
CallDLL #LBNet, "EndTLSClientSession",_
hTLS as ulong,_
EndTLSClientSession as long
End Function