frp
frp 是一個可用於內網穿透的高性能的反向代理應用,支持 tcp, udp, http, https 協議
目前主要的場景是內網穿透,可以用於本地調試微信接口、本地站點公網訪問等。
本文主要講解:
- frp客戶端配置
- 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/f...]
tips
登錄服務端的 dashboard,看看是否連接成功,測試各項轉發是否可用
- http://frp.xxx.com:7500
- 賬號/密碼
服務端-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
創建腳本
注意修改其中的EXEC
,OPTIONS
目錄。
#!/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
更多方案可以參考
以FRP爲例,講述Centos6 添加開機啓動等服務](https://blog.csdn.net/Freshbo...