WebSocketClient can now use custom SSL certificate

Via the `trusted_ssl_certificate` property.
This commit is contained in:
Fabio Alessandrelli 2019-10-07 15:38:03 +02:00
parent 025cc04d9e
commit 33644d7118
4 changed files with 26 additions and 2 deletions

View file

@ -69,8 +69,12 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
String proto_string = p_protocols.join(",");
String str = "ws://";
if (p_ssl)
if (p_ssl) {
str = "wss://";
if (ssl_cert.is_valid()) {
WARN_PRINT_ONCE("Custom SSL certificate is not supported in HTML5 platform.");
}
}
str += p_host + ":" + itos(p_port) + p_path;
_is_connecting = true;

View file

@ -85,6 +85,17 @@ bool WebSocketClient::is_verify_ssl_enabled() const {
return verify_ssl;
}
Ref<X509Certificate> WebSocketClient::get_trusted_ssl_certificate() const {
return ssl_cert;
}
void WebSocketClient::set_trusted_ssl_certificate(Ref<X509Certificate> p_cert) {
ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED);
ssl_cert = p_cert;
}
bool WebSocketClient::is_server() const {
return false;
@ -141,6 +152,11 @@ void WebSocketClient::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
ClassDB::bind_method(D_METHOD("get_trusted_ssl_certificate"), &WebSocketClient::get_trusted_ssl_certificate);
ClassDB::bind_method(D_METHOD("set_trusted_ssl_certificate"), &WebSocketClient::set_trusted_ssl_certificate);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
ADD_SIGNAL(MethodInfo("data_received"));
ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));

View file

@ -31,6 +31,7 @@
#ifndef WEBSOCKET_CLIENT_H
#define WEBSOCKET_CLIENT_H
#include "core/crypto/crypto.h"
#include "core/error_list.h"
#include "websocket_multiplayer_peer.h"
#include "websocket_peer.h"
@ -43,6 +44,7 @@ class WebSocketClient : public WebSocketMultiplayerPeer {
protected:
Ref<WebSocketPeer> _peer;
bool verify_ssl;
Ref<X509Certificate> ssl_cert;
static void _bind_methods();
@ -51,6 +53,8 @@ public:
void set_verify_ssl_enabled(bool p_verify_ssl);
bool is_verify_ssl_enabled() const;
Ref<X509Certificate> get_trusted_ssl_certificate() const;
void set_trusted_ssl_certificate(Ref<X509Certificate> p_cert);
virtual void poll() = 0;
virtual Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocol = PoolVector<String>()) = 0;

View file

@ -237,7 +237,7 @@ void WSLClient::poll() {
ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create());
ERR_FAIL_COND_MSG(ssl.is_null(), "SSL is not available in this build.");
ssl->set_blocking_handshake_enabled(false);
if (ssl->connect_to_stream(_tcp, verify_ssl, _host) != OK) {
if (ssl->connect_to_stream(_tcp, verify_ssl, _host, ssl_cert) != OK) {
disconnect_from_host();
_on_error();
return;