-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntryDNSUpdate.cpp
85 lines (71 loc) · 2.24 KB
/
EntryDNSUpdate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
EntryDNSUpdate.cpp - Library for updating dynamic DNS entry at entrydns.net
Created by Kristian Berg, October 24, 2016.
Released into the public domain.
*/
#include "arduino.h"
#include <WiFiClientSecure.h>
#include "EntryDNSUpdate.h"
EntryDNSUpdate::EntryDNSUpdate(String _token, unsigned long _updateFrequencySeconds): token(_token), updateFrequencySeconds(_updateFrequencySeconds) {
}
void EntryDNSUpdate::update() {
unsigned long now = millis();
if(lastUpdated == 0 || (lastUpdated + updateFrequencySeconds * 1000) < now){
doUpdate();
lastUpdated = now;
}
}
void EntryDNSUpdate::doUpdate() {
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
}
String url = "/records/modify/" + token;
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: EntryDNSUpdateESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
Serial.println("status:");
bool ok = false;
if (client.connected()) {
String firstLine = client.readStringUntil('\n');
Serial.println(firstLine);
if (firstLine.startsWith("200 OK", 9)) {
ok = true;
}
Serial.println("headers:");
String line;
do {
line = client.readStringUntil('\n');
Serial.println(line);
} while (line != "\r");
Serial.println("headers received");
}
String line = client.readStringUntil('\r\n');
Serial.println(line);
int length = (int) strtol( line.c_str(), NULL, 16);
uint8_t body[length];
client.read(body, length);
if (ok) {
Serial.println("EntryDNS updated successfully");
} else {
Serial.println("EntryDNS update failed");
}
Serial.println("body:");
Serial.println("==========");
Serial.println(String((const char*)body).substring(0,length));
Serial.println("==========");
Serial.println("closing connection");
}