使用 pf 在 OS X 上转发端口时出现问题

使用 pf 在 OS X 上转发端口时出现问题

我正在运行 xhyve 虚拟机(带有 docker),使用桥接网络:

bridge100: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=3<RXCSUM,TXCSUM>
    ether a2:99:9b:01:6d:64
    inet 192.168.64.1 netmask 0xffffff00 broadcast 192.168.64.255
    Configuration:
        id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
        maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
        root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
        ipfilter disabled flags 0x2
    member: en5 flags=3<LEARNING,DISCOVER>
            ifmaxaddr 0 port 12 priority 0 path cost 0
    nd6 options=1<PERFORMNUD>
    media: autoselect
    status: active

我想将我的 Mac 上的端口 8000 转发到 VM IP,即端口 8000。我正在使用 pfctl:

[~]$ echo "
rdr pass inet proto tcp from any to any port 8000 -> 192.168.64.9 port 8000
" | sudo pfctl -ef -
Password:
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.

No ALTQ support in kernel
ALTQ related functions disabled
pfctl: pf already enabled

现在,telnet 到 localhost 8000 显示“连接被拒绝”:

[~]$ telnet localhost 8000
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Network is unreachable
telnet: Unable to connect to remote host

但是,telnet 到“192.168.64.9”可以工作:

[~]$ telnet 192.168.64.9 8000
Trying 192.168.64.9...
Connected to ebdev.io.
Escape character is '^]'.

在这种情况下,我该如何转发端口 8000?

答案1

我在 OS X 中对 pfctl 的重定向功能一无所知。也许我也忽略了一些微妙的东西,但你并不孤单。对我来说有用的是 ssh 端口转发。它不太方便,但很容易,而且总是有效。只需输入:

ssh 127.0.0.1 -L127.0.0.1:8000:192.168.64.9:8000

只要该 ssh 会话处于活动状态,端口转发就会起作用。如果您想将侦听套接字绑定到机器上的所有 IP,而不是仅绑定到本地主机,则可以省略“127.0.0.1”。

如果您弄清楚为什么 rdr 规则不起作用,请告诉我们,因为您绝对不是唯一看到这种行为的人。

相关内容