Secure Gateway Tunneling
Relevant source files
The following files were used as context for generating this wiki page:
- packages/core/tunnel/tunnel-service.ts
- packages/web/src/components/systems/onprem/OnPremWizard.tsx
- packages/web/src/components/systems/onprem/TunnelSelector.tsx
- packages/core/tunnel/tunnel-url-rewrite.test.ts
- packages/web/src/lib/agent/skills/content/systems-handling.md
- packages/core/api/response-helpers.ts
Secure Gateway Tunneling enables superglue to connect to private systems that lack public inbound access. This includes on-premise servers, databases behind firewalls, AWS VPCs, and Azure VNets. The system uses a local gateway agent to bridge traffic between the superglue server and the private environment.
Sources: packages/web/src/lib/agent/skills/content/systems-handling.md:65-72, packages/core/tunnel/tunnel-service.ts:133-136
Architecture and Components
The tunneling architecture consists of the superglue server, a gateway agent, and target private systems. The gateway agent establishes a persistent WebSocket connection to the server to act as a secure proxy.
Gateway Agent
The gateway agent resides within the user's private network. It connects to the superglue server using a WebSocket at the /ws/tunnels path. It authenticates using an API key and identifies itself via an x-tunnel-id header.
Sources: packages/core/tunnel/tunnel-service.ts:153-178, packages/web/src/components/systems/onprem/TunnelSelector.tsx:28-35
Connection Channels
The TunnelService manages two distinct types of communication channels:
- Control Channel: A persistent WebSocket used for registration and heartbeats (pings/pongs). The agent registers available targets through this channel.
- Data Channel: Ephemeral WebSocket connections created on-demand for actual request traffic. Each tool execution step that requires tunneling triggers a new data channel.
Sources: packages/core/tunnel/tunnel-service.ts:192-225, packages/core/tunnel/tunnel-service.ts:251-265
Mermaid: Tunnel Architecture Overview
The following diagram shows the relationship between the superglue server, the gateway agent, and the private targets.
Tunnel Execution Flow
When a tool executes a step targeting a tunneled system, the TunnelService creates a local TCP proxy.
- Setup: The server identifies systems needing tunnels in a tool configuration.
- Creation: For each system,
createTunnelinitializes a local TCP server on a random port. - Command: The server sends an
open_tunnelmessage to the gateway agent via the control channel. - Data Channel: The agent opens a new WebSocket (the data channel) back to the server.
- Proxying: The local TCP server pipes data between the execution step and the data channel WebSocket.
- Cleanup: Once the execution finishes, the server closes the TCP server and the associated data sockets.
Sources: packages/core/tunnel/tunnel-service.ts:89-125, packages/core/tunnel/tunnel-service.ts:271-310
Mermaid: Connection Sequence
The following sequence diagram illustrates the process of establishing a data channel for a specific request.
URL Rewriting
To route traffic through the tunnel, the server rewrites the target system's URL to point to the local TCP proxy. The rewriteUrlForTunnel function replaces the hostname with 127.0.0.1 and the specific port assigned to that tunnel.
Protocol Mapping
The service maps various protocols to their tunnel-compatible equivalents. For instance, https is mapped to http because the tunnel handles TLS termination.
| Original Protocol | Tunnel Protocol |
|---|---|
| http / https | http |
| postgres / postgresql | postgres |
| mssql / sqlserver | mssql |
| redis / rediss | redis |
| sftp / ftp / smb | sftp / ftp / smb |
Sources: packages/core/tunnel/tunnel-service.ts:24-42, packages/core/tunnel/tunnel-url-rewrite.test.ts:74-110
Rewriting Logic
The rewrite preserves the path, query string, and credentials (username/password) from the original URL.
// Example Rewriting
// Original: https://internal.api/v1/users?id=123
// Target Port: 54321
// Result: http://127.0.0.1:54321/v1/users?id=123
Sources: packages/core/tunnel/tunnel-service.ts:51-64, packages/core/tunnel/tunnel-url-rewrite.test.ts:133-145
System Configuration and Management
Users configure private systems through the OnPremWizard. A private system requires a tunnelId and a targetName.
Private System Creation
You create a private system by following these steps:
- Select Gateway: Choose from a list of currently connected gateway agents.
- Select Target: Pick a specific target (e.g., a database or API) defined on that gateway.
- Name System: Provide a friendly name for identification within superglue.
- Automatic URL Generation: The wizard generates a internal URL in the format
${scheme}://${tunnelId}.tunnel.
Sources: packages/web/src/components/systems/onprem/OnPremWizard.tsx:64-90, packages/web/src/components/systems/onprem/TunnelSelector.tsx:63-95
Configuration Constraints
Private systems have specific immutability rules:
- Tunnel ID and Target Name: These cannot be changed after the system is created.
- Editable Fields: Users can update the
name,specificInstructions,credentials, and documentation files. - Agent Interaction: AI agents cannot create private systems; they must direct users to the manual setup guide.
Sources: packages/web/src/lib/agent/skills/content/systems-handling.md:73-81, packages/core/api/response-helpers.ts:125-135
Tunnel Connection Data Structure
The TunnelConnection object tracks the state of connected gateways.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the tunnel. |
orgId | string | The organization owning the tunnel. |
connectedAt | string (ISO) | Timestamp of the connection. |
targets | TunnelTarget[] | List of reachable private systems. |
Sources: packages/core/tunnel/tunnel-service.ts:233-241, packages/web/src/components/systems/onprem/TunnelSelector.tsx:6-13
Summary
Secure Gateway Tunneling provides a robust mechanism for accessing non-public resources. By utilizing a local TCP proxy and on-demand WebSocket data channels, superglue ensures that traffic remains encrypted and secure without requiring complex firewall modifications or public exposure of sensitive internal systems.