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
- Variables
- func Dial(ctx context.Context, conn *peer.Conn, network, addr string) (net.Conn, error)
- type Forwarder
- type Spec
- type Stats
- type UDPForwarder
- func (f *UDPForwarder) Addr() net.Addr
- func (f *UDPForwarder) Close() error
- func (f *UDPForwarder) Datagrams() bool
- func (f *UDPForwarder) Done() <-chan struct{}
- func (f *UDPForwarder) Err() error
- func (f *UDPForwarder) Spec() Spec
- func (f *UDPForwarder) Stats() UDPStats
- func (f *UDPForwarder) Target() string
- type UDPStats
Constants ¶
const DefaultHost = "127.0.0.1"
DefaultHost is the bind address and target host used when a spec omits them.
Variables ¶
var ErrSessionClosed = errors.New("session closed")
ErrSessionClosed is the Err of a Forwarder whose session ended.
Functions ¶
Types ¶
type Forwarder ¶
type Forwarder struct {
// contains filtered or unexported fields
}
Forwarder is a running local or reverse forward.
func Listen ¶
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 ¶
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 ¶
Addr is the address being listened on: on the client for a local forward, on the host for a reverse one.
func (*Forwarder) Close ¶
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 ¶
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.
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 ¶
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.
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 ¶
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.