From 864a5235f6b34765c5d8b0668843351b6036ffdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=91=A8=E9=83=A8=E8=90=BD?= <31075951+laozhoubuluo@users.noreply.github.com> Date: Sun, 27 Nov 2022 23:20:26 +0800 Subject: [PATCH] fix(ip): except socket error and using valid IPv6 endpoint --- util/ip.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/util/ip.py b/util/ip.py index b71a21ea..aeb19cd7 100644 --- a/util/ip.py +++ b/util/ip.py @@ -3,7 +3,7 @@ from re import compile from os import name as os_name, popen from socket import socket, getaddrinfo, gethostname, AF_INET, AF_INET6, SOCK_DGRAM -from logging import debug, error +from logging import debug, warning, error try: # python2 from urllib2 import urlopen, Request @@ -19,19 +19,29 @@ def default_v4(): # 默认连接外网的ipv4 - s = socket(AF_INET, SOCK_DGRAM) - s.connect(("1.1.1.1", 53)) - ip = s.getsockname()[0] - s.close() - return ip + try: + s = socket(AF_INET, SOCK_DGRAM) + s.connect(("1.1.1.1", 53)) + ip = s.getsockname()[0] + s.close() + return ip + except Exception as e: + debug(e) + warning('This device not have IPv4 default route, cannot get valid IPv4 address for DDNS.') + return False def default_v6(): # 默认连接外网的ipv6 - s = socket(AF_INET6, SOCK_DGRAM) - s.connect(('1:1:1:1:1:1:1:1', 8)) - ip = s.getsockname()[0] - s.close() - return ip + try: + s = socket(AF_INET6, SOCK_DGRAM) + s.connect(("2606:4700:4700::1111", 53)) + ip = s.getsockname()[0] + s.close() + return ip + except Exception as e: + debug(e) + warning('This device not have IPv6 default route, cannot get valid IPv6 address for DDNS.') + return False def local_v6(i=0): # 本地ipv6地址