# teals

TLS 1.3 server sockets for Elixir that keep TLS 1.3 half-close, for Ranch and
Cowboy, backed by [rustls](https://github.com/rustls/rustls) in a NIF.

In TLS 1.3 a peer's `close_notify` ends only its own direction. OTP `:ssl`
closes both directions when it receives one
([erlang/otp#12296](https://github.com/erlang/otp/issues/12296)), so a server
cannot reply after a client has finished sending. That breaks protocols that
rely on half-close, such as the Docker CLI's attached streams and file
transfer tunnels. teals keeps the write side open, and fits under Cowboy as a
Ranch transport.

Only TLS 1.3 is offered. The design, requirements and tasks are in
[`openspec/changes/add-half-close-tls-server`](https://github.com/upmaru/teals/tree/main/openspec/changes/add-half-close-tls-server).

## Installation

```elixir
def deps do
  [
    {:teals, "~> 0.1"},
    # For Teals.Ranch and Teals.Cowboy.StreamHandler:
    {:cowboy, "~> 2.12"}
  ]
end
```

The NIF comes precompiled for `aarch64-apple-darwin`,
`x86_64-unknown-linux-gnu` and `aarch64-unknown-linux-gnu` (glibc 2.34 or
later), downloaded from this repository's GitHub release and checked against
the checksums in the Hex package. On other targets, or with `TEALS_BUILD=1`,
it builds from source, which needs Rust 1.91 or later and the optional
`{:rustler, "~> 0.38"}` dependency.

## Usage

```elixir
{:ok, config} = Teals.config(certfile: "server.pem", keyfile: "key.pem")

:ranch.start_listener(:api, Teals.Ranch,
  %{socket_opts: [port: 8443, config: config]},
  :cowboy_clear, %{
    env: %{dispatch: dispatch},
    stream_handlers: [Teals.Cowboy.StreamHandler, :cowboy_stream_h]
  })
```

`Teals` documents the socket API, half-close and active mode; `Teals.Ranch`
and `Teals.Cowboy.StreamHandler` document the listener side.

## Development

Tools are pinned with [mise](https://mise.jdx.dev) (Erlang, Elixir, Go for the
interoperability tests' client, Node for OpenSpec) and `rust-toolchain.toml`
(Rust, through rustup).

```bash
mise trust && mise install
mise run setup
mise run check
```

Developing this library builds the NIF from source. Building fetches the
locked Rust crates from crates.io unless they are already in your Cargo cache.
The tests also build a Go TLS client from `test/go_client`, which uses only
Go's standard library; `mix test --exclude go` skips its cases. Tests that
drive the `incus` or Docker CLI, each with scratch configuration in a temporary
directory, run only when that CLI is on the `PATH`.

The load tests are excluded from `mise run check`; `mise run load` runs them
alone. They hold `TEALS_LOAD_CONNECTIONS` connections (default 1000) to
measure memory per connection, make that many handshakes with each server key
type, and run mixed exchanges for `TEALS_LOAD_SECONDS` (default 30). The Go
client opens as many sockets as the server, so the file descriptor limit
(`ulimit -n`) must allow both.

## License

Apache-2.0. See [LICENSE](https://github.com/upmaru/teals/blob/main/LICENSE) and [NOTICE](https://github.com/upmaru/teals/blob/main/NOTICE).
