Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

sgateway

Simple Gateway — a raw TCP relay. It listens on an input URL, dials out to an output URL, and forwards bytes verbatim in both directions. Useful for tunnelling or exposing a backend endpoint through a controlled front.

Architecture

Two sides, each one a C_IOGATE with one channel:

              input_url (listen)                    output_url (connect)
client  ───▶  __input_side__  ──▶  C_SGATEWAY  ──▶  __output_side__  ───▶  upstream
        ◀───                  ◀──             ◀──                    ◀───

One client, one output connection

client connects     -> the output connects (client not read until it is open)
output open         -> what the client had sent goes first, then both ways
client leaves       -> the output is closed
output closes       -> the client is dropped
output cannot connect -> it tries again after timeout_between_connections ms,
                         doubling up to 30 s, while the client waits

What the client sends before its output is open is kept, and goes first when it opens. The client is not read meanwhile, so a client that leaves then is not seen either: what it sent is delivered when its output opens, and then the output is closed. A second client is refused while it waits. A pause or a stop before the output opens drops what was kept, counted in dropMsgs. Bytes for a side that closed (the gateway then closes the other one) are dropped and counted in dropMsgs, with one WARNING each time a side closes (not one per message).

A pause (pause-yuno), or the stop of the yuno, ends the client and its output, and starts no output again. In 7.30.19, with a client that did not read, the end of the output’s stop started it again for the client that was still being closed: a session with the backend while the gateway was paused, and “Destroying a RUNNING gobj” (an ERROR with a stack) at the stop.

When one side ends, the other one is closed at once, and a C_TCP closes by resetting the connection (see The close resets the connection in C_TCP): what that side’s socket had not yet delivered to a slow peer is lost. A backend that answers and closes at once can lose the end of its answer to a slow client.

Back-pressure

The gateway never reads one side faster than the other side writes. After relaying what one side sent, it pauses the reading of that side (rx_paused of its C_TCP), and resumes it at the EV_TX_READY of the side written. A slow peer, or one that does not read at all, slows the other peer down through TCP itself, and the gateway holds one read at most. Up to 7.30.18 the gateway handed over the read buffer itself, and the reading went on: as soon as one side was slower, the buffer still queued was read into again (the data arrived corrupted after the first megabytes), every next message was refused with an ERROR, and against a side that never reads the log grew by megabytes per second.

TLS

The url scheme selects TLS (tcps://). The output takes its TLS configuration from the crypto attribute. By default, it verifies the backend’s certificate against the system CA. For a private CA:

{
    "global": {
        "sgateway.output_url": "tcps://192.0.2.10:2012",
        "sgateway.crypto": {
            "ssl_trusted_certificate": "/yuneta/store/certs/backend-ca.pem",
            "ssl_server_name": "backend.example.com"
        }
    }
}

crypto replaces the default whole: repeat the verifying keys you want. Up to 7.30.18 the gateway had no crypto, and a TLS output was refused at each attempt (“TLS client refused”, an ERROR per second).

The input takes its TLS configuration from input_crypto, which the gateway writes as the crypto of its C_TCP_S at each play. It has no default: a tcps:// input needs its certificate and key.

{
    "global": {
        "sgateway.input_url": "tcps://0.0.0.0:2011",
        "sgateway.input_crypto": {
            "ssl_certificate": "/yuneta/store/certs/gateway.crt",
            "ssl_certificate_key": "/yuneta/store/certs/gateway.key"
        }
    }
}

Without them the input does not listen and the yuno exits with code 0 (“TLS server without its TLS context: exiting”): serving its input is the gateway’s role. Up to 7.30.19, a TLS input was not configurable at all. A global key C_TCP_S.crypto does not reach the server: it is not a service, and the global config reaches only services.

Configuration

Both URLs are required. They are read at each play: if one is empty, the play is refused with an ERROR (“What yuno input url?”), and the yuno keeps running without playing.

AttributeDefaultPurpose
input_url(required)URL the gateway listens on (for example tcp://0.0.0.0:2011)
output_url(required)URL the gateway connects to, one connection per client (for example tcp://192.0.2.10:2012)
crypto{"ssl_use_system_ca": true, "ssl_verify_mode": "required"}TLS of the output side (tcps://), see TLS
input_crypto{}TLS of the input side (tcps://): its certificate and key, see TLS
timeout_between_connections100ms before the output connects again after a failed attempt. The value doubles at each failure, up to 30 s.
timeout1000Period of the rate stats (ms)

Standalone (no agent), the yuno plays itself with yuno.autoplay:

{
    "yuno": {
        "autoplay": true
    },
    "global": {
        "sgateway.input_url": "tcp://0.0.0.0:2011",
        "sgateway.output_url": "tcp://192.0.2.10:2012"
    }
}
sgateway --config-file=sgateway.json

Under the agent, the same global keys go in the yuno’s configuration. Or write the urls at run time (input_url / output_url are persisted), then pause and play the yuno:

ycommand -c 'command-yuno id=<id> service=__yuno__ command=write-attr attribute=input_url  value=tcp://0.0.0.0:2011'
ycommand -c 'command-yuno id=<id> service=__yuno__ command=write-attr attribute=output_url value=tcp://192.0.2.10:2012'
ycommand -c 'pause-yuno id=<id>'
ycommand -c 'play-yuno id=<id>'

Throughput stats

SDF_RSTATS (reset with stats=__reset__): rxMsgs (received from either side), txMsgs (sent to the other side), dropMsgs (dropped: the other side had closed, or a pause or a stop came before the output of the client opened), and the per-second gauges rxMsgsec / txMsgsec with their high-water marks maxrxMsgsec / maxtxMsgsec (recomputed on each timeout tick).

Commands

CommandDescription
helpCommand help
authzsAuthorization help

The gateway is configured through attributes, not commands. Its sides are services, so the C_IOGATE commands reach them: command-yuno id=<id> service=__input_side__ command=view-channels.

Debugging

GClassLevelShows
C_SGATEWAYmessagesRelayed messages (a dump of each one)
C_SGATEWAYconnectionsSide open / Side closed of each side (INFO, side: input or output)
C_TCPconnections, trafficPer-connection events / raw bytes
C_TCP_Slisten, accepted, not-acceptedListener accept activity

No trace is on by default: a peer that connects again and again does not write the log (up to 7.30.18 C_TCP connections, C_TCP_S listen/ accepted/not-accepted and the INFO Side open / Side closed were on, some 1.4 MB of log per 1000 connections). Enable them with ycommand command-yuno id=<id> service=__yuno__ command=set-gclass-trace gclass=<G> set=1 level=<L>, or in the yuno’s configuration:

{
    "yuno": {
        "trace_levels": {
            "C_SGATEWAY": ["connections"]
        }
    }
}

Test

tests/c/sgateway/ runs the real binary between a listener (its output target) and a client (of its input port). It checks bytes both ways, and one output connection per client (it closes when its client leaves, and a second client gets nothing of the first). It checks that the client is dropped when its output goes, and 8 MB each way against a slow peer, whole and in order. It checks a peer that never reads (the gateway does not grow), and a client that leaves while its output cannot connect (what it sent is delivered when the output opens). It checks a stop with a client that does not read (no output connection opens while it stops). Then it runs 20 clients through a TLS input and a TLS output with the default traces, and a log that does not grow with them. Every log is clean at the orderly stop. See the test suite.