Fixed client using unmatching hashing algorithm, added config.ini.examples for both sides

This commit is contained in:
Tracker-Friendly 2024-07-13 10:07:39 +01:00
parent 4d505c156c
commit ccad458270
5 changed files with 55 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

View File

@ -0,0 +1,11 @@
[config]
# Used for authenticating the backup requests. Change to a random password, and keep the same on both the client and server.
BACKUP_KEY=supersecretkey
# Used for encrypting the backup files during transfer. Change to a random password, and keep the same on both the client and server.
CRYPTO_KEY=supersecretkey
# How often the client should backup in seconds. Default is 86400 seconds (24 hours).
BACKUP_INTERVAL=86400
# The URL of the server to send the backups to.
REMOTE_URL=http://example.org:8080
# The file to backup, relative to where the command is run.
FILE_LOCATION=/path/to/file

View File

@ -2,20 +2,39 @@ package main
import ( import (
"concord.hectabit.org/Hectabit/burgerbackup/lib/client" "concord.hectabit.org/Hectabit/burgerbackup/lib/client"
"github.com/spf13/viper"
"log" "log"
"os"
"strconv"
"time" "time"
) )
var (
backupKey = "meow"
backupInterval = 43200
fileLocation = "database.db"
remoteURL = "http://localhost:8088/api/backup"
)
func main() { func main() {
if _, err := os.Stat("config.ini"); err == nil {
log.Println("[INFO] Config loaded at", time.Now().Unix())
} else if os.IsNotExist(err) {
log.Fatalln("[FATAL] config.ini does not exist")
} else {
log.Fatalln("[FATAL] File is in quantum uncertainty:", err)
}
viper.SetConfigName("config")
viper.AddConfigPath("./")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
log.Fatalln("[FATAL] Error in config file at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
}
backupKey := viper.GetString("BACKUP_KEY")
cryptoKey := viper.GetString("CRYPTO_KEY")
backupInterval := viper.GetInt("BACKUP_INTERVAL")
fileLocation := viper.GetString("FILE_LOCATION")
remoteURL := viper.GetString("REMOTE_URL")
for { for {
err, errCode := client.PerformBackup(fileLocation, backupKey, remoteURL) err, errCode := client.PerformBackup(fileLocation, backupKey, cryptoKey, remoteURL)
if err != nil { if err != nil {
if errCode == 0 { if errCode == 0 {
log.Println("[CRITICAL] Unknown in performBackup() file read:", err) log.Println("[CRITICAL] Unknown in performBackup() file read:", err)

View File

@ -0,0 +1,12 @@
[config]
# Used for authenticating the backup requests. Change to a random password, and keep the same on both the client and server.
BACKUP_KEY=supersecretkey
# Used for encrypting the backup files during transfer. Change to a random password, and keep the same on both the client and server.
CRYPTO_KEY=supersecretkey
# The port burgerbackup runs on. Change to Port 80 if not using a reverse proxy.
PORT=8080
# The host burgerbackup runs on. Change to 127.0.0.1 if using a reverse proxy.
HOST=0.0.0.0
# The folder where the backups are stored, relative to where the command is run.
# It is recommended to use an absolute path
BACKUP_FOLDER=/path/to/backup/folder

View File

@ -5,20 +5,22 @@ import (
"concord.hectabit.org/Hectabit/burgerbackup/lib/common" "concord.hectabit.org/Hectabit/burgerbackup/lib/common"
"encoding/json" "encoding/json"
"errors" "errors"
"golang.org/x/crypto/argon2"
"io" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
) )
func PerformBackup(fileLocation string, backupKey string, remoteURL string) (error, int) { func PerformBackup(fileLocation string, backupKey string, cryptoKey string, remoteURL string) (error, int) {
fileContent, err := os.ReadFile(fileLocation) fileContent, err := os.ReadFile(fileLocation)
if err != nil { if err != nil {
log.Println("[CRITICAL] Unknown in performBackup() file read:", err) log.Println("[CRITICAL] Unknown in performBackup() file read:", err)
return err, 0 return err, 0
} }
encryptedContent, err := common.EncryptAES([]byte(backupKey), fileContent) cryptoKeyHashed := argon2.IDKey([]byte(cryptoKey), []byte("burgerbackup"), 1, 64*1024, 4, 32)
encryptedContent, err := common.EncryptAES(cryptoKeyHashed, fileContent)
if err != nil { if err != nil {
log.Println("[CRITICAL] Unknown in performBack() content encryption", err) log.Println("[CRITICAL] Unknown in performBack() content encryption", err)
return err, 1 return err, 1