Documentation
¶
Overview ¶
Package transport opens KoKo terminal sessions against a resolved asset.
Two backends exist and both authenticate with a connection token rather than the user's password (DESIGN.md「KoKo 协议」的「认证」):
- SSH: connect to the KoKo port (default 2222) as JMS-{token_id} with token_value as the password. Each command runs on its own channel, so it gets a native exit code and no marker parsing.
- WebSocket: /koko/ws/terminal/ with the jms_sessionid cookie and the JMS-KOKO subprotocol. The shell is a shared PTY, so output is delimited by the __JMSDONE__ / __JMSRC__ marker protocol.
BackendAuto tries SSH first and falls back to WebSocket, because an external endpoint often permits the WebSocket path while the KoKo SSH port is blocked (DESIGN.md「端点故障转移」第 4 点).
Index ¶
Constants ¶
const ( // DefaultSSHPort is the KoKo SSH port. DefaultSSHPort = 2222 // DialTimeout bounds the TCP connect. A DROP-style firewall would // otherwise hang for 75s. DialTimeout = 15 * time.Second // Keepalive is the SSH keepalive interval and the WebSocket // application-level ping interval. Keepalive = 30 * time.Second // DefaultCols and DefaultRows size the PTY. DefaultCols = 200 DefaultRows = 50 )
Numeric defaults from DESIGN.md「数值基线」
const InteractiveReadTimeout = 30 * time.Second
InteractiveReadTimeout is retained for compatibility with callers that reference the transport package. Interactive reads no longer use a read deadline: Gorilla WebSocket documents every read error as permanent, so a deadline timeout cannot be treated as an idle-loop signal.
const PathConnectionToken = "/api/v1/authentication/connection-token/"
PathConnectionToken creates a connection token.
const PathWSTerminal = "/koko/ws/terminal/"
PathWSTerminal is the KoKo WebSocket terminal endpoint.
/koko/ws/token/ returns 404 on some versions, so the terminal path is the only one used (DESIGN.md「WebSocket 终端」).
const SSHBannerTimeout = 2 * time.Second
SSHBannerTimeout bounds the SSH handshake, including the wait for the server's identification string.
A real SSH server sends its banner immediately after accepting the connection, so two seconds is generous. The value is deliberately far below DialTimeout: the point is to fail fast so the WebSocket fallback can start.
const WSSubprotocol = "JMS-KOKO"
WSSubprotocol is the subprotocol KoKo requires on the handshake.
Variables ¶
var ErrCommandTimeout = errors.New("command timeout")
ErrCommandTimeout reports a command that exceeded the caller's timeout.
The backends return it wrapped, and it is deliberately distinct from the net timeout a *connect* produces: a connect that timed out may succeed on another endpoint or backend, so the caller is free to retry it, but a command that timed out has already been delivered to the remote host. Replaying it is not safe — `rm` and `reboot` are not idempotent — so the classification has to be able to tell the two apart (DESIGN.md「TerminalPool」 第 5 点: a command that produced output is never sent again).
var ErrUnsupported = errors.New("unsupported backend")
ErrUnsupported reports a backend name that is not one of the three above.
Functions ¶
This section is empty.
Types ¶
type BackendType ¶
type BackendType string
BackendType selects a transport backend.
const ( BackendSSH BackendType = "ssh" BackendWS BackendType = "ws" BackendAuto BackendType = "auto" )
Backend kinds. BackendAuto is the default and tries SSH then WebSocket.
func ParseBackend ¶ added in v0.1.5
func ParseBackend(raw string) (BackendType, error)
ParseBackend validates a backend name: "ssh", "ws", "auto", or empty for auto. It exists because two surfaces accept the name from a user — the CLI --backend flag and the JMS_MCP_BACKEND environment variable — and a value one of them rejects must not be silently accepted by the other (DESIGN.md「端点故障转移」第 4 点).
type ConnectOptions ¶
type ConnectOptions struct {
// Backend selects SSH, WebSocket, or auto. The empty value means auto.
Backend BackendType
// SSHPort is the KoKo SSH port; 0 means DefaultSSHPort.
SSHPort int
// Cols and Rows size the PTY; 0 means the defaults.
Cols, Rows int
// Keepalive overrides the keepalive interval; 0 means Keepalive.
Keepalive time.Duration
// Protocol is the connection-token protocol ("ssh" or "sftp"); the
// empty value means "ssh".
Protocol string
// ConnectMethod is the token's connect_method; the empty value means
// "web_cli", which SFTP must override with "web_sftp".
ConnectMethod string
}
ConnectOptions tunes one connection attempt.
type Terminal ¶
type Terminal interface {
// Execute runs one command and returns its output and exit code.
//
// A non-zero remote exit is NOT an error: it is reported in
// Result.ExitCode so the caller can distinguish "the command failed"
// from "the transport failed".
Execute(ctx context.Context, cmd string, timeout time.Duration) (Result, error)
// Backend reports which backend is live.
Backend() BackendType
// Interactive attaches the local console to the remote shell. It
// returns when the remote shell exits.
Interactive(ctx context.Context) error
// Close tears the terminal down. It is safe to call more than once.
Close() error
}
Terminal is one connected KoKo session.
Execute is serialized by the caller (connpool holds a per-terminal lock): a shared PTY cannot interleave two commands, and even the SSH backend's per-channel execution is ordered to keep output attribution simple.
func Connect ¶
func Connect(ctx context.Context, sess *auth.Session, asset assets.Info, opts ConnectOptions) (Terminal, error)
Connect opens a terminal for asset on sess, using the backend selected by opts.
The endpoint is already bound to sess (DESIGN.md「端点故障转移」第 1 点): the KoKo address is derived from sess.BaseURL and opts.SSHPort, never re-resolved here.
type Token ¶
Token is a connection token as returned by POST /api/v1/authentication/connection-token/.
func NewConnectionToken ¶
func NewConnectionToken(ctx context.Context, sess *auth.Session, asset assets.Info, protocol, connectMethod string) (Token, error)
NewConnectionToken creates a connection token for asset.
It is exported because SFTP opens its own token with a different protocol/connect_method pair, and because tests need to observe the request shape.