# Persistent Browser Listener — Windows Server 2019

Full browser profile (cookies + IndexedDB + cache + service workers).
Cookies are encrypted at rest — never stored as plain text.

## Architecture

| Component | Path | Purpose |
|-----------|------|---------|
| Browser profile | `data/browser_profile/` | Playwright persistent context — survives reboot |
| Device fingerprint | `data/device.json` | Fixed UA, viewport, timezone — never changes |
| Encrypted vault | `data/vault/cookies.enc` | Fernet-encrypted cookie backup |

## Session recovery (automatic)

1. **Profile reload** — refresh page + re-open inbox
2. **Cookie restore** — decrypt vault and inject cookies into browser
3. **Manual login** — alert sent; run `login_persistent.bat` on server

## Setup on Windows Server 2019

```powershell
cd C:\ins\instagram-listener
python -m pip install -r requirements-browser.txt
install_playwright_browsers.bat
```

Use Python **3.11 or 3.12** (not 3.15). Check: `python --version`
If you need instagrapi API mode, use `requirements.txt` instead.

Generate encryption key:

```powershell
python tools\generate_vault_key.py
```

Copy template and edit `.env`:

```powershell
copy env.windows.persistent.txt .env
notepad .env
```

Set:
- `INSTAGRAM_USERNAME=Mydownibot`
- `INSTAGRAM_PASSWORD="..."`  (double quotes if password has #)
- `PROFILE_ENCRYPTION_KEY=` (from generate_vault_key.py)
- `HOST_API_TOKEN=` (from admin panel)

Install Mozilla Firefox if Playwright browsers fail.

## Step 1 — Login (once, or when session dies)

RDP to server, run:

```powershell
login_persistent.bat
```

- Browser opens (visible)
- Log in to Instagram
- Press ENTER when inbox works
- Encrypted cookie backup saved automatically

## Step 2 — Run 24/7

```powershell
run_persistent.bat
```

Or Task Scheduler:
- Program: `C:\ins\instagram-listener\run_persistent.bat`
- Run whether user is logged on or not
- Run as account that owns the profile folder

## After server reboot

Listener restarts → loads `data/browser_profile` → session restores automatically.
No manual login unless Instagram invalidated the session.

## .env keys

```env
LISTENER_MODE=browser
BROWSER_PROFILE_DIR=./data/browser_profile
BROWSER_VAULT_DIR=./data/vault
PROFILE_ENCRYPTION_KEY=...
PROFILE_DEVICE_SEED=Mydownibot-persistent-2026
BROWSER_HEADLESS=1
BROWSER_CHANNEL=firefox
```

## Security

- Never commit `.env` or `data/vault/cookies.enc`
- Keep `PROFILE_ENCRYPTION_KEY` secret — losing it means vault cannot be decrypted
- Browser profile dir contains runtime state while listener runs (unavoidable for IndexedDB)

## Troubleshooting

| Problem | Fix |
|---------|-----|
| PROFILE_ENCRYPTION_KEY missing | `python tools\generate_vault_key.py` |
| Manual login alert | `login_persistent.bat` via RDP |
| Inbox HTTP 403 | Re-run login_persistent.bat |
| Firefox not found | Install Firefox ESR |
| Headless fails on old Windows | `BROWSER_HEADLESS=0` + keep RDP session |

## Test

1. `/linkinstagram` in Telegram bot
2. Share a reel to @Mydownibot in Instagram DM
3. Check `logs\listener.log`
