Skip to main content

WebSocket Tunnel - 透明反向代理隧道,支持服务端嵌入和客户端 SDK

Project description

tunely - WebSocket 隧道

WebSocket 透明反向代理隧道 - Python 服务端和客户端 SDK。

特性

  • 透明代理: HTTP 请求通过 WebSocket 隧道转发
  • SSE 支持: 完整支持 Server-Sent Events 流式响应 (v0.2.0+)
  • 连接保护: 防止意外抢占已有连接
  • 自动重连: 客户端断线自动重连

安装

pip install tunely

使用

服务端(嵌入 FastAPI)

from fastapi import FastAPI
from tunely import TunnelServer

app = FastAPI()
tunnel_server = TunnelServer()
app.include_router(tunnel_server.router)

@app.on_event("startup")
async def startup():
    await tunnel_server.initialize()

# 普通请求转发
response = await tunnel_server.forward(
    domain="my-agent",
    method="POST",
    path="/api/chat",
    body={"message": "hello"}
)

# SSE 流式转发 (v0.2.0+)
async for msg in tunnel_server.forward_stream(
    domain="my-agent",
    method="POST",
    path="/api/stream",
    body={"message": "hello"}
):
    if isinstance(msg, StreamStartMessage):
        print(f"Stream started: status={msg.status}")
    elif isinstance(msg, StreamChunkMessage):
        print(f"Chunk: {msg.data}")
    elif isinstance(msg, StreamEndMessage):
        print(f"Stream ended: {msg.total_chunks} chunks")

客户端

from tunely import TunnelClient

client = TunnelClient(
    server_url="ws://server/ws/tunnel",
    token="tun_xxx",
    target_url="http://localhost:8080"
)
await client.run()

命令行

# 连接到隧道服务器
tunely connect --server ws://server/ws/tunnel --token tun_xxx --target http://localhost:8080

# 强制抢占已有连接
tunely connect --server ws://server/ws/tunnel --token tun_xxx --target http://localhost:8080 --force

SSE 支持说明

从 v0.2.0 开始,tunely 自动检测 SSE 响应(Content-Type: text/event-stream)并进行流式传输:

  1. 客户端: 自动检测 SSE 响应,发送 StreamStart → StreamChunk* → StreamEnd 消息
  2. 服务端: 使用 forward_stream() 方法获取 AsyncIterator 处理流式数据

协议版本

  • v1.0: 基础请求-响应
  • v1.1: 添加 SSE 流式响应支持

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tunely-0.4.0.tar.gz (123.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tunely-0.4.0-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file tunely-0.4.0.tar.gz.

File metadata

  • Download URL: tunely-0.4.0.tar.gz
  • Upload date:
  • Size: 123.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for tunely-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a25b3cd912dde7705fa7d04f20df0a9dbef848eef9fbf3524bbcf6b17c39788e
MD5 387312b8d4cfa9a1f4d95668c004b835
BLAKE2b-256 1daf8e795d87ca004c2294950b4586facbaf3e62d0108c89c67c89bed4dd67c1

See more details on using hashes here.

File details

Details for the file tunely-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: tunely-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for tunely-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20bc88a2a9fe4e61c7a26f5078a2324417dc2f7abdb736d96ae6b711eb01376e
MD5 94cdcf6c95e82d673d37c2661f237f8a
BLAKE2b-256 62a68420485ad8954ab5fab34ec3b842a56ee6d25c06b622d4e90463b2a16af7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page