Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable cerificate checks for SSL connectiond and to set a #459

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ typedef struct {
String plainAuthorization; ///< Base64 encoded Auth request

String extraHeaders;
String userAgentHeader;

bool cHttpHeadersValid; ///< non-websocket http header validity indicator
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
Expand Down
32 changes: 31 additions & 1 deletion src/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void WebSocketsClient::beginSSL(const char * host, uint16_t port, const char * u
_client.isSSL = true;
_fingerprint = fingerprint;
_CA_cert = NULL;
_isSecure = true;
}

void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String fingerprint, String protocol) {
Expand All @@ -110,7 +111,16 @@ void WebSocketsClient::beginSslWithCA(const char * host, uint16_t port, const ch
_client.isSSL = true;
_fingerprint = "";
_CA_cert = CA_cert;
_isSecure = true;
}

/**
* disables certificate checks for SSL connections.
*/
void WebSocketsClient::setInsecure() {
_isSecure = false;
}

#endif

void WebSocketsClient::beginSocketIO(const char * host, uint16_t port, const char * url, const char * protocol) {
Expand All @@ -128,6 +138,7 @@ void WebSocketsClient::beginSocketIOSSL(const char * host, uint16_t port, const
_client.isSocketIO = true;
_client.isSSL = true;
_fingerprint = "";
_isSecure = true;
}

void WebSocketsClient::beginSocketIOSSL(String host, uint16_t port, String url, String protocol) {
Expand All @@ -140,6 +151,7 @@ void WebSocketsClient::beginSocketIOSSLWithCA(const char * host, uint16_t port,
_client.isSSL = true;
_fingerprint = "";
_CA_cert = CA_cert;
_isSecure = true;
}
#endif

Expand All @@ -164,6 +176,11 @@ void WebSocketsClient::loop(void) {
}
_client.ssl = new WEBSOCKETS_NETWORK_SSL_CLASS();
_client.tcp = _client.ssl;

if (!_isSecure) {
_client.ssl->setInsecure();
}

if(_CA_cert) {
DEBUG_WEBSOCKETS("[WS-Client] setting CA certificate");
#if defined(ESP32)
Expand Down Expand Up @@ -343,6 +360,14 @@ void WebSocketsClient::setExtraHeaders(const char * extraHeaders) {
_client.extraHeaders = extraHeaders;
}

/**
* set an user agent which will override the default user agent.
* @param userAgent the user agent header to set
*/
void WebSocketsClient::setUserAgent(const char * userAgent) {
_client.userAgentHeader = userAgent;
}

/**
* set the reconnect Interval
* how long to wait after a connection initiate failed
Expand Down Expand Up @@ -571,7 +596,12 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
handshake += client->extraHeaders + NEW_LINE;
}

handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
if (client->userAgentHeader.length() > 0) {
handshake += WEBSOCKETS_STRING("User-Agent: ") + client->userAgentHeader;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing \r\n ?

handshake += WEBSOCKETS_STRING("User-Agent: ") + client->userAgentHeader + NEW_LINE;

} else {
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
}


if(client->base64Authorization.length() > 0) {
handshake += WEBSOCKETS_STRING("Authorization: Basic ");
Expand Down
7 changes: 5 additions & 2 deletions src/WebSocketsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class WebSocketsClient : protected WebSockets {
void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
void setInsecure();
#endif

#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
Expand Down Expand Up @@ -85,9 +86,10 @@ class WebSocketsClient : protected WebSockets {
void setAuthorization(const char * auth);

void setExtraHeaders(const char * extraHeaders = NULL);

void setUserAgent(const char * userAgent);

void setReconnectInterval(unsigned long time);

void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
void disableHeartbeat();

Expand All @@ -100,6 +102,7 @@ class WebSocketsClient : protected WebSockets {
#if defined(HAS_SSL)
String _fingerprint;
const char * _CA_cert;
bool _isSecure;
#endif
WSclient_t _client;

Expand Down