zhboner / realm

A network relay tool
MIT License
1.61k stars 291 forks source link

feature request: inbound interface binding #138

Closed Zerorigin closed 1 week ago

Zerorigin commented 2 months ago

打算将 realm 部署在网关路由器上,网关上的虚拟接口 lan(br-lan)绑定了多个网口设备 ethX,并通过 vlan 划分了多个虚拟子接口,我想在其中一个虚拟子接口监听特定端口进行转发,但 realm 似乎还是会监听所有的 IPv4、IPv6 地址,而不是仅监听虚拟子接口 interface 上的 IPv4、IPv6 地址。

我的配置如下:

[log]
level = "warn"

[network]
no_tcp = false
use_udp = false

# DeepLX - API
[[endpoints]]
listen = "[::]:1188"
remote = "1.2.3.4:1188"
interface = "vlan_Services"
zephyrchien commented 2 months ago

@Zerorigin Thanks for the detailed description which is really helpful to understand your use case and locate the problem.

Currently the interface argument is just for outbound connections/traffics. Inbound ones has not been implemented but I will add this missing feature soon when I have time.

zephyrchien commented 2 weeks ago

@Zerorigin Hey, maybe you could use specific ip address instead of [::]

Zerorigin commented 2 weeks ago

@Zerorigin Hey, maybe you could use specific ip address instead of [::]

不太可行,因为 IPv6 地址不是固定的。

zephyrchien commented 2 weeks ago

Okay. Technically I think it's feasible to bind a LISTEN socket to a given interface with setsockopt(SO_BINDTODEVICE), which would be helpful to your use case.

zephyrchien commented 1 week ago

Hey @Zerorigin ,

Now you could specify the inbound interface by adding -e <iface> to cmd arguments; or with a config file (docs here):

[[endpoint]]
#...
listen_interface = "<iface>"
Zerorigin commented 1 week ago

Hey @Zerorigin ,

Now you could specify the inbound interface by adding -e <iface> to cmd arguments; or with a config file (docs here):

[[endpoint]]
#...
listen_interface = "<iface>"

大佬,有点强,setsockopt(SO_BINDTODEVICE)相关资料其实之前我也搜过,但是感觉自己估计搞不定,就没尝试编写代码测试。 我这两天抽空测试下新的配置项,到时候再来找您反馈。

Zerorigin commented 1 week ago

Now you could specify the inbound interface by adding -e <iface> to cmd arguments; or with a config file (docs here):

[[endpoint]]
#...
listen_interface = "<iface>"

在基于 Linux 内核的路由网关平台上测试了下, 能在子接口的 IPv4 & IPv6 入站正确监听相关端口并转发了, 并且网关宽带重新拨号后,也能在子接口新分配的 IP 上监听, 感觉没啥问题了,这个 issue 应该可以考虑关闭了。