Prototype: Web Traffic Interception (TamperMonkey + Rust)

# Prototype: Web Traffic Interception System

**Date:** 2026-06-08  
**Context:** First prototype for interfacing poker bot with a website  
**Stack:** Firefox + TamperMonkey → Rust (Axum) backend → SQLite  

---

## Architecture

```
[Firefox + TamperMonkey] --HTTP/WS--> [Rust Server (Axum)] --> [SQLite]
```

### TamperMonkey (Client)
- Intercepts XHR/Fetch requests and overrides prototypes
- Extracts page content via DOM observation
- Sends data to backend via `GM_xmlhttpRequest` (bypasses CORS)
- Batching + debouncing for performance
- Offline retry queue for reliability
- Configurable URL filters (include/exclude patterns)

### Rust Backend (Axum)
- REST endpoints: `/api/traffic`, `/api/traffic/batch`, `/api/content`
- WebSocket endpoint: `/ws` for real-time streaming
- SQLite storage via `sqlx`
- CORS middleware (permissive for dev)
- Key dependencies: `axum 0.7`, `tokio`, `sqlx`, `serde`, `dashmap`, `tower-http`

---

## Project Structure

```
traffic-collector/
├── backend/
│   ├── Cargo.toml
│   ├── src/
│   │   ├── main.rs          # Server bootstrap, CORS, tracing
│   │   ├── handlers.rs      # REST + WS route handlers
│   │   ├── models.rs        # TrafficEntry, ContentSnapshot, ResourceType
│   │   └── db.rs            # SQLite init, insert, query
│   └── migrations/
│       └── 001_init.sql
├── userscript/
│   └── traffic-collector.user.js
└── docker-compose.yml
```

---

## Key Data Models

**TrafficEntry:** `id`, `session_id`, `url`, `method`, `status`, `request_headers`, `response_headers`, `request_body`, `response_body`, `content_type`, `size_bytes`, `duration_ms`, `timestamp`, `page_url`, `resource_type`

**ContentSnapshot:** `id`, `session_id`, `page_url`, `title`, `content`, `meta_description`, `meta_keywords`, `timestamp`

---

## TamperMonkey Userscript Grants

```javascript
// @grant  GM_xmlhttpRequest     // Cross-origin requests (bypasses CORS)
// @grant  GM_getValue            // Persistent storage
// @grant  GM_setValue
// @grant  GM_addValueChangeListener
// @grant  GM_websocket           // WebSocket from userscript
// @connect localhost              // Allow connections to local server
// @run-at document-start          // Hook before page scripts load
```

---

## Firefox Advantages
- More permissive `GM_xmlhttpRequest` than Chrome
- `about:debugging#/runtime/this-firefox` for TamperMonkey inspection
- Better privacy model
- `GM.registerMenuCommand` for UI controls (toggle capture, flush buffer, show stats)

---

## JetBrains Setup
- **RustRover** for Rust backend development
- **WebStorm** (optional) for userscript editing
- Run config with env vars: `DATABASE_URL`, `RUST_LOG`, `BIND_ADDR`
- File template for `.user.js` in Settings → Editor → File and Code Templates

---

## Running

```bash
# Start backend
cd backend
RUST_LOG=debug DATABASE_URL=sqlite:./data/traffic.db cargo run

# Verify
curl http://localhost:3000/health
# → {"success":true,"data":"healthy","error":null}

# Install userscript: TamperMonkey Dashboard → Utilities → Import .user.js
```

---

## Verification Checklist

| Component | Check |
|---|---|
| Rust backend compiles | `cargo build` ✓ |
| Server starts | `curl localhost:3000/health` ✓ |
| TamperMonkey installed | Firefox addon ✓ |
| Userscript enabled | TM Dashboard ✓ |
| Traffic captured | Browser Console `[TrafficCollector]` logs ✓ |
| Data stored | `sqlite3 data/traffic.db "SELECT COUNT(*) FROM traffic"` ✓ |

---

## Next Steps for Poker Integration
- Define target site URL patterns in `@match` and `FILTER.include`
- Parse poker-specific data structures (hand histories, player actions, table state)
- Add real-time processing pipeline for decision-making
- Extend backend with poker logic modules (connect to existing super-marvin engine)

id: 6794e0bb60ae4da68720868546ddf628
parent_id: 2c8da247905946c3aa19eb4936e16323
created_time: 2026-06-08T07:42:57.892Z
updated_time: 2026-06-08T07:42:57.892Z
is_conflict: 0
latitude: 0.00000000
longitude: 0.00000000
altitude: 0.0000
author: 
source_url: 
is_todo: 0
todo_due: 0
todo_completed: 0
source: joplin-desktop
source_application: net.cozic.joplin-desktop
application_data: 
order: 1780904577892
user_created_time: 2026-06-08T07:42:57.892Z
user_updated_time: 2026-06-08T07:42:57.892Z
encryption_cipher_text: 
encryption_applied: 0
markup_language: 1
is_shared: 0
share_id: 
conflict_original_id: 
master_key_id: 
user_data: 
deleted_time: 0
type_: 1