# Echomark Proxy Setup - SOLUTION

**Date**: 2026-01-14
**Status**: ✅ READY TO USE

---

## Problem Solved

Chrome was blocking direct requests from the extension to the Echomark server due to CORS and SSL certificate issues. The proxy solution:

- Runs locally on your machine
- Accepts requests from the Chrome extension (no CORS restrictions)
- Forwards them to Echomark API with proper authentication
- Handles the self-signed SSL certificate
- Returns responses back to the extension

---

## Setup Instructions

### Step 1: Install Dependencies

```bash
cd proxy-server
npm install
```

This will install:
- `express` - Web server
- `cors` - CORS handling
- `multer` - File upload handling
- `form-data` - Multipart form data

### Step 2: Start the Proxy Server

```bash
npm start
```

You should see:
```
🚀 Proxy server running on http://localhost:3001
📝 Health check: http://localhost:3001/health
🔑 Using Anthropic API key: sk-ant-api03-jfXF2C...
🔐 Echomark proxy endpoints:
   - GET  http://localhost:3001/echomark/ping
   - POST http://localhost:3001/echomark/document/mark/pdf
   - POST http://localhost:3001/echomark/document/mark/image
   - POST http://localhost:3001/echomark/detect
```

### Step 3: Test the Proxy

Test the Echomark connection through the proxy:

```bash
curl http://localhost:3001/echomark/ping
```

Should return:
```json
{"message":"pong"}
```

### Step 4: Reload the Extension

1. Go to `chrome://extensions/`
2. Find "Filevine Document Viewer"
3. Click the reload button 🔄

### Step 5: Test Watermarking

1. Navigate to any Filevine project
2. Click "Protect" on a PDF document
3. Fill in recipient information
4. Click "Protect Document"

**It should work now!** ✅

---

## How It Works

### Request Flow

```
Chrome Extension
    ↓
    fetch('http://localhost:3001/echomark/document/mark/pdf')
    ↓
Proxy Server (port 3001)
    ↓
    forwards with Bearer token & handles SSL
    ↓
Echomark API (https://ec2-52-8-108-29...)
    ↓
    returns watermarked document
    ↓
Proxy Server
    ↓
    forwards response back
    ↓
Chrome Extension
```

### What the Proxy Does

1. **Accepts multipart/form-data** from extension
2. **Adds authentication** (Bearer token)
3. **Handles SSL** (accepts self-signed certificate)
4. **Forwards request** to Echomark server
5. **Returns response** back to extension

---

## Configuration

The extension is already configured to use the proxy by default:

**File**: [src/lib/echomark-client.js](filevine-plugin/src/lib/echomark-client.js#L316)

```javascript
// Singleton instance - uses proxy by default
export const echomarkClient = new EchomarkClient(
  'http://localhost:3001/echomark',
  null,
  true // Use proxy
);
```

To switch back to direct connection (not recommended):

```javascript
export const echomarkClient = new EchomarkClient(
  'https://ec2-52-8-108-29.us-west-1.compute.amazonaws.com:5345/echomark/api/v2',
  '79f8eaa8-735d-480c-a594-891a9afe9b89',
  false // Direct connection
);
```

---

## Available Endpoints

### GET /echomark/ping
Test connection to Echomark API

**Request:**
```bash
curl http://localhost:3001/echomark/ping
```

**Response:**
```json
{"message":"pong"}
```

### POST /echomark/document/mark/pdf
Watermark a PDF document

**Used by**: Extension automatically when you click "Protect" on a PDF

**Request**: multipart/form-data with:
- `original` - PDF file
- `mark_options` - JSON string with watermarking options

**Response**: multipart/form-data with:
- `marked_copy` - Watermarked PDF
- `marking_schema` - JSON schema for leak detection

### POST /echomark/document/mark/image
Watermark an image file

**Used by**: Extension automatically when you click "Protect" on an image

**Request**: multipart/form-data with:
- `original` - Image file
- `mark_options` - JSON string with watermarking options

**Response**: multipart/form-data with:
- `marked_copy` - Watermarked image
- `marking_schema` - JSON schema for leak detection

### POST /echomark/detect
Detect leaked document and identify recipient

**Used by**: Extension's leak investigation feature (future)

**Request**: multipart/form-data with:
- `artifact` - Leaked document/image
- `original_1`, `original_2`, ... - Possible original documents
- `detect_options` - JSON string with detection options

**Response**: JSON with:
- `scores` - Array of confidence scores for each original
- `confidence` - Overall confidence level

---

## Troubleshooting

### Proxy Won't Start

**Error**: `Port 3001 already in use`

**Solution**: Kill the process using port 3001:
```bash
lsof -ti:3001 | xargs kill -9
npm start
```

### Connection Refused

**Error**: `Failed to fetch` or `ECONNREFUSED`

**Check**:
1. Is the proxy running? Look for the startup message
2. Is it on the right port? Should be `http://localhost:3001`
3. Check the terminal for error messages

**Solution**: Restart the proxy:
```bash
cd proxy-server
npm start
```

### Echomark API Errors

**Error**: `[Echomark Proxy] Response status: 400` or `500`

**Check proxy logs** in the terminal where `npm start` is running.

Common issues:
- Missing bearer token (proxy adds this automatically)
- Invalid mark_options format
- File too large (100MB limit)
- Echomark server is down

**Solution**: Check the proxy logs for detailed error messages.

### Extension Still Failing

1. **Check the proxy is running**:
   ```bash
   curl http://localhost:3001/health
   ```
   Should return `{"status":"ok",...}`

2. **Test Echomark connection**:
   ```bash
   curl http://localhost:3001/echomark/ping
   ```
   Should return `{"message":"pong"}`

3. **Check extension console**:
   - `chrome://extensions/` → Service worker → Console
   - Look for `[Echomark]` prefixed messages
   - Should show requests to `http://localhost:3001/echomark`

4. **Reload the extension**:
   - `chrome://extensions/` → Reload button
   - Try watermarking again

---

## Running in Production

For development and testing, you need to keep the proxy running:

```bash
cd proxy-server
npm start
```

**Keep this terminal open** while using the extension.

For production deployment, consider:
1. Installing a proper SSL certificate on the Echomark server (Let's Encrypt)
2. Configuring CORS properly on the Echomark server
3. Then switching back to direct connection

See: [ECHOMARK_SSL_FIX.md](filevine-plugin/ECHOMARK_SSL_FIX.md) for SSL certificate installation.

---

## Security Notes

**This is secure**:
- Proxy only runs on localhost (127.0.0.1)
- Not accessible from the internet
- Bearer token stored in proxy server (not in extension)
- Same approach as the Anthropic API proxy

**In production**:
- Proxy must be running on user's machine
- Each user needs to start the proxy
- Alternative: fix CORS and SSL on Echomark server

---

## Summary

✅ **Proxy server updated** with Echomark endpoints
✅ **Extension configured** to use proxy by default
✅ **Dependencies added** (multer, form-data)
✅ **SSL certificate handling** (rejectUnauthorized: false)
✅ **CORS bypass** (requests from localhost, no CORS restrictions)
✅ **Bearer token authentication** (handled by proxy)

**Next steps**:
1. Run `cd proxy-server && npm install`
2. Run `npm start`
3. Reload extension
4. Test watermarking

**You're ready to use Echomark!** 🎉

---

**Created**: 2026-01-14
**Solution**: Proxy-based architecture to bypass CORS and SSL issues
**Status**: ✅ READY FOR TESTING
