Skip to content

Repository files navigation

AI Relay

Bypass network restrictions and access ChatGPT from isolated networks. A relay system that queues prompts from an internal server (with limited internet access) to a local bridge machine (with open internet), then delivers AI responses back.

Go License: MIT


What is AI Relay?

AI Relay is a queue-based relay system that enables ChatGPT access in environments where:

  • The internal network has restricted or no access to international internet
  • A separate machine (e.g., laptop with Starlink or VPN) has open internet access

Instead of the internal server calling OpenAI directly (which would fail), a worker runs on the machine with internet. It polls the internal server for pending prompts, sends them to the OpenAI API, and returns the responses. This effectively bypasses filtering by routing AI traffic through the bridge machine.

Use Cases

  • Corporate networks with strict outbound filtering
  • Regions where AI services are blocked or throttled
  • Air-gapped or semi-isolated networks with one bridge to the internet
  • Educational or research environments with limited external access

Architecture

┌─────────────────────────────────────────────────────────────────────────┐
│                     RESTRICTED / INTERNAL NETWORK                        │
│                                                                         │
│   Users submit prompts via web UI                                       │
│   ┌──────────────┐         ┌─────────────────────┐                     │
│   │   Browser    │ ──────► │   Internal Server    │                     │
│   │   (Users)    │ ◄────── │   (Go + SQLite)     │                     │
│   └──────────────┘         │   • No OpenAI call  │                     │
│                            │   • Queue in DB     │                     │
│                            └──────────┬──────────┘                     │
│                                       │                                 │
│                                       │ Polling (outbound only)         │
└───────────────────────────────────────┼─────────────────────────────────┘
                                        │
                                        ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                     BRIDGE MACHINE (Open Internet)                        │
│                                                                         │
│   ┌─────────────────────┐                    ┌─────────────────────┐  │
│   │   Worker (Go)        │ ──── HTTPS ──────► │   OpenAI API         │  │
│   │   • Fetches jobs     │ ◄─── Response ──── │   (ChatGPT)          │  │
│   │   • Sends to OpenAI  │                    └─────────────────────┘  │
│   │   • Returns results │                                               │
│   └─────────────────────┘                                               │
└─────────────────────────────────────────────────────────────────────────┘

Key design: The internal server never initiates connections to the outside. Only the worker (on the bridge machine) polls the server and talks to OpenAI. This works even when inbound connections are blocked.


Features

Feature Description
Queue-based Prompts are stored in SQLite; worker processes them in order
Polling model Worker fetches jobs periodically—no inbound ports needed on internal server
Session auth Secure login with bcrypt and session cookies
RTL / Persian UI Right-to-left support, Vazirmatn font
Rate limiting Per-user limits to prevent abuse
CSRF protection Forms protected against cross-site request forgery
Atomic job claiming Prevents duplicate processing by multiple workers
Docker support One-command deployment with Docker Compose

Quick Start

Prerequisites

  • Go 1.21+
  • OpenAI API key
  • (Optional) Docker & Docker Compose

1. Clone

git clone https://github.com/YOUR_USERNAME/ai-relay.git
cd ai-relay

2. Configure

cp .env.example .env
# Edit .env: set WORKER_TOKEN, DEFAULT_PASSWORD, OPENAI_API_KEY

3. Run Internal Server (on restricted network)

go run ./cmd/server
# Or with Docker:
# docker-compose up -d

Server runs at http://localhost:8080 (or :8081 if port 8080 is in use).

4. Run Worker (on bridge machine with internet)

# .env on bridge machine:
# INTERNAL_SERVER_URL=http://internal-server-ip:8080
# WORKER_TOKEN=<same as server>
# OPENAI_API_KEY=sk-...

go run ./cmd/worker

5. Use

  1. Open http://<server-ip>:8080/login
  2. Log in (default: admin / admin123)
  3. Submit prompts; worker fetches and processes them; responses appear in the dashboard

Configuration

Server (.env)

Variable Description Default
SERVER_PORT HTTP port 8080
SESSION_SECRET Session encryption key Required
WORKER_TOKEN Shared secret for worker API Required
DEFAULT_USERNAME Initial user admin
DEFAULT_PASSWORD Initial password changeme123
DATABASE_PATH SQLite path ./data/ai_relay.db

Worker (.env)

Variable Description Default
INTERNAL_SERVER_URL Internal server URL Required
WORKER_TOKEN Must match server Required
OPENAI_API_KEY OpenAI API key Required
OPENAI_MODEL Model name gpt-4o-mini
WORKER_POLL_INTERVAL_SECONDS Poll interval 5

Project Structure

ai-relay/
├── cmd/
│   ├── server/     # Internal server (web + API)
│   └── worker/     # Bridge worker (polls + OpenAI)
├── internal/
│   ├── config/     # Config loading
│   ├── db/         # SQLite layer
│   ├── handlers/   # HTTP handlers
│   ├── middleware/ # Auth, CSRF, rate limit
│   ├── models/     # Data models
│   ├── openai/     # OpenAI client (worker only)
│   └── services/   # Worker HTTP client
├── web/
│   ├── static/     # CSS
│   └── templates/  # HTML
├── docker-compose.yml
├── Dockerfile
└── .env.example

Docker

# Build and run
docker-compose up -d --build

# Default port: 8081 (8080 if available)
# Data persisted in ./data/

Security

  • Never commit .env — it contains secrets. Use .env.example as a template.
  • If .env was accidentally committed: git rm --cached .env then commit the removal.
  • Set SESSION_SECRET, WORKER_TOKEN, DEFAULT_PASSWORD via environment — no defaults in code.
  • Use HTTPS in production.
  • Generate tokens: openssl rand -base64 32
  • Restrict worker API access by IP if possible.

License

MIT License. See LICENSE for details.

About

Bypass network restrictions and access ChatGPT from isolated networks.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages