diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a9ce45 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/c_cpp_properties.json diff --git a/EasyDDNS.cpp b/EasyDDNS.cpp index e5cf11c..6335689 100644 --- a/EasyDDNS.cpp +++ b/EasyDDNS.cpp @@ -12,10 +12,11 @@ void EasyDDNSClass::service(String ddns_service) { ddns_choice = ddns_service; } -void EasyDDNSClass::client(String ddns_domain, String ddns_username, String ddns_password) { +void EasyDDNSClass::client(String ddns_domain, String ddns_username, String ddns_password, String ddns_identifier) { ddns_d = ddns_domain; ddns_u = ddns_username; ddns_p = ddns_password; + ddns_id = ddns_identifier; } void EasyDDNSClass::update(unsigned long ddns_update_interval, bool use_local_ip) { @@ -38,12 +39,12 @@ void EasyDDNSClass::update(unsigned long ddns_update_interval, bool use_local_ip HTTPClient http; http.begin(client, "http://ifconfig.me/ip"); int httpCode = http.GET(); - if (httpCode > 0) { - if (httpCode == HTTP_CODE_OK) { - new_ip = http.getString(); - } + if (httpCode > 0 && httpCode == HTTP_CODE_OK) { + new_ip = http.getString(); } else { - http.end(); + if(_ddnsErrorFunc != nullptr){ + _ddnsErrorFunc(httpCode, http.getString()); + } return; } http.end(); @@ -74,6 +75,8 @@ void EasyDDNSClass::update(unsigned long ddns_update_interval, bool use_local_ip update_url = "http://sync.afraid.org/u/" + ddns_u + "/"; } else if (ddns_choice == "ovh") { update_url = "http://" + ddns_u + ":" + ddns_p + "@www.ovh.com/nic/update?system=dyndns&hostname=" + ddns_d + "&myip=" + new_ip + ""; + } else if (ddns_choice == "cloudflare") { + update_url = "https://api.cloudflare.com/client/v4/zones/" + ddns_p + "/dns_records/" + ddns_id; } else { Serial.println("## INPUT CORRECT DDNS SERVICE NAME ##"); return; @@ -81,10 +84,24 @@ void EasyDDNSClass::update(unsigned long ddns_update_interval, bool use_local_ip // ######## CHECK & UPDATE ######### // if (old_ip != new_ip) { - WiFiClient client; HTTPClient http; - http.begin(client, update_url); - int httpCode = http.GET(); + int httpCode; + + if (ddns_choice == "cloudflare"){ + // ######## HANDLE CLOUDFLARE ######### // + WiFiClientSecure client; + client.setFingerprint("EE:E9:CE:78:7E:95:78:C9:51:5F:ED:C5:68:15:39:2B:07:1A:9C:BB"); + http.begin(client, update_url); + http.addHeader("Content-Type", "application/json"); + http.addHeader("Authorization", "Bearer " + ddns_u); + String body = "{\"content\": \"" + new_ip + "\", \"name\":\"" + ddns_d + "\", \"type\": \"A\"}"; + httpCode = http.PUT(body); + }else{ + WiFiClient client; + http.begin(client, update_url); + httpCode = http.GET(); + } + if (httpCode == 200) { // Send a callback notification if(_ddnsUpdateFunc != nullptr){ @@ -92,6 +109,9 @@ void EasyDDNSClass::update(unsigned long ddns_update_interval, bool use_local_ip } // Replace Old IP with new one to detect further changes. old_ip = new_ip; + // Send an callback error + }else if(_ddnsErrorFunc != nullptr){ + _ddnsErrorFunc(httpCode, http.getString()); } http.end(); } diff --git a/EasyDDNS.h b/EasyDDNS.h index 291a9c2..2374c63 100644 --- a/EasyDDNS.h +++ b/EasyDDNS.h @@ -10,6 +10,7 @@ Written in 2017 by Ayush Sharma. Licensed under MIT. #include "Arduino.h" #include "stdlib_noniso.h" +#include #if defined(ESP8266) #include "ESP8266WiFi.h" @@ -21,32 +22,39 @@ Written in 2017 by Ayush Sharma. Licensed under MIT. #define HARDWARE "esp32" #endif + // Handler to notify user about new public IP typedef std::function DDNSUpdateHandler; +typedef std::function DDNSErrorHandler; class EasyDDNSClass{ public: void service(String ddns_service); - void client(String ddns_domain, String ddns_username, String ddns_password = ""); + void client(String ddns_domain, String ddns_username, String ddns_password = "", String ddns_identifier = ""); void update(unsigned long ddns_update_interval, bool use_local_ip = false); // Callback void onUpdate(DDNSUpdateHandler handler) { _ddnsUpdateFunc = handler; } + void onError(DDNSErrorHandler handler) { + _ddnsErrorFunc = handler; + } private: DDNSUpdateHandler _ddnsUpdateFunc = nullptr; + DDNSErrorHandler _ddnsErrorFunc = nullptr; unsigned long interval; unsigned long previousMillis; String new_ip; - String old_ip; + String old_ip = "0.0.0.0"; String update_url; String ddns_u; String ddns_p; String ddns_d; + String ddns_id; String ddns_choice; }; extern EasyDDNSClass EasyDDNS; diff --git a/README.md b/README.md index 77fbfaf..414b772 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ EasyDDNS Library can be implemented in your major projects as a sidekick. It is - freemyip - afraid.org - OVH.com +- Cloudflare If you don't know what's DDNS, then you can find more info about DDNS here: [WiKipedia](https://en.wikipedia.org/wiki/Dynamic_DNS) @@ -101,6 +102,7 @@ void setup() { - "strato" - "freemyip" - "afraid.org" + - "cloudflare" */ EasyDDNS.service("duckdns"); @@ -110,6 +112,9 @@ void setup() { For DDNS Providers where you get username and password: ( Leave the password field empty "" if not required ) Use this: EasyDDNS.client("domain", "username", "password"); + + For Cloudflare: + Use this: EasyDDNS.client("domain", "token", "zone_identifier", "identifier"); */ EasyDDNS.client("12345.duckdns.org", "token"); // Enter your DDNS Domain & Token @@ -118,6 +123,13 @@ void setup() { Serial.print("EasyDDNS - IP Change Detected: "); Serial.println(newIP); }); + + // Get Notified when your IP changes + EasyDDNS.onError([&](int httpCode, String errorMsg){ + Serial.print("EasyDDNS - Error Detected: "); + Serial.println(String(httpCode)); + Serial.println(errorMsg); + }); } void loop() { diff --git a/examples/DDNS_Client/DDNS_Client.ino b/examples/DDNS_Client/DDNS_Client.ino index c728265..8d63603 100644 --- a/examples/DDNS_Client/DDNS_Client.ino +++ b/examples/DDNS_Client/DDNS_Client.ino @@ -37,6 +37,7 @@ void setup() { - "strato" - "freemyip" - "afraid.org" + - "cloudflare" */ EasyDDNS.service("duckdns"); @@ -46,6 +47,9 @@ void setup() { For DDNS Providers where you get username and password: ( Leave the password field empty "" if not required ) Use this: EasyDDNS.client("domain", "username", "password"); + + For Cloudflare: + Use this: EasyDDNS.client("domain", "token", "zone_identifier", "identifier"); */ EasyDDNS.client("12345.duckdns.org", "token"); // Enter your DDNS Domain & Token @@ -54,6 +58,13 @@ void setup() { Serial.print("EasyDDNS - IP Change Detected: "); Serial.println(newIP); }); + + // Debug Error Message + EasyDDNS.onError([&](int httpCode, String errorMsg){ + Serial.print("EasyDDNS - Error Detected: "); + Serial.println(String(httpCode)); + Serial.println(errorMsg); + }); } void loop() {