Files
EasyTier/easytier-go/examples/dial/main.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

306 lines
7.3 KiB
Go

package main
import (
"context"
"encoding/binary"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/netip"
"os"
"os/signal"
"strings"
"syscall"
"time"
corehost "github.com/EasyTier/EasyTier/easytier-go"
"github.com/EasyTier/EasyTier/easytier-go/internal/contextutil"
)
const maxUDPPayload = 65507
type peerList []string
func (peers *peerList) Set(value string) error {
if strings.TrimSpace(value) == "" {
return fmt.Errorf("peer URI must not be empty")
}
*peers = append(*peers, value)
return nil
}
func (peers *peerList) String() string {
return strings.Join(*peers, ",")
}
type options struct {
peers peerList
networkName string
networkSecret string
ipv4 string
network string
address string
connectTimeout time.Duration
}
func main() {
options, err := parseOptions(os.Args[1:])
if err == flag.ErrHelp {
return
}
if err != nil {
log.Printf("invalid arguments: %v", err)
os.Exit(2)
}
ctx, stop := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer stop()
if err := run(ctx, options, os.Stdin, os.Stdout); err != nil {
log.Printf("EasyTier dial failed: %v", err)
os.Exit(1)
}
}
func parseOptions(arguments []string) (options, error) {
flags := flag.NewFlagSet("dial", flag.ContinueOnError)
var options options
flags.Var(&options.peers, "p", "EasyTier peer URI; may be repeated")
flags.StringVar(&options.networkName, "network-name", "", "EasyTier network name")
flags.StringVar(&options.networkSecret, "network-secret", "", "EasyTier network secret")
flags.StringVar(&options.ipv4, "ipv4", "", "fixed EasyTier IPv4 address and prefix")
flags.StringVar(&options.network, "network", "tcp4", "overlay network: tcp4 or udp4")
flags.StringVar(&options.address, "address", "", "overlay IPv4 destination")
flags.DurationVar(
&options.connectTimeout,
"connect-timeout",
10*time.Second,
"time to wait for an overlay route and connection",
)
if err := flags.Parse(arguments); err != nil {
return options, err
}
if flags.NArg() != 0 {
return options, fmt.Errorf("unexpected argument %q", flags.Arg(0))
}
if err := options.validate(); err != nil {
return options, err
}
return options, nil
}
func (options options) validate() error {
if len(options.peers) == 0 {
return fmt.Errorf("at least one -p peer is required")
}
if strings.TrimSpace(options.networkName) == "" {
return fmt.Errorf("--network-name is required")
}
if options.networkSecret == "" {
return fmt.Errorf("--network-secret is required")
}
prefix, err := netip.ParsePrefix(options.ipv4)
if err != nil {
return fmt.Errorf("parse --ipv4: %w", err)
}
if !prefix.Addr().Is4() {
return fmt.Errorf("--ipv4 must be an IPv4 prefix")
}
if options.network != "tcp4" && options.network != "udp4" {
return fmt.Errorf("--network must be tcp4 or udp4")
}
if options.connectTimeout <= 0 {
return fmt.Errorf("--connect-timeout must be positive")
}
address, err := netip.ParseAddrPort(options.address)
if err != nil || !address.Addr().Is4() || address.Port() == 0 {
return fmt.Errorf("--address must be an IPv4 socket address with a nonzero port")
}
return nil
}
func run(
ctx context.Context,
options options,
input io.Reader,
output io.Writer,
) error {
prefix, err := netip.ParsePrefix(options.ipv4)
if err != nil {
return fmt.Errorf("parse instance IPv4 prefix: %w", err)
}
config, err := corehost.NewInstanceConfigBuilder(options.networkName).
NetworkSecret(options.networkSecret).
IPv4(prefix).
AddPeers([]string(options.peers)...).
Build()
if err != nil {
return fmt.Errorf("build EasyTier instance config: %w", err)
}
host, err := corehost.New(ctx, corehost.Options{})
if err != nil {
return fmt.Errorf("create EasyTier host: %w", err)
}
defer host.Close(context.Background())
instance, err := host.CreateInstance(ctx, config)
if err != nil {
return fmt.Errorf("create EasyTier instance: %w", err)
}
defer instance.Close(context.Background())
if err := instance.Start(ctx); err != nil {
return fmt.Errorf("start EasyTier instance: %w", err)
}
target := netip.MustParseAddrPort(options.address)
connectContext, cancelConnect := context.WithTimeout(ctx, options.connectTimeout)
defer cancelConnect()
if target.Addr() != prefix.Addr() {
if err := waitForRoute(connectContext, instance, target.Addr()); err != nil {
return fmt.Errorf("wait for overlay route to %s: %w", target.Addr(), err)
}
}
connection, err := instance.Dial(
connectContext,
options.network,
options.address,
)
if err != nil {
return err
}
defer connection.Close()
stopClose := contextutil.AfterFunc(ctx, func() {
_ = connection.Close()
})
defer stopClose()
log.Printf("connected %s to %s through EasyTier", options.network, options.address)
switch options.network {
case "tcp4":
err = relayTCP(connection, input, output)
case "udp4":
err = exchangeUDP(ctx, connection, input, output)
}
if ctx.Err() != nil {
return nil
}
return err
}
func waitForRoute(
ctx context.Context,
instance *corehost.Instance,
target netip.Addr,
) error {
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
for {
routes, err := instance.ListRoute(ctx)
if err != nil {
return err
}
if routesReach(routes, target) {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}
func routesReach(routes []*corehost.Route, target netip.Addr) bool {
octets := target.As4()
targetValue := binary.BigEndian.Uint32(octets[:])
for _, route := range routes {
if route.GetIpv4Addr().GetAddress().GetAddr() == targetValue {
return true
}
for _, cidr := range route.GetProxyCidrs() {
prefix, err := netip.ParsePrefix(cidr)
if err == nil && prefix.Contains(target) {
return true
}
}
}
return false
}
func relayTCP(connection net.Conn, input io.Reader, output io.Writer) error {
writeResult := make(chan error, 1)
go func() {
_, err := io.Copy(connection, input)
writeResult <- err
}()
_, readErr := io.Copy(output, connection)
select {
case writeErr := <-writeResult:
return errors.Join(writeErr, readErr)
default:
return readErr
}
}
func exchangeUDP(
ctx context.Context,
connection net.Conn,
input io.Reader,
output io.Writer,
) error {
type readResult struct {
payload []byte
err error
}
result := make(chan readResult, 1)
go func() {
payload, err := io.ReadAll(io.LimitReader(input, maxUDPPayload+1))
result <- readResult{payload: payload, err: err}
}()
var payload []byte
select {
case <-ctx.Done():
return ctx.Err()
case read := <-result:
if read.err != nil {
return fmt.Errorf("read UDP payload: %w", read.err)
}
payload = read.payload
}
if len(payload) == 0 {
return fmt.Errorf("UDP input must contain one non-empty datagram")
}
if len(payload) > maxUDPPayload {
return fmt.Errorf("UDP input exceeds the maximum payload of %d bytes", maxUDPPayload)
}
written, err := connection.Write(payload)
if err != nil {
return fmt.Errorf("write UDP datagram: %w", err)
}
if written != len(payload) {
return io.ErrShortWrite
}
response := make([]byte, maxUDPPayload)
length, err := connection.Read(response)
if err != nil {
return fmt.Errorf("read UDP datagram: %w", err)
}
written, err = output.Write(response[:length])
if err != nil {
return fmt.Errorf("write UDP response: %w", err)
}
if written != length {
return io.ErrShortWrite
}
return nil
}