FRP内网穿透
1.什么是frp
frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。
2.frp能做什么?
通过在具有公网 IP 的节点上部署 frp 服务端,可以轻松地将内网服务穿透到公网,同时提供诸多专业的功能特性,这包括:
客户端服务端通信支持 TCP、KCP 以及 Websocket 等多种协议。
采用 TCP 连接流式复用,在单个连接间承载更多请求,节省连接建立时间。
代理组间的负载均衡。
端口复用,多个服务通过同一个服务端端口暴露。
多个原生支持的客户端插件(静态文件查看,HTTP、SOCK5 代理等),便于独立使用 frp 客户端完成某些工作。
高度扩展性的服务端插件系统,方便结合自身需求进行功能扩展。
服务端和客户端 UI 页面。
3.安装
下载地址https://github.com/fatedier/frp/releases
4.如何使用?
启动方式
#linux服务端启动,可根据实际需要进行后台运行
./frps -c frps.ini
#windows客户端启动
frpc -c frpc.ini
1. 通过 SSH 访问内网机器
在具有公网 IP 的机器上部署 frps,修改 frps.ini 文件,这里使用了最简化的配置,设置了 frp 服务器用户接收客户端连接的端口:
[common]
bind_port = 7000在需要被访问的内网机器上(SSH 服务通常监听在 22 端口)部署 frpc,修改 frpc.ini 文件,假设 frps 所在服务器的公网 IP 为 x.x.x.x:
[common]
server_addr = x.x.x.x
server_port = 7000
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000local_ip
和local_port
配置为本地需要暴露到公网的服务地址和端口。remote_port
表示在 frp 服务端监听的端口,访问此端口的流量将会被转发到本地服务对应的端口
2.通过自定义域名访问内网的 Web 服务
修改 frps.ini 文件,设置监听 HTTP 请求端口为 8080:
[common]
bind_port = 7000
vhost_http_port = 8080修改 frpc.ini 文件,假设 frps 所在的服务器的 IP 为 x.x.x.x,
local_port
为本地机器上 Web 服务监听的端口, 绑定自定义域名为custom_domains
。[common]
server_addr = x.x.x.x
server_port = 7000
[web]
type = http
local_port = 80
custom_domains = www.yourdomain.com
[web2]
type = http
local_port = 8080
custom_domains = www.yourdomain2.com分别启动 frps 和 frpc。
将
www.yourdomain.com
和www.yourdomain2.com
的域名 A 记录解析到 IPx.x.x.x
,如果服务器已经有对应的域名,也可以将 CNAME 记录解析到服务器原先的域名。或者可以通过修改 HTTP 请求的 Host 字段来实现同样的效果。通过浏览器访问
http://www.yourdomain.com:8080
即可访问到处于内网机器上 80 端口的服务,访问http://www.yourdomain2.com:8080
则访问到内网机器上 8080 端口的服务。
3. 更多示例参考官网https://gofrp.org/docs/examples/vhost-http/
4. 其他配置
平时在使用frp时,需要配置链接验证,web监控等
服务端
[common]
bind_port = 7004 #绑定的端口
dashboard_port = 7500 #绑定的web访问端口 ip+port
token = 12345678 #客户端链接服务端的token
dashboard_user = zhangsan #web监控访问用户名
dashboard_pwd = zhangsan #web监控访问密码
vhost_http_port = 7005 #http监听端口
客户端
[common]
server_addr = xxxxxx #服务端真实ip地址(可访问的公网服务器)
server_port = 7004 #绑定的端口 对应服务端端口
token = 12345678 # 链接token
[ssh] #当前配置名称(命名)
type = tcp #链接类型
local_ip = 127.0.0.1 #本地ip地址
local_port = 27017 #本地服务端口
remote_port = 7003 #使用的服务器端口
[mongo]
type = http
local_port = 27017
custom_domains = 域名
5. 服务端参考配置
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
bind_addr | string | 服务端监听地址 | 0.0.0.0 | ||
bind_port | int | 服务端监听端口 | 7000 | 接收 frpc 的连接 | |
bind_udp_port | int | 服务端监听 UDP 端口 | 0 | 用于辅助创建 P2P 连接 | |
kcp_bind_port | int | 服务端监听 KCP 协议端口 | 0 | 用于接收采用 KCP 连接的 frpc | |
proxy_bind_addr | string | 代理监听地址 | 同 bind_addr | 可以使代理监听在不同的网卡地址 | |
log_file | string | 日志文件地址 | ./frps.log | 如果设置为 console,会将日志打印在标准输出中 | |
log_level | string | 日志等级 | info | trace, debug, info, warn, error | |
log_max_days | int | 日志文件保留天数 | 3 | ||
disable_log_color | bool | 禁用标准输出中的日志颜色 | false | ||
detailed_errors_to_client | bool | 服务端返回详细错误信息给客户端 | true | ||
tcp_mux_keepalive_interval | int | tcp_mux 的心跳检查间隔时间 | 60 | 单位:秒 | |
tcp_keepalive | int | 和客户端底层 TCP 连接的 keepalive 间隔时间,单位秒 | 7200 | 负数不启用 | |
heartbeat_timeout | int | 服务端和客户端心跳连接的超时时间 | 90 | 单位:秒 | |
user_conn_timeout | int | 用户建立连接后等待客户端响应的超时时间 | 10 | 单位:秒 | |
udp_packet_size | int | 代理 UDP 服务时支持的最大包长度 | 1500 | 服务端和客户端的值需要一致 | |
tls_cert_file | string | TLS 服务端证书文件路径 | |||
tls_key_file | string | TLS 服务端密钥文件路径 | |||
tls_trusted_ca_file | string | TLS CA 证书路径 |
权限验证
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
authentication_method | string | 鉴权方式 | token | token, oidc | |
authenticate_heartbeats | bool | 开启心跳消息鉴权 | false | ||
authenticate_new_work_conns | bool | 开启建立工作连接的鉴权 | false | ||
token | string | 鉴权使用的 token 值 | 客户端需要设置一样的值才能鉴权通过 | ||
oidc_issuer | string | oidc_issuer | |||
oidc_audience | string | oidc_audience | |||
oidc_skip_expiry_check | bool | oidc_skip_expiry_check | |||
oidc_skip_issuer_check | bool | oidc_skip_issuer_check |
管理配置
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
allow_ports | string | 允许代理绑定的服务端端口 | 格式为 1000-2000,2001,3000-4000 | ||
max_pool_count | int | 最大连接池大小 | 5 | ||
max_ports_per_client | int | 限制单个客户端最大同时存在的代理数 | 0 | 0 表示没有限制 | |
tls_only | bool | 只接受启用了 TLS 的客户端连接 | false |
Dashboard, 监控
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
dashboard_addr | string | 启用 Dashboard 监听的本地地址 | 0.0.0.0 | ||
dashboard_port | int | 启用 Dashboard 监听的本地端口 | 0 | ||
dashboard_user | string | HTTP BasicAuth 用户名 | |||
dashboard_pwd | string | HTTP BasicAuth 密码 | |||
enable_prometheus | bool | 是否提供 Prometheus 监控接口 | false | 需要同时启用了 Dashboard 才会生效 | |
asserts_dir | string | 静态资源目录 | Dashboard 使用的资源默认打包在二进制文件中,通过指定此参数使用自定义的静态资源 | ||
pprof_enable | bool | 启动 Go HTTP pprof | false | 用于应用调试 |
HTTP & HTTPS
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
vhost_http_port | int | 为 HTTP 类型代理监听的端口 | 0 | 启用后才支持 HTTP 类型的代理,默认不启用 | |
vhost_https_port | int | 为 HTTPS 类型代理监听的端口 | 0 | 启用后才支持 HTTPS 类型的代理,默认不启用 | |
vhost_http_timeout | int | HTTP 类型代理在服务端的 ResponseHeader 超时时间 | 60 | ||
subdomain_host | string | 二级域名后缀 | |||
custom_404_page | string | 自定义 404 错误页面地址 |
TCPMUX
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
tcpmux_httpconnect_port | int | 为 TCPMUX 类型代理监听的端口 | 0 | 启用后才支持 TCPMUX 类型的代理,默认不启用 | |
tcpmux_passthrough | bool | 是否透传 CONNECT 请求 | false | 通常在本地服务是 HTTP Proxy 时使用 |
6.客户端参考配置
基础配置
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
server_addr | string | 连接服务端的地址 | 0.0.0.0 | ||
server_port | int | 连接服务端的端口 | 7000 | ||
connect_server_local_ip | string | 连接服务端时所绑定的本地 IP | |||
dial_server_timeout | int | 连接服务端的超时时间 | 10 | ||
dial_server_keepalive | int | 和服务端底层 TCP 连接的 keepalive 间隔时间,单位秒 | 7200 | 负数不启用 | |
http_proxy | string | 连接服务端使用的代理地址 | 格式为 {protocol}://user:passwd@192.168.1.128:8080 protocol 目前支持 http、socks5、ntlm | ||
log_file | string | 日志文件地址 | ./frpc.log | 如果设置为 console,会将日志打印在标准输出中 | |
log_level | string | 日志等级 | info | trace, debug, info, warn, error | |
log_max_days | int | 日志文件保留天数 | 3 | ||
disable_log_color | bool | 禁用标准输出中的日志颜色 | false | ||
pool_count | int | 连接池大小 | 0 | ||
user | string | 用户名 | 设置此参数后,代理名称会被修改为 {user}.{proxyName},避免代理名称和其他用户冲突 | ||
dns_server | string | 使用 DNS 服务器地址 | 默认使用系统配置的 DNS 服务器,指定此参数可以强制替换为自定义的 DNS 服务器地址 | ||
login_fail_exit | bool | 第一次登陆失败后是否退出 | true | ||
protocol | string | 连接服务端的通信协议 | tcp | tcp, kcp, websocket | |
tls_enable | bool | 启用 TLS 协议加密连接 | false | ||
tls_cert_file | string | TLS 客户端证书文件路径 | |||
tls_key_file | string | TLS 客户端密钥文件路径 | |||
tls_trusted_ca_file | string | TLS CA 证书路径 | |||
tls_server_name | string | TLS Server 名称 | 为空则使用 server_addr | ||
disable_custom_tls_first_byte | bool | TLS 不发送 0x17 | false | 当为 true 时,不能端口复用 | |
tcp_mux_keepalive_interval | int | tcp_mux 的心跳检查间隔时间 | 60 | 单位:秒 | |
heartbeat_interval | int | 向服务端发送心跳包的间隔时间 | 30 | 建议启用 tcp_mux_keepalive_interval,将此值设置为 -1 | |
heartbeat_timeout | int | 和服务端心跳的超时时间 | 90 | ||
udp_packet_size | int | 代理 UDP 服务时支持的最大包长度 | 1500 | 服务端和客户端的值需要一致 | |
start | string | 指定启用部分代理 | 当配置了较多代理,但是只希望启用其中部分时可以通过此参数指定,默认为全部启用 | ||
meta_xxx | map | 附加元数据 | 会传递给服务端插件,提供附加能力 |
权限验证
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
authentication_method | string | 鉴权方式 | token | token, oidc | 需要和服务端一致 |
authenticate_heartbeats | bool | 开启心跳消息鉴权 | false | 需要和服务端一致 | |
authenticate_new_work_conns | bool | 开启建立工作连接的鉴权 | false | 需要和服务端一致 | |
token | string | 鉴权使用的 token 值 | 需要和服务端设置一样的值才能鉴权通过 | ||
oidc_client_id | string | oidc_client_id | |||
oidc_client_secret | string | oidc_client_secret | |||
oidc_audience | string | oidc_audience | |||
oidc_token_endpoint_url | string | oidc_token_endpoint_url | |||
oidc_additional_xxx | map | OIDC 附加参数 | map 结构,key 需要以 oidc_additional_ 开头 |
UI
参数 | 类型 | 说明 | 默认值 | 可选值 | 备注 |
---|---|---|---|---|---|
admin_addr | string | 启用 AdminUI 监听的本地地址 | 0.0.0.0 | ||
admin_port | int | 启用 AdminUI 监听的本地端口 | 0 | ||
admin_user | string | HTTP BasicAuth 用户名 | |||
admin_pwd | string | HTTP BasicAuth 密码 | |||
asserts_dir | string | 静态资源目录 | AdminUI 使用的资源默认打包在二进制文件中,通过指定此参数使用自定义的静态资源 | ||
pprof_enable | bool | 启动 Go HTTP pprof | false | 用于应用调试 |
- 本文标签: Linux 内网穿透
- 本文链接: https://www.tianyajuanke.top/article/40
- 版权声明: 本文由吴沛芙原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权