Allows koreader sync to run over http

This commit is contained in:
Justin Mitchell 2026-01-14 11:57:07 -05:00
parent 614b000c31
commit b32bf51f0a
4 changed files with 53 additions and 24 deletions

View File

@ -153,9 +153,9 @@ std::string KOReaderCredentialStore::getBaseUrl() const {
return DEFAULT_SERVER_URL;
}
// Normalize URL: add https:// if no protocol specified
// Normalize URL: add http:// if no protocol specified (local servers typically don't have SSL)
if (serverUrl.find("://") == std::string::npos) {
return "https://" + serverUrl;
return "http://" + serverUrl;
}
return serverUrl;

View File

@ -57,7 +57,7 @@ class KOReaderCredentialStore {
void setServerUrl(const std::string& url);
const std::string& getServerUrl() const { return serverUrl; }
// Get base URL for API calls (with https:// normalization, falls back to default)
// Get base URL for API calls (with http:// normalization if no protocol, falls back to default)
std::string getBaseUrl() const;
// Document matching method

View File

@ -3,6 +3,7 @@
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ctime>
@ -19,6 +20,10 @@ void addAuthHeaders(HTTPClient& http) {
http.addHeader("x-auth-user", KOREADER_STORE.getUsername().c_str());
http.addHeader("x-auth-key", KOREADER_STORE.getMd5Password().c_str());
}
bool isHttpsUrl(const std::string& url) {
return url.rfind("https://", 0) == 0;
}
} // namespace
KOReaderSyncClient::Error KOReaderSyncClient::authenticate() {
@ -27,14 +32,20 @@ KOReaderSyncClient::Error KOReaderSyncClient::authenticate() {
return NO_CREDENTIALS;
}
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure);
client->setInsecure();
HTTPClient http;
std::string url = KOREADER_STORE.getBaseUrl() + "/users/auth";
Serial.printf("[%lu] [KOSync] Authenticating: %s\n", millis(), url.c_str());
http.begin(*client, url.c_str());
HTTPClient http;
std::unique_ptr<WiFiClientSecure> secureClient;
WiFiClient plainClient;
if (isHttpsUrl(url)) {
secureClient.reset(new WiFiClientSecure);
secureClient->setInsecure();
http.begin(*secureClient, url.c_str());
} else {
http.begin(plainClient, url.c_str());
}
addAuthHeaders(http);
const int httpCode = http.GET();
@ -59,24 +70,32 @@ KOReaderSyncClient::Error KOReaderSyncClient::getProgress(const std::string& doc
return NO_CREDENTIALS;
}
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure);
client->setInsecure();
HTTPClient http;
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress/" + documentHash;
Serial.printf("[%lu] [KOSync] Getting progress: %s\n", millis(), url.c_str());
http.begin(*client, url.c_str());
HTTPClient http;
std::unique_ptr<WiFiClientSecure> secureClient;
WiFiClient plainClient;
if (isHttpsUrl(url)) {
secureClient.reset(new WiFiClientSecure);
secureClient->setInsecure();
http.begin(*secureClient, url.c_str());
} else {
http.begin(plainClient, url.c_str());
}
addAuthHeaders(http);
const int httpCode = http.GET();
if (httpCode == 200) {
// Parse JSON response
JsonDocument doc;
const DeserializationError error = deserializeJson(doc, *client);
// Parse JSON response from response string
String responseBody = http.getString();
http.end();
JsonDocument doc;
const DeserializationError error = deserializeJson(doc, responseBody);
if (error) {
Serial.printf("[%lu] [KOSync] JSON parse failed: %s\n", millis(), error.c_str());
return JSON_ERROR;
@ -114,14 +133,20 @@ KOReaderSyncClient::Error KOReaderSyncClient::updateProgress(const KOReaderProgr
return NO_CREDENTIALS;
}
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure);
client->setInsecure();
HTTPClient http;
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress";
Serial.printf("[%lu] [KOSync] Updating progress: %s\n", millis(), url.c_str());
http.begin(*client, url.c_str());
HTTPClient http;
std::unique_ptr<WiFiClientSecure> secureClient;
WiFiClient plainClient;
if (isHttpsUrl(url)) {
secureClient.reset(new WiFiClientSecure);
secureClient->setInsecure();
http.begin(*secureClient, url.c_str());
} else {
http.begin(plainClient, url.c_str());
}
addAuthHeaders(http);
http.addHeader("Content-Type", "application/json");

View File

@ -112,14 +112,18 @@ void KOReaderSettingsActivity::handleSelection() {
updateRequired = true;
}));
} else if (selectedIndex == 2) {
// Sync Server URL
// Sync Server URL - prefill with https:// if empty to save typing
const std::string currentUrl = KOREADER_STORE.getServerUrl();
const std::string prefillUrl = currentUrl.empty() ? "https://" : currentUrl;
exitActivity();
enterNewActivity(new KeyboardEntryActivity(
renderer, mappedInput, "Sync Server URL", KOREADER_STORE.getServerUrl(), 10,
renderer, mappedInput, "Sync Server URL", prefillUrl, 10,
128, // maxLength - URLs can be long
false, // not password
[this](const std::string& url) {
KOREADER_STORE.setServerUrl(url);
// Clear if user just left the prefilled https://
const std::string urlToSave = (url == "https://" || url == "http://") ? "" : url;
KOREADER_STORE.setServerUrl(urlToSave);
KOREADER_STORE.saveToFile();
exitActivity();
updateRequired = true;