Skip to content
forked from GNOME/glib

Conversation

@superstructor
Copy link

Add WebSocket-based POSIX sockets proxy client to enable TCP/HTTP connectivity in WASM builds via Cloudflare Workers backend.

Architecture

WASM (glib.wasm) <--WebSocket--> Cloudflare Workers <--TCP--> Internet
     │                                  │
     │ Binary protocol                  │
     │ (19 message types)               │
     └─ GIO APIs work unchanged         └─ Real TCP sockets

Changes

New Files:

  • gio/gposixsocketsproxy.h: Proxy client API declarations

  • gio/gposixsocketsproxy.c: WebSocket client with binary protocol (1335 lines)

    • Implements 19 POSIX socket operations (socket, connect, send, recv, etc.)
    • Uses Emscripten WebSocket API for communication
    • Async message queue for request/response matching
    • Thread-safe with GMutex protection
  • gio/tests/gio-proxy-test.c: Integration tests (322 lines)

    • Test socket creation, DNS resolution, TCP connections
    • HTTP GET request/response validation
    • High-level GSocketClient API testing

Modified Files:

  • gio/gsocket.c: Integrated proxy into GSocket operations (114 lines)

    • g_socket(): Use proxy_socket() on Emscripten
    • g_socket_connect(): Use proxy_connect() on Emscripten
    • send/recv: Use proxy_send()/proxy_recv() on Emscripten
    • Automatic proxy initialization with configurable URL
  • gio/meson.build: Add proxy sources for Emscripten builds

  • deno.json: Add test:proxy and test:proxy:local tasks

Protocol

Binary protocol with little-endian encoding, 19 message types matching Emscripten's POSIX sockets proxy specification:

  • SOCKET, CONNECT, SEND, RECV, SHUTDOWN
  • BIND, LISTEN, ACCEPT
  • GETSOCKNAME, GETPEERNAME
  • GETSOCKOPT, SETSOCKOPT
  • GETADDRINFO, GETNAMEINFO
  • SENDTO, RECVFROM, SENDMSG, RECVMSG, SOCKETPAIR

Configuration

Set proxy URL via environment variable:

export POSIX_PROXY_URL="wss://posix-proxy.discere.cloud/v1"

Defaults to wss://posix-proxy.discere.cloud/v1 if not set.

Testing

# Build WASM with proxy support
deno task build:wasm

# Test with production proxy
deno task test:proxy

# Test with local proxy (development)
deno task test:proxy:local

Benefits

  • ✅ Full GIO networking API support in WASM
  • ✅ GSocket, GSocketClient, GSocketConnection work unchanged
  • ✅ DNS resolution via proxy_getaddrinfo()
  • ✅ HTTP requests through proxy tunnel
  • ✅ Zero changes required to existing GIO client code
  • ✅ Thread-safe proxy operations

Impact

Enables TCP/HTTP connectivity for all GIO-based applications compiled to WASM, unlocking network-dependent features like HTTP clients, WebSocket connections, and remote API access.

Add WebSocket-based POSIX sockets proxy client to enable TCP/HTTP
connectivity in WASM builds via Cloudflare Workers backend.

## Architecture

```
WASM (glib.wasm) <--WebSocket--> Cloudflare Workers <--TCP--> Internet
     │                                  │
     │ Binary protocol                  │
     │ (19 message types)               │
     └─ GIO APIs work unchanged         └─ Real TCP sockets
```

## Changes

**New Files:**
- gio/gposixsocketsproxy.h: Proxy client API declarations
- gio/gposixsocketsproxy.c: WebSocket client with binary protocol (1335 lines)
  * Implements 19 POSIX socket operations (socket, connect, send, recv, etc.)
  * Uses Emscripten WebSocket API for communication
  * Async message queue for request/response matching
  * Thread-safe with GMutex protection

- gio/tests/gio-proxy-test.c: Integration tests (322 lines)
  * Test socket creation, DNS resolution, TCP connections
  * HTTP GET request/response validation
  * High-level GSocketClient API testing

**Modified Files:**
- gio/gsocket.c: Integrated proxy into GSocket operations (114 lines)
  * g_socket(): Use proxy_socket() on Emscripten
  * g_socket_connect(): Use proxy_connect() on Emscripten
  * send/recv: Use proxy_send()/proxy_recv() on Emscripten
  * Automatic proxy initialization with configurable URL

- gio/meson.build: Add proxy sources for Emscripten builds
- deno.json: Add test:proxy and test:proxy:local tasks

## Protocol

Binary protocol with little-endian encoding, 19 message types matching
Emscripten's POSIX sockets proxy specification:
- SOCKET, CONNECT, SEND, RECV, SHUTDOWN
- BIND, LISTEN, ACCEPT
- GETSOCKNAME, GETPEERNAME
- GETSOCKOPT, SETSOCKOPT
- GETADDRINFO, GETNAMEINFO
- SENDTO, RECVFROM, SENDMSG, RECVMSG, SOCKETPAIR

## Configuration

Set proxy URL via environment variable:
```bash
export POSIX_PROXY_URL="wss://posix-proxy.discere.cloud/v1"
```

Defaults to wss://posix-proxy.discere.cloud/v1 if not set.

## Testing

```bash
# Build WASM with proxy support
deno task build:wasm

# Test with production proxy
deno task test:proxy

# Test with local proxy (development)
deno task test:proxy:local
```

## Benefits

- ✅ Full GIO networking API support in WASM
- ✅ GSocket, GSocketClient, GSocketConnection work unchanged
- ✅ DNS resolution via proxy_getaddrinfo()
- ✅ HTTP requests through proxy tunnel
- ✅ Zero changes required to existing GIO client code
- ✅ Thread-safe proxy operations

## Impact

Enables TCP/HTTP connectivity for all GIO-based applications compiled
to WASM, unlocking network-dependent features like HTTP clients,
WebSocket connections, and remote API access.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants