Documentation
¶
Overview ¶
Package nwquic wraps Apple's Network.framework QUIC transport for headless pre-shared-key (PSK) authenticated multiplexed streams and datagrams.
Connections authenticate mutually using an ephemeral in-memory ECDSA P-256 TLS 1.3 identity whose certificate embeds an HMAC-SHA256 proof over the shared Config.PSK, Config.Identity, and public key, verified during the handshake without touching any macOS Keychain or triggering GUI prompts. Each Conn wraps an network.NWConnectionGroup backed by a QUIC multiplex descriptor and supports concurrent bidirectional Stream channels as well as RFC 9221 QUIC datagrams.
Basic usage:
cfg := nwquic.Config{
ALPN: "demo",
PSK: []byte("0123456789abcdef0123456789abcdef"),
Identity: []byte("node-a"),
EnableDatagrams: true,
}
ln, err := nwquic.Listen("127.0.0.1:0", cfg)
if err != nil {
log.Fatal(err)
}
defer ln.Close()
Example ¶
package main
import (
"context"
"fmt"
"io"
"time"
"github.com/tmc/apple/x/nwquic"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cfg := nwquic.Config{
ALPN: "nwquic-example",
PSK: []byte("0123456789abcdef0123456789abcdef"),
Identity: []byte("example-peer"),
}
ln, err := nwquic.ListenContext(ctx, "127.0.0.1:0", cfg)
if err != nil {
panic(err)
}
defer ln.Close()
go func() {
conn, err := ln.Accept(ctx)
if err != nil {
return
}
defer conn.Close()
s, err := conn.AcceptStream(ctx)
if err != nil {
return
}
defer s.Close()
msg, _ := io.ReadAll(s)
_, _ = s.Write(msg)
_ = s.CloseWrite()
}()
conn, err := nwquic.Dial(ctx, ln.Addr().String(), cfg)
if err != nil {
panic(err)
}
defer conn.Close()
s, err := conn.OpenStream(ctx)
if err != nil {
panic(err)
}
defer s.Close()
_, _ = s.Write([]byte("hello quic"))
_ = s.CloseWrite()
reply, _ := io.ReadAll(s)
fmt.Println(string(reply))
}
Output: hello quic
Index ¶
- Constants
- Variables
- type Config
- type Conn
- func (c *Conn) AcceptStream(ctx context.Context) (*Stream, error)
- func (c *Conn) Close() error
- func (c *Conn) OpenStream(ctx context.Context) (*Stream, error)
- func (c *Conn) ReceiveDatagram(ctx context.Context) ([]byte, error)
- func (c *Conn) SendDatagram(ctx context.Context, data []byte) error
- func (c *Conn) TransferReport(ctx context.Context) (TransferReport, error)
- type Listener
- type Stream
- type TransferReport
Examples ¶
Constants ¶
const ( // DefaultALPN is the application-layer protocol identifier used when // Config.ALPN is empty. DefaultALPN = "nwquic" )
Variables ¶
var ErrClosed = errors.New("nwquic: closed")
ErrClosed is returned when operating on a closed listener, connection, or stream.
var ErrDatagramsDisabled = errors.New("nwquic: datagrams disabled")
ErrDatagramsDisabled is returned when sending or receiving datagrams on a connection whose Config did not enable datagrams.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ALPN is the TLS application-layer protocol negotiation string.
// If empty, DefaultALPN is used.
ALPN string
// PSK is the pre-shared key bytes shared between client and server.
PSK []byte
// Identity is the pre-shared key identity bytes.
Identity []byte
// MaxBidiStreams sets the initial maximum number of concurrent
// bidirectional QUIC streams. Zero defaults to 100.
MaxBidiStreams uint64
// EnableDatagrams enables RFC 9221 QUIC datagram frames.
EnableDatagrams bool
}
Config configures a headless PSK-authenticated QUIC listener or client.
Both PSK and Identity must be non-empty. The zero value is not usable.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a multiplexed QUIC connection backed by an network.NWConnectionGroup.
func (*Conn) AcceptStream ¶
AcceptStream waits for and returns the next peer-initiated QUIC stream.
func (*Conn) Close ¶
Close closes the connection group and all associated streams and datagram flows.
func (*Conn) OpenStream ¶
OpenStream opens a new bidirectional QUIC stream on the connection.
func (*Conn) ReceiveDatagram ¶
ReceiveDatagram waits for the next inbound QUIC DATAGRAM frame.
func (*Conn) SendDatagram ¶
SendDatagram sends an unreliable QUIC DATAGRAM frame on the connection.
func (*Conn) TransferReport ¶
func (c *Conn) TransferReport(ctx context.Context) (TransferReport, error)
TransferReport collects and aggregates NWDataTransferReport metrics across the connection's streams.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener accepts incoming multiplexed QUIC connections.
func ListenContext ¶
ListenContext starts a QUIC listener bound to addr, respecting ctx for startup.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is a bidirectional QUIC stream multiplexed over a Conn.
func (*Stream) Close ¶
Close cancels the stream and releases its underlying Network.framework resources.
func (*Stream) CloseWrite ¶
CloseWrite sends a QUIC stream FIN so the peer observes io.EOF on Read while allowing this side to continue reading until the peer also finishes.
func (*Stream) StreamID ¶
StreamID returns the QUIC stream identifier reported by NWQuicGetStreamID.
func (*Stream) TransferReport ¶
func (s *Stream) TransferReport(ctx context.Context) (TransferReport, error)
TransferReport collects transport statistics for this stream via network.NWDataTransferReportCollect.
type TransferReport ¶
type TransferReport struct {
Duration time.Duration
SentAppBytes uint64
ReceivedAppBytes uint64
SentWireBytes uint64
ReceivedWireBytes uint64
RetransmittedBytes uint64
MinRTT time.Duration
SmoothedRTT time.Duration
RTTVariance time.Duration
PathCount uint32
}
TransferReport holds transport statistics collected from Network.framework's NWDataTransferReport.