Pārlūkot izejas kodu

Use PROGMEM in a few places; dont bother verifying fingerprint

Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
7d2273e0a6
2 mainītis faili ar 47 papildinājumiem un 47 dzēšanām
  1. 36 34
      lib/GithubClient/GithubClient.cpp
  2. 11 13
      lib/GithubClient/GithubClient.h

+ 36 - 34
lib/GithubClient/GithubClient.cpp

@@ -1,29 +1,31 @@
 #include <GithubClient.h>
 #include <FS.h>
+#include <Size.h>
+
+static const char HTTP_REQUEST_FORMAT[] PROGMEM =
+  "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: esp8266_milight_hub\r\nConnection: close\r\n\r\n";
 
 Stream& GithubClient::stream(const String& path) {
   if (!client.connect(domain.c_str(), 443)) {
     Serial.println(F("Failed to connect to github over HTTPS."));
     return client;
   }
-  
-  if (!client.verify(sslFingerprint.c_str(), domain.c_str())) {
-    Serial.println(F("Failed to verify github certificate"));
-    return client;
-  }
-  
+
+  char buffer[strlen_P(HTTP_REQUEST_FORMAT)+1];
+  strcpy_P(buffer, HTTP_REQUEST_FORMAT);
+
   client.printf(
-    "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: esp8266_milight_hub\r\nConnection: close\r\n\r\n",
-    path.c_str(), 
+    buffer,
+    path.c_str(),
     domain.c_str()
   );
-               
+
   return client;
 }
-  
+
 bool GithubClient::download(const String& path, Stream& dest) {
   Stream& client = stream(path);
-  
+
   if (client.available()) {
     if (!client.find("\r\n\r\n")) {
       Serial.println(F("Error seeking to body"));
@@ -33,69 +35,69 @@ bool GithubClient::download(const String& path, Stream& dest) {
     Serial.println(F("Failed to open stream to Github"));
     return false;
   }
-  
+
   Serial.println(F("Downloading..."));
-  
+
   size_t bytes = 0;
   size_t nextCheckpoint = 4096;
-               
+
   while (client.available()) {
     size_t l = client.readBytes(buffer, GITHUB_CLIENT_BUFFER_SIZE);
     size_t w = dest.write(buffer, l);
-    
+
     dest.flush();
-    
+
     if (w != l) {
       printf_P(PSTR("Error writing to stream. Expected to write %d bytes, but only wrote %d\n"), l, w);
       return false;
     }
-    
+
     bytes += w;
-    
+
     if (bytes % 10 == 0) {
       printf_P(".");
     }
-    
+
     if (bytes >= nextCheckpoint) {
       printf("[%d KB]\n", bytes/1024);
       nextCheckpoint += 4096;
     }
-    
+
     yield();
   }
-  
+
   Serial.println(F("\n"));
-  
+
   return true;
 }
 
 bool GithubClient::download(const String& path, const String& fsPath) {
   String tmpFile = fsPath + ".download_tmp";
   File f = SPIFFS.open(tmpFile.c_str(), "w");
-  
+
   if (!f) {
     Serial.print(F("ERROR - could not open file for downloading: "));
     Serial.println(fsPath);
     return false;
   }
   printf(".");
-  
+
   if (!download(path, f)) {
     f.close();
     return false;
   }
-  
+
   f.flush();
   f.close();
-  
+
   SPIFFS.remove(fsPath);
   SPIFFS.rename(tmpFile, fsPath);
-  
-  printf("Finished downloading file: %s\n", fsPath.c_str());
-  
+
+  printf_P(PSTR("Finished downloading file: %s\n"), fsPath.c_str());
+
   return true;
 }
-  
+
 String GithubClient::buildRepoPath(const String& username, const String& repo, const String& repoPath) {
   String path = String("/") + username + "/" + repo + "/master/" + repoPath;
   return path;
@@ -104,11 +106,11 @@ String GithubClient::buildRepoPath(const String& username, const String& repo, c
 String GithubClient::buildApiRequest(const String &username, const String &repo, const String &path) {
   return String("/repos/") + username + "/" + repo + path;
 }
-  
+
 GithubClient GithubClient::rawDownloader() {
-  return GithubClient(GITHUB_RAW_DOMAIN, GITHUB_RAW_FINGERPRINT);
+  return GithubClient(GITHUB_RAW_DOMAIN);
 }
 
 GithubClient GithubClient::apiClient() {
-  return GithubClient(GITHUB_API_DOMAIN, GITHUB_API_FINGERPRINT);
-}
+  return GithubClient(GITHUB_API_DOMAIN);
+}

+ 11 - 13
lib/GithubClient/GithubClient.h

@@ -2,39 +2,37 @@
 #include <WiFiClientSecure.h>
 
 #ifndef _GITHUB_CLIENT
-#define _GITHUB_CLIENT 
+#define _GITHUB_CLIENT
 
 #define GITHUB_CLIENT_BUFFER_SIZE 32
 
-#define GITHUB_RAW_FINGERPRINT "CC AA 48 48 66 46 0E 91 53 2C 9C 7C 23 2A B1 74 4D 29 9D 33"
+// #define GITHUB_RAW_FINGERPRINT "CC AA 48 48 66 46 0E 91 53 2C 9C 7C 23 2A B1 74 4D 29 9D 33"
 #define GITHUB_RAW_DOMAIN "raw.githubusercontent.com"
 
-#define GITHUB_API_FINGERPRINT "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"
+// #define GITHUB_API_FINGERPRINT "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"
 #define GITHUB_API_DOMAIN "api.github.com"
 
 class GithubClient {
 public:
-  GithubClient(const char* domain, const char* sslFingerprint)
-    : domain(String(domain)),
-      sslFingerprint(String(sslFingerprint))
+  GithubClient(const char* domain)
+    : domain(String(domain))
   { }
-  
+
   Stream& stream(const String& path);
   bool download(const String& path, Stream& dest);
   bool download(const String& path, const String& fsPath);
-  
+
   static GithubClient rawDownloader();
   static GithubClient apiClient();
-  
+
   static String buildRepoPath(const String& username, const String& repo, const String& path);
   static String buildApiRequest(const String& username, const String& repo, const String& path);
-  
+
   uint8_t buffer[GITHUB_CLIENT_BUFFER_SIZE];
-  
+
 private:
   WiFiClientSecure client;
   const String domain;
-  const String sslFingerprint;
 };
 
-#endif
+#endif