README.md 29.7 KB
Newer Older
A
Asher Oto 已提交
1

F
fatedier 已提交
2
# frp
3

F
fatedier 已提交
4
[![Build Status](https://circleci.com/gh/fatedier/frp.svg?style=shield)](https://circleci.com/gh/fatedier/frp)
F
fatedier 已提交
5
[![GitHub release](https://img.shields.io/github/tag/fatedier/frp.svg?label=release)](https://github.com/fatedier/frp/releases)
6

F
fatedier 已提交
7 8
[README](README.md) | [中文文档](README_zh.md)

F
fatedier 已提交
9 10
## What is frp?

W
Weisi Dai 已提交
11
frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the Internet. As of now, it supports **TCP** and **UDP**, as well as **HTTP** and **HTTPS** protocols, where requests can be forwarded to internal services by domain name.
F
fatedier 已提交
12

W
Weisi Dai 已提交
13
frp also has a P2P connect mode.
F
fatedier 已提交
14

F
fatedier 已提交
15
## Table of Contents
F
fatedier 已提交
16

F
fatedier 已提交
17
<!-- vim-markdown-toc GFM -->
F
fatedier 已提交
18

W
Weisi Dai 已提交
19
* [Development Status](#development-status)
F
fatedier 已提交
20
* [Architecture](#architecture)
E
Eric Larssen 已提交
21
* [Example Usage](#example-usage)
F
fatedier 已提交
22
    * [Access your computer in LAN by SSH](#access-your-computer-in-lan-by-ssh)
F
fatedier 已提交
23 24
    * [Visit your web service in LAN by custom domains](#visit-your-web-service-in-lan-by-custom-domains)
    * [Forward DNS query request](#forward-dns-query-request)
W
Weisi Dai 已提交
25 26
    * [Forward Unix domain socket](#forward-unix-domain-socket)
    * [Expose a simple HTTP file server](#expose-a-simple-http-file-server)
F
fatedier 已提交
27
    * [Enable HTTPS for local HTTP(S) service](#enable-https-for-local-https-service)
W
Weisi Dai 已提交
28
    * [Expose your service privately](#expose-your-service-privately)
F
fatedier 已提交
29
    * [P2P Mode](#p2p-mode)
F
fatedier 已提交
30
* [Features](#features)
W
Weisi Dai 已提交
31 32
    * [Configuration Files](#configuration-files)
    * [Using Environment Variables](#using-environment-variables)
F
fatedier 已提交
33
    * [Split Configures Into Different Files](#split-configures-into-different-files)
F
fatedier 已提交
34
    * [Dashboard](#dashboard)
F
fatedier 已提交
35
    * [Admin UI](#admin-ui)
F
fatedier 已提交
36 37
    * [Monitor](#monitor)
        * [Prometheus](#prometheus)
W
Weisi Dai 已提交
38
    * [Authenticating the Client](#authenticating-the-client)
F
fatedier 已提交
39 40
        * [Token Authentication](#token-authentication)
        * [OIDC Authentication](#oidc-authentication)
F
fatedier 已提交
41
    * [Encryption and Compression](#encryption-and-compression)
F
fatedier 已提交
42
        * [TLS](#tls)
W
Weisi Dai 已提交
43
    * [Hot-Reloading frpc configuration](#hot-reloading-frpc-configuration)
F
fatedier 已提交
44
    * [Get proxy status from client](#get-proxy-status-from-client)
W
Weisi Dai 已提交
45
    * [Only allowing certain ports on the server](#only-allowing-certain-ports-on-the-server)
F
fatedier 已提交
46
    * [Port Reuse](#port-reuse)
F
fatedier 已提交
47 48
    * [Bandwidth Limit](#bandwidth-limit)
        * [For Each Proxy](#for-each-proxy)
F
fatedier 已提交
49
    * [TCP Stream Multiplexing](#tcp-stream-multiplexing)
F
fatedier 已提交
50
    * [Support KCP Protocol](#support-kcp-protocol)
W
Weisi Dai 已提交
51
    * [Connection Pooling](#connection-pooling)
F
fatedier 已提交
52
    * [Load balancing](#load-balancing)
W
Weisi Dai 已提交
53 54 55
    * [Service Health Check](#service-health-check)
    * [Rewriting the HTTP Host Header](#rewriting-the-http-host-header)
    * [Setting other HTTP Headers](#setting-other-http-headers)
F
fatedier 已提交
56
    * [Get Real IP](#get-real-ip)
F
fatedier 已提交
57 58
        * [HTTP X-Forwarded-For](#http-x-forwarded-for)
        * [Proxy Protocol](#proxy-protocol)
F
fatedier 已提交
59 60 61 62
    * [Require HTTP Basic Auth (Password) for Web Services](#require-http-basic-auth-password-for-web-services)
    * [Custom Subdomain Names](#custom-subdomain-names)
    * [URL Routing](#url-routing)
    * [TCP Port Multiplexing](#tcp-port-multiplexing)
W
Weisi Dai 已提交
63
    * [Connecting to frps via HTTP PROXY](#connecting-to-frps-via-http-proxy)
F
fatedier 已提交
64
    * [Range ports mapping](#range-ports-mapping)
F
fatedier 已提交
65 66
    * [Client Plugins](#client-plugins)
    * [Server Manage Plugins](#server-manage-plugins)
F
fatedier 已提交
67 68
* [Development Plan](#development-plan)
* [Contributing](#contributing)
F
fatedier 已提交
69
* [Donation](#donation)
F
fatedier 已提交
70
    * [AliPay](#alipay)
F
fatedier 已提交
71
    * [Wechat Pay](#wechat-pay)
A
Asher Oto 已提交
72
    * [PayPal](#paypal)
F
fatedier 已提交
73

F
fatedier 已提交
74 75
<!-- vim-markdown-toc -->

W
Weisi Dai 已提交
76
## Development Status
F
fatedier 已提交
77

W
Weisi Dai 已提交
78
frp is under development. Try the latest release version in the `master` branch, or use the `dev` branch for the version in development.
F
fatedier 已提交
79

W
Weisi Dai 已提交
80
**The protocol might change at a release and we don't promise backwards compatibility. Please check the release log when upgrading the client and the server.**
F
fatedier 已提交
81

F
fatedier 已提交
82
## Architecture
F
fatedier 已提交
83

F
fatedier 已提交
84
![architecture](/doc/pic/architecture.png)
F
fatedier 已提交
85

F
fatedier 已提交
86
## Example Usage
F
fatedier 已提交
87

W
Weisi Dai 已提交
88
Firstly, download the latest programs from [Release](https://github.com/fatedier/frp/releases) page according to your operating system and architecture.
F
fatedier 已提交
89

W
Weisi Dai 已提交
90
Put `frps` and `frps.ini` onto your server A with public IP.
F
fatedier 已提交
91

W
Weisi Dai 已提交
92
Put `frpc` and `frpc.ini` onto your server B in LAN (that can't be connected from public Internet).
F
fatedier 已提交
93

F
fatedier 已提交
94
### Access your computer in LAN by SSH
F
fatedier 已提交
95

96
1. Modify `frps.ini` on server A and set the `bind_port` to be connected to frp clients:
F
fatedier 已提交
97 98 99 100 101 102 103

  ```ini
  # frps.ini
  [common]
  bind_port = 7000
  ```

W
Weisi Dai 已提交
104
2. Start `frps` on server A:
F
fatedier 已提交
105 106 107

  `./frps -c ./frps.ini`

W
Weisi Dai 已提交
108
3. On server B, modify `frpc.ini` to put in your `frps` server public IP as `server_addr` field:
F
fatedier 已提交
109 110 111 112 113 114 115 116

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [ssh]
F
fatedier 已提交
117 118
  type = tcp
  local_ip = 127.0.0.1
F
fatedier 已提交
119
  local_port = 22
F
fatedier 已提交
120
  remote_port = 6000
F
fatedier 已提交
121 122
  ```

123 124
Note that `local_port` (listened on client) and `remote_port` (exposed on server) are for traffic goes in/out the frp system, whereas `server_port` is used between frps.

W
Weisi Dai 已提交
125
4. Start `frpc` on server B:
F
fatedier 已提交
126 127 128

  `./frpc -c ./frpc.ini`

W
Weisi Dai 已提交
129
5. From another machine, SSH to server B like this (assuming that username is `test`):
F
fatedier 已提交
130 131 132

  `ssh -oPort=6000 test@x.x.x.x`

F
fatedier 已提交
133
### Visit your web service in LAN by custom domains
F
fatedier 已提交
134

W
Weisi Dai 已提交
135
Sometimes we want to expose a local web service behind a NAT network to others for testing with your own domain name and unfortunately we can't resolve a domain name to a local IP.
F
fatedier 已提交
136

W
Weisi Dai 已提交
137
However, we can expose an HTTP(S) service using frp.
F
fatedier 已提交
138

W
Weisi Dai 已提交
139
1. Modify `frps.ini`, set the vhost HTTP port to 8080:
F
fatedier 已提交
140 141 142 143 144 145 146 147

  ```ini
  # frps.ini
  [common]
  bind_port = 7000
  vhost_http_port = 8080
  ```

W
Weisi Dai 已提交
148
2. Start `frps`:
F
fatedier 已提交
149 150 151

  `./frps -c ./frps.ini`

W
Weisi Dai 已提交
152
3. Modify `frpc.ini` and set `server_addr` to the IP address of the remote frps server. The `local_port` is the port of your web service:
F
fatedier 已提交
153 154 155 156 157 158 159 160 161 162

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [web]
  type = http
  local_port = 80
W
Weisi Dai 已提交
163
  custom_domains = www.example.com
F
fatedier 已提交
164 165
  ```

W
Weisi Dai 已提交
166
4. Start `frpc`:
F
fatedier 已提交
167 168 169

  `./frpc -c ./frpc.ini`

W
Weisi Dai 已提交
170
5. Resolve A record of `www.example.com` to the public IP of the remote frps server or CNAME record to your origin domain.
F
fatedier 已提交
171

W
Weisi Dai 已提交
172
6. Now visit your local web service using url `http://www.example.com:8080`.
F
fatedier 已提交
173

F
fatedier 已提交
174 175
### Forward DNS query request

W
Weisi Dai 已提交
176
1. Modify `frps.ini`:
F
fatedier 已提交
177 178 179 180 181 182 183

  ```ini
  # frps.ini
  [common]
  bind_port = 7000
  ```

W
Weisi Dai 已提交
184
2. Start `frps`:
F
fatedier 已提交
185 186 187

  `./frps -c ./frps.ini`

W
Weisi Dai 已提交
188
3. Modify `frpc.ini` and set `server_addr` to the IP address of the remote frps server, forward DNS query request to Google Public DNS server `8.8.8.8:53`:
F
fatedier 已提交
189 190 191 192 193 194 195 196 197 198 199

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [dns]
  type = udp
  local_ip = 8.8.8.8
  local_port = 53
F
fatedier 已提交
200
  remote_port = 6000
F
fatedier 已提交
201 202 203 204 205 206
  ```

4. Start frpc:

  `./frpc -c ./frpc.ini`

W
Weisi Dai 已提交
207
5. Test DNS resolution using `dig` command:
F
fatedier 已提交
208

H
Hyduan 已提交
209
  `dig @x.x.x.x -p 6000 www.google.com`
F
fatedier 已提交
210

W
Weisi Dai 已提交
211
### Forward Unix domain socket
F
fatedier 已提交
212

W
Weisi Dai 已提交
213
Expose a Unix domain socket (e.g. the Docker daemon socket) as TCP.
F
fatedier 已提交
214

W
Weisi Dai 已提交
215
Configure `frps` same as above.
F
fatedier 已提交
216

W
Weisi Dai 已提交
217
1. Start `frpc` with configuration:
F
fatedier 已提交
218 219

  ```ini
F
fatedier 已提交
220
  # frpc.ini
F
fatedier 已提交
221
  [common]
F
fatedier 已提交
222 223 224 225 226 227 228 229
  server_addr = x.x.x.x
  server_port = 7000

  [unix_domain_socket]
  type = tcp
  remote_port = 6000
  plugin = unix_domain_socket
  plugin_unix_path = /var/run/docker.sock
F
fatedier 已提交
230 231
  ```

W
Weisi Dai 已提交
232
2. Test: Get Docker version using `curl`:
F
fatedier 已提交
233

F
fatedier 已提交
234 235
  `curl http://x.x.x.x:6000/version`

W
Weisi Dai 已提交
236
### Expose a simple HTTP file server
F
fatedier 已提交
237

W
Weisi Dai 已提交
238
Browser your files stored in the LAN, from public Internet.
F
fatedier 已提交
239

W
Weisi Dai 已提交
240
Configure `frps` same as above.
F
fatedier 已提交
241

W
Weisi Dai 已提交
242
1. Start `frpc` with configuration:
F
fatedier 已提交
243 244 245 246 247 248 249 250 251 252 253

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [test_static_file]
  type = tcp
  remote_port = 6000
  plugin = static_file
W
Weisi Dai 已提交
254
  plugin_local_path = /tmp/files
F
fatedier 已提交
255 256 257 258 259
  plugin_strip_prefix = static
  plugin_http_user = abc
  plugin_http_passwd = abc
  ```

W
Weisi Dai 已提交
260
2. Visit `http://x.x.x.x:6000/static/` from your browser and specify correct user and password to view files in `/tmp/files` on the `frpc` machine.
F
fatedier 已提交
261

262 263 264
### Enable HTTPS for local HTTP(S) service

You may substitute `https2https` for the plugin, and point the `plugin_local_addr` to a HTTPS endpoint.
F
fatedier 已提交
265

W
Weisi Dai 已提交
266
1. Start `frpc` with configuration:
F
fatedier 已提交
267 268 269 270 271 272 273

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

W
Weisi Dai 已提交
274
  [test_https2http]
F
fatedier 已提交
275
  type = https
W
Weisi Dai 已提交
276
  custom_domains = test.example.com
F
fatedier 已提交
277 278 279 280 281 282

  plugin = https2http
  plugin_local_addr = 127.0.0.1:80
  plugin_crt_path = ./server.crt
  plugin_key_path = ./server.key
  plugin_host_header_rewrite = 127.0.0.1
Z
zhouwenfeng 已提交
283
  plugin_header_X-From-Where = frp
F
fatedier 已提交
284 285
  ```

W
Weisi Dai 已提交
286
2. Visit `https://test.example.com`.
F
fatedier 已提交
287

W
Weisi Dai 已提交
288
### Expose your service privately
F
fatedier 已提交
289

W
Weisi Dai 已提交
290
Some services will be at risk if exposed directly to the public network. With **STCP** (secret TCP) mode, a preshared key is needed to access the service from another client.
F
fatedier 已提交
291

W
Weisi Dai 已提交
292
Configure `frps` same as above.
F
fatedier 已提交
293

W
Weisi Dai 已提交
294
1. Start `frpc` on machine B with the following config. This example is for exposing the SSH service (port 22), and note the `sk` field for the preshared key, and that the `remote_port` field is removed here:
F
fatedier 已提交
295 296 297 298 299 300 301

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

F
fatedier 已提交
302 303 304 305 306
  [secret_ssh]
  type = stcp
  sk = abcdefg
  local_ip = 127.0.0.1
  local_port = 22
F
fatedier 已提交
307 308
  ```

W
Weisi Dai 已提交
309
2. Start another `frpc` (typically on another machine C) with the following config to access the SSH service with a security key (`sk` field):
F
fatedier 已提交
310

F
fatedier 已提交
311 312 313 314 315 316
  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

F
fatedier 已提交
317
  [secret_ssh_visitor]
F
fatedier 已提交
318
  type = stcp
F
fatedier 已提交
319
  role = visitor
F
fatedier 已提交
320 321 322 323 324
  server_name = secret_ssh
  sk = abcdefg
  bind_addr = 127.0.0.1
  bind_port = 6000
  ```
F
fatedier 已提交
325

W
Weisi Dai 已提交
326
3. On machine C, connect to SSH on machine B, using this command:
F
fatedier 已提交
327

W
Weisi Dai 已提交
328
  `ssh -oPort=6000 127.0.0.1`
F
fatedier 已提交
329

F
fatedier 已提交
330 331
### P2P Mode

W
Weisi Dai 已提交
332
**xtcp** is designed for transmitting large amounts of data directly between clients. A frps server is still needed, as P2P here only refers the actual data transmission.
F
fatedier 已提交
333

W
Weisi Dai 已提交
334
Note it can't penetrate all types of NAT devices. You might want to fallback to **stcp** if **xtcp** doesn't work.
F
fatedier 已提交
335

W
Weisi Dai 已提交
336
1. In `frps.ini` configure a UDP port for xtcp:
F
fatedier 已提交
337 338

  ```ini
W
Weisi Dai 已提交
339
  # frps.ini
F
fatedier 已提交
340 341 342
  bind_udp_port = 7001
  ```

W
Weisi Dai 已提交
343
2. Start `frpc` on machine B, expose the SSH port. Note that `remote_port` field is removed:
F
fatedier 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [p2p_ssh]
  type = xtcp
  sk = abcdefg
  local_ip = 127.0.0.1
  local_port = 22
  ```

W
Weisi Dai 已提交
358
3. Start another `frpc` (typically on another machine C) with the config to connect to SSH using P2P mode:
F
fatedier 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
  server_port = 7000

  [p2p_ssh_visitor]
  type = xtcp
  role = visitor
  server_name = p2p_ssh
  sk = abcdefg
  bind_addr = 127.0.0.1
  bind_port = 6000
  ```

W
Weisi Dai 已提交
375
4. On machine C, connect to SSH on machine B, using this command:
F
fatedier 已提交
376

W
Weisi Dai 已提交
377
  `ssh -oPort=6000 127.0.0.1`
F
fatedier 已提交
378

F
fatedier 已提交
379
## Features
F
fatedier 已提交
380

W
Weisi Dai 已提交
381
### Configuration Files
F
fatedier 已提交
382

W
Weisi Dai 已提交
383
Read the full example configuration files to find out even more features not described here.
F
fatedier 已提交
384

W
Weisi Dai 已提交
385
[Full configuration file for frps (Server)](./conf/frps_full.ini)
F
fatedier 已提交
386

W
Weisi Dai 已提交
387
[Full configuration file for frpc (Client)](./conf/frpc_full.ini)
F
fatedier 已提交
388

W
Weisi Dai 已提交
389
### Using Environment Variables
F
fatedier 已提交
390

W
Weisi Dai 已提交
391
Environment variables can be referenced in the configuration file, using Go's standard format:
F
fatedier 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405

```ini
# frpc.ini
[common]
server_addr = {{ .Envs.FRP_SERVER_ADDR }}
server_port = 7000

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = {{ .Envs.FRP_SSH_REMOTE_PORT }}
```

W
Weisi Dai 已提交
406
With the config above, variables can be passed into `frpc` program like this:
F
fatedier 已提交
407 408 409 410 411 412 413

```
export FRP_SERVER_ADDR="x.x.x.x"
export FRP_SSH_REMOTE_PORT="6000"
./frpc -c ./frpc.ini
```

W
Weisi Dai 已提交
414
`frpc` will render configuration file template using OS environment variables. Remember to prefix your reference with `.Envs`.
F
fatedier 已提交
415

F
fatedier 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
### Split Configures Into Different Files

You can split multiple proxy configs into different files and include them in the main file.

```ini
# frpc.ini
[common]
server_addr = x.x.x.x
server_port = 7000
includes=./confd/*.ini
```

```ini
# ./confd/test.ini
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
```

F
fatedier 已提交
437 438
### Dashboard

G
Guy Lewin 已提交
439
Check frp's status and proxies' statistics information by Dashboard.
F
fatedier 已提交
440 441 442 443 444 445

Configure a port for dashboard to enable this feature:

```ini
[common]
dashboard_port = 7500
446
# dashboard's username and password are both optional
F
fatedier 已提交
447 448
dashboard_user = admin
dashboard_pwd = admin
F
fatedier 已提交
449 450
```

451
Then visit `http://[server_addr]:7500` to see the dashboard, with username and password both being `admin`.
F
fatedier 已提交
452 453 454

![dashboard](/doc/pic/dashboard.png)

F
fatedier 已提交
455 456
### Admin UI

W
Weisi Dai 已提交
457
The Admin UI helps you check and manage frpc's configuration.
F
fatedier 已提交
458

G
Guy Lewin 已提交
459
Configure an address for admin UI to enable this feature:
F
fatedier 已提交
460 461 462 463 464 465 466 467 468

```ini
[common]
admin_addr = 127.0.0.1
admin_port = 7400
admin_user = admin
admin_pwd = admin
```

469
Then visit `http://127.0.0.1:7400` to see admin UI, with username and password both being `admin`.
F
fatedier 已提交
470

F
fatedier 已提交
471 472 473 474 475 476 477 478 479 480 481 482
### Monitor

When dashboard is enabled, frps will save monitor data in cache. It will be cleared after process restart.

Prometheus is also supported.

#### Prometheus

Enable dashboard first, then configure `enable_prometheus = true` in `frps.ini`.

`http://{dashboard_addr}/metrics` will provide prometheus monitor data.

W
Weisi Dai 已提交
483
### Authenticating the Client
F
fatedier 已提交
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
There are 2 authentication methods to authenticate frpc with frps. 

You can decide which one to use by configuring `authentication_method` under `[common]` in `frpc.ini` and `frps.ini`.

Configuring `authenticate_heartbeats = true` under `[common]` will use the configured authentication method to add and validate authentication on every heartbeat between frpc and frps.

Configuring `authenticate_new_work_conns = true` under `[common]` will do the same for every new work connection between frpc and frps.

#### Token Authentication

When specifying `authentication_method = token` under `[common]` in `frpc.ini` and `frps.ini` - token based authentication will be used.

Make sure to specify the same `token` in the `[common]` section in `frps.ini` and `frpc.ini` for frpc to pass frps validation

#### OIDC Authentication

When specifying `authentication_method = oidc` under `[common]` in `frpc.ini` and `frps.ini` - OIDC based authentication will be used.

OIDC stands for OpenID Connect, and the flow used is called [Client Credentials Grant](https://tools.ietf.org/html/rfc6749#section-4.4).

To use this authentication type - configure `frpc.ini` and `frps.ini` as follows:

```ini
# frps.ini
[common]
authentication_method = oidc
oidc_issuer = https://example-oidc-issuer.com/
oidc_audience = https://oidc-audience.com/.default
```

```ini
# frpc.ini
[common]
authentication_method = oidc
oidc_client_id = 98692467-37de-409a-9fac-bb2585826f18 # Replace with OIDC client ID
oidc_client_secret = oidc_secret
oidc_audience = https://oidc-audience.com/.default
oidc_token_endpoint_url = https://example-oidc-endpoint.com/oauth2/v2.0/token
```
F
fatedier 已提交
524 525 526

### Encryption and Compression

W
Weisi Dai 已提交
527
The features are off by default. You can turn on encryption and/or compression:
F
fatedier 已提交
528 529 530 531 532

```ini
# frpc.ini
[ssh]
type = tcp
F
fatedier 已提交
533 534
local_port = 22
remote_port = 6000
F
fatedier 已提交
535
use_encryption = true
F
fatedier 已提交
536
use_compression = true
F
fatedier 已提交
537 538
```

F
fatedier 已提交
539 540
#### TLS

W
Weisi Dai 已提交
541
frp supports the TLS protocol between `frpc` and `frps` since v0.25.0.
F
fatedier 已提交
542

W
Weisi Dai 已提交
543
For port multiplexing, frp sends a first byte `0x17` to dial a TLS connection.
F
fatedier 已提交
544

A
Asher Oto 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
Configure `tls_enable = true` in the `[common]` section to `frpc.ini` to enable this feature.

To **enforce** `frps` to only accept TLS connections - configure `tls_only = true` in the `[common]` section in `frps.ini`. **This is optional.**

**`frpc` TLS settings (under the `[common]` section):**
```ini
tls_enable = true
tls_cert_file = certificate.crt
tls_key_file = certificate.key
tls_trusted_ca_file = ca.crt
```

**`frps` TLS settings (under the `[common]` section):**
```ini
tls_only = true
tls_enable = true
tls_cert_file = certificate.crt
tls_key_file = certificate.key
tls_trusted_ca_file = ca.crt
```

You will need **a root CA cert** and **at least one SSL/TLS certificate**. It **can** be self-signed or regular (such as Let's Encrypt or another SSL/TLS certificate provider).

If you using `frp` via IP address and not hostname, make sure to set the appropriate IP address in the Subject Alternative Name (SAN) area when generating SSL/TLS Certificates.
G
Guy Lewin 已提交
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
Given an example:

* Prepare openssl config file. It exists at `/etc/pki/tls/openssl.cnf` in Linux System and `/System/Library/OpenSSL/openssl.cnf` in MacOS, and you can copy it to current path, like `cp /etc/pki/tls/openssl.cnf ./my-openssl.cnf`. If not, you can build it by yourself, like:
```
cat > my-openssl.cnf << EOF
[ ca ]
default_ca = CA_default
[ CA_default ]
x509_extensions = usr_cert
[ req ]
default_bits        = 2048
default_md          = sha256
default_keyfile     = privkey.pem
distinguished_name  = req_distinguished_name
attributes          = req_attributes
x509_extensions     = v3_ca
string_mask         = utf8only
[ req_distinguished_name ]
[ req_attributes ]
[ usr_cert ]
basicConstraints       = CA:FALSE
nsComment              = "OpenSSL Generated Certificate"
subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid,issuer
[ v3_ca ]
subjectKeyIdentifier   = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints       = CA:true
EOF
```

* build ca certificates:
```
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=example.ca.com" -days 5000 -out ca.crt
```

* build frps certificates:
```
openssl genrsa -out server.key 2048

openssl req -new -sha256 -key server.key \
    -subj "/C=XX/ST=DEFAULT/L=DEFAULT/O=DEFAULT/CN=server.com" \
    -reqexts SAN \
    -config <(cat my-openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:localhost,IP:127.0.0.1,DNS:example.server.com")) \
    -out server.csr

openssl x509 -req -days 365 \
	-in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
	-extfile <(printf "subjectAltName=DNS:localhost,IP:127.0.0.1,DNS:example.server.com") \
	-out server.crt
```

* build frpc certificates:
```
openssl genrsa -out client.key 2048
openssl req -new -sha256 -key client.key \
    -subj "/C=XX/ST=DEFAULT/L=DEFAULT/O=DEFAULT/CN=client.com" \
    -reqexts SAN \
    -config <(cat my-openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:client.com,DNS:example.client.com")) \
    -out client.csr

openssl x509 -req -days 365 \
    -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
	-extfile <(printf "subjectAltName=DNS:client.com,DNS:example.client.com") \
	-out client.crt
```

W
Weisi Dai 已提交
638
### Hot-Reloading frpc configuration
F
fatedier 已提交
639

W
Weisi Dai 已提交
640
The `admin_addr` and `admin_port` fields are required for enabling HTTP API:
F
fatedier 已提交
641 642 643 644 645 646 647 648

```ini
# frpc.ini
[common]
admin_addr = 127.0.0.1
admin_port = 7400
```

F
fatedier 已提交
649
Then run command `frpc reload -c ./frpc.ini` and wait for about 10 seconds to let `frpc` create or update or remove proxies.
F
fatedier 已提交
650

G
Guy Lewin 已提交
651
**Note that parameters in [common] section won't be modified except 'start'.**
F
fatedier 已提交
652

F
fatedier 已提交
653 654
You can run command `frpc verify -c ./frpc.ini` before reloading to check if there are config errors.

F
fatedier 已提交
655 656
### Get proxy status from client

W
Weisi Dai 已提交
657
Use `frpc status -c ./frpc.ini` to get status of all proxies. The `admin_addr` and `admin_port` fields are required for enabling HTTP API.
F
fatedier 已提交
658

W
Weisi Dai 已提交
659
### Only allowing certain ports on the server
F
fatedier 已提交
660

W
Weisi Dai 已提交
661
`allow_ports` in `frps.ini` is used to avoid abuse of ports:
F
fatedier 已提交
662 663 664 665

```ini
# frps.ini
[common]
F
fatedier 已提交
666
allow_ports = 2000-3000,3001,3003,4000-50000
F
fatedier 已提交
667 668
```

W
Weisi Dai 已提交
669
`allow_ports` consists of specific ports or port ranges (lowest port number, dash `-`, highest port number), separated by comma `,`.
F
fatedier 已提交
670

F
fatedier 已提交
671 672
### Port Reuse

G
Guy Lewin 已提交
673
`vhost_http_port` and `vhost_https_port` in frps can use same port with `bind_port`. frps will detect the connection's protocol and handle it correspondingly.
F
fatedier 已提交
674 675 676

We would like to try to allow multiple proxies bind a same remote port with different protocols in the future.

F
fatedier 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
### Bandwidth Limit

#### For Each Proxy

```ini
# frpc.ini
[ssh]
type = tcp
local_port = 22
remote_port = 6000
bandwidth_limit = 1MB
```

Set `bandwidth_limit` in each proxy's configure to enable this feature. Supported units are `MB` and `KB`.

F
fatedier 已提交
692 693
### TCP Stream Multiplexing

W
Weisi Dai 已提交
694
frp supports tcp stream multiplexing since v0.10.0 like HTTP2 Multiplexing, in which case all logic connections to the same frpc are multiplexed into the same TCP connection.
F
fatedier 已提交
695

W
Weisi Dai 已提交
696
You can disable this feature by modify `frps.ini` and `frpc.ini`:
F
fatedier 已提交
697 698 699 700 701 702 703

```ini
# frps.ini and frpc.ini, must be same
[common]
tcp_mux = false
```

F
fatedier 已提交
704 705 706 707
### Support KCP Protocol

KCP is a fast and reliable protocol that can achieve the transmission effect of a reduction of the average latency by 30% to 40% and reduction of the maximum delay by a factor of three, at the cost of 10% to 20% more bandwidth wasted than TCP.

W
Weisi Dai 已提交
708
KCP mode uses UDP as the underlying transport. Using KCP in frp:
F
fatedier 已提交
709

W
Weisi Dai 已提交
710
1. Enable KCP in frps:
F
fatedier 已提交
711 712 713 714 715

  ```ini
  # frps.ini
  [common]
  bind_port = 7000
W
Weisi Dai 已提交
716
  # Specify a UDP port for KCP.
F
fatedier 已提交
717 718 719
  kcp_bind_port = 7000
  ```

W
Weisi Dai 已提交
720 721 722
  The `kcp_bind_port` number can be the same number as `bind_port`, since `bind_port` field specifies a TCP port.

2. Configure `frpc.ini` to use KCP to connect to frps:
F
fatedier 已提交
723 724 725 726 727

  ```ini
  # frpc.ini
  [common]
  server_addr = x.x.x.x
W
Weisi Dai 已提交
728
  # Same as the 'kcp_bind_port' in frps.ini
F
fatedier 已提交
729 730 731 732
  server_port = 7000
  protocol = kcp
  ```

W
Weisi Dai 已提交
733
### Connection Pooling
F
fatedier 已提交
734

W
Weisi Dai 已提交
735
By default, frps creates a new frpc connection to the backend service upon a user request. With connection pooling, frps keeps a certain number of pre-established connections, reducing the time needed to establish a connection.
F
fatedier 已提交
736

W
Weisi Dai 已提交
737
This feature is suitable for a large number of short connections.
F
fatedier 已提交
738

W
Weisi Dai 已提交
739
1. Configure the limit of pool count each proxy can use in `frps.ini`:
F
fatedier 已提交
740

F
fatedier 已提交
741
  ```ini
F
fatedier 已提交
742 743
  # frps.ini
  [common]
F
fatedier 已提交
744
  max_pool_count = 5
F
fatedier 已提交
745 746 747 748
  ```

2. Enable and specify the number of connection pool:

E
Eric Larssen 已提交
749
  ```ini
F
fatedier 已提交
750
  # frpc.ini
F
fatedier 已提交
751 752
  [common]
  pool_count = 1
F
fatedier 已提交
753 754
  ```

F
fatedier 已提交
755 756 757
### Load balancing

Load balancing is supported by `group`.
F
fatedier 已提交
758

F
fatedier 已提交
759
This feature is only available for types `tcp`, `http`, `tcpmux` now.
F
fatedier 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

```ini
# frpc.ini
[test1]
type = tcp
local_port = 8080
remote_port = 80
group = web
group_key = 123

[test2]
type = tcp
local_port = 8081
remote_port = 80
group = web
group_key = 123
```

`group_key` is used for authentication.

W
Weisi Dai 已提交
780
Connections to port 80 will be dispatched to proxies in the same group randomly.
F
fatedier 已提交
781

W
Weisi Dai 已提交
782
For type `tcp`, `remote_port` in the same group should be the same.
F
fatedier 已提交
783

W
Weisi Dai 已提交
784
For type `http`, `custom_domains`, `subdomain`, `locations` should be the same.
F
fatedier 已提交
785

W
Weisi Dai 已提交
786
### Service Health Check
F
fatedier 已提交
787 788 789

Health check feature can help you achieve high availability with load balancing.

W
Weisi Dai 已提交
790
Add `health_check_type = tcp` or `health_check_type = http` to enable health check.
F
fatedier 已提交
791

W
Weisi Dai 已提交
792
With health check type **tcp**, the service port will be pinged (TCPing):
F
fatedier 已提交
793 794 795 796 797 798 799

```ini
# frpc.ini
[test1]
type = tcp
local_port = 22
remote_port = 6000
W
Weisi Dai 已提交
800
# Enable TCP health check
F
fatedier 已提交
801
health_check_type = tcp
W
Weisi Dai 已提交
802
# TCPing timeout seconds
F
fatedier 已提交
803
health_check_timeout_s = 3
W
Weisi Dai 已提交
804
# If health check failed 3 times in a row, the proxy will be removed from frps
F
fatedier 已提交
805
health_check_max_failed = 3
W
Weisi Dai 已提交
806
# A health check every 10 seconds
F
fatedier 已提交
807 808 809
health_check_interval_s = 10
```

W
Weisi Dai 已提交
810 811
With health check type **http**, an HTTP request will be sent to the service and an HTTP 2xx OK response is expected:

F
fatedier 已提交
812 813 814 815 816 817
```ini
# frpc.ini
[web]
type = http
local_ip = 127.0.0.1
local_port = 80
W
Weisi Dai 已提交
818 819
custom_domains = test.example.com
# Enable HTTP health check
F
fatedier 已提交
820
health_check_type = http
W
Weisi Dai 已提交
821 822
# frpc will send a GET request to '/status'
# and expect an HTTP 2xx OK response
F
fatedier 已提交
823 824
health_check_url = /status
health_check_timeout_s = 3
W
Weisi Dai 已提交
825 826
health_check_max_failed = 3
health_check_interval_s = 10
F
fatedier 已提交
827 828
```

W
Weisi Dai 已提交
829
### Rewriting the HTTP Host Header
F
fatedier 已提交
830

W
Weisi Dai 已提交
831 832 833
By default frp does not modify the tunneled HTTP requests at all as it's a byte-for-byte copy.

However, speaking of web servers and HTTP requests, your web server might rely on the `Host` HTTP header to determine the website to be accessed. frp can rewrite the `Host` header when forwarding the HTTP requests, with the `host_header_rewrite` field:
F
fatedier 已提交
834

F
fatedier 已提交
835 836
```ini
# frpc.ini
E
Eric Larssen 已提交
837
[web]
F
fatedier 已提交
838 839
type = http
local_port = 80
W
Weisi Dai 已提交
840 841
custom_domains = test.example.com
host_header_rewrite = dev.example.com
F
fatedier 已提交
842 843
```

W
Weisi Dai 已提交
844
The HTTP request will have the the `Host` header rewritten to `Host: dev.example.com` when it reaches the actual web server, although the request from the browser probably has `Host: test.example.com`.
F
fatedier 已提交
845

W
Weisi Dai 已提交
846
### Setting other HTTP Headers
F
fatedier 已提交
847

W
Weisi Dai 已提交
848
Similar to `Host`, You can override other HTTP request headers with proxy type `http`.
F
fatedier 已提交
849 850 851 852 853 854

```ini
# frpc.ini
[web]
type = http
local_port = 80
W
Weisi Dai 已提交
855 856
custom_domains = test.example.com
host_header_rewrite = dev.example.com
F
fatedier 已提交
857 858 859
header_X-From-Where = frp
```

W
Weisi Dai 已提交
860 861 862
Note that parameter(s) prefixed with `header_` will be added to HTTP request headers.

In this example, it will set header `X-From-Where: frp` in the HTTP request.
F
fatedier 已提交
863

F
fatedier 已提交
864 865
### Get Real IP

F
fatedier 已提交
866 867
#### HTTP X-Forwarded-For

W
Weisi Dai 已提交
868
This feature is for http proxy only.
F
fatedier 已提交
869

W
Weisi Dai 已提交
870
You can get user's real IP from HTTP request headers `X-Forwarded-For` and `X-Real-IP`.
F
fatedier 已提交
871 872 873

#### Proxy Protocol

W
Weisi Dai 已提交
874
frp supports Proxy Protocol to send user's real IP to local services. It support all types except UDP.
F
fatedier 已提交
875 876 877 878 879 880 881 882

Here is an example for https service:

```ini
# frpc.ini
[web]
type = https
local_port = 443
W
Weisi Dai 已提交
883
custom_domains = test.example.com
F
fatedier 已提交
884

W
Weisi Dai 已提交
885
# now v1 and v2 are supported
F
fatedier 已提交
886 887 888
proxy_protocol_version = v2
```

W
Weisi Dai 已提交
889
You can enable Proxy Protocol support in nginx to expose user's real IP in HTTP header `X-Real-IP`, and then read `X-Real-IP` header in your web service for the real IP.
F
fatedier 已提交
890

891
### Require HTTP Basic Auth (Password) for Web Services
F
fatedier 已提交
892 893 894

Anyone who can guess your tunnel URL can access your local web server unless you protect it with a password.

W
Weisi Dai 已提交
895
This enforces HTTP Basic Auth on all requests with the username and password specified in frpc's configure file.
F
fatedier 已提交
896

F
fatedier 已提交
897
It can only be enabled when proxy type is http.
F
fatedier 已提交
898 899 900 901 902 903

```ini
# frpc.ini
[web]
type = http
local_port = 80
W
Weisi Dai 已提交
904
custom_domains = test.example.com
F
fatedier 已提交
905
http_user = abc
906
http_pwd = abc
F
fatedier 已提交
907 908
```

W
Weisi Dai 已提交
909
Visit `http://test.example.com` in the browser and now you are prompted to enter the username and password.
F
fatedier 已提交
910

911
### Custom Subdomain Names
F
fatedier 已提交
912

W
Weisi Dai 已提交
913
It is convenient to use `subdomain` configure for http and https types when many people share one frps server.
F
fatedier 已提交
914 915 916 917 918 919

```ini
# frps.ini
subdomain_host = frps.com
```

W
Weisi Dai 已提交
920
Resolve `*.frps.com` to the frps server's IP. This is usually called a Wildcard DNS record.
F
fatedier 已提交
921 922 923 924 925 926 927 928 929

```ini
# frpc.ini
[web]
type = http
local_port = 80
subdomain = test
```

W
Weisi Dai 已提交
930
Now you can visit your web service on `test.frps.com`.
F
fatedier 已提交
931 932 933

Note that if `subdomain_host` is not empty, `custom_domains` should not be the subdomain of `subdomain_host`.

934
### URL Routing
F
fatedier 已提交
935

W
Weisi Dai 已提交
936
frp supports forwarding HTTP requests to different backend web services by url routing.
F
fatedier 已提交
937

W
Weisi Dai 已提交
938
`locations` specifies the prefix of URL used for routing. frps first searches for the most specific prefix location given by literal strings regardless of the listed order.
F
fatedier 已提交
939 940 941 942 943 944

```ini
# frpc.ini
[web01]
type = http
local_port = 80
W
Weisi Dai 已提交
945
custom_domains = web.example.com
F
fatedier 已提交
946 947 948 949 950
locations = /

[web02]
type = http
local_port = 81
W
Weisi Dai 已提交
951
custom_domains = web.example.com
F
fatedier 已提交
952 953 954
locations = /news,/about
```

W
Weisi Dai 已提交
955 956
HTTP requests with URL prefix `/news` or `/about` will be forwarded to **web02** and other requests to **web01**.

F
fatedier 已提交
957
### TCP Port Multiplexing
958 959 960

frp supports receiving TCP sockets directed to different proxies on a single port on frps, similar to `vhost_http_port` and `vhost_https_port`.

F
fatedier 已提交
961
The only supported TCP port multiplexing method available at the moment is `httpconnect` - HTTP CONNECT tunnel.
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

When setting `tcpmux_httpconnect_port` to anything other than 0 in frps under `[common]`, frps will listen on this port for HTTP CONNECT requests.

The host of the HTTP CONNECT request will be used to match the proxy in frps. Proxy hosts can be configured in frpc by configuring `custom_domain` and / or `subdomain` under `type = tcpmux` proxies, when `multiplexer = httpconnect`.

For example:

```ini
# frps.ini
[common]
bind_port = 7000
tcpmux_httpconnect_port = 1337
```

```ini
# frpc.ini
[common]
server_addr = x.x.x.x
server_port = 7000

[proxy1]
type = tcpmux
multiplexer = httpconnect
custom_domains = test1

[proxy2]
type = tcpmux
multiplexer = httpconnect
custom_domains = test2
```

In the above configuration - frps can be contacted on port 1337 with a HTTP CONNECT header such as:

```
CONNECT test1 HTTP/1.1\r\n\r\n
```
and the connection will be routed to `proxy1`.

W
Weisi Dai 已提交
1000
### Connecting to frps via HTTP PROXY
F
fatedier 已提交
1001

W
Weisi Dai 已提交
1002
frpc can connect to frps using HTTP proxy if you set OS environment variable `HTTP_PROXY`, or if `http_proxy` is set in frpc.ini file.
F
fatedier 已提交
1003

F
fatedier 已提交
1004 1005
It only works when protocol is tcp.

F
fatedier 已提交
1006 1007
```ini
# frpc.ini
F
fatedier 已提交
1008
[common]
F
fatedier 已提交
1009 1010 1011 1012 1013
server_addr = x.x.x.x
server_port = 7000
http_proxy = http://user:pwd@192.168.1.128:8080
```

F
fatedier 已提交
1014 1015
### Range ports mapping

W
Weisi Dai 已提交
1016
Proxy with names that start with `range:` will support mapping range ports.
F
fatedier 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

```ini
# frpc.ini
[range:test_tcp]
type = tcp
local_ip = 127.0.0.1
local_port = 6000-6006,6007
remote_port = 6000-6006,6007
```

W
Weisi Dai 已提交
1027
frpc will generate 8 proxies like `test_tcp_0`, `test_tcp_1`, ..., `test_tcp_7`.
F
fatedier 已提交
1028

F
fatedier 已提交
1029
### Client Plugins
F
fatedier 已提交
1030

W
Weisi Dai 已提交
1031
frpc only forwards requests to local TCP or UDP ports by default.
F
fatedier 已提交
1032

F
fatedier 已提交
1033
Plugins are used for providing rich features. There are built-in plugins such as `unix_domain_socket`, `http_proxy`, `socks5`, `static_file`, `http2https`, `https2http`, `https2https` and you can see [example usage](#example-usage).
F
fatedier 已提交
1034

W
Weisi Dai 已提交
1035
Specify which plugin to use with the `plugin` parameter. Configuration parameters of plugin should be started with `plugin_`. `local_ip` and `local_port` are not used for plugin.
F
fatedier 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

Using plugin **http_proxy**:

```ini
# frpc.ini
[http_proxy]
type = tcp
remote_port = 6000
plugin = http_proxy
plugin_http_user = abc
plugin_http_passwd = abc
```

`plugin_http_user` and `plugin_http_passwd` are configuration parameters used in `http_proxy` plugin.

F
fatedier 已提交
1051 1052 1053 1054
### Server Manage Plugins

Read the [document](/doc/server_plugin.md).

F
fatedier 已提交
1055 1056
Find more plugins in [gofrp/plugin](https://github.com/gofrp/plugin).

F
fatedier 已提交
1057 1058
## Development Plan

W
Weisi Dai 已提交
1059
* Log HTTP request information in frps.
F
fatedier 已提交
1060

1061 1062
## Contributing

F
fatedier 已提交
1063
Interested in getting involved? We would like to help you!
1064

F
fatedier 已提交
1065 1066
* Take a look at our [issues list](https://github.com/fatedier/frp/issues) and consider sending a Pull Request to **dev branch**.
* If you want to add a new feature, please create an issue first to describe the new feature, as well as the implementation approach. Once a proposal is accepted, create an implementation of the new features and submit it as a pull request.
G
Guy Lewin 已提交
1067 1068
* Sorry for my poor English. Improvements for this document are welcome, even some typo fixes.
* If you have great ideas, send an email to fatedier@gmail.com.
F
fatedier 已提交
1069

G
Guy Lewin 已提交
1070
**Note: We prefer you to give your advise in [issues](https://github.com/fatedier/frp/issues), so others with a same question can search it quickly and we don't need to answer them repeatedly.**
F
fatedier 已提交
1071

F
fatedier 已提交
1072 1073
## Donation

G
Guy Lewin 已提交
1074
If frp helps you a lot, you can support us by:
F
fatedier 已提交
1075

F
fatedier 已提交
1076 1077
frp QQ group: 606194980

F
fatedier 已提交
1078 1079 1080 1081
### AliPay

![donation-alipay](/doc/pic/donate-alipay.png)

F
fatedier 已提交
1082 1083 1084 1085
### Wechat Pay

![donation-wechatpay](/doc/pic/donate-wechatpay.png)

A
Asher Oto 已提交
1086
### PayPal
F
fatedier 已提交
1087

A
Asher Oto 已提交
1088
Donate money by [PayPal](https://www.paypal.me/fatedier) to my account **fatedier@gmail.com**.