Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Add Cloudflare #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/c_cpp_properties.json
38 changes: 29 additions & 9 deletions EasyDDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -74,24 +75,43 @@ 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;
}

// ######## 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){
_ddnsUpdateFunc(old_ip.c_str(), new_ip.c_str());
}
// 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();
}
Expand Down
12 changes: 10 additions & 2 deletions EasyDDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Written in 2017 by Ayush Sharma. Licensed under MIT.

#include "Arduino.h"
#include "stdlib_noniso.h"
#include <WiFiClientSecure.h>

#if defined(ESP8266)
#include "ESP8266WiFi.h"
Expand All @@ -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<void(const char* old_ip, const char* new_ip)> DDNSUpdateHandler;
typedef std::function<void(int httpCode, String errorMsg)> 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;
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -101,6 +102,7 @@ void setup() {
- "strato"
- "freemyip"
- "afraid.org"
- "cloudflare"
*/
EasyDDNS.service("duckdns");

Expand All @@ -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

Expand All @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions examples/DDNS_Client/DDNS_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void setup() {
- "strato"
- "freemyip"
- "afraid.org"
- "cloudflare"
*/
EasyDDNS.service("duckdns");

Expand All @@ -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

Expand All @@ -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() {
Expand Down