Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add checksum checking + backups
Browse files Browse the repository at this point in the history
  • Loading branch information
smartfrigde committed Feb 4, 2022
1 parent 3d32d48 commit 57ff483
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Experimental updater for ArmCord made in Nim.
- **Quick and lightweight**
- **One binary is whole updater**
- **Supports custom update servers**
- **Checks for checksums**
## How to use it?
### 1. Client side:
- Download the latest version of the updater from [here](https://github.com/ArmCord/Updater/releases/latest).
Expand Down
3 changes: 2 additions & 1 deletion examples/example_build_info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"version": "3.0.4",
"update_endpoint": "https://armcord.xyz/",
"note": "https://discord.gg/uaW5vMY3V6"
"note": "https://discord.gg/uaW5vMY3V6",
"md5": "3d605bd6a1447afc5e4239b44cf393a3"
}
54 changes: 34 additions & 20 deletions src/Updater.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import puppy
import std/json
import std/strutils
from os import fileExists
import std/os
import std/md5
var attempts = 1
proc checkInternetConnection() =
try:
discard fetch("https://example.com/")
Expand All @@ -15,7 +17,33 @@ proc checkIfValidDirectory() =
else:
echo("Please place the updater in correct path (resources folder). Exiting.")
quit(1)

proc downloadUpdate(endpoint:string) =
echo("New version available!")
echo("Getting the latest asar.")
try:
moveFile("app.asar", "app.asar.old")
moveFile("build_info.json", "build_info.json.old")
writeFile("app.asar", fetch(endpoint & "app.asar"))
writeFile("build_info.json", fetch(endpoint & "latest.json"))
except Exception as e:
echo("Failed to download the latest asar: ", e.msg)
quit(1)
echo ("Downloaded the latest asar. Checking checksums")
if (parseJson(readFile("build_info.json"))["md5"].getStr() == getMD5(readFile("app.asar"))):
echo("Checksums match..")
else:
attempts = attempts + 1
if (attempts > 3):
moveFile("app.asar.old", "app.asar")
moveFile("build_info.json.old", "build_info.json")
echo("Checksums didn't match after 2 attempts to download the update. Reverting the update. Exiting.")
quit(1)
else:
echo("Checksums do not match. Update failed! Retrying")
downloadUpdate(endpoint)
removeFile("app.asar.old")
removeFile("build_info.json.old")
echo("Update installation has finished. You should be able to use the new version of ArmCord.")
when isMainModule:
echo("ArmCord Updater")
echo("Platform: " & hostOS)
Expand All @@ -33,15 +61,7 @@ when isMainModule:
var a = parseInt(latestVersion.replace(".", "")) # Remove the dots to compare
var b = parseInt(currentVersion.replace(".", "")) # Remove the dots to compare
if (a > b):
echo("New version available!")
echo("Getting the latest asar.")
try:
writeFile("app.asar", fetch(endpoint & "app.asar"))
writeFile("build_info.json", fetch(endpoint & "latest.json"))
except Exception as e:
echo("Failed to download the latest asar: ", e.msg)
quit(1)
echo("Update installation has finished. You should be able to use the new version of ArmCord.")
downloadUpdate(endpoint)
else:
echo("No new version available.")
if (a < b):
Expand All @@ -50,13 +70,7 @@ when isMainModule:
write(stdout, "[y/n]: ")
var input = readLine(stdin)
if (input == "y"):
echo("Getting the latest asar.")
try:
writeFile("app.asar", fetch(endpoint & "app.asar"))
writeFile("build_info.json", fetch(endpoint & "latest.json"))
except Exception as e:
echo("Failed to download the latest asar: ", e.msg)
quit(1)
echo("Update installation has finished. You should be able to use the new version of ArmCord.")
downloadUpdate(endpoint)
else:
echo("Downgrading won't be performed.")
echo("Downgrading won't be performed.")

0 comments on commit 57ff483

Please sign in to comment.