@@ -12,13 +12,26 @@ WiFiClientSecure client;
1212HTTPClient http;
1313
1414int CubeServer::_begin_client (String path) {
15+ #ifdef ARDUINO_ARCH_ESP32 // ESP32 version only (8266 automatically verifies)--
16+ client.setInsecure (); // Not an issue since we'll manually verify the certificate:
17+ #else // ESP8266 version only (BearSSL)--
18+ // client.allowSelfSignedCerts();
19+ client.setX509Time (this ->_timestamp );
20+ client.setFingerprint (this ->_server_fingerprint );
21+ #endif
1522 int connStatus = client.connect (this ->_conf .API_HOST , this ->_conf .API_PORT );
16- if (connStatus >= 0 ) {
17- // if(client.verify(server_fingerprint, this->_conf.API_HOST)) {
18- http.begin (client, this ->_conf .API_HOST + ' :' + this ->_conf .API_PORT + path);
23+ #ifdef ARDUINO_ARCH_ESP8266
24+ ++connStatus;
25+ #endif
26+ if (connStatus > 0 ) {
27+ #ifdef ARDUINO_ARCH_ESP32 // ESP32 version only (8266 automatically verifies)--
28+ if (!client.verify (this ->_server_fingerprint , this ->_conf .API_CN ))
29+ return VERIFICATION_FAILED;
30+ #endif
31+ // TODO: Make this more efficient by avoiding Strings:
32+ http.begin (client, String (this ->_conf .API_HOST ) + ' :' + this ->_conf .API_PORT + path);
1933 http.setAuthorization (this ->_team_name , this ->_team_secret );
2034 return VERIFICATION_OK;
21- // } return VERIFICATION_FAILED;
2235 }
2336 return connStatus;
2437}
@@ -28,8 +41,8 @@ CubeServer::CubeServer(const char * team_name, const char * team_secret, const c
2841 this ->_team_name = team_name;
2942 this ->_team_secret = team_secret;
3043 this ->_conf = conf;
31- client. setX509Time (timestamp); // Use the build timestamp since we don't have NTP access
32- client. setFingerprint (server_fingerprint) ;
44+ this -> _server_fingerprint = server_fingerprint;
45+ this -> _timestamp = timestamp ;
3346}
3447
3548int CubeServer::connect (bool (*connection_wait_loop)()) {
@@ -38,6 +51,7 @@ int CubeServer::connect(bool (*connection_wait_loop)()) {
3851 while (WiFi.status () != WL_CONNECTED) {
3952 if (!connection_wait_loop ()) return false ;
4053 }
54+
4155 return client.connect (this ->_conf .API_HOST , this ->_conf .API_PORT );
4256}
4357
@@ -58,6 +72,7 @@ int CubeServer::get_status(GameStatus* stats_var) {
5872 StaticJsonDocument<512 > doc;
5973 DeserializationError error = deserializeJson (doc, http.getStream ());
6074 if (error) {
75+ http.end ();
6176 return -1 ;
6277 }
6378 stats_var->unix_time = doc[" unix_time" ];
@@ -77,8 +92,8 @@ int CubeServer::post(char *json) {
7792 if (verification_status == VERIFICATION_OK) {
7893 http.addHeader (" Content-Type" , " application/x-www-form-urlencoded" , false , true );
7994 int httpCode = http.POST (String (" data=" ) + json + ' &' );
80- return httpCode;
8195 http.end ();
96+ return httpCode;
8297 }
8398 http.end ();
8499 return verification_status;
0 commit comments