Skip to content

Commit

Permalink
dump rUID/auth to content.json
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Oct 4, 2023
1 parent 081a831 commit ca2cf7e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions include/contentJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct
char *cloud_ruid;
uint8_t *cloud_auth;
size_t cloud_auth_len;
bool_t cloud_valid;
bool_t cloud_override;
char *tonie_model;

bool_t _stream;
Expand All @@ -25,7 +25,7 @@ typedef struct

} contentJson_t;

#define CONTENT_JSON_VERSION 4
#define CONTENT_JSON_VERSION 5

error_t load_content_json(const char *content_path, contentJson_t *content_json);
error_t save_content_json(const char *content_path, contentJson_t *content_json);
Expand Down
3 changes: 2 additions & 1 deletion include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define TONIES_CUSTOM_JSON_PATH "config/tonies.custom.json"
#define CONFIG_PATH "config/config.ini"
#define CONFIG_OVERLAY_PATH "config/config.overlay.ini"
#define CONFIG_VERSION 4
#define CONFIG_VERSION 5
#define MAX_OVERLAYS 16 + 1

typedef enum
Expand Down Expand Up @@ -58,6 +58,7 @@ typedef struct
bool markCustomTagByPass;
bool prioCustomContent;
bool updateOnLowerAudioId;
bool dumpRuidAuthContentJson;
} settings_cloud_t;

typedef struct
Expand Down
7 changes: 4 additions & 3 deletions src/contentJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ error_t load_content_json(const char *content_path, contentJson_t *content_json)
content_json->cache = content_jsonGetBool(contentJson, "cache");
content_json->cloud_ruid = content_jsonGetString(contentJson, "cloud_ruid");
content_json->cloud_auth = content_jsonGetBytes(contentJson, "cloud_auth", &content_json->cloud_auth_len);
content_json->cloud_override = content_jsonGetBool(contentJson, "cloud_override");
content_json->tonie_model = content_jsonGetString(contentJson, "tonie_model");
content_json->cloud_valid = true;

// TODO: use checkCustomTonie to validate
if (osStrlen(content_json->cloud_ruid) != 16)
{
// TODO validate rUID
content_json->cloud_valid = false;
content_json->cloud_override = false;
}
if (content_json->cloud_auth_len != AUTH_TOKEN_LENGTH)
{
content_json->cloud_valid = false;
content_json->cloud_override = false;
}

if (osStrlen(content_json->source) > 0)
Expand Down Expand Up @@ -200,6 +200,7 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)
cJSON_AddBoolToObject(contentJson, "cache", content_json->cache);
content_AddStringToObject(contentJson, "cloud_ruid", content_json->cloud_ruid);
content_AddByteArrayToObject(contentJson, "cloud_auth", content_json->cloud_auth, content_json->cloud_auth_len);
cJSON_AddBoolToObject(contentJson, "cloud_override", content_json->cloud_override);
content_AddStringToObject(contentJson, "tonie_model", content_json->tonie_model);
cJSON_AddNumberToObject(contentJson, "_version", CONTENT_JSON_VERSION);

Expand Down
36 changes: 31 additions & 5 deletions src/handler_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ void markLiveTonie(tonie_info_t *tonieInfo)
TRACE_INFO("Marked custom tonie %s\r\n", tonieInfo->contentPath);
}

void dumpRuidAuth(contentJson_t *content_json, char *ruid, uint8_t *authentication)
{
if (!content_json->cloud_override && osStrlen(content_json->cloud_ruid) == 0)
{
osFreeMem(content_json->cloud_auth);
content_json->cloud_auth_len = AUTH_TOKEN_LENGTH;
content_json->cloud_auth = osAllocMem(content_json->cloud_auth_len);
osMemcpy(content_json->cloud_auth, authentication, content_json->cloud_auth_len);

osFreeMem(content_json->cloud_ruid);
content_json->cloud_ruid = strdup(ruid);
content_json->_updated = true;
TRACE_INFO("Dumped rUID %s and auth into content.json\r\n", content_json->cloud_ruid);
}
}

error_t handleCloudLog(HttpConnection *connection, const char_t *uri, const char_t *queryString, client_ctx_t *client_ctx)
{
if (client_ctx->settings->cloud.enabled && client_ctx->settings->cloud.enableV1Log)
Expand Down Expand Up @@ -213,16 +229,21 @@ error_t handleCloudClaim(HttpConnection *connection, const char_t *uri, const ch
httpPrepareHeader(connection, NULL, 0);
connection->response.statusCode = 200;

if (!tonieInfo.contentConfig.nocloud || tonieInfo.contentConfig.cloud_valid)
if (client_ctx->settings->cloud.dumpRuidAuthContentJson)
{
if (checkCustomTonie(ruid, token, client_ctx->settings) && !tonieInfo.contentConfig.cloud_valid)
dumpRuidAuth(&tonieInfo.contentConfig, ruid, token);
}

if (!tonieInfo.contentConfig.nocloud || tonieInfo.contentConfig.cloud_override)
{
if (checkCustomTonie(ruid, token, client_ctx->settings) && !tonieInfo.contentConfig.cloud_override)
{
TRACE_INFO(" >> custom tonie detected, nothing forwarded\r\n");
markCustomTonie(&tonieInfo);
}
else if (client_ctx->settings->cloud.enabled && client_ctx->settings->cloud.enableV1Claim)
{
if (tonieInfo.contentConfig.cloud_valid)
if (tonieInfo.contentConfig.cloud_override)
{
token = tonieInfo.contentConfig.cloud_auth;
convertTokenBytesToString(token, msg, client_ctx->settings->log.logFullAuth);
Expand Down Expand Up @@ -285,14 +306,19 @@ error_t handleCloudContent(HttpConnection *connection, const char_t *uri, const
getContentPathFromCharRUID(ruid, &tonieInfo.contentPath, client_ctx->settings);
tonieInfo = getTonieInfo(tonieInfo.contentPath, client_ctx->settings);

if (!tonieInfo.contentConfig.nocloud && !noPassword && checkCustomTonie(ruid, token, client_ctx->settings) && !tonieInfo.contentConfig.cloud_valid)
if (!tonieInfo.contentConfig.nocloud && !noPassword && checkCustomTonie(ruid, token, client_ctx->settings) && !tonieInfo.contentConfig.cloud_override)
{
TRACE_INFO(" >> custom tonie detected, nothing forwarded\r\n");
markCustomTonie(&tonieInfo);
}

settings_t *settings = client_ctx->settings;

if (client_ctx->settings->cloud.dumpRuidAuthContentJson)
{
dumpRuidAuth(&tonieInfo.contentConfig, ruid, token);
}

bool setLive = false;
const char *assignFile = NULL;

Expand Down Expand Up @@ -446,7 +472,7 @@ error_t handleCloudContent(HttpConnection *connection, const char_t *uri, const
{
TRACE_INFO("Serve cloud content from %s\r\n", uri);

if (tonieInfo.contentConfig.cloud_valid)
if (tonieInfo.contentConfig.cloud_override)
{
token = tonieInfo.contentConfig.cloud_auth;
convertTokenBytesToString(token, msg, client_ctx->settings->log.logFullAuth);
Expand Down
1 change: 1 addition & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ static void option_map_init(uint8_t settingsId)
OPTION_BOOL("cloud.markCustomTagByPass", &settings->cloud.markCustomTagByPass, TRUE, "Autodetect custom tags", "Automatically mark custom tags by password")
OPTION_BOOL("cloud.prioCustomContent", &settings->cloud.prioCustomContent, TRUE, "Prioritize custom content", "Prioritize custom content over tonies content (force update)")
OPTION_BOOL("cloud.updateOnLowerAudioId", &settings->cloud.updateOnLowerAudioId, FALSE, "Update content on lower audio id", "Update content on a lower audio id")
OPTION_BOOL("cloud.dumpRuidAuthContentJson", &settings->cloud.dumpRuidAuthContentJson, FALSE, "Dump rUID/auth", "Dump the rUID and authentication into the content JSON.")

OPTION_TREE_DESC("toniebox", "Toniebox")
OPTION_BOOL("toniebox.overrideCloud", &settings->toniebox.overrideCloud, TRUE, "Override cloud settings", "Override tonies cloud settings")
Expand Down

0 comments on commit ca2cf7e

Please sign in to comment.