Unverified Commit 004037d4 authored by Christian Eichler's avatar Christian Eichler Committed by GitHub
Browse files

Merge pull request #6 from Absurd-Mind/https

add support for https, configurable port, and context
parents e8e4a7ec 72371e72
......@@ -17,6 +17,34 @@
// Server fuer Upload
#define SYNC_HTTP_HOST "i4time.cs.fau.de"
// Port fuer die Verbindung
#define SYNC_PORT 443
// Context auf dem Webserver
#define SYNC_CONTEXT "/"
// HTTPS auf 1 setzen um es einzuschalten
#define ENABLE_HTTPS 1
// Root Zertifikat des Webservers (DST Root CA X3, Let's encrypt)
#define ROOT_CERTIFICATE "-----BEGIN CERTIFICATE-----\n" \
"MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" \
"PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" \
"Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" \
"AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" \
"rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" \
"OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" \
"xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" \
"7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" \
"aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" \
"HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" \
"SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" \
"ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" \
"AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" \
"R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" \
"JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" \
"Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" \
"-----END CERTIFICATE-----\n"
// Uploadinterval (in Sekunden)
#define SYNC_INTERVAL (3600*4)
......
// Copyright (c) 2019 Laura Lawniczak, Christian Eichler, Bernhard Heinloth
// SPDX-License-Identifier: AGPL-3.0-only
#include "config.h"
#include <Arduino.h>
#include <WiFi.h>
#include <esp_adc_cal.h>
#include <esp_wifi.h>
#include <esp_wpa2.h>
......@@ -11,10 +12,21 @@
#include <driver/rtc_io.h>
#include <driver/adc.h>
#include "config.h"
#if ENABLE_HTTPS == 1
#include <ssl_client.h>
#include <WiFiClientSecure.h>
WiFiClientSecure client;
#else
#include <WiFi.h>
WiFiClient client;
#endif
#include "accelerometer.h"
#include "timelog.h"
#ifdef SERIAL_DEBUG
#define SDBG(MSG) Serial.print(MSG)
#define SDBGF(MSG, ...) Serial.printf(MSG, __VA_ARGS__)
......@@ -51,7 +63,6 @@ RTC_DATA_ATTR unsigned bug_counter = 0;
RTC_DATA_ATTR bool wifi_active = false;
static Accelerometer accel = Accelerometer(ACCEL_CS_PIN);
static hw_timer_t *timer = nullptr;
......@@ -79,6 +90,10 @@ void setup() {
adc_power_off();
}
#if ENABLE_HTTPS == 1
client.setCACert(ROOT_CERTIFICATE);
#endif
client.setTimeout(1500);
// Light blue LED (on FireBeetle)
pinMode(STATUS_LED_PIN, OUTPUT);
......@@ -368,7 +383,7 @@ static bool uploadData() {
const size_t content_length = (timelog_entry == 0) ? 15 : (17 + timelog_entry * 9);
#ifdef SERIAL_DEBUG
{
SDBGF("Trying to send %d timelog entries (Content-Length: %d Bytes) to " SYNC_HTTP_HOST "\n", timelog_entry, content_length);
SDBGF("Trying to send %d timelog entries (Content-Length: %d Bytes) to " SYNC_HOST "\n", timelog_entry, content_length);
SDBGF(" MAC: %04X%08X\n",(uint16_t)(mac>>32), (uint32_t)mac);
SDBGF(" Battery: %d%%\n", battery);
SDBGF(" Current Time: %s (UNIX Timestamp: %ld)\n", formatTime(now), now);
......@@ -379,11 +394,9 @@ static bool uploadData() {
}
#endif
WiFiClient client;
client.setTimeout(1500);
if(client.connect(SYNC_HTTP_HOST, 80)) {
client.printf("POST /%04X%08X/upload HTTP/1.1\r\n", (uint16_t)(mac>>32), (uint32_t)mac);
client.print ("Host: " SYNC_HTTP_HOST "\r\n");
if(client.connect(SYNC_HOST, SYNC_PORT)) {
client.printf("POST " SYNC_CONTEXT "%04X%08X/upload HTTP/1.1\r\n", (uint16_t)(mac>>32), (uint32_t)mac);
client.print ("Host: " SYNC_HOST "\r\n");
client.print ("User-Agent: i4timecube\r\n");
client.print ("Content-Type: application/x-www-form-urlencoded\r\n");
client.printf("Content-Length: %d\r\n\r\n", content_length);
......@@ -396,7 +409,7 @@ static bool uploadData() {
}
}
SDBGLN(" Response from " SYNC_HTTP_HOST ":");
SDBGLN(" Response from " SYNC_HOST ":");
for (int t = 0; t<20 && client.connected();t++) {
String line = client.readStringUntil('\n');
SDBGF(" [line %d] %s\n", t, line.c_str());
......@@ -407,8 +420,10 @@ static bool uploadData() {
break;
}
}
} else
} else {
SDBGLN("Connection failed");
return false;
}
}
return false;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment