41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { getNodeBySlug, getUserLifecycleState } from "@/lib/store";
|
|
|
|
export function parseHy2Userpass(auth: string) {
|
|
const separator = auth.indexOf(":");
|
|
if (separator <= 0 || separator === auth.length - 1) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
username: auth.slice(0, separator),
|
|
password: auth.slice(separator + 1),
|
|
};
|
|
}
|
|
|
|
export async function verifyNodeToken(slug: string, token: string | null) {
|
|
const node = await getNodeBySlug(slug);
|
|
if (!node) {
|
|
return { ok: false, reason: "node_not_found" as const, node: null };
|
|
}
|
|
|
|
if (!node.enabled) {
|
|
return { ok: false, reason: "node_disabled" as const, node };
|
|
}
|
|
|
|
if (!token || token !== node.auth_token) {
|
|
return { ok: false, reason: "node_token_invalid" as const, node };
|
|
}
|
|
|
|
return { ok: true, reason: "ok" as const, node };
|
|
}
|
|
|
|
export function evaluateUserForAuth(user: {
|
|
enabled: number;
|
|
expires_at: string | null;
|
|
traffic_limit_bytes: number | null;
|
|
used_tx_bytes: number;
|
|
used_rx_bytes: number;
|
|
}) {
|
|
return getUserLifecycleState(user);
|
|
}
|