V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
K8dcnPEZ6V8b8Z6
V2EX  ›  宽带症候群

公网 IP 的服务,只能从「非本机地址」访问,求解

  •  
  •   K8dcnPEZ6V8b8Z6 · 2023-05-02 16:11:43 +08:00 · 2664 次点击
    这是一个创建于 368 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题,手头有公网 v4 ,在 NUC 上开了 TCP 和 UDP 服务若干(群晖 portal ,DNS ,WEB 等),并通过主路由( TL-R5408PE )做好端口映射,对外皆为高端口。经测试,从外部 IP (两台 VPS 和手机热点)访问这些服务都正常,但从同一内网的机器走公网 IP 访问的话,全部失败。
    个人猜测应当修改路由器的 NAT 或路由设置,但由于网络知识所限,不知应改哪个地方,程心求助!
    25 条回复    2023-05-16 21:55:03 +08:00
    fat00119
        1
    fat00119  
       2023-05-02 16:13:45 +08:00 via Android
    端口回流
    opengps
        2
    opengps  
       2023-05-02 16:23:33 +08:00 via Android
    有的路由器确实会内部访问不到自己外部端口
    crab
        3
    crab  
       2023-05-02 16:49:33 +08:00
    Archeb
        4
    Archeb  
       2023-05-02 19:25:38 +08:00
    需要做 Hairpin NAT ,也就是端口回流,可能是你的路由器固件没有自动实现这个功能。
    opengps
        5
    opengps  
       2023-05-02 19:43:58 +08:00
    对了,我当时变通解决,用 hosts 强制指定域名解析到内网指定的机器
    K8dcnPEZ6V8b8Z6
        6
    K8dcnPEZ6V8b8Z6  
    OP
       2023-05-02 21:29:14 +08:00
    @Archeb 事实上好像没这个功能……
    K8dcnPEZ6V8b8Z6
        7
    K8dcnPEZ6V8b8Z6  
    OP
       2023-05-02 21:30:23 +08:00
    @fat00119 多谢大佬快速回复,搜到了一些教程
    @opengps 找来找去好像真的只能用自建 DNS 的方法解决了,遗憾
    handshake
        8
    handshake  
       2023-05-04 15:32:34 +08:00
    防火墙添加一条 nat 规则,转换源地址为内部网段即可
    tkyb123
        9
    tkyb123  
       2023-05-05 14:13:46 +08:00
    小米路由器同样遇到了这样的问题。。。不知道如何解决,求大神指点
    ziwen1943
        10
    ziwen1943  
       2023-05-05 15:38:17 +08:00
    端口回源没配置吧,我以前也遇到过这种问题,一般是对应的防火墙或者负载均衡加 NAT 或者回源规则就好。
    busier
        11
    busier  
       2023-05-05 19:21:22 +08:00
    RouterOS 也遇到这个问题!网上找的解决脚本也是错误一堆!好在自己慢慢改搞定了!
    jinqzzz
        12
    jinqzzz  
       2023-05-05 19:57:51 +08:00
    tp 企业路由是在 NAPT 设置源地址,但是我用过的 tp 路由上这个功能没一个正常的
    dont39
        13
    dont39  
       364 天前
    如果有 ipv6 地址的话,可以在域名解析那边加上设备的 ipv6 地址,内网的机器通过域名访问的时候会优先走 ipv6 从而绕过这个问题。
    raycheung
        14
    raycheung  
       364 天前
    @busier #11 老兄是如何配置实现的,能劳驾贴下脚本参考学习下吗?
    K8dcnPEZ6V8b8Z6
        15
    K8dcnPEZ6V8b8Z6  
    OP
       364 天前
    @jinqzzz 我后面解决了,是问客服要了另一个版本的固件,没想到更新后就可以了
    busier
        16
    busier  
       363 天前
    @raycheung

    Router OS 端口映射,支持回流

    /ip firewall nat
    add action=src-nat chain=srcnat comment=SNAT to-addresses=111.175.157.94
    add action=dst-nat chain=dstnat comment=HairpinNAT dst-address=111.175.157.94 dst-port=21194 protocol=udp to-addresses=192.168.88.2 to-ports=21194
    add action=dst-nat chain=dstnat comment=HairpinNAT dst-address=111.175.157.94 dst-port=8443 protocol=tcp to-addresses=192.168.88.201 to-ports=8443

    说明:action=src-nat 规则行设置 NAT ,action=dst-nat 规则行设置支持回流的端口映射
    其中 111.175.157.94 为公网 IP ,这里可以不手动修改,利用下面的脚本自动修改!
    特别注意的是,注释:comment 指令不能省略,下面的脚本根据这个来更新规则的!

    ================================================
    Router OS 自动更新上面规则中公网 IP 的脚本

    :global ipaddr [/ip address get [/ip address find interface="pppoe-out1"] address];
    :set ipaddr [:pick $ipaddr 0 ([len $ipaddr]-3)];

    :global oldip [/ip firewall nat get [/ip firewall nat find chain=srcnat action=src-nat] to-addresses];

    :if ($ipaddr != $oldip) do={
    /ip firewall nat set [/ip firewall nat find chain=srcnat action=src-nat comment="SNAT"] to-addresses=$ipaddr;
    /ip firewall nat set [/ip firewall nat find chain=dstnat action=dst-nat comment="HairpinNAT"] dst-address=$ipaddr;
    }

    将此脚本放到 RouterOS 中运行,其中 pppoe-out1 是 PPPoE 拨号接口的名字!
    这个脚本从 pppoe-out1 接口获取公网 IP ,然后去 firewall nat 表更新 comment="SNAT" 和 comment="HairpinNAT" 规则中的公网 IP
    设置这个脚本在 RouterOS 中每 10 分钟运行一下就可以了,完美解决 RouterOS 回流问题!

    就是这么简单!
    raycheung
        17
    raycheung  
       362 天前
    @busier #16 感谢老哥
    fastcache
        18
    fastcache  
       362 天前
    @raycheung 不用脚本的配置

    /ip firewall nat
    add action=masquerade chain=srcnat comment="Hairpin NAT" connection-mark=Hairpin-NAT
    /ip firewall mangle
    add action=mark-connection chain=prerouting comment="Mark connections for hairpin NAT" dst-address-list=wan-ip new-connection-mark=Hairpin-NAT passthrough=yes src-address-list=LANs
    raycheung
        19
    raycheung  
       362 天前
    @fastcache #18 多谢大佬提供不一样的思路,相对 op 的方案,个人感觉你这个会更优。
    busier
        20
    busier  
       362 天前
    @fastcache 你的配置中 dst-address-list=wan-ip ,如果是 PPPoE 动态获得地址,你的 wan-ip 这个地址列表如何更新?
    fastcache
        21
    fastcache  
       362 天前
    @busier 或许我这个配置比较笨
    /ip firewall address-list
    add address=****(你的 ddns ) list=wan-ip
    busier
        22
    busier  
       361 天前 via iPhone
    @fastcache 是个方法
    ppbaozi
        23
    ppbaozi  
       361 天前
    @raycheung
    楼上都过于复杂了...只需要一条配置
    /ip firewall nat
    add action=masquerade chain=srcnat src-address=192.168.1.0/24 dst-address=192.168.1.0/24 out-interface=LAN

    192.168.1.0/24 换成你的内网段
    out-interface 换成你的内网端口或桥
    busier
        24
    busier  
       354 天前
    @fastcache 今天试验了一下!你的方法有问题!当 WAN 口不能解析 ddns 地址的时候,wan-ip 直接从 address-list 中消失!
    例如重启 ROS 之后 wan-ip 这个 address-list 就没了!
    busier
        25
    busier  
       354 天前
    @ppbaozi 这方法好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   870 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 19:24 · PVG 03:24 · LAX 12:24 · JFK 15:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.