support for 3rd party server urls

This commit is contained in:
Justin Mitchell
2026-01-04 23:02:48 -05:00
parent d75d911c1b
commit ae34fc76e1
4 changed files with 75 additions and 12 deletions

View File

@@ -15,6 +15,9 @@ constexpr uint8_t KOREADER_FILE_VERSION = 1;
// KOReader credentials file path
constexpr char KOREADER_FILE[] = "/.crosspoint/koreader.bin";
// Default sync server URL
constexpr char DEFAULT_SERVER_URL[] = "https://sync.koreader.rocks:443";
// Obfuscation key - "KOReader" in ASCII
// This is NOT cryptographic security, just prevents casual file reading
constexpr uint8_t OBFUSCATION_KEY[] = {0x4B, 0x4F, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72};
@@ -48,6 +51,9 @@ bool KOReaderCredentialStore::saveToFile() const {
obfuscate(obfuscatedPwd);
serialization::writeString(file, obfuscatedPwd);
// Write server URL
serialization::writeString(file, serverUrl);
file.close();
Serial.printf("[%lu] [KRS] Saved KOReader credentials to file\n", millis());
return true;
@@ -70,11 +76,26 @@ bool KOReaderCredentialStore::loadFromFile() {
}
// Read username
serialization::readString(file, username);
if (file.available()) {
serialization::readString(file, username);
} else {
username.clear();
}
// Read and deobfuscate password
serialization::readString(file, password);
obfuscate(password); // XOR is symmetric, so same function deobfuscates
if (file.available()) {
serialization::readString(file, password);
obfuscate(password); // XOR is symmetric, so same function deobfuscates
} else {
password.clear();
}
// Read server URL
if (file.available()) {
serialization::readString(file, serverUrl);
} else {
serverUrl.clear();
}
file.close();
Serial.printf("[%lu] [KRS] Loaded KOReader credentials for user: %s\n", millis(), username.c_str());
@@ -109,3 +130,21 @@ void KOReaderCredentialStore::clearCredentials() {
saveToFile();
Serial.printf("[%lu] [KRS] Cleared KOReader credentials\n", millis());
}
void KOReaderCredentialStore::setServerUrl(const std::string& url) {
serverUrl = url;
Serial.printf("[%lu] [KRS] Set server URL: %s\n", millis(), url.empty() ? "(default)" : url.c_str());
}
std::string KOReaderCredentialStore::getBaseUrl() const {
if (serverUrl.empty()) {
return DEFAULT_SERVER_URL;
}
// Normalize URL: add https:// if no protocol specified
if (serverUrl.find("://") == std::string::npos) {
return "https://" + serverUrl;
}
return serverUrl;
}

View File

@@ -11,6 +11,7 @@ class KOReaderCredentialStore {
static KOReaderCredentialStore instance;
std::string username;
std::string password;
std::string serverUrl; // Custom sync server URL (empty = default)
// Private constructor for singleton
KOReaderCredentialStore() = default;
@@ -43,6 +44,13 @@ class KOReaderCredentialStore {
// Clear credentials
void clearCredentials();
// Server URL management
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)
std::string getBaseUrl() const;
};
// Helper macro to access credential store

View File

@@ -10,9 +10,6 @@
#include "KOReaderCredentialStore.h"
namespace {
// Base URL for KOReader sync server
constexpr char BASE_URL[] = "https://sync.koreader.rocks:443";
// Device identifier for CrossPoint reader
constexpr char DEVICE_NAME[] = "CrossPoint";
constexpr char DEVICE_ID[] = "crosspoint-reader";
@@ -34,7 +31,7 @@ KOReaderSyncClient::Error KOReaderSyncClient::authenticate() {
client->setInsecure();
HTTPClient http;
std::string url = std::string(BASE_URL) + "/users/auth";
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());
@@ -66,7 +63,7 @@ KOReaderSyncClient::Error KOReaderSyncClient::getProgress(const std::string& doc
client->setInsecure();
HTTPClient http;
std::string url = std::string(BASE_URL) + "/syncs/progress/" + documentHash;
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());
@@ -121,7 +118,7 @@ KOReaderSyncClient::Error KOReaderSyncClient::updateProgress(const KOReaderProgr
client->setInsecure();
HTTPClient http;
std::string url = std::string(BASE_URL) + "/syncs/progress";
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());