前言:
frp 是一个开源、简洁易用、高性能的内网穿透和反向代理软件,支持 tcp, udp, http, https等协议。frp 项目官网是 https://github.com/fatedier/frp
frp工作原理
● 服务端运行,监听一个主端口,等待客户端的连接;
● 客户端连接到服务端的主端口,同时告诉服务端要监听的端口和转发类型;
● 服务端fork新的进程监听客户端指定的端口;
● 外网用户连接到客户端指定的端口,服务端通过和客户端的连接将数据转发到客户端;
● 客户端进程再将数据转发到本地服务,从而实现内网对外暴露服务的能力。
下面是自动配置脚本
求关注,求打赏!!!
求关注,求打赏!!!
求关注,求打赏!!!
#!/bin/bash
function Install_frp() {
local frp_type=$1
local frp_token=$2
local install_dir="/usr/local/bin"
local version="0.48.0"
local host=`ip address | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | awk -F '/' '{print $1}' | head -n 1`
# 下载和解压缩文件
wget -P ${install_dir} https://github.com/fatedier/frp/releases/download/v0.48.0/frp_${version}_linux_amd64.tar.gz
cd ${install_dir} && tar xf ${install_dir}/frp_${version}_linux_amd64.tar.gz
mv ${install_dir}/frp_${version}_linux_amd64 ${install_dir}/${frp_type}
if [[ ${frp_type} == "frps" ]]; then
local frp_password=$3
sudo tee $install_dir/${frp_type}/${frp_type}.ini > /dev/null <<EOF
[common]
bind_port = 7000
authentication_method = token
token = ${frp_token}
dashboard_port = 7600
dashboard_user = admin
dashboard_pwd = ${frp_password}
EOF
else
local frp_ipaddress=$3
local frp_server_port=$4
local frp_host_port=$5
local frp_hostname=$6
sudo tee $install_dir/${frp_type}/${frp_type}.ini > /dev/null <<EOF
[common]
server_addr = ${frp_ipaddress}
server_port = 7000
authentication_method = token
token = ${frp_token}
[${frp_hostname}]
type = tcp
local_ip = 0.0.0.0
local_port = ${frp_host_port}
remote_port = ${frp_server_port}
EOF
fi
# 设置 frp systemd 服务
sudo tee /etc/systemd/system/${frp_type}.service > /dev/null <<EOT
[Unit]
Description=Frp ${frp_type}
After=network.target
[Service]
Type=simple
User=root
ExecStart=$install_dir/${frp_type}/${frp_type} -c $install_dir/${frp_type}/${frp_type}.ini
Restart=always
[Install]
WantedBy=multi-user.target
EOT
# 启动 frp 服务
sudo systemctl daemon-reload
sudo systemctl enable ${frp_type}.service
sudo systemctl restart ${frp_type}.service
# 打印info
if [[ ${frp_type} == "frps" ]]; then
local frp_password=$3
printf """
----------------------------------
服务端(${version})
访问web端url: http://${host}:7600
用户名:admin
密码:${frp_password}
Token: ${frp_token}
----------------------------------
"""
echo ""
else
local frp_address=$3
local frp_server_port=$4
local frp_host_port=$5
local frp_hostname=$7
printf """
----------------------------------
客户端:${version}
内网端口:${frp_host_port}
映射地址:${frp_address}:${frp_server_port}
----------------------------------
"""
echo ""
fi
# 清理文件
rm -rf ${install_dir}/frp.tar.gz
}
function Check_evn() {
if [[ -e "/usr/local/bin/frps" ]] || [[ -e "/usr/local/bin/frpc" ]]; then
echo "注意:/usr/local/bin 目录下已经存在 frps 或 frpc 文件!"
echo "当前安装路径:/usr/local/bin"
echo "当前版本号:v0.48.0"
echo "-------------------------------------"
echo "如果您需要重新部署,请先删除旧文件:sudo rm -rf /usr/local/bin/frp*"
echo ""
exit;
fi
}
function main() {
Check_evn
printf """
---------------------------------------------
安装路径:/usr/local/bin
版本:v0.48.0
---------------------------------------------
部署 frp:
1. frps 服务端
2. frpc 客户端
3. 卸载frp
---------------------------------------------
"""
echo ""
stty erase ^h
read -p "请输入要部署的服务(1 或 2): " number
if [[ ${number} -eq 1 ]]; then
read -p "请输入token(用于和服务端匹配): " token
read -p "请输入web管理端密码: " password
Install_frp frps ${token} ${password}
elif [[ ${number} -eq 2 ]]; then
read -p "服务端主机ip地址:" ip_address
read -p "服务端端映射端口:" server_port
read -p "主机端需要映射端口:" host_port
read -p "请输入token(用于和服务端匹配): " token
read -p "请输入服务的名字: " hostname
Install_frp frpc ${token} ${ip_address} ${server_port} ${host_port} ${hostname}
elif [[ ${number} -eq 3 ]]; then
rm -rf /usr/local/bin/frp*
rm -rf /etc/systemd/system/frps.service
rm -rf /etc/systemd/system/frpc.service
echo "卸载成功。。。"
else
echo "输入有误,请重新运行脚本并输入数字 1 或 2。"
fi
}
main