Skip to content

Commit d58eca7

Browse files
authored
v2.4
1 parent 7f17fc5 commit d58eca7

File tree

9 files changed

+109
-62
lines changed

9 files changed

+109
-62
lines changed

ESP32_AdBlocker.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Currently it does not have an IPv6 address and some devices use IPv6 by default, so disable IPv6 DNS on
88
your device / router to force it to use IPv4 DNS.
99
10-
Blocklist files can downloaded from hosting sites and should either be in HOSTS format
10+
Blocklist files can be downloaded from hosting sites and should either be in HOSTS format
1111
or Adblock format (only domain name entries processed)
1212
1313
arduino-esp32 library DNSServer.cpp modified as custom AdBlockerDNSServer.cpp so that DNSServer::processNextRequest()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ To switch back to usual DNS Server, eg Google, enter:
2929

3030
Download github files into the Arduino IDE sketch folder, removing `-main` from the application folder name.
3131

32-
Compile using arduino core v2.x or V3.x with PSRAM enabled and the following Partition scheme:
32+
Compile using arduino core v2.x or V3.x (min v3.0.3) with PSRAM enabled and the following Partition scheme:
3333
* ESP32-S3 - `8M with spiffs (...)`
3434
* ESP32 - `Minimal SPIFFS (...)`
3535

appGlobals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
#define DBG_ON false // esp debug output
2323
#define DOT_MAX 50
2424
#define HOSTNAME_GRP 0
25+
#define USE_IP6 false
2526

2627
#define APP_NAME "ESP32_AdBlocker" // max 15 chars
27-
#define APP_VER "2.3"
28+
#define APP_VER "2.4"
2829

2930
#define HTTP_CLIENTS 2 // http, ws
3031
#define MAX_STREAMS 0
@@ -56,7 +57,7 @@
5657
#define EXTPIN 100
5758

5859
// to determine if newer data files need to be loaded
59-
#define CFG_VER 2
60+
#define CFG_VER 3
6061

6162
#ifdef CONFIG_IDF_TARGET_ESP32S3
6263
#define SERVER_STACK_SIZE (1024 * 8)

appSpecific.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static bool downloadBlockList() {
134134
// download blocklist file from web
135135
bool res = false;
136136
WiFiClientSecure wclient;
137-
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate)) {
137+
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate, BLOCKLIST)) {
138138
HTTPClient https;
139139
size_t downloadSize = 0;
140140
char progStr[10];
@@ -263,7 +263,7 @@ void showBlockList(int maxItems) {
263263

264264
/************************ webServer callbacks *************************/
265265

266-
bool updateAppStatus(const char* variable, const char* value) {
266+
bool updateAppStatus(const char* variable, const char* value, bool fromUser) {
267267
// update vars from configs and browser input
268268
bool res = true;
269269
int intVal = atoi(value);

globals.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
#include "ping/ping_sock.h"
2727
#include <Preferences.h>
2828
#include <regex>
29+
#if !CONFIG_IDF_TARGET_ESP32C3
2930
#include <SD_MMC.h>
31+
#endif
3032
#include <LittleFS.h>
3133
#include <sstream>
3234
#include <Update.h>
3335
#include <WiFi.h>
3436
#include <HTTPClient.h>
37+
////#include <NetworkClient.h> // v3.x only
38+
////#include <NetworkClientSecure.h> // v3.x only
3539
#include <WiFiClient.h>
3640
#include <WiFiClientSecure.h>
3741
#include <esp_http_server.h>
@@ -57,6 +61,7 @@
5761
#define CSS_EXT ".css"
5862
#define ICO_EXT ".ico"
5963
#define SVG_EXT ".svg"
64+
#define JPG_EXT ".jpg"
6065
#define CONFIG_FILE_PATH DATA_DIR "/configs" TEXT_EXT
6166
#define LOG_FILE_PATH DATA_DIR "/log" TEXT_EXT
6267
#define OTA_FILE_PATH DATA_DIR "/OTA" HTML_EXT
@@ -80,6 +85,8 @@
8085
#define NULL_TEMP -127
8186
#define OneMHz 1000000
8287
#define USECS 1000000
88+
#define MAGIC_NUM 987654321
89+
#define MAX_FAIL 5
8390

8491
// global mandatory app specific functions, in appSpecific.cpp
8592
bool appDataFiles();
@@ -89,7 +96,7 @@ void appSpecificWsBinHandler(uint8_t* wsMsg, size_t wsMsgLen);
8996
void appSpecificWsHandler(const char* wsMsg);
9097
void appSpecificTelegramTask(void* p);
9198
void buildAppJsonString(bool filter);
92-
bool updateAppStatus(const char* variable, const char* value);
99+
bool updateAppStatus(const char* variable, const char* value, bool fromUser = true);
93100

94101
// global general utility functions in utils.cpp / utilsFS.cpp / peripherals.cpp
95102
void buildJsonString(uint8_t filter);
@@ -151,7 +158,8 @@ float readTemperature(bool isCelsius, bool onlyDS18 = false);
151158
float readVoltage();
152159
void remote_log_init();
153160
void remoteServerClose(WiFiClientSecure& sclient);
154-
bool remoteServerConnect(WiFiClientSecure& sclient, const char* serverName, uint16_t serverPort, const char* serverCert);
161+
bool remoteServerConnect(WiFiClientSecure& sclient, const char* serverName, uint16_t serverPort, const char* serverCert, uint8_t connIdx);
162+
void remoteServerReset();
155163
void removeChar(char* s, char c);
156164
void replaceChar(char* s, char c, char r);
157165
void reset_log();
@@ -173,7 +181,7 @@ bool startWifi(bool firstcall = true);
173181
void stopPing();
174182
void syncToBrowser(uint32_t browserUTC);
175183
bool updateConfigVect(const char* variable, const char* value);
176-
void updateStatus(const char* variable, const char* _value);
184+
void updateStatus(const char* variable, const char* _value, bool fromUser = true);
177185
esp_err_t uploadHandler(httpd_req_t *req);
178186
void urlDecode(char* inVal);
179187
bool urlEncode(const char* inVal, char* encoded, size_t maxSize);
@@ -184,6 +192,7 @@ void wsAsyncSend(const char* wsData);
184192
void startMqttClient();
185193
void stopMqttClient();
186194
void mqttPublish(const char* payload);
195+
void mqttPublishPath(const char* suffix, const char* payload);
187196
// telegram.cpp
188197
bool getTgramUpdate(char* response);
189198
bool sendTgramMessage(const char* info, const char* item, const char* parseMode);
@@ -258,13 +267,6 @@ extern char mqtt_user[];
258267
extern char mqtt_user_Pass[];
259268
extern char mqtt_topic_prefix[];
260269

261-
// External Heartbeat
262-
extern bool external_heartbeat_active;
263-
extern char external_heartbeat_domain[]; //External Heartbeat domain/IP
264-
extern char external_heartbeat_uri[]; //External Heartbeat uri (i.e. /myesp32-cam-hub/index.php)
265-
extern char external_heartbeat_port[]; //External Heartbeat server port to connect.
266-
extern char external_heartbeat_token[]; //External Heartbeat server auth token.
267-
268270
// control sending alerts
269271
extern size_t alertBufferSize;
270272
extern byte* alertBuffer;
@@ -347,6 +349,8 @@ extern bool formatIfMountFailed ; // Auto format the file system if mount failed
347349
(method == HTTP_UNLINK) ? "UNLINK" : \
348350
"UNKNOWN"
349351

352+
enum RemoteFail {SETASSIST, GETEXTIP, TGRAMCONN, FSFTP, EMAILCONN, EXTERNALHB, BLOCKLIST, REMFAILCNT}; // REMFAILCNT always last
353+
350354
/*********************** Log formatting ************************/
351355

352356
//#define USE_LOG_COLORS // uncomment to colorise log messages (eg if using idf.py, but not arduino)

prefs.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ void showConfigVect() {
6060
}
6161

6262
void reloadConfigs() {
63-
while (getNextKeyVal()) updateStatus(variable, value);
63+
while (getNextKeyVal()) updateStatus(variable, value, false);
64+
#if INCLUDE_MQTT
65+
if (mqtt_active) {
66+
buildJsonString(1);
67+
mqttPublishPath("config", jsonBuff);
68+
}
69+
#endif
6470
}
6571

6672
static int getKeyPos(std::string thisKey) {
@@ -221,7 +227,7 @@ static bool loadPrefs() {
221227
return true;
222228
}
223229

224-
void updateStatus(const char* variable, const char* _value) {
230+
void updateStatus(const char* variable, const char* _value, bool fromUser) {
225231
// called from controlHandler() to update app status from changes made on browser
226232
// or from loadConfig() to update app status from stored preferences
227233
bool res = true;
@@ -301,8 +307,7 @@ void updateStatus(const char* variable, const char* _value) {
301307
#if INCLUDE_MQTT
302308
else if (!strcmp(variable, "mqtt_active")) {
303309
mqtt_active = (bool)intVal;
304-
if (mqtt_active) startMqttClient();
305-
else stopMqttClient();
310+
if (!mqtt_active) stopMqttClient();
306311
}
307312
else if (!strcmp(variable, "mqtt_broker")) strncpy(mqtt_broker, value, MAX_HOST_LEN-1);
308313
else if (!strcmp(variable, "mqtt_port")) strncpy(mqtt_port, value, 4);
@@ -353,7 +358,7 @@ void updateStatus(const char* variable, const char* _value) {
353358
if (intVal) savePrefs();
354359
saveConfigVect();
355360
} else {
356-
res = updateAppStatus(variable, value);
361+
res = updateAppStatus(variable, value, fromUser);
357362
if (!res) LOG_VRB("Unrecognised config: %s", variable);
358363
}
359364
if (res) updateConfigVect(variable, value);
@@ -508,7 +513,6 @@ bool loadConfig() {
508513
loadConfigVect();
509514
//showConfigVect();
510515
loadPrefs(); // overwrites any corresponding entries in config
511-
512516
// load variables from stored config vector
513517
reloadConfigs();
514518
debugMemory("loadConfig");

setupAssist.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static bool wgetFile(const char* filePath) {
2828
File f = fp.open(filePath, FILE_WRITE);
2929
if (f) {
3030
WiFiClientSecure wclient;
31-
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate)) {
31+
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate, SETASSIST)) {
3232
HTTPClient https;
3333
if (https.begin(wclient, GITHUB_HOST, HTTPS_PORT, downloadURL, true)) {
3434
LOG_INF("Downloading %s from %s", filePath, downloadURL);
@@ -37,14 +37,14 @@ static bool wgetFile(const char* filePath) {
3737
if (httpCode == HTTP_CODE_OK) {
3838
fileSize = https.writeToStream(&f);
3939
if (fileSize <= 0) {
40+
LOG_WRN("Download failed: writeToStream - %s", https.errorToString(fileSize).c_str());
4041
httpCode = 0;
41-
LOG_WRN("Download failed: writeToStream");
4242
} else LOG_INF("Downloaded %s, size %s", filePath, fmtSize(fileSize));
4343
} else LOG_WRN("Download failed, error: %s", https.errorToString(httpCode).c_str());
4444
https.end();
4545
f.close();
4646
if (httpCode == HTTP_CODE_OK) {
47-
if (!strcmp(filePath, CONFIG_FILE_PATH)) doRestart("config file downloaded");
47+
if (!strcmp(filePath, CONFIG_FILE_PATH)) doRestart("Config file downloaded");
4848
res = true;
4949
} else {
5050
LOG_WRN("HTTP Get failed with code: %d", httpCode);

0 commit comments

Comments
 (0)