Improved serverlist structure

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