forward

package
v1.0.11 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: 28 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 forward is the RemoHop port-forwarding service.

Local forwards (like ssh -L) listen on the client and tunnel every accepted TCP connection to an address the host can reach. Reverse forwards (like ssh -R) make the host listen and carry each inbound connection back to the client, which dials a local target.

Wire protocol, all on ordinary session streams:

  • "forward": request {network, addr}. The host dials, answers with one JSON status message {ok, error} and then both sides copy raw bytes.

  • "forward.listen": request {network, bind, id}. The host listens and answers {ok, error, addr}. The stream then stays open as a control stream: closing it (or the session ending) closes the listener.

  • "forward.accept": opened by the host towards the client for every connection the reverse listener accepts, request {id}. The client dials the target registered under id, answers with a status message and the two sides copy raw bytes.

  • "forward.udp": one stream per UDP forward; see udp.go. Packets travel as QUIC datagrams when they fit and as frames on that stream otherwise.

A running `remohop forward` process also writes a small record next to its internal/bg entry and listens on a token-protected loopback control socket, which is what `remohop forward` (list) and `remohop unforward` use.

Every TCP connection uses its own stream. A FIN on one side becomes CloseWrite on the other so half-close survives the tunnel; any other error resets the stream and the TCP connection.

Index

Constants

View Source
const DefaultHost = "127.0.0.1"

DefaultHost is the bind address and target host used when a spec omits them.

Variables

View Source
var ErrSessionClosed = errors.New("session closed")

ErrSessionClosed is the Err of a Forwarder whose session ended.

Functions

func Dial

func Dial(ctx context.Context, conn *peer.Conn, network, addr string) (net.Conn, error)

Dial opens a connection to addr (host:port) as seen from the host and returns it as a net.Conn. network is "tcp", "tcp4" or "tcp6". The returned connection also has a CloseWrite method for half-close. A refused or unreachable target is reported as an error.

Types

type Forwarder

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

Forwarder is a running local or reverse forward.

func Listen

func Listen(ctx context.Context, conn *peer.Conn, spec Spec) (*Forwarder, error)

Listen starts a local forward: the client listens on spec's bind address and every connection is tunnelled to spec's target as seen from the host. The forward runs until Close, ctx is cancelled or the session ends.

func ListenReverse

func ListenReverse(ctx context.Context, conn *peer.Conn, spec Spec) (*Forwarder, error)

ListenReverse starts a reverse forward: the host listens on spec's bind address and every connection is carried back to the client, which dials spec's target. Addr is the address the host is listening on.

func (*Forwarder) Addr

func (f *Forwarder) Addr() net.Addr

Addr is the address being listened on: on the client for a local forward, on the host for a reverse one.

func (*Forwarder) Close

func (f *Forwarder) Close() error

Close stops listening, aborts open connections and waits for everything to wind down. It is safe to call more than once.

func (*Forwarder) Done

func (f *Forwarder) Done() <-chan struct{}

Done is closed once the forwarder has stopped and released its resources.

func (*Forwarder) Err

func (f *Forwarder) Err() error

Err says why the forwarder stopped: nil after Close, the context's error after cancellation, ErrSessionClosed when the session died, or the failure that killed the listener. It is nil while the forwarder is running.

func (*Forwarder) Reverse

func (f *Forwarder) Reverse() bool

Reverse reports whether the host listens (-R) rather than the client.

func (*Forwarder) Spec

func (f *Forwarder) Spec() Spec

Spec is the spec the forwarder was started with. For port 0 the actual port is in Addr.

func (*Forwarder) Stats

func (f *Forwarder) Stats() Stats

Stats returns the current counters.

func (*Forwarder) Target

func (f *Forwarder) Target() string

Target is the address connections are carried to.

type Spec

type Spec struct {
	BindHost string // "" listens on all interfaces
	BindPort int    // 0 picks a free port
	Host     string
	Port     int
}

Spec describes one forward in the ssh -L / -R notation.

For a local forward, BindHost:BindPort is where the client listens and Host:Port is the target as seen from the host. For a reverse forward, BindHost:BindPort is where the host listens and Host:Port is the target as seen from the client.

func ParseSpec

func ParseSpec(s string) (Spec, error)

ParseSpec parses a forward spec in one of these forms:

PORT:TARGETPORT                  target is 127.0.0.1:TARGETPORT
PORT:TARGETHOST:TARGETPORT
BIND:PORT:TARGETHOST:TARGETPORT

IPv6 literals are written in brackets ([::1]:8080:[::1]:80). A bind of "*" or an empty bind listens on all interfaces. PORT may be 0 to pick a free port.

func (Spec) Bind

func (s Spec) Bind() string

Bind is the listen address in host:port form.

func (Spec) String

func (s Spec) String() string

func (Spec) Target

func (s Spec) Target() string

Target is the target address in host:port form.

type Stats

type Stats struct {
	Connections uint64 // connections accepted so far
	Active      int64  // connections open now
	BytesUp     int64  // from the accepted connection towards the target
	BytesDown   int64  // from the target back to the accepted connection
}

Stats is a snapshot of a Forwarder's counters.

type UDPForwarder

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

UDPForwarder is a running local UDP forward.

func ListenUDP

func ListenUDP(ctx context.Context, conn *peer.Conn, spec Spec) (*UDPForwarder, error)

ListenUDP starts a local UDP forward: the client listens on spec's bind address and every packet is carried to spec's target as seen from the host; replies return to the source address they answer. A source address that is silent for a minute is forgotten. The forward runs until Close, ctx is cancelled or the session ends.

func (*UDPForwarder) Addr

func (f *UDPForwarder) Addr() net.Addr

Addr is the local UDP address being listened on.

func (*UDPForwarder) Close

func (f *UDPForwarder) Close() error

Close stops the forward and waits for it to wind down.

func (*UDPForwarder) Datagrams

func (f *UDPForwarder) Datagrams() bool

Datagrams reports whether packets travel as QUIC datagrams (when they fit) rather than only on the forward's stream.

func (*UDPForwarder) Done

func (f *UDPForwarder) Done() <-chan struct{}

Done is closed once the forwarder has stopped and released its resources.

func (*UDPForwarder) Err

func (f *UDPForwarder) Err() error

Err says why the forwarder stopped, like Forwarder.Err.

func (*UDPForwarder) Spec

func (f *UDPForwarder) Spec() Spec

Spec is the spec the forwarder was started with.

func (*UDPForwarder) Stats

func (f *UDPForwarder) Stats() UDPStats

Stats returns the current counters.

func (*UDPForwarder) Target

func (f *UDPForwarder) Target() string

Target is the address packets are carried to.

type UDPStats

type UDPStats struct {
	Flows       int    // source addresses currently tracked
	PacketsUp   uint64 // local -> remote target (accepted from local senders)
	PacketsDown uint64 // remote target -> local
	BytesUp     uint64
	BytesDown   uint64
	Dropped     uint64 // packets dropped (flow table full, or transport error)
}

UDPStats is a snapshot of a UDPForwarder's counters.

Jump to

Keyboard shortcuts

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