Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[debug] 获取公网ip的api(ipip.net)存在问题 #425

Closed
char-46 opened this issue Feb 15, 2024 · 3 comments
Closed

[debug] 获取公网ip的api(ipip.net)存在问题 #425

char-46 opened this issue Feb 15, 2024 · 3 comments
Labels

Comments

@char-46
Copy link
Contributor

char-46 commented Feb 15, 2024

根据 #420 所述,服务商更改了 api 地址,现在是之前使用的myip4.ipip.net合并到了myip.ipip.net,不确定将来会不会myip6.ipip.net也合并过来…… 虽然目前还没见到像之前搜狐的 api 全部指向上海的离谱情况(#367 #368),但是 ipip.net 对于该 api 的说明中写道不能保证这个服务的质量,结合下一条问题,我建议更换新的默认 api。

现在myip6.ipip.net同时指向了 ipv4 和 ipv6 两个地址,故响应会根据请求分别返回 ipv4 和 ipv6 地址。根据代码,对于 URL 的访问采用了 urllib 的 urlopen 函数,该函数并不能控制通过哪一个版本的 ip 协议连接到服务器,即具体返回哪一个取决于系统优先使用哪一个连接…… 个人认为将其交由系统决定或许不是一个明智的选择。

nslookup 命令返回的结果
$ nslookup myip6.ipip.net 1.1.1.1
Server:         1.1.1.1
Address:        1.1.1.1

Non-authoritative answer:
Name:   myip6.ipip.net
Address: 139.198.30.138
Name:   myip6.ipip.net
Address: 2402:e7c0:0:200::5

综上,我建议撤销 #420 的合并。


将 urlopen 方法使用 socket 重写可以一劳永逸,但重构代价或许远高于更换 api 的方法。

能够区分 ipv4 和 ipv6 的 api 例如 ip.sb 提供的 api。其提供的 https://api-ipv4.ip.sb/iphttps://api-ipv6.ip.sb/ip 分别只解析到了 ipv4 和 ipv6。

nslookup 命令返回的结果
$ nslookup api-ipv4.ip.sb 1.1.1.1
Server:         1.1.1.1
Address:        1.1.1.1

Non-authoritative answer:
Name:   api-ipv4.ip.sb
Address: 104.26.13.31
Name:   api-ipv4.ip.sb
Address: 104.26.12.31

$ nslookup api-ipv6.ip.sb 1.1.1.1
Server:         1.1.1.1
Address:        1.1.1.1#53

Non-authoritative answer:
Name:   api-ipv6.ip.sb
Address: 2a09:0:8::25

在我尝试修改 api之后发现相应会出现权限错误,检查文档后发现其要求使用Mozilla作为 UA。经过实测,该 api 屏蔽了 UA 中包含curl的请求,只要不包含curl作为 UA 即可获取到正确的结果。因此我将 UA 修改为Mozilla/5.0 Python/ddns

commit: 8219fa2

PR: #418

@char-46
Copy link
Contributor Author

char-46 commented Feb 16, 2024

根据 #426,似乎没有办法指定 urlopen 所使用的协议。用 socket 重写一个 http(s) client 的成本或许过于高昂。

@laozhoubuluo
Copy link
Contributor

myip6.ipip.net 现在也遇到了证书过期的问题,现在 IP 接口的问题需要合并 #418 且发版才能解决了。

@char-46
Copy link
Contributor Author

char-46 commented Apr 20, 2024

经过了 #418#419#420#426#435#436,该 Issue 已解决。

@char-46 char-46 closed this as completed Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants