dnspod-shell icon indicating copy to clipboard operation
dnspod-shell copied to clipboard

求助:“如何直接获取wan口IP地址直接上报而不使用网络查询”

Open refined-fish opened this issue 2 years ago • 13 comments

解析内网IP需要,但运行脚本时会使用网络获取公网IP

refined-fish avatar Sep 14 '22 07:09 refined-fish

将“ardnspod”脚本本体中,ipv4的获取方法删除替换为

local hostIp=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 6-`

#eth要换成自己对应的wan口网卡 解决

refined-fish avatar Sep 14 '22 08:09 refined-fish

可以参考5.0分支中获取网卡ip的办法,并对ip进行正则过滤

rehiy avatar Sep 14 '22 13:09 rehiy

@rehiy 这个就是https://github.com/rehiy/dnspod-shell/pull/112 可以解决的问题,可以考虑一下review?

@refined-fish 您也可以review一下上面的PR,看看内容是否满足需求?

Vigilans avatar Sep 16 '22 08:09 Vigilans

由于获取wan口ip具有非常大的不确定性,所以从6.2分支开始就不再作为主要考虑的范围,改为从外部获取。如果需要这个功能,还是建议使用旧版的分支。https://github.com/rehiy/dnspod-shell/blob/v6.1.x/ardnspod

rehiy avatar Sep 16 '22 08:09 rehiy

@rehiy 这个功能可以不作为默认(也即arWanIp4arWanIp6)提供,但仍可以添加额外参数后提供(即 https://github.com/rehiy/dnspod-shell/pull/112 添加的第四个参数dev)

这样区分之后,对于广大一般用户保证默认的从外部获取的行为,而添加额外的参数是用户的显式行为,表明用户是知道自己在做什么的,这样分层个人感觉还是合理的。

而且从外部获取地址往往获取的都是IPv6临时地址,过一段时间就会变,而对于服务器来说往往永久地址更好。否则在IP地址变的一小段时间服务器域名直接变成不可达状态。

Vigilans avatar Sep 16 '22 08:09 Vigilans

可以同步维护6.1分支

rehiy avatar Sep 16 '22 08:09 rehiy

v6.2 分支执行的策略是:

ipv4 默认从本地获取,如果获取失败,降级为使用dnspod的ddns接口自动获取 ipv6 默认从本地获取,如果获取失败,从外部接口获取(将来ddns接口支持后改为和v4一致的策略)

从多年维护的情况来看,这是符合大多数用户的策略。极个别用户需要上报私网ip到dnspod,是需要修改获取ip的正则的。

rehiy avatar Sep 16 '22 08:09 rehiy

v6.1.x使用的是正则,并且对多IPv6地址没有什么偏好(尽管官方定义了各种IPv6地址的特点,这些都显示在了ip addr里的flag里),这有一部分是因为对IPv6地址的使用经验和了解不够成熟导致的。

#112 里并没有使用正则来处理,而是直接使用ip addr里的各种flag来识别地址,这是因为正则的错误率确实很高,而使用地址的flag则是完全在RFC定义的IPv6规范下取得地址的,相比正则可以保证结果一定是符合预期的:

       dynamic and permanent
              (IPv6 only) only list addresses installed due to stateless
              address configuration or only list permanent (not dynamic)
              addresses. These two flags are inverses of each other, so
              -dynamic is equal to permanent and -permanent is equal to
              dynamic.

       tentative
              (IPv6 only) only list addresses which have not yet passed
              duplicate address detection.

       -tentative
              (IPv6 only) only list addresses which are not in the
              process of duplicate address detection currently.

       deprecated
              (IPv6 only) only list deprecated addresses.

       -deprecated
              (IPv6 only) only list addresses not being deprecated.

       temporary or secondary
              List temporary IPv6 or secondary IPv4 addresses only. The
              Linux kernel shares a single bit for those, so they are
              actually aliases for each other although the meaning
              differs depending on address family.

       -temporary or -secondary
              These flags are aliases for primary.

       primary
              List only primary addresses, in IPv6 exclude temporary
              ones. This flag is the inverse of temporary and secondary.

       -primary
              This is an alias for temporary or secondary.

实际上整个流程就不应该优先考虑依赖正则,例如v6.1.x里那么多preferred_lft的排序处理,明明直接加上-deprecated就可以过滤掉所有无效的地址,用正则和awk解析反而很难达到效果。#112 则是严格按照https://man7.org/linux/man-pages/man8/ip-address.8.html的规范来获取地址的。

Vigilans avatar Sep 16 '22 08:09 Vigilans

此外,v6.2已经规定好了“默认情况下作为客户端,查询对外地址”(从wan口获取是不能符合这个要求的,而从ip route get获取则符合),这样把语义规范好了是件很好的事。

作为主分支,我们是否可以开始考虑基于这个规范好的默认语义,开始提供更多语义:

  • arDdnsCheck domain subdomain 6:默认语义,查询默认对外地址(IPv6里为temporary flag地址)
  • arDdnsCheck domain subdomain 6 dev:查询dev上的永久地址(IPv6里为permanent flag地址)
  • arDdnsCheck domain subdomain 6 dev mac:作为路由器,获取子设备地址(在IPv6里,这非常有用, 可以仅在路由器端为大量子设备(乃至物联网设备)大批量更新域名)。

Vigilans avatar Sep 16 '22 08:09 Vigilans

这是因为最初很多用户报告不支持一些参数才这样设计的,比如 ip route 命令,在很多老旧的设备上是不存在的。 6.2分支可以尝试考虑抛去老旧设备的包袱。

rehiy avatar Sep 16 '22 08:09 rehiy

这是因为最初很多用户报告不支持一些参数才这样设计的,比如 ip route 命令,在很多老旧的设备上是不存在的。 6.2分支可以尝试考虑抛去老旧设备的包袱。

这个确实很有道理,考虑到各个系统的不一样,直接获取存在不可用的可能,不过要是能够将直接获取指令作为一个可选项也是可以的,通过传入参数或者conf指定获取方式

refined-fish avatar Sep 16 '22 10:09 refined-fish

@rehiy 这个就是#112 可以解决的问题,可以考虑一下review?

@refined-fish 您也可以review一下上面的PR,看看内容是否满足需求?

感谢帮助,我试过dev,存在作者提到的指令不可用情况,dev参数在我的固件上不生效,故我采用的if指定接口获取的

refined-fish avatar Sep 16 '22 10:09 refined-fish

我试过dev,存在作者提到的指令不可用情况,dev参数在我的固件上不生效

@refined-fish 是指ip addr show dev eth0失败了吗?(注意需要show,ip addr dev eth0是不行的)

考虑到各个系统的不一样,直接获取存在不可用的可能

实际上我是考虑到了ip addr(也就是ip-address)已经在之前的ardnspod有所使用了,所以继续使用这一命令的;ip-route由于之前没用过,确实可能在某些固件上不存在。

Vigilans avatar Sep 16 '22 10:09 Vigilans

已合并之前的PR,可以测试下

rehiy avatar Nov 24 '22 15:11 rehiy