配置frp实现内网穿透(图文详细步骤)

最近看到有人在网上发布FRP的教程,但是写的都不详细,我自己整理了一下

目前主流的内网穿透有神卓互联,性能超级强劲,由C语言编写,亲测性能非常高,搞技术的达人应该都知道,但是这个是企业级收费的,界面如下,接下来就介绍一个免费的软件,叫FRP,功能差不多,只是用起来有点麻烦。

配置frp实现内网穿透(图文详细步骤)_第1张图片

一、frp的作用

利用处于内网或防火墙后的机器,对外网环境提供 http 或 https 服务。
对于 http, https 服务支持基于域名的虚拟主机,支持自定义域名绑定,使多个域名可以共用一个80端口。
利用处于内网或防火墙后的机器,对外网环境提供 tcp 和 udp 服务,例如在家里通过 ssh 访问处于公司内网环境内的主机。

二、配置说明

1、实现功能

(1)外网通过ssh访问内网机器

(2)自定义绑定域名访问内网web服务

2、配置前准备

(1)公网服务器1台

(2)内网服务器1台(我这里演示的是linux环境,win10上面vmware安装的centos7)

(3)公网服务器绑定域名1个(实现二1中(1)功能不需要公网服务器绑定域名,二1中(2)功能必须需要公网服务器绑定域名)

(4)内网服务器部署一个web服务,可以用tomcat模拟,这里就不演示了

三、安装frp

1、公网服务器与内网服务器都需要下载frp进行安装,公网服务器(服务端)配置关注步骤6,内网服务器(客户端)关注步骤7

2、下载linux版本frp_0.13.0_linux_amd64.tar.gz

3、新建目录mkdir -p /usr/local/frp,上传frp_0.13.0_linux_amd64.tar.gz至linux服务器该目录下

4、解压tar -zxvf  frp_0.13.0_linux_amd64.tar.gz

5、进入解压目录cd frp_0.13.0_linux_amd64,这里主要关注4个文件,分别是frpc、frpc.ini和frps、frps.ini,前者两个文件是客户端所关注文件,后者两个文件是服务端所关注两个文件。

6、配置服务端(公网服务器),首先删掉frpc、frpc.ini两个文件,然后再进行配置,vi ./frps.ini,
 

[common]
bind_port = 7000           #与客户端绑定的进行通信的端口
vhost_http_port = 6081     #访问客户端web服务自定义的端口号

保存然后启动服务./frps -c ./frps.ini,这是前台启动,后台启动命令为nohup ./frps -c ./frps.ini &

7、配置客户端(内网服务器),首先删掉frps、frps.ini两个文件,然后再进行配置,vi ./frpc.ini

[common]
server_addr = 120.56.37.48   #公网服务器ip
server_port = 7000            #与服务端bind_port一致
 
#公网通过ssh访问内部服务器
[ssh]
type = tcp              #连接协议
local_ip = 192.168.3.48 #内网服务器ip
local_port = 22         #ssh默认端口号
remote_port = 6000      #自定义的访问内部ssh端口号
 
#公网访问内部web服务器以http方式
[web]
type = http         #访问协议
local_port = 8081   #内网web服务的端口号
custom_domains = repo.iwi.com   #所绑定的公网服务器域名,一级、二级域名都可以

保存然后执行./frpc -c ./frpc.ini启动,这是前台启动,后台启动命令为nohup ./frpc -c ./frpc.ini &

这样就可以了。

以下是脚本

#! /bin/bash
# chkconfig: 2345 55 25
### BEGIN INIT INFO
# Provides:          frps
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the frps
# Description:       starts frps using start-stop
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
ProgramName="Frps"
ProgramPath="/usr/local/frps"
NAME=frps
BIN=${ProgramPath}/${NAME}
CONFIGFILE=${ProgramPath}/frps.ini
SCRIPTNAME=/etc/init.d/${NAME}
version="03.20"
program_version=`${BIN} --version`
RET_VAL=0

[ -x ${BIN} ] || exit 0
strLog=""
fun_clangcn()
{
    echo ""
    echo "+---------------------------------------------------------+"
    echo "|     Manager for ${ProgramName}, Author Clang, Mender MvsCode      |"
    echo "+---------------------------------------------------------+"
    echo ""
}

fun_check_run(){
    PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
    if [ ! -z $PID  ]; then
        return 0
    else
        return 1
    fi
}
fun_load_config(){
    if [ ! -r ${CONFIGFILE} ]; then
        echo "config file ${CONFIGFILE} not found"
        return 1
    fi
}
fun_start()
{
    if [ "${arg1}" = "start" ]; then
      fun_clangcn
    fi
    if fun_check_run; then
        echo "${ProgramName} (pid $PID) already running."
        return 0
    fi
    fun_load_config
    echo -n "Starting ${ProgramName}(${program_version})..."
    ${BIN} -c ${CONFIGFILE} >/dev/null 2>&1 &
    sleep 1
    if ! fun_check_run; then
        echo "start failed"
        return 1
    fi
    echo " done"
    echo "${ProgramName} (pid $PID)is running."
    return 0
}

fun_stop(){
    if [ "${arg1}" = "stop" ] || [ "${arg1}" = "restart" ]; then
      fun_clangcn
    fi
    if fun_check_run; then
        echo -n "Stoping ${ProgramName} (pid $PID)... "
        kill $PID
        if [ "$?" != 0 ] ; then
            echo " failed"
            return 1
        else
            echo " done"
        fi
    else
        echo "${ProgramName} is not running."
    fi
    return 0
}
fun_restart(){
    fun_stop
    fun_start
}
fun_status(){
    PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
    if [ ! -z $PID ]; then
        echo "${ProgramName} (pid $PID) is running..."
    else
        echo "${ProgramName} is stopped"
        exit 0
    fi
}
checkos(){
    if   grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        OS=CentOS
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        OS=Debian
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        OS=Ubuntu
    elif grep -Eqi "Alpine" /etc/issue || grep -Eq "Alpine" /etc/*-release; then
        OS=Alpine
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        OS=Fedora
    else
        echo "Not support OS, Please reinstall OS and retry!"
        return 1
    fi
}
fun_config(){
    if [ -s ${CONFIGFILE} ]; then
        vi ${CONFIGFILE}
    else
        echo "${ProgramName} configuration file not found!"
        return 1
    fi
}
fun_version(){
    echo "${ProgramName} version ${program_version}"
    return 0
}
fun_help(){
    ${BIN} --help
    return 0
}

arg1=$1
[  -z ${arg1} ]
case "${arg1}" in
    start|stop|restart|status|config)
        fun_${arg1}
    ;;
    [vV][eE][rR][sS][iI][oO][nN]|-[vV][eE][rR][sS][iI][oO][nN]|--[vV][eE][rR][sS][iI][oO][nN]|-[vV]|--[vV])
        fun_version
    ;;
    [Cc]|[Cc][Oo][Nn][Ff]|[Cc][Oo][Nn][Ff][Ii][Gg]|-[Cc]|-[Cc][Oo][Nn][Ff]|-[Cc][Oo][Nn][Ff][Ii][Gg]|--[Cc]|--[Cc][Oo][Nn][Ff]|--[Cc][Oo][Nn][Ff][Ii][Gg])
        fun_config
    ;;
    [Hh]|[Hh][Ee][Ll][Pp]|-[Hh]|-[Hh][Ee][Ll][Pp]|--[Hh]|--[Hh][Ee][Ll][Pp])
        fun_help
    ;;
    *)
        fun_clangcn
        echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|version}"
        RET_VAL=1
    ;;
esac
exit $RET_VAL

关键脚本


# 安装frps
install_frps(){
	wget -N --no-check-certificate ${releases_url}

	tar -zxvf frp*.tar.gz

	rm -rf /usr/local/frps
	mkdir /usr/local/frps

	mv ./frp*/frps /usr/local/frps/frps
	mv ./frp*/frps_full.ini /usr/local/frps/frps.ini

	rm -rf ./frp*
}


# 添加开机自启动
add_auto_run(){
	touch /etc/systemd/system/frps.service
	cat < /etc/systemd/system/frps.service
[Unit]
Description=frps server
After=network.target
Wants=network.target
[Service]
Type=simple
PIDFile=/var/run/frps.pid
ExecStart=/usr/local/frps/frps -c /usr/local/frps/frps.ini
RestartPreventExitStatus=23
Restart=always
User=root
[Install]
WantedBy=multi-user.target
EOF
}


# 启动frps
run_frps(){
	systemctl daemon-reload
	systemctl enable frps >/dev/null 2>&1
	systemctl start frps
	systemctl restart frps
}


# 卸载frps
set_uninstall(){
	systemctl stop frps
	systemctl disable frps
	rm -rf /usr/local/frps
	rm -rf /etc/systemd/system/frps.service >/dev/null 2>&1
	echo -e "卸载成功!"
}

===================================

set_bind_port(){
	get_value=""
	echo -e "你正在设置 bind_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_bind_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^bind_port/c\bind_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_bind_udp_port(){
	get_value=""
	echo -e "你正在设置 bind_udp_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_bind_udp_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^bind_udp_port/c\bind_udp_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_kcp_bind_port(){
	get_value=""
	echo -e "你正在设置 kcp_bind_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_kcp_bind_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^kcp_bind_port/c\kcp_bind_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_vhost_http_port(){
	get_value=""
	echo -e "你正在设置 vhost_http_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_vhost_http_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^vhost_http_port/c\vhost_http_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_vhost_https_port(){
	get_value=""
	echo -e "你正在设置 vhost_https_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_vhost_https_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^vhost_https_port/c\vhost_https_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_dashboard_port(){
	get_value=""
	echo -e "你正在设置 dashboard_port "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_dashboard_port
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^dashboard_port/c\dashboard_port = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_dashboard_user(){
	get_value=""
	echo -e "你正在设置 dashboard_user "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_dashboard_user
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^dashboard_user/c\dashboard_user = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}



set_dashboard_pwd(){
	get_value=""
	echo -e "你正在设置 dashboard_pwd "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_dashboard_pwd
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^dashboard_pwd/c\dashboard_pwd = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_token(){
	get_value=""
	echo -e "你正在设置 token "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_token
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^token/c\token = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}


set_subdomain_host(){
	get_value=""
	echo -e "你正在设置 subdomain_host "

	read -e -p "请输入:" get_value
	[[ -z ${get_value} ]] && get_value="none"
	if [ "${get_value}" = "none" ];then
	set_subdomain_host
	else
	echo -e "你设置的值为:${get_value}"
	fi

	sed -i '/^subdomain_host/c\subdomain_host = '"${get_value}"'' /usr/local/frps/frps.ini
	systemctl restart frps
	echo -e "设置成功!"
}

# ====================================


# 关闭apache2 释放80端口
set_unapache2(){
	systemctl disable httpd >/dev/null 2>&1
	systemctl stop httpd >/dev/null 2>&1
	killall -9 httpd >/dev/null 2>&1

	systemctl disable apache2 >/dev/null 2>&1
	systemctl stop apache2 >/dev/null 2>&1
	killall -9 apache2 >/dev/null 2>&1

	systemctl disable firewalld >/dev/null 2>&1
	systemctl stop firewalld >/dev/null 2>&1
	killall -9 firewalld >/dev/null 2>&1

	systemctl disable iptables >/dev/null 2>&1
	systemctl stop iptables >/dev/null 2>&1
	killall -9 iptables >/dev/null 2>&1

	echo -e "关闭 apache2 成功!"
	echo -e "关闭 防火墙 成功!"
}


# 安装流程
set_install(){
	get_version
	install_frps
	add_auto_run
	run_frps
	load_menu
}


# 脚本菜单
case "$1" in
	bind_port|bind_udp_port|kcp_bind_port|vhost_http_port|vhost_https_port|dashboard_port|dashboard_user|dashboard_pwd|token|subdomain_host|install|uninstall|unapache2)
	set_$1
	;;
	*)
	echo -e "缺少参数"
	;;
esac

完美!

你可能感兴趣的:(内网穿透,服务器,linux,网络)