forked from Berry-Dash/launcher
Config system
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
"core:window:allow-toggle-maximize",
|
||||
"decorum:allow-show-snap-overlay",
|
||||
"fs:default",
|
||||
"fs:allow-applocaldata-read",
|
||||
"fs:allow-applocaldata-write",
|
||||
"dialog:default"
|
||||
]
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export enum Keys {
|
||||
SERVER_RECEIVE_TRANSFER_KEY = '',
|
||||
SERVER_SEND_TRANSFER_KEY = ''
|
||||
SERVER_SEND_TRANSFER_KEY = '',
|
||||
CONFIG_ENCRYPTION_KEY = ''
|
||||
}
|
||||
|
||||
14
src/types/NormalConfig.ts
Normal file
14
src/types/NormalConfig.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { SettingsType } from './SettingsType'
|
||||
|
||||
export class NormalConfig {
|
||||
constructor (
|
||||
public version: string,
|
||||
public settings: SettingsType = new SettingsType()
|
||||
) {}
|
||||
|
||||
static import (data: any) {
|
||||
const cfg = new NormalConfig(data.version)
|
||||
Object.assign(cfg.settings, data.settings)
|
||||
return cfg
|
||||
}
|
||||
}
|
||||
6
src/types/SettingsType.ts
Normal file
6
src/types/SettingsType.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class SettingsType {
|
||||
constructor (
|
||||
public checkForNewVersionOnLoad: boolean = true,
|
||||
public useWineOnUnixWhenNeeded: boolean = false
|
||||
) {}
|
||||
}
|
||||
70
src/util/BazookaManager.ts
Normal file
70
src/util/BazookaManager.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { app } from '@tauri-apps/api'
|
||||
import { NormalConfig } from '../types/NormalConfig'
|
||||
import {
|
||||
BaseDirectory,
|
||||
create,
|
||||
exists,
|
||||
mkdir,
|
||||
readTextFile,
|
||||
writeFile,
|
||||
writeTextFile
|
||||
} from '@tauri-apps/plugin-fs'
|
||||
import { decrypt, encrypt } from './Encryption'
|
||||
import { Keys } from '../enums/Keys'
|
||||
|
||||
export async function readConfig (): Promise<NormalConfig> {
|
||||
const version = await app.getVersion()
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('config.dat', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('config.dat', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
encrypt(
|
||||
JSON.stringify(new NormalConfig(version)),
|
||||
Keys.CONFIG_ENCRYPTION_KEY
|
||||
)
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
return new NormalConfig(version)
|
||||
}
|
||||
const config = await readTextFile('config.dat', options)
|
||||
return NormalConfig.import(
|
||||
JSON.parse(decrypt(config, Keys.CONFIG_ENCRYPTION_KEY))
|
||||
)
|
||||
}
|
||||
|
||||
export async function writeConfig (data: NormalConfig) {
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('config.dat', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('config.dat', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
encrypt(JSON.stringify(data), Keys.CONFIG_ENCRYPTION_KEY)
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
} else {
|
||||
await writeFile(
|
||||
'config.dat',
|
||||
new TextEncoder().encode(
|
||||
encrypt(JSON.stringify(data), Keys.CONFIG_ENCRYPTION_KEY)
|
||||
),
|
||||
options
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import CryptoJS from 'crypto-js'
|
||||
import { Keys } from '../enums/Keys'
|
||||
|
||||
export function encrypt (plainText: string): string {
|
||||
export function encrypt (plainText: string, key: string = Keys.SERVER_SEND_TRANSFER_KEY): string {
|
||||
const iv = CryptoJS.lib.WordArray.random(16)
|
||||
const encrypted = CryptoJS.AES.encrypt(
|
||||
plainText,
|
||||
CryptoJS.enc.Utf8.parse(Keys.SERVER_SEND_TRANSFER_KEY),
|
||||
CryptoJS.enc.Utf8.parse(key),
|
||||
{
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
@@ -16,7 +16,7 @@ export function encrypt (plainText: string): string {
|
||||
return CryptoJS.enc.Base64.stringify(combined)
|
||||
}
|
||||
|
||||
export function decrypt (dataB64: string): string {
|
||||
export function decrypt (dataB64: string, key: string = Keys.SERVER_RECEIVE_TRANSFER_KEY): string {
|
||||
const data = CryptoJS.enc.Base64.parse(dataB64)
|
||||
const iv = CryptoJS.lib.WordArray.create(data.words.slice(0, 4), 16)
|
||||
const ciphertext = CryptoJS.lib.WordArray.create(
|
||||
@@ -27,7 +27,7 @@ export function decrypt (dataB64: string): string {
|
||||
|
||||
const decrypted = CryptoJS.AES.decrypt(
|
||||
cipherParams,
|
||||
CryptoJS.enc.Utf8.parse(Keys.SERVER_RECEIVE_TRANSFER_KEY),
|
||||
CryptoJS.enc.Utf8.parse(key),
|
||||
{
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
|
||||
Reference in New Issue
Block a user