本文最后更新于 2020 年 3 月 8 日,文章已超过 6 个月!内容可能已失效,请自行测试 ~

frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp 协议,为 http 和 https 应用协议提供了额外的能力,且尝试性支持了点对点穿透。

Frp安装及管理脚本内容:

#!/bin/bash
#frp管理脚本
#Frp安装路径
Frp_Path='/usr/local/frpd'
#Frps路径
Frps_Path="${Frp_Path}/frps"
#Frpc路径
Frpc_Path="${Frp_Path}/frpc"
#Frps配置文件路径
Frps_Conf="${Frp_Path}/frps.ini"
#Frpc配置文件路径
Frpc_Conf="${Frp_Path}/frpc.ini"
#Frp版本
Frp_Version_local="${Frp_Path}/.Frp_Version"
#Frp日志目录
Frp_Logs="${Frp_Path}/logs"
#Frp日志地址
Frps_Logs_File="${Frp_Logs}/frps.log"
#Frps server端口
Frps_Port='1281'
#Frpc 端口
Frpc_Port='10801'
#Frps 仪表盘地址
Frps_Dashboard_Addr='0.0.0.0'
#Frps 仪表盘端口
Frps_Dashboard_Port='1288'
#Frps 仪表盘用户名
Frps_Dashboard_User='adminis'
#Frps 仪表盘密码
Frps_Dashboard_Pwd='YMcc_duyonged'
#Frps 授权验证
Frps_Token='Uinux_ymcc_duyonge'
request_Page() {
	Page_Content=$(curl -s https://github.com/fatedier/frp/releases)
	Frp_Version=$(echo "${Page_Content}" |sed -n 's#.*/fatedier/frp/releases/tag/.*>\(.*\)<.*#\1#p' |head -1 )
	#Frp解压路径
	Frpd_Path="${Frp_Path}/frp_${Frp_Version/v}_linux_amd64"
	Frp_File_Name="${Frp_Path}/${Frp_Version}.tar.gz"
	Frp_Down_Url=$(echo "${Page_Content}" |sed -n "s#.*\(/fatedier/frp/releases/download/${Frp_Version}.*linux.*amd64.*\)\".*rel.*#https://github.com\1#p")
}
Frp_Init() {
	#查询Frps进程
	Frps_Auth_Check=$(/usr/bin/ps -C frps |awk '/frp/ {print $1}')
	#查询Frpc进程
	Frpc_Auth_Check=$(/usr/bin/ps -C frpc |awk '/frp/ {print $1}')
	case $1 in
		start)
			#判断Frps服务是否启动
			if [ ! -z "${Frps_Auth_Check}" ];then
				echo -e '\033[31mFrps正在运行中..\033[0m'
			else
				#后台启动Frps服务
				nohup ${Frps_Path} -c ${Frps_Conf} &> /dev/null &
				sleep 0.1
				Frps_Auth_Check=$(/usr/bin/ps -C frps |awk '/frp/ {print $1}')
				if [ -z "${Frps_Auth_Check}" ];then
					echo -e '\033[31mFrps启动失败!\033[0m'
				else
					echo -e '\033[32mFrps启动成功!\033[0m'
				fi
			fi
			#判断Frpc服务是否启动
			if [ -z "${Frpc_Auth_Check}" ];then
				#后台启动Frpc服务
				nohup ${Frpc_Path} -c ${Frpc_Conf} &> /dev/null &
				Frpc_Auth_Check=$(/usr/bin/ps -C frpc |awk '/frp/ {print $1}')
				if [ -z "${Frpc_Auth_Check}" ];then
					echo -e '\033[31mFrpc启动失败!\033[0m'
				else
					echo -e '\033[32mFrpc启动成功!\033[0m'
				fi
			else
				echo -e '\033[31mFrpc正在运行中..\033[0m'
			fi
			;;
		stop)
			if [ ! -z "${Frps_Auth_Check}" ];then
				/usr/bin/kill -9 "${Frps_Auth_Check}"
				echo -e '\033[32mFrps服务已停止!\033[0m'
			else
				echo -e "\033[31mFrps服务未启动!\033[0m"
			fi
			if [ ! -z "${Frpc_Auth_Check}" ];then
				/usr/bin/kill -9 "${Frpc_Auth_Check}"
				echo -e '\033[32mFrpc服务已停止!\033[0m'
			else
				echo -e "\033[31mFrpc服务未启动!\033[0m"
			fi
			;;
		*)
			echo -e '\033[31m请输入正确参数!\033[0m'
	esac
}

Frp_Down() {
	if [ ! -d "${Frp_Path}" ];then
		echo -e "\033[31m${Frp_Path} 不存在!\033[0m"
		mkdir "${Frp_Path}"
		if [ ! -d "${Frp_Path}" ];then
			echo -e "\033[31m${Frp_Path} 创建失败!\033[0m"
			exit 1
		fi
	fi
	Frpd_Down_Name=" ${Frp_File_Name#/*/*/*/*}"
	echo -ne "\033[36m${Frpd_Down_Name} 下载中..\r\033[0m"
	wget -q -O ${Frp_File_Name} ${Frp_Down_Url}
	if [ ! -f ${Frp_File_Name} ];then
		echo -e "\033[31m${Frpd_Down_Name} \r\033[0m下载失败!"
		exit 1
	else
		echo -e "\033[32m${Frpd_Down_Name} \r\033[0m下载成功!"
	fi
	cd ${Frp_Path}/
	tar zxf ${Frp_File_Name}
	if [ -d ${Frpd_Path} ];then
		echo -e "\033[32m${Frpd_Down_Name} \033[0m解压成功!"
	else
		echo -e "\033[31m${Frpd_Down_Name} \033[0m解压失败!"
		echo "${Frp_Version}"
		exit 1
	fi
	if [ ! -f ${Frps_Conf} ];then
		cp ${Frpd_Path}/frp* ./
		#检测Frps是否存在
		if [ ! -f "${Frps_Path}" ];then
			echo -e "\033[31m${Frps_Path} 不存在!\033[0m"
		#检测Frpc是否存在
		elif [ ! -f "${Frpc_Path}" ];then
		#	Frp_Installr install
			echo -e "\033[31m${Frpc_Path} 不存在!\033[0m"
		#检测Frps配置文件是否存在
		elif [ ! -f "${Frps_Conf}" ];then
			echo -e "\033[31m${Frps_Conf} 不存在!\033[0m"
		#检测Frpc配置文件是否存在
		elif [ ! -f "${Frpc_Conf}" ];then
			echo -e "\033[31m${Frpc_Conf} 不存在!\033[0m"
		else
			echo -e "\033[32m文件拷贝成功!\033[0m"
		fi
		sed -i "s/\(^bind_port[[:space:]]=[[:space:]]\).*/\1${Frps_Port}/" ${Frps_Conf}
		sed -i "\$akcp_bind_port = ${Frps_Port}" ${Frps_Conf}
		sed -i "\$adashboard_addr = ${Frps_Dashboard_Addr}" ${Frps_Conf}
		sed -i "\$adashboard_port = ${Frps_Dashboard_Port}" ${Frps_Conf}
		sed -i "\$adashboard_user = ${Frps_Dashboard_User}" ${Frps_Conf}
		sed -i "\$adashboard_pwd = ${Frps_Dashboard_Pwd}" ${Frps_Conf}
		sed -i "\$atoken = ${Frps_Token}" ${Frps_Conf}
		sed -i "\$amax_ports_per_client = 0" ${Frps_Conf}
		sed -i "\$atcp_mux = true" ${Frps_Conf}
		sed -i "\$alog_file = ${Frps_Logs_File}" ${Frps_Conf}
		sed -i "\$alog_level = trace" ${Frps_Conf}
		sed -i "\$alog_max_days = 3" ${Frps_Conf}
		if [ -f ${Frpc_Conf} ];then
			sed -i "s/\(server_port = \).*/\1${Frps_Port}/g" ${Frpc_Conf}
			sed -i "3atoken = ${Frps_Token}" ${Frpc_Conf}
		fi
	else
		cp ${Frpd_Path}/frps ${Frp_Path}
	fi
	rm -rf ${Frpd_Path}/
	echo ${Frp_Version} > ${Frp_Version_local}
	if [ ! -e ${Frp_Logs} ];then
		mkdir -p ${Frp_Logs}
	fi
}

Frp_Installr() {
	request_Page
	if [ ! -e ${Frp_Path} ];then
		mkdir ${Frp_Path}
	fi
	case $1 in
		install)
			if [ ! -f "${Frps_Path}" ];then
				cd ${Frp_Path}
				Frp_Down
			else
				echo -e "\033[32m${Frps_Path} 已存在!\033[0m"
			fi
			;;
		update)
			if [ ! -e ${Frp_Version_local} ] ;then
				echo -e "\033[31m${Frp_Version_local}\033[0m 不存在!"
				Frp_Installr install
			elif [ "$(cat ${Frp_Version_local})" = "${Frp_Version}" ] ;then
				echo -e "已是最新版:\033[32m ${Frp_Version}\033[0m"
			elif [ ! "$(cat ${Frp_Version_local})" = "${Frp_Version}" ] ;then
				Frp_Init stop
				Frp_Down
			fi
			;;
		*)
			echo -e "请输入正确参数: \033[31minstall | update\033[0m"
	esac
}

Uninstall() {
	if [ -f ${Frps_Conf} ];then
		Frp_Init stop
		cp ${Frps_Conf} /tmp/frps.ini.bak
		rm -rf ${Frp_Path}
		rm -rf /usr/local/bin/${0}
		echo -e "\033[32mFrp已卸载\033[0m,配置文件保存至 /tmp/frps.ini.bak ."
	else
		echo -e "卸载失败,配置文件\033[31m ${Frps_Conf} \033[0m不存在!"
	fi
}

case $1 in
	update)
		Frp_Installr $1
		;;
	install)
		Frp_Installr $1
		;;
	start)
		Frp_Init $1
		;;
	stop)
		Frp_Init $1
		;;
	restart)
		Frp_Init stop
		Frp_Init start
		;;
	uninstall)
		Uninstall
		;;
	*)
		echo '请输入正确参数(update|install|start|stop|restart|uninstall'
esac

将脚本内容拷贝至/usr/local/sbin/frpd

cp ~/frpd.sh /usr/local/sbin/frpd
chmod +x /usr/local/sbin/frpd

脚本参数说明:

install #安装
update #更新最新版
start #启动服务
stop #停止服务
restart #重启服务
uninstall #卸载服务

该文章采用「CC 协议」,转载必须注明作者和本文链接.
分类: Linux