内网穿透就是将内网服务暴露到公网可访问,适用于没有公网IP的家庭或企业。
比较著名的开源实现有:
其中,ngrok提供了免费的服务器,可供测试或临时使用。
nps最全面,适合二次开发,提供给多个租户使用。
而frp使用极其简单,非常适合个人用者。
简述
frp由客户端和服务端构成,每个端分别有一个可执行文件,和一个配置文件(使用toml格式)。完了。
服务端
fprs
frps有两种方式运行,一种是使用可执行文件并配置为systemd服务,另一种是使用docker。
使用可执行文件
下载对应的可执行文件包,linux可使用uname -m查看。
1 2
   | wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz tar -zxvf ./frp_0.52.3_linux_amd64.tar.gz
   | 
 
修改frp下的fps.toml,一般只需要两个端口就完事。
为了安全,要添加权限验证。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  bindPort = 7000 
 
  webserver.port = 7500
  webserver.user = "admin" webserver.apssword = "frps1234"
 
  vhostHTTPPort = 7080 vhostHTTPSPort = 7081
 
  auth.method = "token"  auth.additionalScopes = ["HeartBeats", "NewWorkConns"] auth.token = "your-unguessable-token" 
 
  | 
 
在/etc/systemd/system/下添加frp.service:
1 2 3 4 5 6 7 8 9 10 11 12 13
   | [Unit]
  Description = frp server After = network.target syslog.target Wants = network.target
  [Service] Type = simple
  ExecStart = /root/frp_0.52.3_linux_amd64/frps -c /root/frp_0.52.3_linux_amd64/frps.toml
  [Install] WantedBy = multi-user.target
   | 
 
开启服务
1 2
   | systemctl start frps systemctl enable frps
   | 
 
 
查看服务状态和日志
1 2 3
   | systemctl status frps
  journalctl -u frps -b
   | 
 
使用docker
使用docker则简单多了,只需要修改frps.toml,然后运行docker即可。
这里默认frps.toml创建在了/root/frp/下。
1
   | docker run --restart=always --network host -d -v /root/frp/frps.toml:/etc/frp/frps.toml --name frps snowdreamtech/frps
   | 
 
nginx
穿透web服务搭配nginx反向代理食用最佳。注意http://127.0.0.1:7080中的7080就是上面的vhostHTTPPort。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | server {     server_name   *.frp.yourdomain.com;     listen        80;
      location / {         proxy_set_header  Host $host;         proxy_set_header  X-Real-IP $remote_addr;         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;                 proxy_pass        http://127.0.0.1:7080;
          proxy_set_header Upgrade $http_upgrade;         proxy_set_header Connection "upgrade";             } }
  | 
 
客户端
配置
修改frpc.toml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   |  serverAddr = "x.x.x.x"  serverPort = 7000
 
  auth.method = "token" auth.additionalScopes = ["NewWorkConns"]  auth.token = "your-unguessable-token" 
  [[proxies]] name = "ssh" type = "tcp" localIP = "192.168.8.1" localPort = 22 remotePort = 6000
  [[proxies]] name = "jellyfin" type = "http" localIP = "192.168.8.1" localPort = 8096 customDomains = ["jellyfin.frp.yourdomain.com"]
  [[proxies]] name = "luci" type = "http" localIP = "192.168.8.1" localPort = 80 customDomains = ["luci.frp.yourdomain.com"]
  [[proxies]] name = "aria2" type = "http" localIP = "192.168.8.1" localPort = 6800 customDomains = ["aria2.frp.yourdomain.com"]
  [[proxies]] name = "clash" type = "http" localIP = "192.168.8.1" localPort = 9090 customDomains = ["clash.frp.yourdomain.com"]
  注意`name`不要有重复。
 
 
  ```bash ssh 内网用户名@x.x.x.x -p 6000 输入内网用户密码: 
 
  | 
 
frps在接收到客户端的remote_port = 6000后就会代理6000端口,于是可以使用ssh直接登录内网机器。
ariang
需要修改ariang的rpc地址,把默认的http://${host}:6800/jsonrpc改成http://aria2.frp.yourdomain.com:80/jsonrpc才能使用。
使用docker
在本地创建frpc.toml,比如/root/frpc/frpc.toml。
然后使用第三方docker image:
1
   | docker run -d --name=frpc --restart=always -v /root/frpc/frpc.toml:/etc/frp/frpc.toml snowdreamtech/frpc
   | 
 
修改配置则需要重启docker:
openclash
需要修改openclash控制台的连接设置,默认是:
1 2 3
   | Host: 192.168.8.1 端口: 9090 密钥:123456
   | 
 
需要修改为:
1 2 3
   | Host: clash.frp.yourdomain.com 端口: 80 密钥:123456
   |