Skip to content

Commit f8dfb42

Browse files
committed
FEATURE: secured websockets client for ESP8266. currently does not support setting fingerprint or ca (so it is not very secured, dosent validate the chain, but does use ssl)
1 parent 618a484 commit f8dfb42

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

src/tiny_websockets/internals/ws_common.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ namespace websockets {
5353
#include <tiny_websockets/network/esp8266/esp8266_tcp.hpp>
5454
#define WSDefaultTcpClient websockets::network::Esp8266TcpClient
5555
#define WSDefaultTcpServer websockets::network::Esp8266TcpServer
56+
57+
#ifndef _WS_CONFIG_NO_SSL
58+
// OpenSSL Dependent
59+
#define WSDefaultSecuredTcpClient websockets::network::SecuredEsp8266TcpClient
60+
#endif //_WS_CONFIG_NO_SSL
61+
5662
#elif defined(ESP32)
5763

5864
#define PLATFORM_DOES_NOT_SUPPORT_BLOCKING_READ

src/tiny_websockets/network/esp8266/esp8266_tcp.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,76 @@ namespace websockets { namespace network {
7676
WiFiClient client;
7777
};
7878

79+
class SecuredEsp8266TcpClient : public TcpClient {
80+
public:
81+
SecuredEsp8266TcpClient(WiFiClientSecure c) : client(c) {
82+
client.setNoDelay(true);
83+
client.setInsecure();
84+
}
85+
86+
SecuredEsp8266TcpClient() {
87+
client.setInsecure();
88+
}
89+
90+
bool connect(const WSString& host, const int port) {
91+
auto didConnect = client.connect(host.c_str(), port);
92+
client.setNoDelay(true);
93+
return didConnect;
94+
}
95+
96+
bool poll() {
97+
return client.available();
98+
}
99+
100+
bool available() override {
101+
return client.connected();
102+
}
103+
104+
void send(const WSString& data) override {
105+
client.write(reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str())), data.size());
106+
}
107+
108+
void send(const WSString&& data) override {
109+
client.write(reinterpret_cast<uint8_t*>(const_cast<char*>(data.c_str())), data.size());
110+
}
111+
112+
void send(const uint8_t* data, const uint32_t len) override {
113+
client.write(data, len);
114+
}
115+
116+
WSString readLine() override {
117+
int val;
118+
WSString line;
119+
do {
120+
val = client.read();
121+
if(val < 0) continue;
122+
line += (char)val;
123+
} while(val != '\n');
124+
if(!available()) close();
125+
return line;
126+
}
127+
128+
void read(uint8_t* buffer, const uint32_t len) override {
129+
client.read(buffer, len);
130+
}
131+
132+
void close() override {
133+
client.stop();
134+
}
135+
136+
virtual ~SecuredEsp8266TcpClient() {
137+
client.stop();
138+
}
139+
140+
protected:
141+
int getSocket() const override {
142+
return -1;
143+
}
144+
145+
private:
146+
WiFiClientSecure client;
147+
};
148+
79149
#define DUMMY_PORT 0
80150

81151
class Esp8266TcpServer : public TcpServer {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
// WS Client Config
43
#define _WS_CONFIG_NO_TRUE_RANDOMNESS
5-
//#define _WS_CONFIG_SKIP_HANDSHAKE_ACCEPT_VALIDATION
6-
//#define _WS_CONFIG_MAX_MESSAGE_SIZE 1000
7-
#define _WS_CONFIG_NO_SSL
4+
5+
#ifndef ESP8266
6+
#define _WS_CONFIG_NO_SSL
7+
#endif

0 commit comments

Comments
 (0)