Improved serverlist structure
This commit is contained in:
+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