# Sinsay Mirror Proxy

Full-featured transparent mirror of `www.sinsay.com/pl/pl` on your own domain, with:

- All pages/assets/catalog proxied live from Sinsay
- Own cart (Redis) + checkout form (Postgres)  
- Telegram bot: instant push notifications for new visitors, cart adds, and orders
- Admin panel at `/_admin/` (basic auth)

---

## Quick Start

### 1. Configure environment

```bash
cp .env.example .env
# Edit .env and fill in your domain, Telegram token, passwords, etc.
```

### 2. Point DNS

Add an **A record** for your domain → your server IP.

### 3. Launch

```bash
docker compose up -d
```

Caddy automatically obtains a TLS certificate from Let's Encrypt.

### 4. Verify cart endpoints (IMPORTANT)

Open Chrome DevTools on `https://www.sinsay.com/pl/pl`, add a product to cart,
and inspect the Network tab. Find the exact cart API paths and update
`app/src/config.ts → intercept.cartPathPrefixes`.

See [INVENTORY.md](INVENTORY.md) for detailed instructions.

After updating config, rebuild:

```bash
docker compose build app && docker compose up -d app
```

---

## Architecture

```
Browser → Caddy (TLS) → Fastify proxy
                         ├── /_cdn/media/* → media.sinsay.com (stream)
                         ├── /api/basket/*  → Redis cart (intercepted)
                         ├── /checkout      → Own checkout form (HTML)
                         ├── /api/our/order → Postgres + Telegram notification
                         ├── /_admin/       → Admin panel (basic auth)
                         └── /*             → www.sinsay.com (proxy + URL rewrite + script inject)
```

---

## Environment Variables

| Variable | Description |
|----------|-------------|
| `DOMAIN` | Your public domain, e.g. `shop.example.com` |
| `ORIGIN` | Full origin URL, e.g. `https://shop.example.com` |
| `SESSION_SECRET` | Random secret for cookie signing |
| `POSTGRES_USER/PASS/DB` | Postgres credentials |
| `ADMIN_USER/PASS` | Basic auth for `/_admin/` |
| `TG_BOT_TOKEN` | Get from [@BotFather](https://t.me/BotFather) |
| `TG_ADMIN_CHAT_ID` | Your Telegram chat ID (comma-separated for multiple) |
| `IPINFO_TOKEN` | ipinfo.io token for GeoIP (free tier: 50k req/month) |
| `PROXY_POOL_ENABLED` | `true` to enable residential proxy rotation |
| `PROXY_POOL_URLS` | Comma-separated proxy URLs when pool enabled |

---

## Telegram Bot Commands

| Command | Description |
|---------|-------------|
| `/stats` | Today's visitors, cart adds, orders, revenue |
| `/orders` | Last 10 orders with action buttons |
| `/online` | Unique visitors active in the last 5 minutes |
| `/visitor <id>` | Full event history for a visitor |

---

## Admin Panel

Visit `https://your.domain.com/_admin/` and log in with `ADMIN_USER` / `ADMIN_PASS`.

Sections:
- **Orders** — list and detail view, filter by status
- **Visitors** — all tracked visitors with geo/event counts

---

## Anti-bot

If Sinsay starts blocking your server IP:

1. Get residential proxy URLs (Bright Data, Smartproxy, etc.)
2. Set `PROXY_POOL_ENABLED=true` and `PROXY_POOL_URLS=...` in `.env`
3. Restart: `docker compose up -d app`

---

## Development

```bash
cd app
cp ../.env.example .env  # fill in values
npm install
npm run db:push          # push schema to local Postgres
npm run dev              # tsx watch mode
```
