Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions chromefleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ async def list_containers() -> list[str]:
raise Exception(f"Unable to list all containers: {e}")


async def get_container_last_activity(container_name: str) -> float | None:
try:
run_podman(["exec", container_name, "sh", "-c", "cp $HOME/chrome-profile/Default/History db"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is copy necessary or can we just query the History file directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Chromium is still running, it will lock write access to that file (because sqlite3 will want to read and write).


result = run_podman(["exec", container_name, "sqlite3", "db", "select MAX(last_visit_time) from urls;"])

if result.returncode == 0 and result.stdout:
chromium_time = float(result.stdout.strip())
unix_epoch = (chromium_time / 1_000_000) - 11644473600
return unix_epoch
return None
except subprocess.CalledProcessError:
return None
except Exception:
return None


async def configure_container(container_name: str, config: dict[str, str]) -> None:
ip = await get_tailscale_ip(container_name)
print(f"Configuring container {container_name} with IP {ip} and config {config}...")
Expand Down Expand Up @@ -235,12 +252,13 @@ async def get_browser(browser_id: str):
raise HTTPException(status_code=404, detail=detail)
MAX_ATTEMPTS = 3
for attempt in range(MAX_ATTEMPTS):
print(f"Getting Tailscale IP address for {container_name}: attempt {attempt + 1}/{MAX_ATTEMPTS}...")
print(f"Getting information for {container_name}: attempt {attempt + 1}/{MAX_ATTEMPTS}...")
try:
ip_address = await get_tailscale_ip(container_name)
cdp_url = f"http://{ip_address}:9222"
last_activity_timestamp = await get_container_last_activity(container_name)
print(f"Browser {browser_id}: ip_address={ip_address} cdp_url={cdp_url}.")
return {"ip_address": ip_address, "cdp_url": cdp_url}
return {"ip_address": ip_address, "cdp_url": cdp_url, "last_activity_timestamp": last_activity_timestamp}
except Exception as e:
await asyncio.sleep(1)
if attempt + 1 == MAX_ATTEMPTS:
Expand Down
9 changes: 8 additions & 1 deletion webui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chrome Fleet</title>
<script src="https://cdn.jsdelivr.net/npm/timeago.js@4/dist/timeago.min.js"></script>
</head>

<body>
Expand All @@ -30,6 +31,8 @@ <h1>Chrome Fleet</h1>

const linkify = address => `<a href="http://${address}" target="_blank">${address}</a>`;

const formatTimestamp = timestamp => timestamp ? timeago.format(new Date(timestamp * 1000)) : '-';

const update = async () => {
try {
const browsers = await listBrowsers();
Expand All @@ -49,13 +52,17 @@ <h1>Chrome Fleet</h1>
const ipCell = document.createElement('td');
ipCell.textContent = '-';
row.appendChild(ipCell);
const activityCell = document.createElement('td');
activityCell.textContent = '-';
row.appendChild(activityCell);
table.appendChild(row);
});
for (const id of browsers) {
const browser = await getBrowser(id);
const row = $(id);
if (browser && row && row.cells[1]) {
row.cells[1].innerHTML = linkify(browser.ip_address);
row.cells[2].textContent = formatTimestamp(browser.last_activity_timestamp);
}
}
} catch (error) {
Expand All @@ -66,7 +73,7 @@ <h1>Chrome Fleet</h1>

document.addEventListener('DOMContentLoaded', () => {
update();
setInterval(update, 5000);
setInterval(update, 60 * 1000);
});
</script>
</body>
Expand Down