From c7d4e71b864f6bf6378fdedbe5794d38bb569fd6 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Mon, 14 Aug 2023 03:05:54 +0800 Subject: [PATCH] initial commit --- README.md | 6 +++--- rdlist_strip.sh | 20 ++++++++++++++++++++ rdlist_transform.sh | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 rdlist_strip.sh create mode 100755 rdlist_transform.sh diff --git a/README.md b/README.md index aab3ae9..18c2c54 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # rdlist-transform -Some bash scripts to add double hashes to rdlist and to strip the room_ids. - -These transformations will generate a "safer" version of rdlist that can be loaded into trusted redlight servers. \ No newline at end of file +Some bash scripts to add double hashes to rdlist (rdlist_transform.sh) and to strip the room_ids from every file in rdlist (rdlist_strip.sh). + +These transformations will generate a "safer" version of rdlist that can be loaded into trusted redlight servers. diff --git a/rdlist_strip.sh b/rdlist_strip.sh new file mode 100644 index 0000000..4d52523 --- /dev/null +++ b/rdlist_strip.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Prompt user for rdlist location +echo "Enter the path to rdlist:" +read RDLIST + +# Check and create blank JSON files +echo "{}" > "$RDLIST/dist/known_rooms.json" +echo "{}" > "$RDLIST/dist/memberships.json" + +# Blank out the values of "room_id" in $RDLIST/dist/summaries.json +if [ -f "$RDLIST/dist/summaries.json" ]; then + jq -c 'map(if .room? and .room.room_id then .room.room_id = "" else . end)' "$RDLIST/dist/summaries.json" > "$RDLIST/dist/summaries.json.tmp" + mv "$RDLIST/dist/summaries.json.tmp" "$RDLIST/dist/summaries.json" + echo "Processed $RDLIST/dist/summaries.json and blanked room_id values." +else + echo "$RDLIST/dist/summaries.json not found!" +fi + +echo "Script completed. The room_ids have been stripped from rdlist." diff --git a/rdlist_transform.sh b/rdlist_transform.sh new file mode 100755 index 0000000..db1f5a4 --- /dev/null +++ b/rdlist_transform.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Ask user for the JSON file location +echo "Enter the path to summaries.json:" +read JSON_FILE + +# Check if file exists +if [ ! -f "$JSON_FILE" ]; then + echo "File not found!" + exit 1 +fi + +# Ask user for the output file location +echo "Enter the path where the output should be written:" +read OUTPUT_FILE + +TMP_JSON="$JSON_FILE.tmp" +cp "$JSON_FILE" "$TMP_JSON" + +# Process each report and update in TMP_JSON +jq --raw-output '.[] | select(.room? and .room.room_id) | .room.room_id' "$JSON_FILE" | while read -r room_id; do + # Double hash the room_id + HASHED_ID=$(echo -n "$room_id" | sha256sum -b | cut -d ' ' -f1 | xxd -r -p | sha256sum | cut -d ' ' -f1) + + # Update hashed id in TMP_JSON inside 'room' + jq --arg room_id "$room_id" --arg hashed_id "$HASHED_ID" 'map(if .room? and .room.room_id == $room_id then .room += {"room_id_hash": $hashed_id} else . end)' "$TMP_JSON" > "$TMP_JSON.tmp" && mv "$TMP_JSON.tmp" "$TMP_JSON" +done + +# For compact output +jq -c '.' "$TMP_JSON" > "$OUTPUT_FILE" + +rm "$TMP_JSON" + +echo "Processing complete. Modified JSON saved to $OUTPUT_FILE. The summaries.json file now has double hashes of the room_id in every report!"