rathole/README.md

215 lines
11 KiB
Markdown
Raw Permalink Normal View History

2021-12-11 05:30:42 -07:00
# rathole
2022-01-25 01:08:04 -07:00
2021-12-18 03:34:26 -07:00
![rathole-logo](./docs/img/rathole-logo.png)
2021-12-11 05:30:42 -07:00
2022-01-17 02:19:23 -07:00
[![GitHub stars](https://img.shields.io/github/stars/rapiz1/rathole)](https://github.com/rapiz1/rathole/stargazers)
2022-01-18 23:14:07 -07:00
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/rapiz1/rathole)](https://github.com/rapiz1/rathole/releases)
2023-03-07 08:07:46 -07:00
![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/rapiz1/rathole/rust.yml?branch=main)
2022-01-18 23:14:07 -07:00
[![GitHub all releases](https://img.shields.io/github/downloads/rapiz1/rathole/total)](https://github.com/rapiz1/rathole/releases)
[![Docker Pulls](https://img.shields.io/docker/pulls/rapiz1/rathole)](https://hub.docker.com/r/rapiz1/rathole)
[![Join the chat at https://gitter.im/rapiz1/rathole](https://badges.gitter.im/rapiz1/rathole.svg)](https://gitter.im/rapiz1/rathole?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[English](README.md) | [简体中文](README-zh.md)
A secure, stable and high-performance reverse proxy for NAT traversal, written in Rust
2021-12-11 05:30:42 -07:00
2021-12-27 00:46:38 -07:00
rathole, like [frp](https://github.com/fatedier/frp) and [ngrok](https://github.com/inconshreveable/ngrok), can help to expose the service on the device behind the NAT to the Internet, via a server with a public IP.
<!-- TOC -->
- [rathole](#rathole)
- [Features](#features)
- [Quickstart](#quickstart)
- [Configuration](#configuration)
- [Logging](#logging)
- [Tuning](#tuning)
- [Benchmark](#benchmark)
2023-03-07 06:51:19 -07:00
- [Planning](#planning)
<!-- /TOC -->
2021-12-27 00:46:38 -07:00
## Features
2021-12-11 05:30:42 -07:00
- **High Performance** Much higher throughput can be achieved than frp, and more stable when handling a large volume of connections. See [Benchmark](#benchmark)
- **Low Resource Consumption** Consumes much fewer memory than similar tools. See [Benchmark](#benchmark). [The binary can be](docs/build-guide.md) **as small as ~500KiB** to fit the constraints of devices, like embedded devices as routers.
- **Security** Tokens of services are mandatory and service-wise. The server and clients are responsible for their own configs. With the optional Noise Protocol, encryption can be configured at ease. No need to create a self-signed certificate! TLS is also supported.
- **Hot Reload** Services can be added or removed dynamically by hot-reloading the configuration file. HTTP API is WIP.
2021-12-24 07:42:04 -07:00
2021-12-11 05:30:42 -07:00
## Quickstart
2022-01-07 07:40:07 -07:00
A full-powered `rathole` can be obtained from the [release](https://github.com/rapiz1/rathole/releases) page. Or [build from source](docs/build-guide.md) **for other platforms and minimizing the binary**. A [Docker image](https://hub.docker.com/r/rapiz1/rathole) is also available.
2022-01-02 01:38:31 -07:00
2022-01-06 10:30:49 -07:00
The usage of `rathole` is very similar to frp. If you have experience with the latter, then the configuration is very easy for you. The only difference is that configuration of a service is split into the client side and the server side, and a token is mandatory.
2022-01-25 01:08:04 -07:00
To use `rathole`, you need a server with a public IP, and a device behind the NAT, where some services that need to be exposed to the Internet.
2021-12-11 05:30:42 -07:00
Assuming you have a NAS at home behind the NAT, and want to expose its ssh service to the Internet:
1. On the server which has a public IP
Create `server.toml` with the following content and accommodate it to your needs.
2022-01-25 01:08:04 -07:00
2021-12-11 05:30:42 -07:00
```toml
# server.toml
[server]
bind_addr = "0.0.0.0:2333" # `2333` specifies the port that rathole listens for clients
2021-12-11 05:30:42 -07:00
[server.services.my_nas_ssh]
token = "use_a_secret_that_only_you_know" # Token that is used to authenticate the client for the service. Change to an arbitrary value.
bind_addr = "0.0.0.0:5202" # `5202` specifies the port that exposes `my_nas_ssh` to the Internet
2021-12-11 05:30:42 -07:00
```
Then run:
2022-01-25 01:08:04 -07:00
2021-12-11 05:30:42 -07:00
```bash
./rathole server.toml
```
2. On the host which is behind the NAT (your NAS)
Create `client.toml` with the following content and accommodate it to your needs.
2022-01-25 01:08:04 -07:00
2021-12-11 05:30:42 -07:00
```toml
# client.toml
2021-12-11 05:30:42 -07:00
[client]
remote_addr = "myserver.com:2333" # The address of the server. The port must be the same with the port in `server.bind_addr`
[client.services.my_nas_ssh]
token = "use_a_secret_that_only_you_know" # Must be the same with the server to pass the validation
2021-12-11 05:30:42 -07:00
local_addr = "127.0.0.1:22" # The address of the service that needs to be forwarded
```
Then run:
2022-01-25 01:08:04 -07:00
2021-12-11 05:30:42 -07:00
```bash
./rathole client.toml
```
3. Now the client will try to connect to the server `myserver.com` on port `2333`, and any traffic to `myserver.com:5202` will be forwarded to the client's port `22`.
So you can `ssh myserver.com:5202` to ssh to your NAS.
2022-01-25 01:08:04 -07:00
To run `rathole` run as a background service on Linux, checkout the [systemd examples](./examples/systemd).
2021-12-11 05:30:42 -07:00
## Configuration
2022-01-25 01:08:04 -07:00
`rathole` can automatically determine to run in the server mode or the client mode, according to the content of the configuration file, if only one of `[server]` and `[client]` block is present, like the example in [Quickstart](#quickstart).
2021-12-11 05:30:42 -07:00
But the `[client]` and `[server]` block can also be put in one file. Then on the server side, run `rathole --server config.toml` and on the client side, run `rathole --client config.toml` to explicitly tell `rathole` the running mode.
2021-12-11 05:30:42 -07:00
Before heading to the full configuration specification, it's recommend to skim [the configuration examples](./examples) to get a feeling of the configuration format.
2021-12-18 03:35:46 -07:00
2022-03-08 21:22:01 -07:00
See [Transport](./docs/transport.md) for more details about encryption and the `transport` block.
2021-12-24 06:57:37 -07:00
2021-12-11 05:30:42 -07:00
Here is the full configuration specification:
2022-01-25 01:08:04 -07:00
2021-12-11 05:30:42 -07:00
```toml
[client]
remote_addr = "example.com:2333" # Necessary. The address of the server
default_token = "default_token_if_not_specify" # Optional. The default token of services, if they don't define their own ones
2022-03-08 21:22:01 -07:00
heartbeat_timeout = 40 # Optional. Set to 0 to disable the application-layer heartbeat test. The value must be greater than `server.heartbeat_interval`. Default: 40 seconds
retry_interval = 1 # Optional. The interval between retry to connect to the server. Default: 1 second
2021-12-11 05:30:42 -07:00
2021-12-24 23:56:23 -07:00
[client.transport] # The whole block is optional. Specify which transport to use
type = "tcp" # Optional. Possible values: ["tcp", "tls", "noise"]. Default: "tcp"
2022-03-08 21:22:01 -07:00
[client.transport.tcp] # Optional. Also affects `noise` and `tls`
proxy = "socks5://user:passwd@127.0.0.1:1080" # Optional. The proxy used to connect to the server. `http` and `socks5` is supported.
nodelay = true # Optional. Determine whether to enable TCP_NODELAY, if applicable, to improve the latency but decrease the bandwidth. Default: true
keepalive_secs = 20 # Optional. Specify `tcp_keepalive_time` in `tcp(7)`, if applicable. Default: 20 seconds
keepalive_interval = 8 # Optional. Specify `tcp_keepalive_intvl` in `tcp(7)`, if applicable. Default: 8 seconds
2021-12-24 06:57:37 -07:00
2021-12-15 00:12:36 -07:00
[client.transport.tls] # Necessary if `type` is "tls"
trusted_root = "ca.pem" # Necessary. The certificate of CA that signed the server's certificate
hostname = "example.com" # Optional. The hostname that the client uses to validate the certificate. If not set, fallback to `client.remote_addr`
2022-01-25 01:08:04 -07:00
[client.transport.noise] # Noise protocol. See `docs/transport.md` for further explanation
2021-12-24 06:57:37 -07:00
pattern = "Noise_NK_25519_ChaChaPoly_BLAKE2s" # Optional. Default value as shown
local_private_key = "key_encoded_in_base64" # Optional
remote_public_key = "key_encoded_in_base64" # Optional
[client.transport.websocket] # Necessary if `type` is "websocket"
tls = true # If `true` then it will use settings in `client.transport.tls`
2021-12-11 05:30:42 -07:00
[client.services.service1] # A service that needs forwarding. The name `service1` can change arbitrarily, as long as identical to the name in the server's configuration
type = "tcp" # Optional. The protocol that needs forwarding. Possible values: ["tcp", "udp"]. Default: "tcp"
2021-12-11 05:30:42 -07:00
token = "whatever" # Necessary if `client.default_token` not set
local_addr = "127.0.0.1:1081" # Necessary. The address of the service that needs to be forwarded
nodelay = true # Optional. Override the `client.transport.nodelay` per service
retry_interval = 1 # Optional. The interval between retry to connect to the server. Default: inherits the global config
2021-12-11 05:30:42 -07:00
[client.services.service2] # Multiple services can be defined
local_addr = "127.0.0.1:1082"
[server]
2022-01-25 01:08:04 -07:00
bind_addr = "0.0.0.0:2333" # Necessary. The address that the server listens for clients. Generally only the port needs to be change.
2021-12-11 05:30:42 -07:00
default_token = "default_token_if_not_specify" # Optional
2022-03-08 21:22:01 -07:00
heartbeat_interval = 30 # Optional. The interval between two application-layer heartbeat. Set to 0 to disable sending heartbeat. Default: 30 seconds
2021-12-11 05:30:42 -07:00
2021-12-24 23:56:23 -07:00
[server.transport] # Same as `[client.transport]`
2022-01-25 01:08:04 -07:00
type = "tcp"
2022-03-08 21:22:01 -07:00
[server.transport.tcp] # Same as the client
nodelay = true
keepalive_secs = 20
keepalive_interval = 8
2021-12-24 06:57:37 -07:00
[server.transport.tls] # Necessary if `type` is "tls"
2021-12-15 00:12:36 -07:00
pkcs12 = "identify.pfx" # Necessary. pkcs12 file of server's certificate and private key
pkcs12_password = "password" # Necessary. Password of the pkcs12 file
2021-12-24 06:57:37 -07:00
[server.transport.noise] # Same as `[client.transport.noise]`
pattern = "Noise_NK_25519_ChaChaPoly_BLAKE2s"
2022-01-25 01:08:04 -07:00
local_private_key = "key_encoded_in_base64"
remote_public_key = "key_encoded_in_base64"
2021-12-24 06:57:37 -07:00
[server.transport.websocket] # Necessary if `type` is "websocket"
tls = true # If `true` then it will use settings in `server.transport.tls`
2021-12-11 05:30:42 -07:00
[server.services.service1] # The service name must be identical to the client side
type = "tcp" # Optional. Same as the client `[client.services.X.type]
token = "whatever" # Necessary if `server.default_token` not set
2022-01-25 01:08:04 -07:00
bind_addr = "0.0.0.0:8081" # Necessary. The address of the service is exposed at. Generally only the port needs to be change.
nodelay = true # Optional. Same as the client
2021-12-11 05:30:42 -07:00
2022-01-25 01:08:04 -07:00
[server.services.service2]
2021-12-11 05:30:42 -07:00
bind_addr = "0.0.0.1:8082"
```
2021-12-19 04:37:24 -07:00
### Logging
2022-01-25 01:08:04 -07:00
`rathole`, like many other Rust programs, use environment variables to control the logging level. `info`, `warn`, `error`, `debug`, `trace` are available.
2021-12-19 04:37:24 -07:00
2022-03-08 21:22:01 -07:00
```shell
2021-12-19 04:37:24 -07:00
RUST_LOG=error ./rathole config.toml
```
2022-01-25 01:08:04 -07:00
2021-12-19 04:37:24 -07:00
will run `rathole` with only error level logging.
If `RUST_LOG` is not present, the default logging level is `info`.
### Tuning
From v0.4.7, rathole enables TCP_NODELAY by default, which should benefit the latency and interactive applications like rdp, Minecraft servers. However, it slightly decreases the bandwidth.
If the bandwidth is more important, TCP_NODELAY can be opted out with `nodelay = false`.
## Benchmark
2021-12-11 05:30:42 -07:00
rathole has similar latency to [frp](https://github.com/fatedier/frp), but can handle a more connections, provide larger bandwidth, with less memory usage.
2021-12-11 05:30:42 -07:00
For more details, see the separate page [Benchmark](./docs/benchmark.md).
2021-12-11 05:30:42 -07:00
2022-01-07 05:45:00 -07:00
**However, don't take it from here that `rathole` can magically make your forwarded service faster several times than before.** The benchmark is done on local loopback, indicating the performance when the task is cpu-bounded. One can gain quite a improvement if the network is not the bottleneck. Unfortunately, that's not true for many users. In that case, the main benefit is lower resource consumption, while the bandwidth and the latency may not improved significantly.
2022-01-07 05:43:09 -07:00
![http_throughput](./docs/img/http_throughput.svg)
2021-12-18 03:34:26 -07:00
![tcp_bitrate](./docs/img/tcp_bitrate.svg)
![udp_bitrate](./docs/img/udp_bitrate.svg)
![mem](./docs/img/mem-graph.png)
2021-12-11 05:30:42 -07:00
2023-03-07 06:51:19 -07:00
## Planning
2021-12-11 05:30:42 -07:00
- [ ] HTTP APIs for configuration
2022-01-05 01:32:36 -07:00
[Out of Scope](./docs/out-of-scope.md) lists features that are not planned to be implemented and why.