[GH-ISSUE #3127] 内网服务通过frp访问公网服务 #2508

Closed
opened 2026-05-05 13:37:00 -06:00 by gitea-mirror · 6 comments
Owner

Originally created by @gocpplua on GitHub (Oct 12, 2022).
Original GitHub issue: https://github.com/fatedier/frp/issues/3127

Bug Description

我的网络拓扑如下:
公网服务A -- FRPS -- FRPC(主机1) -- 内网服务B(主机2)

服务AB都是微服务,使用REST API架构。 当测试的时候,AB都在内网,我可以使用IP直接通过HTTP访问,但是现在必须将A部署在外网,B部署在内网。同时还有一个限制是主机1上可以访问公网,我部署了FRPC。 主机2不能访问公网,部署服务B。

我遇到的问题是,现在服务A 可以 访问到B的接口, 但是 服务B 如何通过主机1上的FRPC 访问到 公网服务A的接口?

frpc Version

0.44.0

frps Version

0.44.0

System Architecture

linux/amd64, windows/amd64

Configurations

frps.ini

[common]
vhost_http_port = 7000

#日志记录等级,有trace, debug, info, warn, error
log_level = debug
#日志保留时间
log_max_days = 3

Logs

No response

Steps to reproduce

...

Affected area

  • Docs
  • Installation
  • Performance and Scalability
  • Security
  • User Experience
  • Test and Release
  • Developer Infrastructure
  • Client Plugin
  • Server Plugin
  • Extensions
  • Others
Originally created by @gocpplua on GitHub (Oct 12, 2022). Original GitHub issue: https://github.com/fatedier/frp/issues/3127 ### Bug Description 我的网络拓扑如下: 公网服务A -- FRPS -- FRPC(主机1) -- 内网服务B(主机2) 服务AB都是微服务,使用REST API架构。 当测试的时候,AB都在内网,我可以使用IP直接通过HTTP访问,但是现在必须将A部署在外网,B部署在内网。同时还有一个限制是主机1上可以访问公网,我部署了FRPC。 主机2不能访问公网,部署服务B。 我遇到的问题是,现在服务A 可以 访问到B的接口, 但是 服务B 如何通过主机1上的FRPC 访问到 公网服务A的接口? ### frpc Version 0.44.0 ### frps Version 0.44.0 ### System Architecture linux/amd64, windows/amd64 ### Configurations frps.ini [common] vhost_http_port = 7000 #日志记录等级,有trace, debug, info, warn, error log_level = debug #日志保留时间 log_max_days = 3 ### Logs _No response_ ### Steps to reproduce 1. 2. 3. ... ### Affected area - [ ] Docs - [ ] Installation - [ ] Performance and Scalability - [ ] Security - [ ] User Experience - [ ] Test and Release - [ ] Developer Infrastructure - [ ] Client Plugin - [ ] Server Plugin - [ ] Extensions - [ ] Others
Author
Owner

@fatedier commented on GitHub (Oct 12, 2022):

使用 stcp 或在主机 1 部署 http proxy。

<!-- gh-comment-id:1275510579 --> @fatedier commented on GitHub (Oct 12, 2022): 使用 stcp 或在主机 1 部署 http proxy。
Author
Owner

@gocpplua commented on GitHub (Oct 12, 2022):

使用 stcp 或在主机 1 部署 http proxy。

我google了一圈,issue也看了。 发现 #2280 这问题类似。 官网上也说了通过代理连接 frps。 看了https://github.com/fatedier/frp/blob/dev/conf/frpc_full.ini, 但是配置是怎么样的还是不太清楚。
我举个例子:
假设主机1: 192.168.1.128 主机2: 192.168.1.130
现在frps.ini 配置是:
[common]
vhost_http_port = 7000

那么我是只要在主机1配置 frpc.ini:
[common]
server_addr = x.x.x.x
server_port = 7000
http_proxy = http://user:pwd@192.168.1.128:8080

那么,我由下面的问题:

  1. 我主机2 是不是不需要启动frpc,只要在发送http请求的时候,指向主机1:192.168.1.128:8080?
  2. 上面的user:pwd是在哪里设置的?
<!-- gh-comment-id:1275534564 --> @gocpplua commented on GitHub (Oct 12, 2022): > 使用 stcp 或在主机 1 部署 http proxy。 我google了一圈,issue也看了。 发现 #2280 这问题类似。 官网上也说了[通过代理连接 frps](https://gofrp.org/docs/features/common/client/#%E9%80%9A%E8%BF%87%E4%BB%A3%E7%90%86%E8%BF%9E%E6%8E%A5-frps)。 看了https://github.com/fatedier/frp/blob/dev/conf/frpc_full.ini, 但是配置是怎么样的还是不太清楚。 我举个例子: 假设主机1: 192.168.1.128 主机2: 192.168.1.130 现在frps.ini 配置是: [common] vhost_http_port = 7000 那么我是只要在主机1配置 frpc.ini: [common] server_addr = x.x.x.x server_port = 7000 http_proxy = http://user:pwd@192.168.1.128:8080 那么,我由下面的问题: 1. 我主机2 是不是不需要启动frpc,只要在发送http请求的时候,指向主机1:192.168.1.128:8080? 2. 上面的user:pwd是在哪里设置的?
Author
Owner

@berlin2123 commented on GitHub (Oct 12, 2022):

如果你的内网服务B无法访问外网。首先保证主机1可以访问服务A的接口(利用frp实现,可以在frps的服务器上运行frpc,或者在其他能够直接访问服务A的能访问外网的主机上运行frpc)。 然后,在主机1上端口转发(firewall-cmd实现,转发主机1上的某个端口到frps服务器的对应端口,或者在主机1上使用stcp并firewall-cmd转发主机1某个端口到stcp设置的那个端口),服务B直接访问主机1的ip和端口就行。

<!-- gh-comment-id:1276337608 --> @berlin2123 commented on GitHub (Oct 12, 2022): 如果你的内网服务B无法访问外网。首先保证主机1可以访问服务A的接口(利用frp实现,可以在frps的服务器上运行frpc,或者在其他能够直接访问服务A的能访问外网的主机上运行frpc)。 然后,在主机1上端口转发(firewall-cmd实现,转发主机1上的某个端口到frps服务器的对应端口,或者在主机1上使用stcp并firewall-cmd转发主机1某个端口到stcp设置的那个端口),服务B直接访问主机1的ip和端口就行。
Author
Owner

@gocpplua commented on GitHub (Oct 17, 2022):

如果你的内网服务B无法访问外网。首先保证主机1可以访问服务A的接口(利用frp实现,可以在frps的服务器上运行frpc,或者在其他能够直接访问服务A的能访问外网的主机上运行frpc)。 然后,在主机1上端口转发(firewall-cmd实现,转发主机1上的某个端口到frps服务器的对应端口,或者在主机1上使用stcp并firewall-cmd转发主机1某个端口到stcp设置的那个端口),服务B直接访问主机1的ip和端口就行。

感谢,有些复杂,我去尝试下 @berlin2123

<!-- gh-comment-id:1280408773 --> @gocpplua commented on GitHub (Oct 17, 2022): > 如果你的内网服务B无法访问外网。首先保证主机1可以访问服务A的接口(利用frp实现,可以在frps的服务器上运行frpc,或者在其他能够直接访问服务A的能访问外网的主机上运行frpc)。 然后,在主机1上端口转发(firewall-cmd实现,转发主机1上的某个端口到frps服务器的对应端口,或者在主机1上使用stcp并firewall-cmd转发主机1某个端口到stcp设置的那个端口),服务B直接访问主机1的ip和端口就行。 感谢,有些复杂,我去尝试下 @berlin2123
Author
Owner

@ax2009live commented on GitHub (Oct 19, 2022):

主机2 需访问公网服务A,而它不能上外网
方法1:通过一个正向代理上访问,正向代理用 frps 和 frpc 实现;
方法2:路由器开通访问公网服务A指定端口

方法1
主机1 frpc,ini

[common]
server_addr = x.x.x.x (frps 的地址)
server_port = 7000
token = admin (密码)

[http_proxy-128]
type = tcp
remote_port = 8080
plugin = http_proxy
plugin_http_user = user
plugin_http_passwd = password
use_encryption = true
use_compression = true
tls_enable = true

外网程序使用 frps 代理端口 8080 就相当于使用 主机1 作为代理
主机1主机2 在同一局域网,可以相互访问,因此外网程序使用 frps 代理端口 8080 可以访问局域网 主机2 任意开放了的端口;
主机2 不需要启动frpc,

公网服务A 可以通过 frps 代理端口 8080 访问到 服务B的接口 (通过 tcp 转发更直接,只是用来说明理解http_proxy使用方法 )

frps 两种情况
情况1:主机1 所在局域网路由器有公网 ip,frps 跟 主机2 同一局域网,服务B 可以通过 frps 代理端口 8080 访问到 公网服务A的接口
情况2:主机1 所在局域网路由器无公网 ip,frps 跟 主机2 不同在一局域网,主机1 增加 frps1 和 frpc1,
主机1 frpc1,ini

[common]
server_addr = 192.168.1.128 (frps1 的地址:localhost 、127.0.0.1 都可以)
server_port = 7000
token = admin (密码)

[http_proxy-128]
type = tcp
remote_port = 8080
plugin = http_proxy
plugin_http_user = user
plugin_http_passwd = password
use_encryption = true
use_compression = true
tls_enable = true

服务B 可以通过 frps1 代理端口 8080 访问到 公网服务A的接口

方法2
如下最简单:
如果能通过ssh进入路由器后台,在路由器开机脚本加入如下命令即可
iptables -I FORWARD -s 192.168.1.130 -d 外网访问A的ip -p tcp --dport 8081 -j ACCEPT
允许局域网 ip 192.168.1.130 能访问外网服务A的 8081端口;
假设公网服务A的接口端口为 8081

另外逆向思维一下:把中国大陆看做互联网,把除了中国之外看做 局域网
根据上面的方法,frps 在互联网内,frpc 在 局域网 内,frpc 配置 http_proxy,
相关程序使用 frps 的 http_proxy 端口,即可不受限制的访问 局域网 ,懂的都会明白;

<!-- gh-comment-id:1283381738 --> @ax2009live commented on GitHub (Oct 19, 2022): 主机2 需访问公网服务A,而它不能上外网 方法1:通过一个正向代理上访问,正向代理用 frps 和 frpc 实现; 方法2:路由器开通访问公网服务A指定端口 方法1 主机1 frpc,ini <pre> [common] server_addr = x.x.x.x (frps 的地址) server_port = 7000 token = admin (密码) [http_proxy-128] type = tcp remote_port = 8080 plugin = http_proxy plugin_http_user = user plugin_http_passwd = password use_encryption = true use_compression = true tls_enable = true</pre> 外网程序使用 frps 代理端口 8080 就相当于使用 **_主机1_** 作为代理 **_主机1_** 和 **_主机2_** 在同一局域网,可以相互访问,因此外网程序使用 frps 代理端口 8080 可以访问局域网 **_主机2_** 任意开放了的端口; **主机2** 不需要启动frpc, 公网服务A 可以通过 frps 代理端口 8080 访问到 服务B的接口 (通过 tcp 转发更直接,只是用来说明理解http_proxy使用方法 ) frps 两种情况 情况1:**_主机1_** 所在局域网路由器有公网 ip,frps 跟 **_主机2_** 同一局域网,服务B 可以通过 frps 代理端口 8080 访问到 公网服务A的接口 情况2:**_主机1_** 所在局域网路由器无公网 ip,frps 跟 **_主机2_** 不同在一局域网,**_主机1_** 增加 frps1 和 frpc1, 主机1 frpc1,ini <pre> [common] server_addr = 192.168.1.128 (frps1 的地址:localhost 、127.0.0.1 都可以) server_port = 7000 token = admin (密码) [http_proxy-128] type = tcp remote_port = 8080 plugin = http_proxy plugin_http_user = user plugin_http_passwd = password use_encryption = true use_compression = true tls_enable = true</pre> 服务B 可以通过 frps1 代理端口 8080 访问到 公网服务A的接口 方法2 如下最简单: 如果能通过ssh进入路由器后台,在路由器开机脚本加入如下命令即可 iptables -I FORWARD -s 192.168.1.130 -d 外网访问A的ip -p tcp --dport 8081 -j ACCEPT 允许局域网 ip 192.168.1.130 能访问外网服务A的 8081端口; 假设公网服务A的接口端口为 8081 另外逆向思维一下:把中国大陆看做互联网,把除了中国之外看做 **_局域网_**, 根据上面的方法,frps 在互联网内,frpc 在 **_局域网_** 内,frpc 配置 http_proxy, 相关程序使用 frps 的 http_proxy 端口,即可不受限制的访问 **_局域网_** ,懂的都会明白;
Author
Owner

@gocpplua commented on GitHub (Oct 21, 2022):

局域网路由器有公网 ip,frps 跟 主机2 同一局域网,服务B 可以通过 frps 代理端口 8080 访问到 公网服务A的接口

明白了,感谢!!

<!-- gh-comment-id:1286842823 --> @gocpplua commented on GitHub (Oct 21, 2022): > 局域网路由器有公网 ip,frps 跟 **_主机2_** 同一局域网,服务B 可以通过 frps 代理端口 8080 访问到 公网服务A的接口 明白了,感谢!!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/frp#2508
No description provided.