更改

跳转至: 导航搜索

CentOS服务搭建-04-运用FRPS服务搭建黑群晖NAS的FRPC

添加5,356字节, 2024年1月12日 (星期五)
《frps.service》文档
# su获取root
# wget https://github.com/fatedier/frp/releases/download/v0.26.0/frp_0.26.0_linux_amd64.tar.gz
# wget https://github.com/fatedier/frp/releases/download/v0.53.2/frp_0.53.2_linux_amd64.tar.gz
# wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz
# 解压:tar xf frp_0.26.0_linux_amd64.tar.gz
# 改名:mv frp_0.26.0_linux_amd64 frp
==《frps.ini》文档==
<pre>[common]
bind_addr = xxx.xxx.xxx.xxx # frps内网服务器地址,如果有防火墙的话,设置下述端口映射 bind_port = xxxxx # frps服务端口 bind_udp_port = xxxx # udp服务端口vhost_http_port = xxxxx # http服务端口 vhost_https_port = xxxxx # https服务端口 privilege_token = xxxxxxxx # 握手密钥 subdomain_host = xxxx.xxx # 填写顶级域名,意味着对应frpc的2级域名代理启用dashboard_port = xxxx # 服务器面板端口 dashboard_user = xxxxx # 服务器面板用户 dashboard_pwd = xxxxxxx # 服务器面板密码log_file = ./frpslog.loglog_level = infolog_max_days = 3
</pre>
# 拷贝frps.ini 到/etc/frps:cp /源目录/frps.ini /etc/frps/
# 拷贝frps到/usr/etc/frps:cp /源目录/usr/etc/frps
# frps.service文档保存于:/etc/systemd/system/
# 使用systemd配置开机自启(适用于 centos7 Ubuntu 16 或 debian 8) <pre> sudo vim /etc/systemd/system/frps.service 新建此文件,并写入以下内容</pre>
<pre>
[Unit]
Description=frps daemonDgnAfter=network.target
[Service]
Type=simple
# User=rootExecStart=/usr/etc/frps/frps -c /etc/frpsfrps_aker/frps.ini
# /usr/etc/frps 文件目录,注意service文件中要指向frps运行程序(这个没查到网上解释,以为只要放个空文档,其实要放入执行文件,搞了半天才运行成功!!!)
# /etc/frps/frps.ini 配置目录
 
[Install]
WantedBy=multi-user.target
</pre>
===frps.service自启动===
# vim编辑退出:Esc,# :wq(保存退出)
# 启动并设为开机自启。
# (把start改为stop即终止frps)
sudo systemctl enable frps
systemctl status frpfrps
# 查看frps状态
</pre>
 ===* Centos6.5及以下版本frps自启动===
# vi /etc/rc.local
# 在最下面加一行/usr/sbin/frp/frps -c /usr/sbin/frp/frps.ini
# 其中 /usr/sbin/frp是程序放置的目录,自己修改,重启ok
===frps.service自启动进程查看===
# 查看frp是否启动:ps aux | grep frps
# 查看frp启动状态 <pre>sudo systemctl status frps</pre>
</pre>
* 内网机器上面执行 tail -f nohup.out 查看启动命令的执行结果akerlu
 
===firewall常用命令===
* 安装firewall命令
<pre>
yum install firewalld firewalld-config
</pre>
* Firewall开启常见端口命令
<pre>
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=22/tcp --permanent
firewall-cmd --zone=public --add-port=53/udp --permanent
</pre>
* Firewall关闭常见端口命令
<pre>
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --zone=public --remove-port=443/tcp --permanent
firewall-cmd --zone=public --remove-port=22/tcp --permanent
firewall-cmd --zone=public --remove-port=53/udp --permanent
</pre>
* 指定特定源IP访问某端口
<pre>
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.1" port protocol="tcp" port="80" accept"
#添加源IP 192.168.100.1访问80端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.0/24" port protocol="tcp" port="80" accept"
#添加IP段访问80端口
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.100.1" port protocol="tcp" port="80" accept"
#删除源IP 192.168.100.1访问80端口
</pre>
* 指定特定源IP开放所有端口
<pre>
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.180.10.10" accept"
firewall-cmd --reload
</pre>
* 批量添加区间端口
<pre>
firewall-cmd --zone=public --add-port=4400-4600/udp --permanent
firewall-cmd --zone=public --add-port=4400-4600/tcp --permanent
</pre>
* 查看端口列表:
<pre>
firewall-cmd --permanent --list-ports
firewall-cmd --permanent --list-all
firewall-cmd --permanent --list-rich-rules
</pre>
* 其他常用命令
** 启动服务:systemctl start firewalld
** 关闭服务:systemctl stop firewalld
** 重启服务:systemctl restart firewalld、firewall-cmd --reload
** 查看服务状态:systemctl status firewalld、firewall-cmd --state
** 开机自启服务:systemctl enable firewalld
** 开机禁用服务:systemctl disable firewalld
** 查看是否开机自启:systemctl is-enable firewalld
=NAS端安装FRPC=
<pre>
[common]
server_addr = xxx.xxx.xxx.xxx # 服务器地址server_port = xxxx # 服务器端口privilege_token = xxxxxx # token = xxxxxx 有些服务端版本采用这个字节,多数采用“privilege_token”字节,token理解为密码
protocol = tcp
[http]
type = http # http协议orhttps协议,如果两个都要的话就单独复制一段[https]local_ip = 192.168.x.xx # 群晖的内网IPlocal_port = xxxxx # 你自己的web服务器端口custom_domains = xxxxxxx.xxx # 如果你有自己的域名,可以设置你自己的域名,先将自己的顶级域名解析到xxxxxxx.xxx如果你有自己的域名,可以设置你自己的域名,先将自己的顶级域名解析到frp服务器域名(用CNAME记录,不要用A记录如二级域名的话用CNAME记录,不要用A记录)# subdomain = nas # 选择一个可用的子域名,你的访问地址将会是http全局下唯一的子域名,你的访问地址将会是http://nassubdomain.xxx.xxx,服务器端必须已绑定顶级域名,否则不要设置;# 此外,顶级域名需要在域名系统上做好CNAME的泛解析,否则不可用。HOST:*.xxx.xxxxxxxxx;Value:xxx.xxx,服务器端必须已绑定顶级域名,否则不要设置xxx。
[ssh]
type = tcp
local_ip = 192.168.x.xxx
local_port = xxxxxx # 需要转发到的端口,ssh端口默认是22remote_port = xxxxxx # frp服务端的远程监听端口,即你访问服务端的remote_port就相当于访问客户端的 local_port,如果填0则会随机分配一个端口
#[DSM]
# type = tcp # tcp协议
# local_ip = 192.168.x.xxx # 127.0.0.1指穿透本机,也可以填写群晖内网IP.# local_port = xxxxxx # 群晖内网HTTP端口,默认为5000.# remote_port = xxxx # 映射到外网端口,暴露给服务器,这个客户端必须唯一# subdomain = nas # 选择一个可用的子域名,你的访问地址将会是http全局下唯一的子域名,你的访问地址将会是http://nassubdomain.xxx.xxx,服务器端必须已绑定顶级域名,否则不要设置;# 此外,顶级域名需要在域名系统上做好CNAME的泛解析,否则不可用。HOST:*.xxx.xxxxxxxxx;Value:xxx.xxx,服务器端必须已绑定顶级域名,否则不要设置xxx。
</pre>
# 当晚宿舍调试握手,成功!
# 第二日,记录了本次搭建和调试的笔记,或许哪一天我的孩子或者朋友需要参考。<gallery>20201222141012.png|frps服务面板</gallery>
 
==2021/12/1后记==
* 将自家一台闲置的G41主板GAMEMAX机箱的主机做成了群晖NAS,需要同时请求Centos的FRP代理.
* 产生的问题就是FRPS的http类监听端口只有各一个(http、https),而处于app需求,每台nas都需要占用http两个端口。
* 如何实现两台nas的代理需求困扰了两天,尝试了网上数个谬误的推荐,本人也不清楚博主最后是否运行稳定了多久……
# 谬误1:取消frps端vhost监听,使用frpc的remote_host访问……,其实从原理上就是不可能的;当然,大鹿在当时无计可施情况下也试了几次,失败;
# 谬误2:不使用type=http,改为type=tcp……,协议上来讲tcp和http协议是有区别的,如果偶尔成功了也只能说是基础协议层面的bug类问题,我尝试过,但是最后还是不稳定放弃了。
* 最终使用了如下方案:
# 域名服务启用CNAME泛解析,HOST:*.xxx.xxx;Value:xxx.xxx;
# FRPS启用:subdomain_host;
# FRPC启用:subdomain。
* 目前为止其中一台nas成功启用frp服务,另外一台在本周更新frpc和docker,因为设备在100多公里外:)
==2021/12/6多http及ssh服务的问题解决==
* 上周将第二台接入后,第一台无法联机,怀疑还是配置问题。回家后还发现NAS的电源出问题了,似乎是损毁了。
* 重新购买电源,更换后还是无法使用subdomain登陆。
* 经过多轮调试和查询FRPS服务说明文档,今天问题发现并解决,属于多台客户端的frpc配置重定义问题:
# 多台frpc中[common]定义不用变化;
# 多台客户机frpc中[http]、[https]、[ssh]等协议代理请求中不能使用重名。比如使用http和ssh的:
## [http-1]、[http-2]、[https-1]、[https-2];
## [ssh-1]、[ssh-2]。
# 以上修正后,所有访问正常,其他协议类定义可以推断同样依次类推。
# 注意:务必要在FRPS和FRP中定义:<subdomain>。
* 其实从理论上也可以理解,如果协议的代理请求是同样[ID],FRPS服务器确实如何去识别这是谁的要求呢?
* 自此,多台frpc的代理服务配置完成,结案。
* 总结:所有的开发源代码的应用,多参照说明文档,少看网络上那些假大神的瞎捣鼓,看还是要看看,走走弯路也可以学到知识。
==关于nas也可以借助FRPS运用远程ssh进行完全管理==
* 可参考[[CentOS服务搭建-03-开启SSH服务]]
490
个编辑

导航菜单