Almost finished with new launcher
This commit is contained in:
@@ -8,126 +8,118 @@ import {
|
||||
readTextFile,
|
||||
writeFile
|
||||
} from '@tauri-apps/plugin-fs'
|
||||
import { decrypt, encrypt } from './Encryption'
|
||||
import { VersionsConfig } from '../types/VersionsConfig'
|
||||
import { getKey } from './KeysHelper'
|
||||
|
||||
export async function readNormalConfig (): Promise<NormalConfig> {
|
||||
export async function readNormalConfig(): Promise<NormalConfig> {
|
||||
const version = await app.getVersion()
|
||||
try {
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('config.dat', options)
|
||||
const doesConfigExist = await exists('config.json', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('config.dat', options)
|
||||
const file = await create('config.json', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
await encrypt(
|
||||
JSON.stringify(new NormalConfig(version)),
|
||||
await getKey(2)
|
||||
)
|
||||
JSON.stringify(new NormalConfig(version), null, 2),
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
return new NormalConfig(version)
|
||||
}
|
||||
const config = await readTextFile('config.dat', options)
|
||||
const config = await readTextFile('config.json', options)
|
||||
return NormalConfig.import(
|
||||
JSON.parse(await decrypt(config, await getKey(2)))
|
||||
JSON.parse(config)
|
||||
)
|
||||
} catch {
|
||||
return new NormalConfig(version)
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeNormalConfig (data: NormalConfig) {
|
||||
export async function writeNormalConfig(data: NormalConfig) {
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('config.dat', options)
|
||||
const doesConfigExist = await exists('config.json', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('config.dat', options)
|
||||
const file = await create('config.json', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
await encrypt(JSON.stringify(data), await getKey(2))
|
||||
JSON.stringify(data, null, 2)
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
} else {
|
||||
await writeFile(
|
||||
'config.dat',
|
||||
'config.json',
|
||||
new TextEncoder().encode(
|
||||
await encrypt(JSON.stringify(data), await getKey(2))
|
||||
JSON.stringify(data, null, 2)
|
||||
),
|
||||
options
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function readVersionsConfig (): Promise<VersionsConfig> {
|
||||
export async function readVersionsConfig(): Promise<VersionsConfig> {
|
||||
const version = await app.getVersion()
|
||||
try {
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('versions.dat', options)
|
||||
const doesConfigExist = await exists('versions.json', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('versions.dat', options)
|
||||
const file = await create('versions.json', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
await encrypt(
|
||||
JSON.stringify(new VersionsConfig(version)),
|
||||
await getKey(3)
|
||||
)
|
||||
JSON.stringify(new VersionsConfig(version), null, 2)
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
return new VersionsConfig(version)
|
||||
}
|
||||
const config = await readTextFile('versions.dat', options)
|
||||
const config = await readTextFile('versions.json', options)
|
||||
return VersionsConfig.import(
|
||||
JSON.parse(await decrypt(config, await getKey(3)))
|
||||
JSON.parse(config)
|
||||
)
|
||||
} catch {
|
||||
return new VersionsConfig(version)
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeVersionsConfig (data: VersionsConfig) {
|
||||
export async function writeVersionsConfig(data: VersionsConfig) {
|
||||
const options = {
|
||||
baseDir: BaseDirectory.AppLocalData
|
||||
}
|
||||
const doesFolderExist = await exists('', options)
|
||||
const doesConfigExist = await exists('versions.dat', options)
|
||||
const doesConfigExist = await exists('versions.json', options)
|
||||
if (!doesFolderExist || !doesConfigExist) {
|
||||
if (!doesFolderExist) {
|
||||
await mkdir('', options)
|
||||
}
|
||||
const file = await create('versions.dat', options)
|
||||
const file = await create('versions.json', options)
|
||||
await file.write(
|
||||
new TextEncoder().encode(
|
||||
await encrypt(JSON.stringify(data), await getKey(3))
|
||||
JSON.stringify(data, null, 2)
|
||||
)
|
||||
)
|
||||
await file.close()
|
||||
} else {
|
||||
await writeFile(
|
||||
'versions.dat',
|
||||
'versions.json',
|
||||
new TextEncoder().encode(
|
||||
await encrypt(JSON.stringify(data), await getKey(3))
|
||||
JSON.stringify(data, null, 2)
|
||||
),
|
||||
options
|
||||
)
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import CryptoJS from 'crypto-js'
|
||||
import { getKey } from './KeysHelper'
|
||||
|
||||
export async function encrypt (
|
||||
plainText: string,
|
||||
key?: string
|
||||
): Promise<string> {
|
||||
if (!key) key = await getKey(1)
|
||||
const iv = CryptoJS.lib.WordArray.random(16)
|
||||
const encrypted = CryptoJS.AES.encrypt(
|
||||
plainText,
|
||||
CryptoJS.enc.Utf8.parse(key),
|
||||
{
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
}
|
||||
)
|
||||
const combined = iv.concat(encrypted.ciphertext)
|
||||
return CryptoJS.enc.Base64.stringify(combined)
|
||||
}
|
||||
|
||||
export async function decrypt (dataB64: string, key?: string): Promise<string> {
|
||||
if (!key) key = await getKey(0)
|
||||
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(
|
||||
data.words.slice(4),
|
||||
data.sigBytes - 16
|
||||
)
|
||||
const cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext })
|
||||
|
||||
const decrypted = CryptoJS.AES.decrypt(
|
||||
cipherParams,
|
||||
CryptoJS.enc.Utf8.parse(key),
|
||||
{
|
||||
iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
}
|
||||
)
|
||||
const result = decrypted.toString(CryptoJS.enc.Utf8)
|
||||
if (!result) throw new Error(await encrypt('-997'))
|
||||
return result
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
|
||||
export async function getKey (key: number): Promise<string> {
|
||||
try {
|
||||
const message = await invoke('get_keys_config', { key })
|
||||
return message as string
|
||||
} catch (error) {
|
||||
console.error('Failed to get key from Tauri backend', error)
|
||||
return ''
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user