dial

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 40 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dial is the controlling side of connection establishment: it turns a target (9-digit ID, saved alias or host:port) into an authenticated *peer.Conn.

For an ID that means: open the shared UDP socket, ask the rendezvous server for an introduction, punch through the NATs, establish QUIC on the first path that works and run the login handshake. For an address the rendezvous and punching steps are skipped; the login is the same.

The package also owns the client's saved peers (pinned host keys, pairing tokens, aliases) and the commands around them: pair, peers, ping, config.

Index

Constants

View Source
const (
	TransportUDP       = "udp"
	TransportWebSocket = "websocket"
)

Transports, for Info.Transport.

View Source
const MaxLANWait = 10 * time.Second

MaxLANWait bounds how long DiscoverLAN listens.

View Source
const PeersFile = "peers.json"

PeersFile is the client's saved-peers file inside the config directory.

View Source
const SettingOutcomes = "outcomes"

SettingOutcomes is the config key that turns connection outcome reports off. REMOHOP_OUTCOMES=0 overrides it to off.

Variables

View Source
var (
	DefaultSignal    string
	DefaultSignalPin string
	DefaultSignalWS  string
)

DefaultSignal and DefaultSignalPin name the rendezvous server (and optionally its public key) used when the user has configured none. They are empty in source and meant to be set by whoever builds a distribution with its own server:

go build -ldflags "-X github.com/readori/RemoHop/internal/dial.DefaultSignal=rendezvous.example.net:21116" ./cmd/remohop

With nothing set and nothing configured, dialling an ID fails with instructions instead of contacting a host the user never chose.

DefaultSignalWS adds the server's websocket bridge (`remohop signal --ws-listen`), for machines where only HTTPS gets out.

View Source
var ErrHostKeyChanged = errors.New("host identity has changed")

ErrHostKeyChanged is returned when a host presents a different identity key than the one pinned at the first login.

View Source
var ErrNoCredential = errors.New("no credential for this host")

ErrNoCredential is returned when Dial has no password, no saved pairing and no way to ask.

View Source
var ErrSavedPasswordRejected = errors.New("the saved password was rejected")

ErrSavedPasswordRejected comes with ErrNoCredential when the only credential was a saved password and the host rejected it (it has a new password); the saved one has been removed.

View Source
var ErrWouldReplacePairing = errors.New("a full pairing is saved for this host")

ErrWouldReplacePairing is returned by Redeem when target has a full pairing saved, which the grant would replace with narrower rights.

View Source
var OutcomesDefault = true

OutcomesDefault is whether reports are on while the outcomes setting is unset. The mobile apps turn it off (internal/mobile): they have no switch for it on screen yet, and their store privacy labels say they send the developer nothing, so reports there need the setting on.

View Source
var Reuse func(ctx context.Context, target string) (*peer.Conn, bool)

Reuse, if set, is asked for an existing session to the (canonical) target before a new one is established; returning false falls through to a normal dial. It is the seam for a local connection-sharing broker in the style of ssh's ControlMaster: the broker process keeps one session per host and hands short-lived commands a *peer.Conn that proxies streams to it over a local socket (peer.Mux exists for that), so they skip rendezvous, punching and login.

Functions

func CheckAlias

func CheckAlias(alias string) error

CheckAlias validates an alias: a short name that cannot be mistaken for an ID, an address or a remohop command.

func Dial

func Dial(ctx context.Context, target string, opts Options) (*peer.Conn, error)

Dial establishes an authenticated session with target: a 9-digit ID (spaces and dashes allowed), an alias saved with `remohop <ID> pair --name` or `remohop peers alias`, or a direct "host:port".

Credentials are tried in this order: opts.Password; the pairing token saved for the host; opts.Prompt. Closing the returned connection releases everything Dial opened.

Errors wrap the layer that failed, so callers can classify them with signal.CodeOf (unknown ID, host offline), auth.CodeOf (wrong password, locked out), errors.Is(err, nat.ErrNoPath) (hole punching failed), errors.Is(err, ErrNoCredential) and errors.Is(err, ErrHostKeyChanged).

func OutcomesAllowed

func OutcomesAllowed(dir string) bool

OutcomesAllowed reports whether a client keeping its config in dir reports connection outcomes: unless REMOHOP_OUTCOMES says off, or the outcomes setting does.

func Presence

func Presence(ctx context.Context, opts Options, ids []string) (map[string]string, error)

Presence asks the rendezvous server (opts.Signal, else the configured one) which of ids are registered right now: each maps to one of the signal.Presence* states. It asks about those IDs only.

func Redeem

func Redeem(store *Store, target, code, alias string) error

Redeem stores a grant code (from `remohop host grant` on the host) as the credential for target, replacing any saved pairing. The host binds the code to this machine's identity at the first login.

func RedeemKeeping

func RedeemKeeping(store *Store, target, code, alias string, replace bool) error

RedeemKeeping is Redeem that replaces a full pairing saved for target only if replace is set; otherwise it fails with ErrWouldReplacePairing and changes nothing. A grant saved earlier is always replaced.

func RendezvousFirst

func RendezvousFirst(target string, opts Options) error

RendezvousFirst reports a missing rendezvous server for a target that needs one (a 9-digit ID, directly or through its alias) before anything else happens: before the password prompt, and instead of "no credential" when there is no terminal to prompt on. The error wraps signal.ErrNotConfigured.

func SavePairing

func SavePairing(info *Info, p *hostd.Pairing, alias string, dir string) error

SavePairing stores a pairing obtained with hostd.Pair for the host behind a session made by Dial, optionally under an alias, so later Dials log in with it.

Types

type Device

type Device struct {
	// Target is the 9-digit ID or HOST:PORT; empty for an account device
	// that has no ID.
	Target string `json:"target"`
	Alias  string `json:"alias,omitempty"`
	// Account marks a device of this machine's account; Name, Platform
	// and ThisDevice are only set for those.
	Account    bool   `json:"account,omitempty"`
	Name       string `json:"name,omitempty"`
	Platform   string `json:"platform,omitempty"`
	ThisDevice bool   `json:"this_device,omitempty"`
	// Login is "pairing", "key", "saved_password" (a password the user
	// asked a gateway to remember), "account" (a key login the host accepts
	// if its owner turned account-trust on, else the password) or
	// "password".
	Login string `json:"login"`
	// Grant says the pairing is a grant (`remohop peers redeem`): the
	// host's owner limited what this machine may do there.
	Grant bool `json:"grant,omitempty"`
	// Presence is "online", "offline", "unknown" (to the rendezvous
	// server), "rate_limited", "not_checked" (an address, or --offline),
	// "not_a_host" (an account device without an ID), or "error" with
	// Error set.
	Presence     string     `json:"presence"`
	Error        string     `json:"error,omitempty"`
	LastSeen     *time.Time `json:"last_seen,omitempty"`
	HostIdentity string     `json:"host_identity,omitempty"`
	// Running lists the kinds of background commands (vpn, forward, ...)
	// this machine has running to the device.
	Running []string `json:"running,omitempty"`
}

Device is one row of `remohop devices`.

func Devices

func Devices(ctx context.Context, opts DevicesOptions) ([]Device, error)

Devices lists the account's devices, when this machine is logged in to one, followed by the saved peers that are not among them, each with its presence.

type DevicesOptions

type DevicesOptions struct {
	Options
	// Offline skips the rendezvous server.
	Offline bool
}

DevicesOptions configures Devices.

type Info

type Info struct {
	// Target is the canonical target: the 9-digit ID or "host:port".
	Target string
	// Path is how the session reaches the host: directly in every case
	// but PathRelayed. A relayed session that moves to a direct path gets
	// a new Info (InfoOf returns it) with the new Path and Remote; an Info
	// itself never changes.
	Path Path
	// Transport is how this machine's packets travel: TransportUDP, or
	// TransportWebSocket through the rendezvous server's websocket bridge
	// (then Path is always PathRelayed).
	Transport string
	// Local and Remote are the UDP addresses of the session on this
	// machine and of the host as reached.
	Local, Remote netip.AddrPort
	// HostKey is the host's identity key.
	HostKey ed25519.PublicKey
	// Auth is the credential kind the session was authenticated with.
	Auth auth.Kind
	// NewHost is true when this was the first successful login to the
	// host and its key has just been pinned.
	NewHost bool
}

Info describes an established session; see InfoOf.

func InfoOf

func InfoOf(c *peer.Conn) *Info

InfoOf returns the establishment details of a session made by Dial, or nil for any other connection.

type LANHost

type LANHost struct {
	lan.Host
	// Saved lists the hosts saved here (connected to or paired with) that
	// have its identity key: how this machine already knows it.
	Saved []LANSaved `json:"saved,omitempty"`
	// Conflict is a saved target (its ID or this address) whose pinned
	// key is not the one the host announced. For its ID that means the
	// answer may come from an impostor; for the address, that another
	// machine had it before. Connecting there is refused until the saved
	// host is forgotten.
	Conflict string `json:"conflict,omitempty"`
	// Self is this machine's own host.
	Self bool `json:"self,omitempty"`
}

LANHost is a host found on the local network, with what this machine knows about it.

func DiscoverLAN

func DiscoverLAN(ctx context.Context, dir string, wait time.Duration) ([]LANHost, error)

DiscoverLAN asks the local network for RemoHop hosts (lan.Browse) for wait (lan.DefaultWait if 0) and tells, from the saved hosts in dir (the config directory if empty), which of them this machine knows.

type LANSaved

type LANSaved struct {
	Target string `json:"target"`
	Alias  string `json:"alias,omitempty"`
	Paired bool   `json:"paired"`
}

LANSaved is a saved host with the identity key a LAN host announced.

type Options

type Options struct {
	// Signal overrides the rendezvous server ("host[:port]").
	Signal string
	// Password is the host's password. When set it is the only credential
	// tried.
	Password string
	// Pairing is a pairing token held in memory and not saved (the phone
	// apps get one to log in again after a dropped connection). When set
	// it is tried before the saved credentials; a host that no longer
	// accepts it leaves the saved pairing alone.
	Pairing *hostd.Pairing
	// Prompt asks a human for the password when no other credential
	// works. Nil means never ask: the dial fails with an explanation
	// instead, which is what non-interactive callers want.
	Prompt func(ctx context.Context, req PromptRequest) (string, error)
	// Notify, if set, receives one-line notices about a dial in progress
	// that a person would want to see ("waiting for approval on the
	// host", "connected through a relay").
	Notify func(msg string)
	// Dir is the config directory holding the identity key and saved
	// peers; empty means the default.
	Dir string
	// Logger receives progress at debug level; nil means slog.Default().
	Logger *slog.Logger
	// NoRelay refuses a relayed connection even if the rendezvous server
	// offers one. The "relay" setting and REMOHOP_RELAY=0 do the same.
	NoRelay bool
	// NoUpgrade keeps a relayed session on the relay for its whole life
	// instead of moving it to a direct path once one works (upgrade.go).
	// REMOHOP_RELAY_UPGRADE=0 does the same.
	NoUpgrade bool
	// PunchTime overrides how long hole punching may take.
	PunchTime time.Duration
	// SignalWS overrides the rendezvous server's websocket bridge URL and
	// Transport the way it is reached (signal.TransportAuto, UDP or WS);
	// empty means the configuration (signal-ws, REMOHOP_TRANSPORT).
	SignalWS  string
	Transport string

	// PacketConn, if set, is used as the shared socket, and Locals
	// replaces interface enumeration. Tests on a virtual network use both.
	// Listen, if set, opens each socket a dial needs instead (a retried
	// punch needs a fresh one); PacketConn allows no retry.
	PacketConn net.PacketConn
	Listen     func() (net.PacketConn, error)
	Locals     func() []netip.Addr
}

Options configures Dial. The zero value dials with saved credentials only, using the rendezvous server from the config file.

func OptionsFromEnv

func OptionsFromEnv(env *cli.Env) Options

OptionsFromEnv builds the Options a CLI command dials with: the global --signal and --password flags (or REMOHOP_SIGNAL / REMOHOP_PASSWORD), and an interactive password prompt only when stdin is a terminal.

type Path

type Path string

Path says how the session's packets travel.

const (
	// PathLAN: straight to a private address the host listed for itself;
	// both machines are on the same network.
	PathLAN Path = "lan"
	// PathDirect: straight to an address given by the user, or to the
	// host's own public address, without a NAT mapping on the host side.
	PathDirect Path = "direct"
	// PathPunched: through NAT mappings opened by hole punching.
	PathPunched Path = "punched"
	// PathRelayed: no direct path exists; the rendezvous server passes
	// the (end-to-end encrypted) packets between the peers.
	PathRelayed Path = "relayed"
	// PathTailnet: to the host's address on a tailnet both machines are
	// on (Tailscale carries the packets; the session is still RemoHop's
	// own, end to end).
	PathTailnet Path = "tailnet"
)

type Peer

type Peer struct {
	// Target is how the host is dialled: its 9-digit ID or "host:port".
	Target string `json:"target"`
	// Alias is an optional name usable in place of Target on the command
	// line.
	Alias string `json:"alias,omitempty"`
	// HostKey is the host's identity key (auth.EncodeKey), pinned at the
	// first successful login. A host that later presents another key is
	// refused.
	HostKey string `json:"host_key,omitempty"`
	// KeyLogin says the host's owner has authorised this machine's
	// identity key (`remohop host authorize`), so logins need no secret. Set
	// by `remohop peers trust`, which also pins HostKey.
	KeyLogin bool `json:"key_login,omitempty"`
	// TokenID and Token are the pairing credential from `remohop <ID> pair`.
	TokenID  string     `json:"token_id,omitempty"`
	Token    []byte     `json:"token,omitempty"`
	PairedAt *time.Time `json:"paired_at,omitempty"`
	// Grant says the token is a grant redeemed with `remohop peers
	// redeem` (narrower rights, set by the host's owner) rather than a
	// full pairing.
	Grant bool `json:"grant,omitempty"`
	// Password is the host's password, saved only when the user asked a
	// gateway (the desktop app, a mobile app, `remohop web`) to remember
	// the login and the host does not hand out pairings (quick support,
	// the desktop app's own host). It is sent only to the pinned HostKey
	// and is dropped when the host rejects it. Like the tokens, it is a
	// login credential: the file is 0600.
	Password string `json:"password,omitempty"`
	// FirstSeen is when the host key was pinned.
	FirstSeen time.Time `json:"first_seen"`
	// LastSeen is when this machine last found the host online: a
	// successful login, or `remohop devices` seeing it registered. It is this
	// machine's own observation; the rendezvous server is not asked when
	// a host was last around.
	LastSeen *time.Time `json:"last_seen,omitempty"`
}

Peer is what the client remembers about one host.

func (Peer) Paired

func (p Peer) Paired() bool

Paired reports whether a pairing token is stored for the peer.

func (Peer) SavedPassword

func (p Peer) SavedPassword() bool

SavedPassword reports whether a usable saved password is stored: one with a pinned host key to send it to.

type PromptRequest

type PromptRequest struct {
	// Target is the host as the user named it, formatted for display.
	Target string
	// Attempt counts prompts for this dial, starting at 1.
	Attempt int
	// Reason says why a prompt is needed again ("wrong password", ...);
	// empty on the first one.
	Reason string
}

PromptRequest describes a password prompt.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the saved-peers file: pinned host keys, pairing tokens and aliases. It is mode 0600 because the tokens are login credentials. All operations go to the file, with a cross-process lock around changes, so concurrent remohop commands do not lose each other's updates.

func OpenStore

func OpenStore(dir string) (*Store, error)

OpenStore returns the store in dir (the remohop config directory if empty).

func (*Store) ByAlias

func (s *Store) ByAlias(alias string) (Peer, bool, error)

ByAlias returns the saved peer with the given alias.

func (*Store) Forget

func (s *Store) Forget(ref string) (int, error)

Forget removes the peers whose target or alias equals ref, with their pinned key and pairing token, and reports how many were removed.

func (*Store) Get

func (s *Store) Get(target string) (Peer, bool, error)

Get returns the saved peer for a target.

func (*Store) List

func (s *Store) List() ([]Peer, error)

List returns every saved peer in the order they were first seen.

func (*Store) SetAlias

func (s *Store) SetAlias(target, alias string) error

SetAlias names target. An alias is unique: it is taken away from any other peer that had it. An empty alias removes target's name (and adds no entry for a target that is not saved).

func (*Store) SetPassword

func (s *Store) SetPassword(target, password string) error

SetPassword saves (or, when password is empty, removes) the password of the saved peer target. A peer that is not saved is an error: the password only ever goes with a pinned host key.

func (*Store) Update

func (s *Store) Update(target string, fn func(*Peer) error) error

Update applies fn to the saved peer for target, creating the entry if needed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL