-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-https.bas
More file actions
405 lines (320 loc) · 9.1 KB
/
test-https.bas
File metadata and controls
405 lines (320 loc) · 9.1 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
call OpenLBNetDLL
input "press ENTER to begin.";a
hServSock = CreateListenSocket("27016")
if IsSocketInvalid(hServSock) then
print "CreateListenSocket() failed. - ";GetError()
goto [doEnd]
end if
print "CreateListenSocket() successful."
[awaitLoop]
timer 0
print "Checking if connection is available..."
ret = IsReadAvailable(hServSock, 0)
if ret = 0 then
print "No connections yet. Waiting..."
timer 1, [awaitLoop]
wait
end if
if ret = -1 then
print "Error with IsReadAvailable(). - ";GetError()
goto [doSockEnd]
end if
Print "Attempting to accept connection..."
hConn = AcceptConnection(hServSock)
if hConn = -1 then
print "AcceptConnection() failed. - ";GetError()
goto [doSockEnd]
end if
[initReadLoop]
timer 0
ret = IsReadAvailable(hConn, 0)
if ret = 0 then
timer 1, [initReadLoop]
wait
end if
bufLen = 128
buf$ = space$(bufLen)
ret = Receive(hConn, buf$, bufLen)
if ret = 0 or ret = -1 then
print "Receive() failed. - ";ret;" - ";GetError()
a = CloseSocket(hConn)
goto [doSockEnd]
end if
if asc(mid$(buf$, 1, 1)) = hexdec("16") then
print "ClientHello detected, passing to TLS handshake..."
byteCount = ret
goto [beginTLS]
end if
num = ret
goto [firstInputSkip]
[beginTLS]
print "Creating TLS context..."
hTLS = CreateTLSContext()
print "Acquiring TLS credentials..."
ret = BeginTLSServer(hTLS, "localhost")
if ret <> 0 then
print "BeginTLSServer() failed. ret - ";ret;" -- Error - ";GetError()
Print dechex$( (abs(ret) XOR hexdec("FFFFFFFF")) + 1)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
Print "Finishing connection..."
[handshakeLoop]
TLSActive = 1
a = SetTLSSocket(hTLS, hConn)
ret = PerformServerHandshake(hTLS, 0, buf$, byteCount)
if ret <> 0 then
print "PerformServerHandshake() failed. - ";ret; " - Error: ";dechex$(GetError())
Print dechex$( (abs(ret) XOR hexdec("FFFFFFFF")) + 1)
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
goto [doSockEnd]
end if
[bufLoop]
timer 0
if TLSActive then
ret = IsTLSReadAvailable(hTLS, 0)
else
ret = IsReadAvailable(hConn, 0)
end if
if ret = 0 then
'No data waiting. Stop and wait.
timer 1, [bufLoop]
wait
end if
bufLen = 512
buf$ = space$(bufLen)
if TLSActive then
num = DecryptReceive(hTLS, buf$, bufLen)
else
num = Receive(hConn, buf$, bufLen)
end if
If num = -1 or (TLSActive = 0 AND num = 0) then
Print "Socket error occurred. - ";GetError()
a = CloseSocket(hConn)
a = DestroyTLSContext(hTLS)
goto [awaitLoop]
End if
[firstInputSkip]
crlf$ = chr$(13) + chr$(10)
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$
if cmdBuf$ <> crlf$ and cmdBuf$ <> lf$ then
cmdBuf$ = right$(cmdBuf$, len(cmdBuf$) - lineComplete - CR)
goto [lineLoop]
end if
responseStatus$ = "HTTP/1.0 200 OK"
responseHeaders$ = "Server: LB Test" + crlf$
responseHeaders$ = responseHeaders$ + "Content-Language: en" + crlf$
responseHeaders$ = responseHeaders$ + "Content-Type: text/html; charset=utf8" + crlf$
responseHeaders$ = responseHeaders$ + "Connection: close" + crlf$
if TLSActive then
secure$ = "yes"
else
secure$ = "no"
end if
responseHeaders$ = responseHeaders$ + "X-Request-Secure: " + secure$ + crlf$
content$ = "<html><head><title>LB HTTPS Test</title></head>" + crlf$
content$ = content$ + "<body><h1>HTTPS test successful!</h1></body></html>"
lenContent = len(content$)
responseHeaders$ = responseHeaders$ + "Content-Length: " + str$(lenContent) + crlf$
response$ = responseStatus$ + crlf$ + responseHeaders$ + crlf$ + content$
lenResponse = len(response$)
if TLSActive then
ret = EncryptSend(hTLS, response$, lenResponse)
else
ret = Send(hConn, response$, lenResponse)
end if
print
print response$
print "Closing data socket..."
a = CloseSocket(hConn)
print "Destroying TLS context..."
a = DestroyTLSContext(hTLS)
[doSockEnd]
print "Closing server socket..."
a = CloseSocket(hServSock)
[doEnd]
print "Closing TLS DLL..."
call CloseLBNetDLL
print "Performing wait..."
timer 1000, [asdffdsa]
wait
[asdffdsa]
timer 0
print "Ending program..."
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, servernName$)
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