2020-04-08 20:44:30 +03:00
|
|
|
module websocket
|
2020-04-08 15:22:31 +03:00
|
|
|
|
2020-05-26 13:50:37 +03:00
|
|
|
// On linux, prefer a localy build openssl, because it is
|
|
|
|
// much more likely for it to be newer, than the system
|
|
|
|
// openssl from libssl-dev. If there is no local openssl,
|
|
|
|
// the next flag is harmless, since it will still use the
|
|
|
|
// (older) system openssl.
|
|
|
|
#flag linux -I/usr/local/include/openssl -L/usr/local/lib
|
2020-04-08 15:22:31 +03:00
|
|
|
#flag -lssl
|
2020-05-26 13:50:37 +03:00
|
|
|
// MacPorts
|
|
|
|
#flag darwin -I/opt/local/include
|
|
|
|
#flag darwin -L/opt/local/lib
|
|
|
|
// Brew
|
|
|
|
#flag darwin -I/usr/local/opt/openssl/include
|
|
|
|
#flag darwin -L/usr/local/opt/openssl/lib
|
2020-04-08 15:22:31 +03:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
2020-05-26 13:50:37 +03:00
|
|
|
struct SSL_CTX {
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SSL {
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SSL_METHOD {
|
|
|
|
}
|
2020-04-08 15:22:31 +03:00
|
|
|
|
|
|
|
fn C.SSL_load_error_strings()
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_library_init()
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-24 08:32:51 +03:00
|
|
|
fn C.SSLv23_client_method() &C.SSL_METHOD
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-24 08:32:51 +03:00
|
|
|
fn C.SSL_CTX_new() &C.SSL_CTX
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-24 08:32:51 +03:00
|
|
|
fn C.SSL_new() &C.SSL
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_set_fd() int
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_connect() int
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_shutdown()
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_free()
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_CTX_free()
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_write() int
|
2020-05-26 13:50:37 +03:00
|
|
|
|
2020-04-08 15:22:31 +03:00
|
|
|
fn C.SSL_read() int
|
|
|
|
|
2020-05-26 13:50:37 +03:00
|
|
|
fn (mut ws Client) connect_ssl() {
|
|
|
|
l.i('Using secure SSL connection')
|
2020-04-08 15:22:31 +03:00
|
|
|
C.SSL_load_error_strings()
|
|
|
|
C.SSL_library_init()
|
2020-04-24 08:32:51 +03:00
|
|
|
ws.sslctx = C.SSL_CTX_new(C.SSLv23_client_method())
|
2020-05-26 13:50:37 +03:00
|
|
|
if ws.sslctx == 0 {
|
2020-04-08 15:22:31 +03:00
|
|
|
l.f("Couldn't get ssl context")
|
|
|
|
}
|
2020-04-24 08:32:51 +03:00
|
|
|
ws.ssl = C.SSL_new(ws.sslctx)
|
2020-05-26 13:50:37 +03:00
|
|
|
if ws.ssl == 0 {
|
2020-04-08 15:22:31 +03:00
|
|
|
l.f("Couldn't create OpenSSL instance.")
|
|
|
|
}
|
2020-04-24 08:32:51 +03:00
|
|
|
if C.SSL_set_fd(ws.ssl, ws.socket.sockfd) != 1 {
|
2020-04-08 15:22:31 +03:00
|
|
|
l.f("Couldn't assign ssl to socket.")
|
|
|
|
}
|
2020-04-24 08:32:51 +03:00
|
|
|
if C.SSL_connect(ws.ssl) != 1 {
|
2020-04-08 15:22:31 +03:00
|
|
|
l.f("Couldn't connect using SSL.")
|
|
|
|
}
|
2020-05-26 13:50:37 +03:00
|
|
|
}
|