使用FRP 0.58版本进行内网穿透的详细教程

什么是FRP?

FRP(Fast Reverse Proxy)是一款高性能的反向代理应用,主要用于内网穿透。通过FRP,您可以将内网服务暴露给外网用户,无需进行复杂的网络配置。

准备工作

  1. 服务器:一台具备公网IP的服务器,用于部署FRP服务端。
  2. 客户端:需要进行内网穿透的设备。
  3. FRP程序 :可以从FRP官方GitHub页面下载最新版本。

部署步骤

  1. 服务器 A (具有公共 IP 地址)

    • 解压并放置 frps 二进制文件和 frps.toml 配置文件。

    • 修改 frps.toml:

      toml 复制代码
      [common]
      bind_port = 7000
    • 启动 frps:

      sh 复制代码
      ./frps -c ./frps.toml
  2. 服务器 B (在无法从公共互联网直接访问的服务器上)

    • 解压并放置 frpc 二进制文件和 frpc.toml 配置文件。

    • 修改 frpc.toml:

      toml 复制代码
      [common]
      server_addr = "x.x.x.x"
      server_port = 7000
      
      [ssh]
      type = "tcp"
      local_ip = "127.0.0.1"
      local_port = 22
      remote_port = 6000
    • 启动 frpc:

      sh 复制代码
      ./frpc -c ./frpc.toml

通过 SSH 访问服务器 B

从另一台机器通过服务器 A 访问服务器 B (假设用户名是 test):

sh 复制代码
ssh -oPort=6000 test@x.x.x.x

通过多个 SSH 服务共享同一端口

配置 frps.toml:

toml 复制代码
[common]
bind_port = 7000
tcpmux_httpconnect_port = 5002

配置内部机器 A 和 B 的 frpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[ssh1]
type = "tcpmux"
multiplexer = "httpconnect"
custom_domains = ["machine-a.example.com"]
local_ip = "127.0.0.1"
local_port = 22

[ssh2]
type = "tcpmux"
multiplexer = "httpconnect"
custom_domains = ["machine-b.example.com"]
local_ip = "127.0.0.1"
local_port = 22

通过 SSH ProxyCommand 访问:

sh 复制代码
ssh -o 'proxycommand socat - PROXY:x.x.x.x:%h:%p,proxyport=5002' test@machine-a.example.com

使用自定义域访问内部 Web 服务

配置 frps.toml:

toml 复制代码
[common]
bind_port = 7000
vhost_http_port = 8080

配置 frpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[web]
type = "http"
local_port = 80
custom_domains = ["www.example.com"]

在浏览器中访问 http://www.example.com:8080

转发 DNS 查询请求

配置 frps.toml:

toml 复制代码
[common]
bind_port = 7000

配置 frpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[dns]
type = "udp"
local_ip = "8.8.8.8"
local_port = 53
remote_port = 6000

测试 DNS 解析:

sh 复制代码
dig @x.x.x.x -p 6000 www.google.com

转发 Unix 域套接字

配置 frps.tomlfrpc.toml:

toml 复制代码
[common]
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"

测试配置:

sh 复制代码
curl http://x.x.x.x:6000/version

公开简单的 HTTP 文件服务器

配置 frps.tomlfrpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[test_static_file]
type = "tcp"
remote_port = 6000
plugin = "static_file"
plugin_local_path = "/tmp/files"
plugin_strip_prefix = "static"
plugin_http_user = "abc"
plugin_http_passwd = "abc"

从浏览器访问 http://x.x.x.x:6000/static/

为本地 HTTP(S) 服务启用 HTTPS

配置 frpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[test_https2http]
type = "https"
custom_domains = ["test.example.com"]
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"
plugin_request_headers = ["x-from-where: frp"]

访问 https://test.example.com

使用 STCP 模式

配置 frps.tomlfrpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[secret_ssh]
type = "stcp"
secretkey = "abcdefg"
local_ip = "127.0.0.1"
local_port = 22

在另一台机器上启动 frpc:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[secret_ssh_visitor]
type = "stcp"
server_name = "secret_ssh"
secretkey = "abcdefg"
bind_addr = "127.0.0.1"
bind_port = 6000

使用 SSH 连接:

sh 复制代码
ssh -oPort=6000 127.0.0.1

使用 P2P 模式

配置 frps.tomlfrpc.toml:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[p2p_ssh]
type = "xtcp"
secretkey = "abcdefg"
local_ip = "127.0.0.1"
local_port = 22

在另一台机器上启动 frpc:

toml 复制代码
[common]
server_addr = "x.x.x.x"
server_port = 7000

[p2p_ssh_visitor]
type = "xtcp"
server_name = "p2p_ssh"
secretkey = "abcdefg"
bind_addr = "127.0.0.1"
bind_port = 6000
keep_tunnel_open = false

使用 SSH 连接:

sh 复制代码
ssh -oPort=6000 127.0.0.1

配置文件和环境变量

您可以使用 TOML、YAML 或 JSON 格式的配置文件。环境变量可以在配置文件中引用,采用 Go 的标准格式:

toml 复制代码
# frpc.toml
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 }}"

设置环境变量:

sh 复制代码
export FRP_SERVER_ADDR=x.x.x.x
export FRP_SSH_REMOTE_PORT=6000
./frpc -c ./frpc.toml

服务器仪表板

配置 frps.toml:

toml 复制代码
[common]
web_server_addr = "0.0.0.0"
web_server_port = 7500
web_server_user = "admin"
web_server_pwd = "admin"

访问 http://[server_addr]:7500 查看仪表板。

客户端管理界面

配置 frpc.toml:

toml 复制代码
[common]
web_server_addr = "127.0.0.1"
web_server_port = 7400
web_server_user = "admin"
web_server_pwd = "admin"

访问 http://127.0.0.1:7400 查看管理界面。

热加载配置

启用 HTTP API:

toml 复制代码
[common]
web_server_addr = "127.0.0.1"
web_server_port = 7400

使用命令重新加载配置:

sh 复制代码
frpc reload -c ./frpc.toml

验证客户端

使用令牌认证:

toml 复制代码
# frps.toml
[common]
auth_token = "abcdefg"

# frpc.toml
[common]
auth_token = "abcdefg"

使用 OIDC 身份验证:

toml 复制代码
# frps.toml
[common]
auth_method = "oidc"
auth_oidc_issuer = "https://example-oidc-issuer.com/"
auth_oidc_audience = "https://oidc-audience.com/.default"

# frpc.toml
[common]
auth_method = "oidc"
auth_oidc_client_id =

 "my-client-id"
auth_oidc_client_secret = "my-client-secret"
auth_oidc_audience = "https://oidc-audience.com/.default"

获取 JWT:

sh 复制代码
export FRP_OIDC_TOKEN=$(curl -X POST -d 'client_id=my-client-id' -d 'client_secret=my-client-secret' -d 'audience=https://oidc-audience.com/.default' https://example-oidc-issuer.com/token | jq -r .access_token)

配置持久化存储

启用持久化存储:

toml 复制代码
# frps.toml
[common]
database_type = "sqlite3"
database_path = "/var/lib/frp/frps.db"

配置日志

toml 复制代码
[common]
log_file = "./frps.log"
log_level = "info"
log_max_days = 3

这样,您可以使用 frp 实现各种隧道需求。可以根据具体需求调整配置以满足安全性和性能的要求。

相关推荐
何中应6 天前
frp技术
网络·frp·内网穿透
嘻哈记4 个月前
通过fail2ban服务监控frps日志实现禁止非法IP
网络协议·frp·fail2ban
Peter_hust5 个月前
FRP内网穿透需要注意的事情
安全·frp·内网穿透
risingsun5296 个月前
FRP + nginx 获取客户端真实IP
运维·tcp/ip·nginx·frp
超凡脫俗7 个月前
CentOS 7 部署frp穿透内网
linux·frp
woshicaiji121388 个月前
基于FRP的远程登录与内网穿透
linux·服务器·frp·内网穿透·虚拟机
胡建飞10 个月前
借助frp的xtcp+danted代理打通两边局域网p2p方式访问
frp·p2p·局域网·xtcp·不依赖服务器网速·打洞·隧道
搬砖的梦先生1 年前
centos 内网实现mail发送
linux·运维·centos·frp·mailx
gcygeeker1 年前
使用frp实现公网使用https访问exsi控制台
https·esxi·vmware·frp