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

fix(ip): except socket error and using valid IPv6 endpoint #373

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions util/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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地址
Expand Down
Loading