require("dotenv").config(); const express = require("express"); const fs = require("fs"); const path = require("path"); const SETTINGS_URL = "https://ollama.com/settings"; const SIGNIN_URL = "https://ollama.com/signin"; const POLL_MINUTES = parseInt(process.env.POLL_MINUTES || "30", 10); const PORT = parseInt(process.env.PORT || "3214", 10); const OLLAMA_EMAIL = process.env.OLLAMA_EMAIL || ""; const OLLAMA_PASSWORD = process.env.OLLAMA_PASSWORD || ""; const AUTH_MODE = (process.env.AUTH_MODE || "").toLowerCase(); const HA_URL = (process.env.HA_URL || "").replace(/\/+$/, ""); const HA_TOKEN = process.env.HA_TOKEN || ""; const COOKIE_FILE = process.env.COOKIE_FILE || path.join(__dirname, ".cookies.json"); let latestState = null; let authCookies = null; function loadCookies() { try { const data = fs.readFileSync(COOKIE_FILE, "utf-8"); authCookies = JSON.parse(data); console.log("[auth] Loaded cookies from file"); return true; } catch { return false; } } function saveCookies(cookies) { authCookies = cookies; try { fs.writeFileSync(COOKIE_FILE, JSON.stringify(cookies, null, 2)); } catch (err) { console.error("[auth] Failed to save cookies:", err.message); } } function cookieString(cookies) { return cookies.map((c) => `${c.name}=${c.value}`).join("; "); } function parseSetCookies(setCookieHeaders) { if (!setCookieHeaders) return []; const arr = Array.isArray(setCookieHeaders) ? setCookieHeaders : [setCookieHeaders]; return arr.map((header) => { const parts = header.split(";")[0].split("="); const name = parts[0].trim(); const value = parts.slice(1).join("=").trim(); return { name, value }; }); } function mergeCookies(existing, incoming) { const map = new Map(); for (const c of existing) map.set(c.name, c); for (const c of incoming) map.set(c.name, c); return [...map.values()]; } async function httpRequest(url, options = {}) { const headers = { ...options.headers }; if (authCookies && authCookies.length > 0) { headers["Cookie"] = cookieString(authCookies); } headers["User-Agent"] = headers["User-Agent"] || "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"; const res = await fetch(url, { ...options, headers, redirect: "manual", }); const setCookies = res.headers.getSetCookie?.() || []; if (setCookies.length > 0 && authCookies) { const parsed = parseSetCookies(setCookies); authCookies = mergeCookies(authCookies, parsed); saveCookies(authCookies); } return res; } async function fetchWithCookies(url, options = {}) { let res = await httpRequest(url, options); const location = res.headers.get("location"); if (location && (res.status === 301 || res.status === 302 || res.status === 303 || res.status === 307 || res.status === 308)) { const nextUrl = new URL(location, url).href; return fetchWithCookies(nextUrl, options); } return res; } function findUsage(html, label) { const idx = html.indexOf(label); if (idx === -1) return null; const slice = html.slice(idx, idx + 3000); const pct = slice.match(/(\d+(?:\.\d+)?)\s*%/); const reset = slice.match(/Resets?\s+in\s+([^<\n.]+?)(?:<|\n|$)/i); if (!pct) return null; return { percent: parseFloat(pct[1]), resetsIn: reset ? reset[1].trim().replace(/\s+/g, " ") : null, }; } async function loginWithEmail() { console.log("[auth] Logging in with email/password..."); authCookies = []; const getRes = await fetch(SIGNIN_URL, { headers: { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" }, redirect: "manual", }); const setCookies = getRes.headers.getSetCookie?.() || []; authCookies = mergeCookies(authCookies, parseSetCookies(setCookies)); let csrfToken = null; if (getRes.status >= 200 && getRes.status < 400) { const html = await getRes.text(); const csrfMatch = html.match(/name="csrf[_-]token"\s+(?:value|content)="([^"]+)"/i) || html.match(/ 0) formHeaders["Cookie"] = cookieString(authCookies); let body = `email=${encodeURIComponent(OLLAMA_EMAIL)}&password=${encodeURIComponent(OLLAMA_PASSWORD)}`; if (csrfToken) body += `&csrf_token=${encodeURIComponent(csrfToken)}&_csrf=${encodeURIComponent(csrfToken)}`; const loginRes = await fetch(SIGNIN_URL, { method: "POST", headers: formHeaders, body, redirect: "manual", }); const loginCookies = loginRes.headers.getSetCookie?.() || []; authCookies = mergeCookies(authCookies, parseSetCookies(loginCookies)); const location = loginRes.headers.get("location"); if (loginRes.status === 303 || loginRes.status === 302 || loginRes.status === 301) { if (location && !location.includes("/signin")) { console.log("[auth] Login successful (redirected to", location, ")"); saveCookies(authCookies); return true; } } if (loginRes.status === 200) { const text = await loginRes.text(); if (text.includes("/settings") || text.includes("dashboard")) { console.log("[auth] Login appears successful (200 with settings link)"); saveCookies(authCookies); return true; } if (text.includes("invalid") || text.includes("wrong") || text.includes("error")) { throw new Error("Login failed: invalid credentials"); } } if (location && location.includes("/signin")) { throw new Error("Login failed: redirected back to signin. Check your credentials."); } saveCookies(authCookies); const verifyRes = await fetchWithCookies(SETTINGS_URL); if (verifyRes.url?.includes("/settings") || verifyRes.status === 200) { const html = await verifyRes.text(); if (!html.includes("/signin") || html.includes("Session usage") || html.includes("Weekly usage")) { console.log("[auth] Login verified via settings page"); return true; } } throw new Error("Login failed: could not authenticate with email/password"); } async function tryWithSavedCookies() { console.log("[auth] Trying with saved cookies..."); const res = await fetchWithCookies(SETTINGS_URL); if (res.status !== 200) { console.log("[auth] Settings page returned", res.status); return false; } const html = await res.text(); if (html.includes("Session usage") || html.includes("Weekly usage")) { console.log("[auth] Saved cookies are valid!"); return true; } if (html.includes("/signin") || html.includes("sign in") || html.length < 2000) { console.log("[auth] Cookies expired or invalid, need to re-authenticate"); return false; } console.log("[auth] Page doesn't contain usage data, might need re-auth"); return false; } async function pollUsage() { let state; try { if (!authCookies || authCookies.length === 0) { if (!loadCookies()) { const mode = AUTH_MODE || (OLLAMA_PASSWORD ? "email" : "cookies"); if (mode === "email") { await loginWithEmail(); } else { console.error("[auth] No cookies file found. Export cookies from your browser first (see README)."); console.error("[auth] Or set OLLAMA_EMAIL + OLLAMA_PASSWORD for email auth."); state = { needsLogin: true, error: "No authentication available. Export cookies or set credentials.", fetchedAt: new Date().toISOString() }; latestState = state; return state; } } } if (!(await tryWithSavedCookies())) { const mode = AUTH_MODE || (OLLAMA_PASSWORD ? "email" : "cookies"); if (mode === "email") { await loginWithEmail(); } else { console.error("[auth] Cookies expired and no email/password configured."); state = { needsLogin: true, error: "Session expired. Re-export cookies or set OLLAMA_EMAIL/OLLAMA_PASSWORD.", fetchedAt: new Date().toISOString() }; latestState = state; return state; } } const res = await fetchWithCookies(SETTINGS_URL); const html = await res.text(); const session = findUsage(html, "Session usage"); const weekly = findUsage(html, "Weekly usage"); if (session || weekly) { state = { session, weekly, fetchedAt: new Date().toISOString() }; } else { if (html.includes("/signin") || html.includes('href="/signin"') || html.length < 2000) { console.log("[auth] Session expired, re-authenticating..."); const mode = AUTH_MODE || (OLLAMA_PASSWORD ? "email" : "cookies"); if (mode === "email") { await loginWithEmail(); const retryRes = await fetchWithCookies(SETTINGS_URL); const retryHtml = await retryRes.text(); const retrySession = findUsage(retryHtml, "Session usage"); const retryWeekly = findUsage(retryHtml, "Weekly usage"); if (retrySession || retryWeekly) { state = { session: retrySession, weekly: retryWeekly, fetchedAt: new Date().toISOString() }; } else { state = { needsLogin: true, error: "Re-authentication succeeded but couldn't find usage data.", fetchedAt: new Date().toISOString() }; } } else { state = { needsLogin: true, error: "Session expired. Re-export cookies or set OLLAMA_EMAIL/OLLAMA_PASSWORD.", fetchedAt: new Date().toISOString() }; } } else { const titleMatch = html.match(/