内网穿透神器FRP

frp

frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp, http, https 协议

目前主要的场景是内网穿透,可以用于本地调试微信接口、本地站点公网访问等。

本文主要讲解:

  1. frp客户端配置
  2. frp服务端配置&&搭建

准备

frp执行区分不同的平台,在服务器上执行命令:

arch

如果输出x86_64则需要下载带linux_amd64的那个压缩包;
如果输出的是其他的,则在文件列表中找 linux 的对应架构的压缩包

  • frp_0.20.0_darwin_amd64.tar.gz mac
  • frp_0.20.0_linux_arm64.tar.gz linux
  • frp_0.20.0_windows_386.zip windows

然后尝试运行./frps --help, 正常会输出帮助信息。

如果提示-bash: ./frps: cannot execute binary file: Exec format error就说明你下错版本了。

客户端-frpc

客户端和服务端版本号一致,目前使用的是v0.20.0

启动命令:

./frpc -c frpc.ini

frpc.ini客户端配置(必须)

[common]
# frp 服务器端地址,ip或域名
server_addr = frp.xxx.com
# frp 服务端端口,即填写服务端配置中的 bind_port
server_port = 7000
token = xxxxx

HTTP(S)

[apixxx]
# http 或 https
type = http

# 默认不需要改变ip,指向本地
local_ip = 127.0.0.1
# 对应服务的端口号
local_port = 9000

# http 可以考虑加密和压缩一下
use_encryption = true
use_compression = true

# 自定义访问网站的用户名和密码,如果不定义的话谁都可以访问,会不安全
http_user = admin
http_pwd = admin

# 对应远程的访问地址web0.frp.xxx.com:10080
# frp.idayer.com 为服务端配置的 subdomain_host 
subdomain = web0

TCP/UDP 范围转发

# 自定义一个配置名称,格式为“[range:名称]”,放在开头
[range:multi-port]

type = tcp
local_ip = 127.0.0.1
use_encryption = false
use_compression = false

# 本地端口和远程端口可以指定多个范围,如下格式,且范围之间必须一一对应
local_port = 6010-6020,6022,6024-6028
remote_port = 16010-16020,16022,16024-16028

最后我们的配置结果如下:

[common]
server_addr = frp.xxx.com
server_port = 7000
token = xxx

[apixxx]
type = http

local_ip = 127.0.0.1
local_port = 9000

use_encryption = true
use_compression = true

http_user = admin
http_pwd = admin

subdomain = web0

更多配置可以看目录下的frpc_full.ini,以及参考(frp配置)[https://github.com/fatedier/frp/blob/master/README_zh.md]

tips

登录服务端的 dashboard,看看是否连接成功,测试各项转发是否可用

服务端-frps

启动命令:

# 使用 -c 参数指定配置文件
./frps -c frps.ini

配置

服务端配置如下:

[common]

# frp 服务端端口(必须)
bind_port = 7000

# frp 服务端密码(必须)
token = 12345678

# 认证超时时间,由于时间戳会被用于加密认证,防止报文劫持后被他人利用
# 因此服务端与客户端所在机器的时间差不能超过这个时间(秒)
# 默认为900秒,即15分钟,如果设置成0就不会对报文时间戳进行超时验证
authentication_timeout = 900

# 仪表盘端口,只有设置了才能使用仪表盘(即后台)
dashboard_port = 7500

# 仪表盘访问的用户名密码,如果不设置,则默认都是 admin
dashboard_user = admin
dashboard_pwd = admin

# 如果你想要用 frp 穿透访问内网中的网站(例如路由器设置页面)
# 则必须要设置以下两个监听端口,不设置则不会开启这项功能
vhost_http_port = 10080
vhost_https_port = 10443

# 此设置需要配合客户端设置,仅在穿透到内网中的 http 或 https 时有用(可选)
# 假设此项设置为 example.com,客户端配置 http 时将 subdomain 设置为 test,
# 则你将 test.example.com 解析到服务端后,可以使用此域名来访问客户端对应的 http
subdomain_host = example.com

frps后台运行

简单方式

可以通过nohup [command] &操作来让命令后续执行。

启动
nohup /root/frp/frps -c /root/frp/frps.ini &
停止

pkill frps

加入开机自启

编辑/etc/rc.local文件,将启动那句命令加到exit 0语句之前(如果有)

centOs 6.x service

创建脚本

注意修改其中的EXECOPTIONS目录。

#!/bin/bash

# chkconfig: - 85 15
# # description: frp init script

RETVAL=0
PROG="frpc"
EXEC="/usr/bin/frpc"
LOCKFILE="/var/lock/subsys/$PROG"
OPTIONS="-c /etc/frp/frpc.ini"

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
else
  echo "/etc/rc.d/init.d/functions is not exists"
  exit 0
fi

start() {
  if [ -f $LOCKFILE ]
  then
    echo "$PROG is already running!"
  else
    echo -n "Starting $PROG: "
    #$EXEC $OPTIONS  &
    nohup $EXEC $OPTIONS >/dev/null 2>&1 &
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $LOCKFILE && success || failure
    echo
    return $RETVAL
  fi
}

stop() {
  echo -n "Stopping $PROG: "
  killproc $EXEC
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -r $LOCKFILE && success || failure
  echo
}

restart ()
{
  stop
  sleep 1
  start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $PROG
    ;;
  restart)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit $RETVAL
修改权限

chmod 755 /etc/init.d/frp

设置开机自启动

chkconfig frp on

命令

启动服务:service frp start

停止服务:service frp stop

查看状态:service frp status

重启服务:service frp restart

更多方案可以参考

0 条评论