Files
EasyTier/easytier-go/internal/reactor/stream.go
T
KKRainbow f26c2aa147 feat(wasi): run EasyTier core on Cloudflare Workers and browsers (#2548)
* fix(core): normalize secure keys for TOML instances

* feat(wasi): run core behind Cloudflare WebSockets

Introduce the Cloudflare Worker WASI host that runs the EasyTier core
behind host-upgraded WebSockets.

- Worker package scaffold (wrangler Durable Object, build-wasm script,
  vitest config) and core-runtime/websocket-host/data-plane runtime.
- WASI host WebSocket tunnel ABI (imports, adapter, runtime exports)
  with bounded receive memory and bounded admission queue.
- Route host sockets through the portable listener plan
  (HostListenerRegistration, listener queue, admission handler split).
- Build the WASM guest with the aes-gcm feature so secure peer
  sessions have their cipher available.

* feat(wasi): add outbound browser client runtime

Add the outbound-only WASI runtime and browser connector host so
browser pages can dial EasyTier peers through WebSocket relays.

- CoreConnectivityMode::{OutboundOnly, InboundOnly} gating for
  listeners, discovery, and direct connectivity modules.
- ExternalTunnelConnector plumbing through composite/connector_host/
  manual for browser WebSocket dials.
- Browser/Node smoke entries with shared helpers
  (smoke-shared.ts).

* feat(wasi): extend browser data plane with TCP half-close

Add the data-plane pieces the browser runtime needs for full-duplex
TCP streams behind host WebSockets:

- Guest TCP shutdown_write operation with submit/take ABI pair
  (DATA_PLANE_ABI_VERSION 3 -> 4) and smoltcp half-close support.
- Worker data-plane TCP listener/stream plumbing and core-runtime
  listener registration.
- Unit coverage for the new session ops and listener wiring.

* refactor(wasi): make host tunnel ABI transport-neutral

Replace WebSocket-specific core and WASI boundaries with a
message-oriented Host Tunnel interface. Keep WebSocket framing and text
rejection in the Cloudflare host while preserving payload boundaries,
ownership, cancellation, backpressure, and EOF behavior.

Rename feature flags and guest imports and exports to the Host Tunnel
ABI. Update both Worker profiles, tests, and architecture documentation.

* feat(web): split WASI hosts into publishable npm packages

Extract the shared JSPI, WASI, Host Tunnel, and data-plane runtime
into @easytier/runtime. Keep ABI handles, guest memory, TOML, and
operation broker details behind its adapter entry point.

Add typed, auto-starting @easytier/browser and factory-based
@easytier/cloudflare packages. Ship a matching Wasm profile with
each platform package and validate its capabilities before packing.

Persist Cloudflare instance identity in Durable Object storage,
centralize WebSocket admission ownership, and add package-level
coverage for the public interfaces.

* fix(web): make public packages portable

Embed the browser Wasm artifact in the published JavaScript entry
point. This lets esbuild consumers bundle the package without an asset
loader or a copied file.

Return Cloudflare's nominal Durable Object base type and document the
named subclass export required by generated Wrangler bindings.

* docs(web): add public package walkthrough

Expand both package READMEs with installation, configuration, local
validation, health checks, and deployment instructions.

Add a standalone Vite and Wrangler example that imports only the
public Browser and Cloudflare entries. Generate Worker bindings from
configuration and keep local secrets outside version control.

* chore(go): import EasyTier Go host

Add the standalone Go host runtime as a monorepo subtree without
carrying its development branch ancestry.

Preserve its API, tests, examples, generated protobuf bindings, and
embedded WASI artifacts.

* refactor(hosts): colocate Go and JavaScript runtimes

Move the browser, Cloudflare, shared runtime, and web example into
the easytier-js subtree. Update workspace metadata, build paths, and
documentation for the new layout.

Adopt github.com/EasyTier/EasyTier/easytier-go as the Go module path.
Resolve artifact and protobuf generation from the enclosing monorepo.

* build(web): isolate JavaScript host workspace

Keep public browser and Cloudflare packages outside the legacy frontend
workspace so root installs and cross-platform builds do not pull workerd.

Make each package build generate its required WASI artifact from a clean
checkout. Add a dedicated workflow that runs the same install and check
commands documented for contributors.

Move JavaScript dependencies into a scoped lockfile and restore the root
workspace lockfile to its pre-host state.
2026-09-06 13:35:02 +08:00

364 lines
8.1 KiB
Go

package reactor
import (
"errors"
"io"
"net"
)
type streamState struct {
connection net.Conn
closed bool
readQueue []*readOperation
readRunning bool
readReady bool
readData []byte
readErr error
writeQueue []*writeOperation
writeRunning bool
writeActive *writeOperation
}
type readOperation struct {
capacity uint32
stream *streamState
}
type writeOperation struct {
operation uint64
stream *streamState
data []byte
done bool
err error
}
type streamReadTask struct {
capacity uint32
stream *streamState
}
func newStreamState(connection net.Conn) *streamState {
return &streamState{connection: connection}
}
func (task streamReadTask) launch(reactor *Reactor) {
if task.stream != nil {
go reactor.runStreamRead(task.capacity, task.stream)
}
}
func (reactor *Reactor) StartRead(handle, operation uint64, capacity uint32) error {
reactor.mu.Lock()
stream, exists := reactor.streams[handle]
if !exists || capacity == 0 {
reactor.mu.Unlock()
return ErrInvalid
}
if err := reactor.claimOperationLocked(operation, operationRead); err != nil {
reactor.mu.Unlock()
return err
}
waiter := &readOperation{
capacity: capacity,
stream: stream,
}
reactor.reads[operation] = waiter
stream.readQueue = append(stream.readQueue, waiter)
ready := stream.readAvailableLocked()
task := reactor.prepareStreamReadLocked(stream)
reactor.mu.Unlock()
task.launch(reactor)
if ready {
reactor.signalCompletion()
}
return nil
}
func (reactor *Reactor) prepareStreamReadLocked(stream *streamState) streamReadTask {
if reactor.closed ||
stream.closed ||
stream.readRunning ||
stream.readReady ||
len(stream.readQueue) == 0 {
return streamReadTask{}
}
stream.readRunning = true
reactor.workers.Add(1)
return streamReadTask{
capacity: stream.readQueue[0].capacity,
stream: stream,
}
}
func (reactor *Reactor) runStreamRead(
capacity uint32,
stream *streamState,
) {
defer reactor.workers.Done()
buffer := make([]byte, capacity)
n, err := stream.connection.Read(buffer)
reactor.mu.Lock()
stream.readRunning = false
stream.readData = append(stream.readData[:0], buffer[:n]...)
stream.readErr = err
stream.readReady = true
ready := len(stream.readQueue) > 0
reactor.mu.Unlock()
if ready {
reactor.signalCompletion()
}
}
func (reactor *Reactor) TakeRead(operation uint64) ([]byte, error) {
reactor.mu.Lock()
waiter, exists := reactor.reads[operation]
if !exists || reactor.operations[operation] != operationRead {
reactor.mu.Unlock()
return nil, ErrInvalid
}
stream := waiter.stream
if len(stream.readQueue) == 0 || stream.readQueue[0] != waiter {
reactor.mu.Unlock()
return nil, ErrPending
}
if !stream.readAvailableLocked() {
reactor.mu.Unlock()
return nil, ErrPending
}
var data []byte
var err error
if stream.readReady {
take := len(stream.readData)
if take > int(waiter.capacity) {
take = int(waiter.capacity)
}
data = append([]byte(nil), stream.readData[:take]...)
if take == len(stream.readData) {
stream.readData = nil
err = stream.readErr
stream.readErr = nil
stream.readReady = false
} else {
stream.readData = stream.readData[take:]
}
} else {
err = net.ErrClosed
}
stream.readQueue[0] = nil
stream.readQueue = stream.readQueue[1:]
if len(stream.readQueue) == 0 {
stream.readQueue = nil
}
delete(reactor.reads, operation)
reactor.releaseOperationLocked(operation, operationRead)
ready := len(stream.readQueue) > 0 && stream.readAvailableLocked()
task := reactor.prepareStreamReadLocked(stream)
reactor.mu.Unlock()
task.launch(reactor)
if ready {
reactor.signalCompletion()
}
if errors.Is(err, io.EOF) {
err = nil
}
return data, err
}
func (reactor *Reactor) cancelStreamReadLocked(
operation uint64,
) (streamReadTask, bool) {
waiter := reactor.reads[operation]
delete(reactor.reads, operation)
if waiter == nil {
return streamReadTask{}, false
}
stream := waiter.stream
wasFirst := len(stream.readQueue) > 0 && stream.readQueue[0] == waiter
stream.readQueue = removeReadOperation(stream.readQueue, waiter)
ready := wasFirst && len(stream.readQueue) > 0 && stream.readAvailableLocked()
task := reactor.prepareStreamReadLocked(stream)
return task, ready
}
func removeReadOperation(
queue []*readOperation,
operation *readOperation,
) []*readOperation {
for index, queued := range queue {
if queued != operation {
continue
}
copy(queue[index:], queue[index+1:])
queue[len(queue)-1] = nil
queue = queue[:len(queue)-1]
if len(queue) == 0 {
return nil
}
return queue
}
return queue
}
func (stream *streamState) readAvailableLocked() bool {
return stream.readReady || stream.closed && !stream.readRunning
}
func (reactor *Reactor) StartWrite(handle, operation uint64, data []byte) error {
data = append([]byte(nil), data...)
reactor.mu.Lock()
stream, exists := reactor.streams[handle]
if !exists {
reactor.mu.Unlock()
return ErrInvalid
}
if err := reactor.claimOperationLocked(operation, operationWrite); err != nil {
reactor.mu.Unlock()
return err
}
request := &writeOperation{
operation: operation,
stream: stream,
data: data,
}
reactor.writes[operation] = request
stream.writeQueue = append(stream.writeQueue, request)
startWorker := !stream.writeRunning
if startWorker {
stream.writeRunning = true
reactor.workers.Add(1)
}
reactor.mu.Unlock()
if startWorker {
go reactor.runStreamWrites(stream)
}
return nil
}
func (reactor *Reactor) runStreamWrites(stream *streamState) {
defer reactor.workers.Done()
for {
reactor.mu.Lock()
if reactor.closed || len(stream.writeQueue) == 0 {
stream.writeQueue = nil
stream.writeRunning = false
reactor.mu.Unlock()
return
}
request := stream.writeQueue[0]
stream.writeQueue[0] = nil
stream.writeQueue = stream.writeQueue[1:]
if len(stream.writeQueue) == 0 {
stream.writeQueue = nil
}
stream.writeActive = request
data := request.data
reactor.mu.Unlock()
written := 0
var writeErr error
for written < len(data) {
n, err := stream.connection.Write(data[written:])
written += n
if err != nil {
writeErr = err
break
}
if n == 0 {
writeErr = io.ErrShortWrite
break
}
}
reactor.mu.Lock()
stream.writeActive = nil
request.data = nil
ready := reactor.writes[request.operation] == request &&
reactor.operations[request.operation] == operationWrite
if ready {
request.err = writeErr
request.done = true
}
reactor.mu.Unlock()
if ready {
reactor.signalCompletion()
}
}
}
func (reactor *Reactor) TakeWrite(operation uint64) error {
reactor.mu.Lock()
result, exists := reactor.writes[operation]
if !exists || reactor.operations[operation] != operationWrite {
reactor.mu.Unlock()
return ErrInvalid
}
if !result.done {
reactor.mu.Unlock()
return ErrPending
}
delete(reactor.writes, operation)
reactor.releaseOperationLocked(operation, operationWrite)
err := result.err
reactor.mu.Unlock()
return err
}
func (reactor *Reactor) cancelStreamWriteLocked(operation uint64) {
request := reactor.writes[operation]
delete(reactor.writes, operation)
if request == nil || request.stream.writeActive == request {
return
}
request.stream.writeQueue = removeWriteOperation(request.stream.writeQueue, request)
request.data = nil
}
func removeWriteOperation(
queue []*writeOperation,
operation *writeOperation,
) []*writeOperation {
for index, queued := range queue {
if queued != operation {
continue
}
copy(queue[index:], queue[index+1:])
queue[len(queue)-1] = nil
queue = queue[:len(queue)-1]
if len(queue) == 0 {
return nil
}
return queue
}
return queue
}
func (reactor *Reactor) closeStreamLocked(stream *streamState) bool {
stream.closed = true
ready := len(stream.readQueue) > 0 && stream.readAvailableLocked()
for _, request := range stream.writeQueue {
request.data = nil
if reactor.writes[request.operation] != request {
continue
}
request.err = net.ErrClosed
request.done = true
ready = true
}
stream.writeQueue = nil
return ready
}
func (stream *streamState) shutdownLocked() {
stream.closed = true
stream.readQueue = nil
stream.writeQueue = nil
}