Frp内网穿透个人食用

FRP.png

概览

一些概述,便于您快速的了解 frp。

官方文档https://gofrp.org/

[github项目地址](fatedier/frp: A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet. (github.com))

frp 是什么?

frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

为什么使用 frp?

通过在具有公网 IP 的节点上部署 frp 服务端,可以轻松地将内网服务穿透到公网,同时提供诸多专业的功能特性,这包括:

  • 客户端服务端通信支持 TCP、KCP 以及 Websocket 等多种协议。
  • 采用 TCP 连接流式复用,在单个连接间承载更多请求,节省连接建立时间。
  • 代理组间的负载均衡。
  • 端口复用,多个服务通过同一个服务端端口暴露。
  • 多个原生支持的客户端插件(静态文件查看,HTTP、SOCK5 代理等),便于独立使用 frp 客户端完成某些工作。
  • 高度扩展性的服务端插件系统,方便结合自身需求进行功能扩展。
  • 服务端和客户端 UI 页面

1.安装(服务器端)

github下载最新版

wget https://github.com/fatedier/frp/releases/download/v0.34.3/frp_0.34.3_linux_amd64.tar.gz

解压进入目录

tar -zxvf frp_0.34.3_linux_amd64.tar.gz
cd frp_0.34.3_linux_amd64

如下文件.frps为服务端执行程序,frpc.ini是配置文件,frps_full.ini是模板文件

frpc的就是客户端文件

[root@localhost frp_0.34.3_linux_amd64]# ll
总用量 22564
-rwxr-xr-x 1 1001 116  9887744 11月 20 17:16 frpc
-rw-r--r-- 1 1001 116     8328 11月 20 17:19 frpc_full.ini
-rw-r--r-- 1 1001 116      126 11月 20 17:19 frpc.ini
-rwxr-xr-x 1 1001 116 13176832 11月 20 17:16 frps
-rw-r--r-- 1 1001 116     4928 11月 20 17:19 frps_full.ini
-rw-r--r-- 1 1001 116       26 11月 20 17:19 frps.ini
-rw-r--r-- 1 1001 116    11358 11月 20 17:19 LICENSE
drwxr-xr-x 2 1001 116       88 11月 20 17:19 systemd

进入 systemd目录打开 frps.service如下

[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini

[Install]
WantedBy=multi-user.target

根据文件内容,移动服务端程序文件

mv frps /usr/bin/frps

创建配置文件目录和文件

mkdir /etc/frp
touch /etc/frp/frps.ini

移动 frps.service文件

mv frps.service /usr/lib/systemd/system/frps.service

之后重新加载单元

systemctl daemon-reload

之后可以使用以下命令进行,启动或查看状态

systemctl start frps    #启动
systemctl status frps     #查看状态
systemctl enable frps    #开机自启动

当然现在还不能启动还没有配置,服务端配置文件

2.配置(服务器端)

编辑配置文件

vim /etc/frp/frps.ini

输入以下

# [common] is integral section
[common]
# A literal address or host name for IPv6 must be enclosed
# in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80"
bind_addr = 0.0.0.0
bind_port = 7000

# udp port to help make udp hole to penetrate nat
bind_udp_port = 7001

# udp port used for kcp protocol, it can be same with 'bind_port'
# if not set, kcp is disabled in frps
kcp_bind_port = 7000

# AuthenticationMethod specifies what authentication method to use authenticate frpc with frps.
# If "token" is specified - token will be read into login message.
# If "oidc" is specified - OIDC (Open ID Connect) token will be issued using OIDC settings. By default, this value is "token".
authentication_method = token

# AuthenticateHeartBeats specifies whether to include authentication token in heartbeats sent to frps. By default, this value is false.
authenticate_heartbeats = false

# AuthenticateNewWorkConns specifies whether to include authentication token in new work connections sent to frps. By default, this value is false.
authenticate_new_work_conns = false

# auth token
token = 12345678

bind_addr = 0.0.0.0 #改成自己IP
token = 12345678 #修改token

更多功能请参考 frps_full.ini模板文件

启动端口为7000

systemctl start frps

这样服务端就配置完成了

3.客户端

安装方法和服务端一样

配置客户端文件

vim /etc/frp/frpc.ini
# [common] is integral section
[common]
# A literal address or host name for IPv6 must be enclosed
# in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80"
server_addr = 0.0.0.0
server_port = 7000

# if you want to connect frps by http proxy or socks5 proxy or ntlm proxy, you can set http_proxy here or in global environment variables
# it only works when protocol is tcp
# http_proxy = http://user:passwd@192.168.1.128:8080
# http_proxy = socks5://user:passwd@192.168.1.128:1080
# http_proxy = ntlm://user:passwd@192.168.1.128:2080

# console or real logFile path like ./frpc.log
log_file = /etc/frp/frpc.log

# trace, debug, info, warn, error
log_level = info

log_max_days = 3

# disable log colors when log_file is console, default is false
disable_log_color = false

# for authentication, should be same as your frps.ini
# AuthenticateHeartBeats specifies whether to include authentication token in heartbeats sent to frps. By default, this value is false.
authenticate_heartbeats = false

# AuthenticateNewWorkConns specifies whether to include authentication token in new work connections sent to frps. By default, this value is false.
authenticate_new_work_conns = false

# auth token
token = 12345678


# communication protocol used to connect to server
# now it supports tcp, kcp and websocket, default is tcp
protocol = tcp

server_addr = 0.0.0.0 #服务器端IP
token = 12345678 #对应的token

修改日志文件所有者,不然会有点小问题

chown nobody.nobody /etc/frp/frpc.log

上面只是基础的连接,还没有配置任何服务

通过 SSH 访问内网机器

在刚才的客户端配置文件最下面加入

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

正常照上面这个格式就能完成大部分穿透
local_port = 22 为内网端口
remote_port = 6000 为映射到服务端的端口

访问格式为

http://服务端IP:6000

通过自定义域名访问内网的 Web 服务

这个示例通过简单配置 HTTP 类型的代理让用户访问到内网的 Web 服务。

HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http_port 用于接收 HTTP 请求,还额外提供了基于 HTTP 协议的诸多功能。

  1. 修改 frps.ini 文件,设置监听 HTTP 请求端口为 8080:
    [common]
    bind_port = 7000
    vhost_http_port = 8080
    
  2. 修改 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
    
  3. 分别启动 frps 和 frpc。
  4. www.yourdomain.comwww.yourdomain2.com 的域名 A 记录解析到 IP x.x.x.x,如果服务器已经有对应的域名,也可以将 CNAME 记录解析到服务器原先的域名。或者可以通过修改 HTTP 请求的 Host 字段来实现同样的效果。
  5. 通过浏览器访问 http://www.yourdomain.com:8080 即可访问到处于内网机器上 80 端口的服务,访问 http://www.yourdomain2.com:8080 则访问到内网机器上 8080 端口的服务。
# frp 

本文由:星际难民
实践,测试,整理发布.如需转载请注明地址 本文标题:Frp内网穿透个人食用
地址:https://530503.xyz/articles/2020/12/15/1608019263853.html

评论

取消