Documentation
¶
Index ¶
- Constants
- func ListProtocols() []string
- func ParseTarget(target string, defaultPort int) (string, int, error)
- func Register(p Protocol)
- type DNSResult
- type ErrorCode
- type FlaggedProtocol
- type GreetConfig
- type GreetError
- type GreetOption
- type GreetResult
- func Greet(ctx context.Context, protocol, target string, opts ...GreetOption) (*GreetResult, error)
- func GreetWith(ctx context.Context, p Protocol, host string, port int, opts ...GreetOption) (*GreetResult, error)
- func NewResult(protocol string, transport Transport, ttdr, rtt, ttfb, ttlb time.Duration, ...) *GreetResult
- type Protocol
- type Transport
Constants ¶
const DefaultTimeout = 5 * time.Second
DefaultTimeout is the deadline for the entire handshake operation.
Variables ¶
This section is empty.
Functions ¶
func ListProtocols ¶
func ListProtocols() []string
ListProtocols returns all registered protocol names, sorted.
func ParseTarget ¶
ParseTarget splits a target string into host and port. Accepted formats: "host", "host:port", "[::1]:port". If no port is specified, defaultPort is used. Port is validated to be in the range 1–65535.
Types ¶
type DNSResult ¶ added in v1.0.2
type DNSResult struct {
Address string // resolved IP address
TTDR time.Duration // time to DNS resolved
}
DNSResult holds the outcome of DNS resolution.
type ErrorCode ¶
type ErrorCode string
ErrorCode is a human-readable error category string. Code always compares against named constants; the string value is immediately readable in logs and debug prints.
const ( ErrResolveHostFailed ErrorCode = "resolve_host_failed" ErrInvalidAddress ErrorCode = "invalid_address" ErrUnknownProtocol ErrorCode = "unknown_protocol" ErrInvalidConfig ErrorCode = "invalid_config" )
Common error codes shared by every protocol.
const ( ErrConnectionRefused ErrorCode = "connection_refused" ErrConnectionTimeout ErrorCode = "connection_timeout" ErrConnectionReset ErrorCode = "connection_reset" ErrNetworkUnreachable ErrorCode = "network_unreachable" ErrConnectionFailed ErrorCode = "connection_failed" )
Generic TCP error codes shared by all TCP-based protocols.
type FlaggedProtocol ¶
type FlaggedProtocol interface {
Protocol
// RegisterFlags adds protocol-specific flags to a flag.FlagSet.
RegisterFlags(fs *flag.FlagSet)
// ParseFlags returns protocol-specific options from parsed flags.
ParseFlags(fs *flag.FlagSet) ([]GreetOption, error)
}
FlaggedProtocol is an optional interface for protocols that need CLI-specific flags. The CLI auto-discovers this via type assertion.
type GreetConfig ¶
type GreetConfig struct {
Timeout time.Duration
ProtocolConfig any // protocol-specific config set via WithProtocolConfig
}
GreetConfig holds resolved options for a Greet call.
func ResolveOptions ¶
func ResolveOptions(opts ...GreetOption) GreetConfig
ResolveOptions applies functional options and fills defaults.
type GreetError ¶
type GreetError struct {
Code ErrorCode
Message string
Protocol string // protocol name; empty for common errors
Cause error // underlying OS or network error
}
GreetError is the structured error type returned by all protocol implementations. It carries a machine-readable code, a human message, the originating protocol name, and the underlying OS / network error.
func (*GreetError) Error ¶
func (e *GreetError) Error() string
Error implements the error interface.
func (*GreetError) Unwrap ¶
func (e *GreetError) Unwrap() error
Unwrap returns the underlying cause, enabling errors.Is / errors.As chains.
type GreetOption ¶
type GreetOption func(*GreetConfig)
GreetOption is a functional option for configuring a Greet call.
func WithProtocolConfig ¶
func WithProtocolConfig(cfg any) GreetOption
WithProtocolConfig carries protocol-specific configuration from FlaggedProtocol.ParseFlags into the Greet call.
func WithTimeout ¶
func WithTimeout(d time.Duration) GreetOption
WithTimeout overrides the default handshake timeout.
type GreetResult ¶
type GreetResult struct {
Protocol string
Transport Transport
TTDR time.Duration // Time to DNS Resolved
RTT time.Duration // Round Trip Time
TTFB time.Duration // Time to First Byte
TTLB time.Duration // Time to Last Byte
Success bool
// Data holds protocol-specific payload. Nil for generic TCP/UDP.
// Cast to the protocol's result type for typed access, e.g.:
// mc, ok := result.Data.(*minecraft.MinecraftResult)
Data any
}
GreetResult holds the outcome of a protocol handshake. Protocols populate Data with a typed result struct (e.g. *MinecraftResult); generic TCP/UDP set Data to nil.
All four timing metrics share the same starting anchor time (start := time.Now() at the beginning of the operation):
|------| TTDR: from start to when DNS resolves |------|--| RTT: from start to when first ACK at TCP transport or Receive at UDP |------|--|-| TTFB: from start to when first byte response at the correct protocol layer |------|--|-------| TTLB: from start to when last byte response at the correct protocol layer
func Greet ¶
func Greet(ctx context.Context, protocol, target string, opts ...GreetOption) (*GreetResult, error)
Greet is the one-stop entry point. It looks up the protocol by name, resolves the target (host or host:port), and performs the handshake.
func GreetWith ¶
func GreetWith(ctx context.Context, p Protocol, host string, port int, opts ...GreetOption) (*GreetResult, error)
GreetWith is like Greet but accepts a pre-resolved Protocol instance.
type Protocol ¶
type Protocol interface {
// Name returns the protocol slug used by the CLI and registry (e.g. "minecraft").
Name() string
// Description is a one-line human-readable summary.
Description() string
// DefaultPort is the well-known port (e.g. 25565 for Minecraft).
DefaultPort() int
// Transport returns TransportTCP or TransportUDP.
Transport() Transport
// Greet performs the handshake and returns a result or error.
Greet(ctx context.Context, host string, port int, opts ...GreetOption) (*GreetResult, error)
}
Protocol is the interface every greeter implementation must satisfy. Implementations register themselves via greet.Register() in init().
