Skip to content

Instantly share code, notes, and snippets.

@cotestatnt
Created January 23, 2022 09:57
Show Gist options
  • Save cotestatnt/570f882e19f12490304d4d217aa1d8af to your computer and use it in GitHub Desktop.
Save cotestatnt/570f882e19f12490304d4d217aa1d8af to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "xxxxxxx"
#define STAPSK "xxxxxxx"
#endif
const char* host = "api.telegram.org";
const char* ssid = STASSID;
const char* password = STAPSK;
void getFingerprint(const char* host, uint8_t* fingerprint = nullptr) {
WiFiClientSecure client;
client.setInsecure();
// Example "F2:AD:29:9C:34:48:DD:8D:F4:CF:52:32:F6:57:33:68:2E:81:C1:90"
char fingerPrintStr[59];
char request[128];
snprintf(request, sizeof(request), "GET /fingerprints.htm?chain=%s", host);
if (!client.connect("www.grc.com", 443)) {
Serial.println("connection failed");
delay(1000);
return;
}
// Make a HTTP request:
client.println(request);
client.println("Host: www.grc.com");
client.println("Connection: close");
client.println();
delay(500);
if (client.available()) {
char searchKey[80];
snprintf(searchKey, sizeof(searchKey), "<td class=\"ledge\">%s</td>", host);
if (client.find(searchKey)) {
// Find next table cell where fingerprint string is placed
if (client.find("<td>") ) {
client.readBytes(fingerPrintStr, 59);
}
}
// Clear RX buffer
while (client.available()) {
client.read();
}
client.stop();
}
// Copy bytes in fingerprint array if requested
if (fingerprint != nullptr) {
uint8_t i = 0;
for (char * pch = strtok(fingerPrintStr, ":"); pch != NULL; pch = strtok(NULL, ":")) {
if (pch != NULL) {
fingerprint[i++] = (uint8_t)strtol(pch, NULL, 16);
}
}
}
}
void setup() {
Serial.begin(115200);
Serial.print("\n\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nWiFi connected. \nIP address: ");
Serial.println(WiFi.localIP());
Serial.print("\nGet fingerprint for host ");
Serial.print(host);
Serial.println(": ");
uint8_t fingerprint[20];
getFingerprint(host, fingerprint);
for(int i=0; i<sizeof(fingerprint); i++) {
Serial.print(fingerprint[i], HEX);
(i < sizeof(fingerprint) -1) ? Serial.print(":") : Serial.println();
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment