2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-08-06 06:54:47 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module http
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
import strings
|
2020-07-12 15:21:40 +03:00
|
|
|
import net.openssl
|
2020-05-20 09:58:57 +03:00
|
|
|
|
2020-07-12 15:21:40 +03:00
|
|
|
const (
|
|
|
|
is_used = openssl.is_used
|
|
|
|
)
|
2019-08-06 06:54:47 +03:00
|
|
|
|
2019-10-10 20:24:36 +03:00
|
|
|
fn (req &Request) ssl_do(port int, method, host_name, path string) ?Response {
|
2019-12-22 01:41:42 +03:00
|
|
|
// ssl_method := C.SSLv23_method()
|
2019-11-24 06:27:02 +03:00
|
|
|
ssl_method := C.TLSv1_2_method()
|
|
|
|
ctx := C.SSL_CTX_new(ssl_method)
|
|
|
|
C.SSL_CTX_set_verify_depth(ctx, 4)
|
|
|
|
flags := C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION
|
|
|
|
C.SSL_CTX_set_options(ctx, flags)
|
|
|
|
mut res := C.SSL_CTX_load_verify_locations(ctx, 'random-org-chain.pem', 0)
|
|
|
|
web := C.BIO_new_ssl_connect(ctx)
|
2019-08-21 20:04:06 +03:00
|
|
|
addr := host_name + ':' + port.str()
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.BIO_set_conn_hostname(web, addr.str)
|
2020-07-12 15:21:40 +03:00
|
|
|
ssl := &openssl.SSL(0)
|
2019-11-24 06:27:02 +03:00
|
|
|
C.BIO_get_ssl(web, &ssl)
|
|
|
|
preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
|
|
|
|
res = C.SSL_set_cipher_list(ssl, preferred_ciphers.str)
|
2019-08-06 06:54:47 +03:00
|
|
|
if res != 1 {
|
2020-05-18 06:10:56 +03:00
|
|
|
println('http: openssl: cipher failed')
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
|
|
|
res = C.SSL_set_tlsext_host_name(ssl, host_name.str)
|
|
|
|
res = C.BIO_do_connect(web)
|
2019-12-09 13:31:24 +03:00
|
|
|
if res != 1 {
|
|
|
|
return error('cannot connect the endpoint')
|
|
|
|
}
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.BIO_do_handshake(web)
|
2019-12-06 15:24:53 +03:00
|
|
|
C.SSL_get_peer_certificate(ssl)
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.SSL_get_verify_result(ssl)
|
2019-12-22 01:41:42 +03:00
|
|
|
// /////
|
2020-05-18 06:10:56 +03:00
|
|
|
req_headers := req.build_request_headers(method, host_name, path)
|
|
|
|
C.BIO_puts(web, req_headers.str)
|
2020-05-20 14:32:59 +03:00
|
|
|
mut content := strings.new_builder(100)
|
2020-05-20 21:40:29 +03:00
|
|
|
mut buff := [bufsize]byte
|
2020-05-20 12:04:28 +03:00
|
|
|
mut readcounter := 0
|
2019-08-06 06:54:47 +03:00
|
|
|
for {
|
2020-05-20 12:04:28 +03:00
|
|
|
readcounter++
|
2020-05-20 21:40:29 +03:00
|
|
|
len := C.BIO_read(web, buff, bufsize)
|
2020-05-18 06:10:56 +03:00
|
|
|
if len <= 0 {
|
2019-11-24 06:27:02 +03:00
|
|
|
break
|
|
|
|
}
|
2020-05-20 12:04:28 +03:00
|
|
|
$if debug_http ? {
|
2020-05-20 14:32:59 +03:00
|
|
|
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
|
2020-05-20 21:40:29 +03:00
|
|
|
eprintln('-'.repeat(20))
|
2020-05-20 14:32:59 +03:00
|
|
|
eprintln(tos(buff, len))
|
2020-05-20 21:40:29 +03:00
|
|
|
eprintln('-'.repeat(20))
|
2020-05-18 06:10:56 +03:00
|
|
|
}
|
2020-05-20 14:32:59 +03:00
|
|
|
content.write_bytes(buff, len)
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
2020-05-20 14:32:59 +03:00
|
|
|
if web != 0 {
|
2019-08-06 06:54:47 +03:00
|
|
|
C.BIO_free_all(web)
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
2020-05-20 14:32:59 +03:00
|
|
|
if ctx != 0 {
|
2019-11-24 06:27:02 +03:00
|
|
|
C.SSL_CTX_free(ctx)
|
2019-08-10 11:05:59 +03:00
|
|
|
}
|
2020-05-20 14:32:59 +03:00
|
|
|
return parse_response(content.str())
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|