Improved serverlist structure
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::{collections::HashMap, fs};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -7,10 +7,11 @@ pub struct KnownServer {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub public_key: String,
|
||||
pub hostname: String,
|
||||
pub is_secure: bool,
|
||||
}
|
||||
|
||||
type ServerList = HashMap<String, KnownServer>;
|
||||
|
||||
fn servers_file_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
|
||||
let dir = app
|
||||
.path()
|
||||
@@ -24,7 +25,7 @@ fn servers_file_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_server_list(app: AppHandle, servers: Vec<KnownServer>) -> Result<(), String> {
|
||||
pub fn save_server_list(app: AppHandle, servers: ServerList) -> Result<(), String> {
|
||||
let path = servers_file_path(&app)?;
|
||||
|
||||
let json = serde_json::to_string_pretty(&servers)
|
||||
@@ -36,13 +37,13 @@ pub fn save_server_list(app: AppHandle, servers: Vec<KnownServer>) -> Result<(),
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_server_list(app: AppHandle) -> Result<Vec<KnownServer>, String> {
|
||||
pub fn get_server_list(app: AppHandle) -> Result<ServerList, String> {
|
||||
let path = servers_file_path(&app)?;
|
||||
|
||||
if !path.exists() {
|
||||
fs::write(&path, "[]").map_err(|e| format!("Failed to write first server list: {e}"))?;
|
||||
|
||||
return Ok(Vec::new());
|
||||
return Ok(ServerList::new());
|
||||
}
|
||||
|
||||
let json = fs::read_to_string(&path).map_err(|e| format!("Failed to read server list: {e}"))?;
|
||||
|
||||
+12
-8
@@ -1,4 +1,9 @@
|
||||
import { getServerList, getWSUrl, KnownServer } from "@/lib/serverList";
|
||||
import {
|
||||
getServerList,
|
||||
getWSUrl,
|
||||
KnownServer,
|
||||
ServerList,
|
||||
} from "@/lib/serverList";
|
||||
import EnclaveServer from "./server";
|
||||
import * as ed from "@noble/ed25519";
|
||||
import { sha512 } from "@noble/hashes/sha2.js";
|
||||
@@ -23,36 +28,35 @@ export default class Enclave {
|
||||
private clientSecretKey: Uint8Array;
|
||||
private clientPublicKey: Uint8Array;
|
||||
public server?: EnclaveServer;
|
||||
public serverList: KnownServer[];
|
||||
public serverList: ServerList;
|
||||
|
||||
public constructor() {
|
||||
this.clientSecretKey = ed.utils.randomSecretKey();
|
||||
this.clientPublicKey = ed.getPublicKey(this.clientSecretKey);
|
||||
this.serverList = [];
|
||||
this.serverList = {};
|
||||
}
|
||||
|
||||
public async init() {
|
||||
this.serverList = await getServerList();
|
||||
}
|
||||
|
||||
public async addServer(hostname: string, isSecure: boolean) {
|
||||
public async connectToServer(hostname: string, isSecure: boolean) {
|
||||
if (this.server) {
|
||||
this.server.disconnect();
|
||||
}
|
||||
|
||||
this.server = new EnclaveServer(getWSUrl({ hostname, isSecure }));
|
||||
this.server = new EnclaveServer(getWSUrl(hostname, isSecure));
|
||||
|
||||
if (!this.server.serverPublicKey) {
|
||||
console.error("Failed to get public key");
|
||||
return;
|
||||
}
|
||||
|
||||
this.serverList.push({
|
||||
this.serverList[hostname] = {
|
||||
name: "",
|
||||
description: "",
|
||||
hostname,
|
||||
isSecure,
|
||||
publicKey: base58.encode(this.server.serverPublicKey),
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,14 @@ export default function ServerList({
|
||||
}: {
|
||||
appRef: React.RefObject<Enclave | null>;
|
||||
}) {
|
||||
if (!appRef.current) return null;
|
||||
|
||||
return (
|
||||
<aside className="bg-card w-16 h-screen p-2 flex flex-col gap-2.5">
|
||||
{appRef.current?.serverList.map((server) => {
|
||||
{Object.entries(appRef.current.serverList).map(([hostname, server]) => {
|
||||
return (
|
||||
<img
|
||||
src={getHTTPUrl(server, "/icon")}
|
||||
src={getHTTPUrl(hostname, server.isSecure, "/icon")}
|
||||
className={
|
||||
server.publicKey === appRef.current?.server?.hostname
|
||||
? "rounded-lg"
|
||||
@@ -25,11 +27,7 @@ export default function ServerList({
|
||||
return;
|
||||
}
|
||||
|
||||
if (appRef.current.server) {
|
||||
appRef.current.server.disconnect();
|
||||
}
|
||||
|
||||
appRef.current.server = new EnclaveServer(getWSUrl(server));
|
||||
appRef.current.connectToServer(hostname, server.isSecure);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
+15
-18
@@ -4,33 +4,30 @@ export interface KnownServer {
|
||||
name: string;
|
||||
description: string;
|
||||
publicKey: string;
|
||||
hostname: string;
|
||||
isSecure: boolean;
|
||||
}
|
||||
|
||||
export async function saveServerList(servers: KnownServer[]): Promise<void> {
|
||||
export type ServerList = Record<string, KnownServer>;
|
||||
|
||||
export async function saveServerList(servers: ServerList): Promise<void> {
|
||||
await invoke("save_server_list", { servers });
|
||||
}
|
||||
|
||||
export async function getServerList(): Promise<KnownServer[]> {
|
||||
export async function getServerList(): Promise<ServerList> {
|
||||
return await invoke("get_server_list");
|
||||
}
|
||||
|
||||
export function getHTTPUrl(
|
||||
server: { hostname: string; isSecure: boolean },
|
||||
path: string,
|
||||
) {
|
||||
const hostname = server.hostname.includes(":")
|
||||
? server.hostname
|
||||
: server.hostname + ":3415";
|
||||
|
||||
return (server.isSecure ? "https://" : "http://") + hostname + path;
|
||||
export function getHTTPUrl(hostname: string, isSecure: boolean, path: string) {
|
||||
return (
|
||||
(isSecure ? "https://" : "http://") +
|
||||
(hostname.includes(":") ? hostname : hostname + ":3415") +
|
||||
path
|
||||
);
|
||||
}
|
||||
|
||||
export function getWSUrl(server: { hostname: string; isSecure: boolean }) {
|
||||
const hostname = server.hostname.includes(":")
|
||||
? server.hostname
|
||||
: server.hostname + ":3415";
|
||||
|
||||
return (server.isSecure ? "wss://" : "ws://") + hostname;
|
||||
export function getWSUrl(hostname: string, isSecure: boolean) {
|
||||
return (
|
||||
(isSecure ? "wss://" : "ws://") +
|
||||
(hostname.includes(":") ? hostname : hostname + ":3415")
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user