feat :
- Added tunnel - Removed cloud feature
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
import initializeCloudSync from "@/shared/services/initializeCloudSync";
|
||||
import initializeApp from "@/shared/services/initializeApp";
|
||||
|
||||
// Initialize cloud sync when this module is imported
|
||||
let initialized = false;
|
||||
|
||||
export async function ensureCloudSyncInitialized() {
|
||||
export async function ensureAppInitialized() {
|
||||
if (!initialized) {
|
||||
try {
|
||||
await initializeCloudSync();
|
||||
await initializeApp();
|
||||
initialized = true;
|
||||
} catch (error) {
|
||||
console.error("[ServerInit] Error initializing cloud sync:", error);
|
||||
console.error("[ServerInit] Error initializing app:", error);
|
||||
}
|
||||
}
|
||||
return initialized;
|
||||
}
|
||||
|
||||
// Auto-initialize when module loads
|
||||
ensureCloudSyncInitialized().catch(console.log);
|
||||
|
||||
export default ensureCloudSyncInitialized;
|
||||
ensureAppInitialized().catch(console.log);
|
||||
|
||||
export default ensureAppInitialized;
|
||||
|
||||
@@ -49,6 +49,8 @@ const defaultData = {
|
||||
apiKeys: [],
|
||||
settings: {
|
||||
cloudEnabled: false,
|
||||
tunnelEnabled: false,
|
||||
tunnelUrl: "",
|
||||
stickyRoundRobinLimit: 3,
|
||||
requireLogin: true,
|
||||
observabilityEnabled: true,
|
||||
@@ -70,6 +72,8 @@ function cloneDefaultData() {
|
||||
apiKeys: [],
|
||||
settings: {
|
||||
cloudEnabled: false,
|
||||
tunnelEnabled: false,
|
||||
tunnelUrl: "",
|
||||
stickyRoundRobinLimit: 3,
|
||||
requireLogin: true,
|
||||
observabilityEnabled: true,
|
||||
|
||||
193
src/lib/tunnel/cloudflared.js
Normal file
193
src/lib/tunnel/cloudflared.js
Normal file
@@ -0,0 +1,193 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import https from "https";
|
||||
import os from "os";
|
||||
import { execSync, spawn } from "child_process";
|
||||
import { savePid, loadPid, clearPid } from "./state.js";
|
||||
|
||||
const BIN_DIR = path.join(os.homedir(), ".9router", "bin");
|
||||
const BINARY_NAME = "cloudflared";
|
||||
const IS_WINDOWS = os.platform() === "win32";
|
||||
const BIN_NAME = IS_WINDOWS ? `${BINARY_NAME}.exe` : BINARY_NAME;
|
||||
const BIN_PATH = path.join(BIN_DIR, BIN_NAME);
|
||||
|
||||
const GITHUB_BASE_URL = "https://github.com/cloudflare/cloudflared/releases/latest/download";
|
||||
|
||||
const PLATFORM_MAPPINGS = {
|
||||
darwin: {
|
||||
x64: "cloudflared-darwin-amd64.tgz",
|
||||
arm64: "cloudflared-darwin-amd64.tgz"
|
||||
},
|
||||
win32: {
|
||||
x64: "cloudflared-windows-amd64.exe"
|
||||
},
|
||||
linux: {
|
||||
x64: "cloudflared-linux-amd64",
|
||||
arm64: "cloudflared-linux-arm64"
|
||||
}
|
||||
};
|
||||
|
||||
function getDownloadUrl() {
|
||||
const platform = os.platform();
|
||||
const arch = os.arch();
|
||||
|
||||
const platformMapping = PLATFORM_MAPPINGS[platform];
|
||||
if (!platformMapping) {
|
||||
throw new Error(`Unsupported platform: ${platform}`);
|
||||
}
|
||||
|
||||
const binaryName = platformMapping[arch];
|
||||
if (!binaryName) {
|
||||
throw new Error(`Unsupported architecture: ${arch} for platform ${platform}`);
|
||||
}
|
||||
|
||||
return `${GITHUB_BASE_URL}/${binaryName}`;
|
||||
}
|
||||
|
||||
function downloadFile(url, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(dest);
|
||||
|
||||
https.get(url, (response) => {
|
||||
if ([301, 302].includes(response.statusCode)) {
|
||||
file.close();
|
||||
fs.unlinkSync(dest);
|
||||
downloadFile(response.headers.location, dest).then(resolve).catch(reject);
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.statusCode !== 200) {
|
||||
file.close();
|
||||
fs.unlinkSync(dest);
|
||||
reject(new Error(`Download failed with status ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
response.pipe(file);
|
||||
|
||||
file.on("finish", () => {
|
||||
file.close(() => resolve(dest));
|
||||
});
|
||||
|
||||
file.on("error", (err) => {
|
||||
file.close();
|
||||
fs.unlinkSync(dest);
|
||||
reject(err);
|
||||
});
|
||||
}).on("error", (err) => {
|
||||
file.close();
|
||||
if (fs.existsSync(dest)) fs.unlinkSync(dest);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function ensureCloudflared() {
|
||||
if (!fs.existsSync(BIN_DIR)) {
|
||||
fs.mkdirSync(BIN_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
if (fs.existsSync(BIN_PATH)) {
|
||||
if (!IS_WINDOWS) {
|
||||
fs.chmodSync(BIN_PATH, "755");
|
||||
}
|
||||
return BIN_PATH;
|
||||
}
|
||||
|
||||
const url = getDownloadUrl();
|
||||
const isArchive = url.endsWith(".tgz");
|
||||
const downloadDest = isArchive ? path.join(BIN_DIR, "cloudflared.tgz") : BIN_PATH;
|
||||
|
||||
await downloadFile(url, downloadDest);
|
||||
|
||||
if (isArchive) {
|
||||
execSync(`tar -xzf "${downloadDest}" -C "${BIN_DIR}"`, { stdio: "pipe" });
|
||||
fs.unlinkSync(downloadDest);
|
||||
}
|
||||
|
||||
if (!IS_WINDOWS) {
|
||||
fs.chmodSync(BIN_PATH, "755");
|
||||
}
|
||||
|
||||
return BIN_PATH;
|
||||
}
|
||||
|
||||
let cloudflaredProcess = null;
|
||||
|
||||
export async function spawnCloudflared(tunnelToken) {
|
||||
const binaryPath = await ensureCloudflared();
|
||||
|
||||
const child = spawn(binaryPath, ["tunnel", "run", "--dns-resolver-addrs", "1.1.1.1:53", "--token", tunnelToken], {
|
||||
detached: false,
|
||||
stdio: ["ignore", "pipe", "pipe"]
|
||||
});
|
||||
|
||||
cloudflaredProcess = child;
|
||||
savePid(child.pid);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let connectionCount = 0;
|
||||
const timeout = setTimeout(() => {
|
||||
resolve(child);
|
||||
}, 90000);
|
||||
|
||||
const handleLog = (data) => {
|
||||
const msg = data.toString();
|
||||
if (msg.includes("Registered tunnel connection")) {
|
||||
connectionCount++;
|
||||
if (connectionCount >= 4) {
|
||||
clearTimeout(timeout);
|
||||
resolve(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
child.stdout.on("data", handleLog);
|
||||
child.stderr.on("data", handleLog);
|
||||
|
||||
child.on("error", (err) => {
|
||||
clearTimeout(timeout);
|
||||
reject(err);
|
||||
});
|
||||
|
||||
child.on("exit", (code) => {
|
||||
clearTimeout(timeout);
|
||||
if (connectionCount === 0) {
|
||||
reject(new Error(`cloudflared exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function killCloudflared() {
|
||||
if (cloudflaredProcess) {
|
||||
try {
|
||||
cloudflaredProcess.kill();
|
||||
} catch (e) { /* ignore */ }
|
||||
cloudflaredProcess = null;
|
||||
}
|
||||
|
||||
const pid = loadPid();
|
||||
if (pid) {
|
||||
try {
|
||||
process.kill(pid);
|
||||
} catch (e) { /* ignore */ }
|
||||
clearPid();
|
||||
}
|
||||
|
||||
// Kill any remaining cloudflared processes
|
||||
try {
|
||||
execSync("pkill -f cloudflared 2>/dev/null || true", { stdio: "ignore" });
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
export function isCloudflaredRunning() {
|
||||
const pid = loadPid();
|
||||
if (!pid) return false;
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
53
src/lib/tunnel/state.js
Normal file
53
src/lib/tunnel/state.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import os from "os";
|
||||
|
||||
const TUNNEL_DIR = path.join(os.homedir(), ".9router", "tunnel");
|
||||
const STATE_FILE = path.join(TUNNEL_DIR, "state.json");
|
||||
const PID_FILE = path.join(TUNNEL_DIR, "cloudflared.pid");
|
||||
|
||||
function ensureDir() {
|
||||
if (!fs.existsSync(TUNNEL_DIR)) {
|
||||
fs.mkdirSync(TUNNEL_DIR, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
export function loadState() {
|
||||
try {
|
||||
if (fs.existsSync(STATE_FILE)) {
|
||||
return JSON.parse(fs.readFileSync(STATE_FILE, "utf8"));
|
||||
}
|
||||
} catch (e) { /* ignore corrupt state */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
export function saveState(state) {
|
||||
ensureDir();
|
||||
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2));
|
||||
}
|
||||
|
||||
export function clearState() {
|
||||
try {
|
||||
if (fs.existsSync(STATE_FILE)) fs.unlinkSync(STATE_FILE);
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
export function savePid(pid) {
|
||||
ensureDir();
|
||||
fs.writeFileSync(PID_FILE, pid.toString());
|
||||
}
|
||||
|
||||
export function loadPid() {
|
||||
try {
|
||||
if (fs.existsSync(PID_FILE)) {
|
||||
return parseInt(fs.readFileSync(PID_FILE, "utf8"));
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
export function clearPid() {
|
||||
try {
|
||||
if (fs.existsSync(PID_FILE)) fs.unlinkSync(PID_FILE);
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
120
src/lib/tunnel/tunnelManager.js
Normal file
120
src/lib/tunnel/tunnelManager.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import crypto from "crypto";
|
||||
import { loadState, saveState, clearState } from "./state.js";
|
||||
import { spawnCloudflared, killCloudflared, isCloudflaredRunning } from "./cloudflared.js";
|
||||
import { getSettings, updateSettings } from "@/lib/localDb";
|
||||
|
||||
const TUNNEL_WORKER_URL = process.env.TUNNEL_WORKER_URL || "https://tunnel.9router.com";
|
||||
const MACHINE_ID_SALT = "9router-tunnel-salt";
|
||||
const API_KEY_SECRET = "9router-tunnel-api-key-secret";
|
||||
const SHORT_ID_LENGTH = 6;
|
||||
const SHORT_ID_CHARS = "abcdefghijklmnpqrstuvwxyz23456789";
|
||||
|
||||
function generateShortId() {
|
||||
let result = "";
|
||||
for (let i = 0; i < SHORT_ID_LENGTH; i++) {
|
||||
result += SHORT_ID_CHARS.charAt(Math.floor(Math.random() * SHORT_ID_CHARS.length));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function getMachineId() {
|
||||
try {
|
||||
const { machineIdSync } = require("node-machine-id");
|
||||
const raw = machineIdSync();
|
||||
return crypto.createHash("sha256").update(raw + MACHINE_ID_SALT).digest("hex").substring(0, 16);
|
||||
} catch (e) {
|
||||
return crypto.randomUUID().replace(/-/g, "").substring(0, 16);
|
||||
}
|
||||
}
|
||||
|
||||
function generateApiKey(machineId) {
|
||||
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
let keyId = "";
|
||||
for (let i = 0; i < 6; i++) {
|
||||
keyId += chars.charAt(Math.floor(Math.random() * chars.length));
|
||||
}
|
||||
const crc = crypto.createHmac("sha256", API_KEY_SECRET).update(machineId + keyId).digest("hex").slice(0, 8);
|
||||
return `sk-${machineId}-${keyId}-${crc}`;
|
||||
}
|
||||
|
||||
async function workerFetch(reqPath, options = {}) {
|
||||
const url = `${TUNNEL_WORKER_URL}${reqPath}`;
|
||||
const res = await fetch(url, {
|
||||
...options,
|
||||
headers: { "Content-Type": "application/json", ...options.headers }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function enableTunnel() {
|
||||
const existing = loadState();
|
||||
if (existing && existing.tunnelUrl && isCloudflaredRunning()) {
|
||||
return { success: true, tunnelUrl: existing.tunnelUrl, shortId: existing.shortId, alreadyRunning: true };
|
||||
}
|
||||
|
||||
killCloudflared();
|
||||
|
||||
const machineId = getMachineId();
|
||||
const shortId = existing?.shortId || generateShortId();
|
||||
const apiKey = existing?.apiKey || generateApiKey(machineId);
|
||||
|
||||
await workerFetch("/api/session/create", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ apiKey, shortId })
|
||||
});
|
||||
|
||||
const tunnelResult = await workerFetch("/api/tunnel/create", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ apiKey })
|
||||
});
|
||||
|
||||
if (tunnelResult.error) {
|
||||
throw new Error(tunnelResult.error);
|
||||
}
|
||||
|
||||
const { token, hostname } = tunnelResult;
|
||||
|
||||
await spawnCloudflared(token);
|
||||
|
||||
saveState({ shortId, apiKey, tunnelUrl: hostname, machineId });
|
||||
|
||||
await updateSettings({ tunnelEnabled: true, tunnelUrl: hostname });
|
||||
|
||||
return { success: true, tunnelUrl: hostname, shortId };
|
||||
}
|
||||
|
||||
export async function disableTunnel() {
|
||||
const state = loadState();
|
||||
|
||||
killCloudflared();
|
||||
|
||||
if (state?.apiKey) {
|
||||
try {
|
||||
await workerFetch("/api/tunnel/delete", {
|
||||
method: "DELETE",
|
||||
body: JSON.stringify({ apiKey: state.apiKey })
|
||||
});
|
||||
} catch (e) { /* ignore worker errors on disable */ }
|
||||
}
|
||||
|
||||
if (state) {
|
||||
saveState({ shortId: state.shortId, apiKey: state.apiKey, machineId: state.machineId, tunnelUrl: null });
|
||||
}
|
||||
|
||||
await updateSettings({ tunnelEnabled: false, tunnelUrl: "" });
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function getTunnelStatus() {
|
||||
const state = loadState();
|
||||
const running = isCloudflaredRunning();
|
||||
const settings = await getSettings();
|
||||
|
||||
return {
|
||||
enabled: settings.tunnelEnabled === true && running,
|
||||
tunnelUrl: state?.tunnelUrl || "",
|
||||
shortId: state?.shortId || "",
|
||||
running
|
||||
};
|
||||
}
|
||||
@@ -78,6 +78,12 @@ if (!global._pendingRequests) {
|
||||
}
|
||||
const pendingRequests = global._pendingRequests;
|
||||
|
||||
// Track last error provider for UI edge coloring (auto-clears after 10s)
|
||||
if (!global._lastErrorProvider) {
|
||||
global._lastErrorProvider = { provider: "", ts: 0 };
|
||||
}
|
||||
const lastErrorProvider = global._lastErrorProvider;
|
||||
|
||||
// Use global to share singleton across Next.js route modules
|
||||
if (!global._statsEmitter) {
|
||||
global._statsEmitter = new EventEmitter();
|
||||
@@ -91,8 +97,9 @@ export const statsEmitter = global._statsEmitter;
|
||||
* @param {string} provider
|
||||
* @param {string} connectionId
|
||||
* @param {boolean} started - true if started, false if finished
|
||||
* @param {boolean} [error] - true if ended with error
|
||||
*/
|
||||
export function trackPendingRequest(model, provider, connectionId, started) {
|
||||
export function trackPendingRequest(model, provider, connectionId, started, error = false) {
|
||||
const modelKey = provider ? `${model} (${provider})` : model;
|
||||
|
||||
// Track by model
|
||||
@@ -107,8 +114,71 @@ export function trackPendingRequest(model, provider, connectionId, started) {
|
||||
pendingRequests.byAccount[accountKey][modelKey] = Math.max(0, pendingRequests.byAccount[accountKey][modelKey] + (started ? 1 : -1));
|
||||
}
|
||||
|
||||
console.log(`[PENDING] ${started ? "START" : "END"} | provider=${provider} | model=${model} | emitter listeners=${statsEmitter.listenerCount("update")}`);
|
||||
statsEmitter.emit("update");
|
||||
// Track error provider (auto-clears after 10s)
|
||||
if (!started && error && provider) {
|
||||
lastErrorProvider.provider = provider.toLowerCase();
|
||||
lastErrorProvider.ts = Date.now();
|
||||
}
|
||||
|
||||
console.log(`[PENDING] ${started ? "START" : "END"}${error ? " (ERROR)" : ""} | provider=${provider} | model=${model} | emitter listeners=${statsEmitter.listenerCount("pending")}`);
|
||||
statsEmitter.emit("pending");
|
||||
}
|
||||
|
||||
/**
|
||||
* Lightweight: get only activeRequests + recentRequests without full stats recalc
|
||||
*/
|
||||
export async function getActiveRequests() {
|
||||
const activeRequests = [];
|
||||
|
||||
// Build active requests from pending state
|
||||
let connectionMap = {};
|
||||
try {
|
||||
const { getProviderConnections } = await import("@/lib/localDb.js");
|
||||
const allConnections = await getProviderConnections();
|
||||
for (const conn of allConnections) {
|
||||
connectionMap[conn.id] = conn.name || conn.email || conn.id;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
for (const [connectionId, models] of Object.entries(pendingRequests.byAccount)) {
|
||||
for (const [modelKey, count] of Object.entries(models)) {
|
||||
if (count > 0) {
|
||||
const accountName = connectionMap[connectionId] || `Account ${connectionId.slice(0, 8)}...`;
|
||||
const match = modelKey.match(/^(.*) \((.*)\)$/);
|
||||
const modelName = match ? match[1] : modelKey;
|
||||
const providerName = match ? match[2] : "unknown";
|
||||
activeRequests.push({ model: modelName, provider: providerName, account: accountName, count });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get recent requests from history (re-read to get latest)
|
||||
const db = await getUsageDb();
|
||||
await db.read();
|
||||
const history = db.data.history || [];
|
||||
const seen = new Set();
|
||||
const recentRequests = [...history]
|
||||
.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))
|
||||
.map((e) => {
|
||||
const t = e.tokens || {};
|
||||
const promptTokens = t.prompt_tokens || t.input_tokens || 0;
|
||||
const completionTokens = t.completion_tokens || t.output_tokens || 0;
|
||||
return { timestamp: e.timestamp, model: e.model, provider: e.provider || "", promptTokens, completionTokens, status: e.status || "ok" };
|
||||
})
|
||||
.filter((e) => {
|
||||
if (e.promptTokens === 0 && e.completionTokens === 0) return false;
|
||||
const minute = e.timestamp ? e.timestamp.slice(0, 16) : "";
|
||||
const key = `${e.model}|${e.provider}|${e.promptTokens}|${e.completionTokens}|${minute}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
})
|
||||
.slice(0, 20);
|
||||
|
||||
// Error provider (auto-clear after 10s)
|
||||
const errorProvider = (Date.now() - lastErrorProvider.ts < 10000) ? lastErrorProvider.provider : "";
|
||||
|
||||
return { activeRequests, recentRequests, errorProvider };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -443,6 +513,7 @@ export async function getUsageStats() {
|
||||
pending: pendingRequests,
|
||||
activeRequests: [],
|
||||
recentRequests,
|
||||
errorProvider: (Date.now() - lastErrorProvider.ts < 10000) ? lastErrorProvider.provider : "",
|
||||
};
|
||||
|
||||
// Build active requests list from pending counts
|
||||
|
||||
Reference in New Issue
Block a user