Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit 00ed3ab

Browse files
committed
Added ESP32 support and fixed server verification
1 parent df78680 commit 00ed3ab

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

server.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@ WiFiClientSecure client;
1212
HTTPClient http;
1313

1414
int 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

3548
int 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;

server.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,49 @@
66
*
77
*/
88

9-
109
#ifndef CUBESERVER_SERVER_H
1110
#define CUBESERVER_SERVER_H
1211

1312
#include <Arduino.h>
1413

15-
#include <ArduinoJson.h>
16-
1714
#ifdef ARDUINO_ARCH_ESP8266 // ESP8266 version--
1815
# include <ESP8266WiFi.h>
1916
# include <ESP8266HTTPClient.h>
20-
#include <WiFiClientSecure.h>
21-
22-
# include <WiFiClient.h>
2317
#elif ARDUINO_ARCH_ESP32 // ESP32 version--
24-
# error "ESP32 is not *yet* supported by this library"
18+
#include <WiFi.h>
19+
#include <HTTPClient.h>
2520
#else
2621
# error "Unsupported Architecture."
2722
#endif
2823

24+
#include <ArduinoJson.h>
25+
#include <WiFiClientSecure.h>
26+
2927
#include "server_options.h"
3028

3129
#include "compile_time.h"
3230

3331

3432
#define VERIFICATION_OK 1
35-
#define VERIFICATION_FAILED -16
33+
#define VERIFICATION_FAILED -1
3634

3735

3836

3937
extern WiFiClientSecure client;
4038
extern HTTPClient http;
41-
extern uint8_t server_fingerprint[32];
4239

4340
typedef struct CubeServerConfig {
44-
String AP_SSID;
45-
String API_HOST;
41+
const char *AP_SSID;
42+
const char *API_CN;
43+
const char *API_HOST;
4644
int API_PORT;
4745
} CubeServerConfig;
4846

4947

5048
const CubeServerConfig CUBESERVER_DEFAULT_CONFIG = {
5149
"CubeServer-API",
52-
"https://192.168.252.1",
50+
"api.local",
51+
"https://api.local",
5352
8081
5453
};
5554

@@ -65,6 +64,9 @@ class CubeServer {
6564
const char * _team_name;
6665
const char * _team_secret;
6766
CubeServerConfig _conf;
67+
const char *_server_fingerprint;
68+
unsigned int _timestamp;
69+
6870

6971
int _begin_client(String path);
7072

@@ -74,7 +76,11 @@ class CubeServer {
7476
CubeServer(const char * team_name, const char * team_secret, const char * server_fingerprint) : CubeServer(team_name, team_secret, server_fingerprint, CUBESERVER_DEFAULT_CONFIG) {};
7577

7678
#ifdef CLIENT_CONF_H
77-
CubeServer() : CubeServer(TEAM_NAME, TEAM_SECRET, SERVER_FINGERPRINT) {};
79+
#ifdef ARDUINO_ARCH_ESP32 // Only ESP32 can handle the sha256 fingerprint:
80+
CubeServer() : CubeServer(TEAM_NAME, TEAM_SECRET, SERVER_FINGERPRINT_SHA256) {};
81+
#else
82+
CubeServer() : CubeServer(TEAM_NAME, TEAM_SECRET, SERVER_FINGERPRINT) {};
83+
#endif
7884
#endif
7985

8086
// connect to the wifi access point:

0 commit comments

Comments
 (0)