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

myip4.ipip.net 似乎也挂了,修改了 ua(#397)。 #418

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 24 additions & 18 deletions util/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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

try:
# python2
from urllib2 import urlopen, Request
Expand All @@ -12,10 +13,10 @@
from urllib.request import urlopen, Request

# IPV4正则
IPV4_REG = r'((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])'
IPV4_REG = r"((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
# IPV6正则
# https://community.helpsystems.com/forums/intermapper/miscellaneous-topics/5acc4fcf-fa83-e511-80cf-0050568460e4
IPV6_REG = r'((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){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[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))" # noqa: E501


def default_v4(): # 默认连接外网的ipv4
Expand All @@ -28,7 +29,7 @@ def default_v4(): # 默认连接外网的ipv4

def default_v6(): # 默认连接外网的ipv6
s = socket(AF_INET6, SOCK_DGRAM)
s.connect(('1:1:1:1:1:1:1:1', 8))
s.connect(("1:1:1:1:1:1:1:1", 8))
ip = s.getsockname()[0]
s.close()
return ip
Expand All @@ -49,20 +50,25 @@ def local_v4(i=0): # 本地ipv4地址
def _open(url, reg):
try:
debug("open: %s", url)
res = urlopen(
Request(url, headers={'User-Agent': 'curl/7.63.0-ddns'}), timeout=60
).read().decode('utf8', 'ignore')
debug("response: %s", res)
res = (
urlopen(
Request(url, headers={"User-Agent": "Mozilla/5.0 Python/ddns"}),
timeout=60,
)
.read()
.decode("utf8", "ignore")
)
debug("response: %s", res)
return compile(reg).search(res).group()
except Exception as e:
error(e)


def public_v4(url="https://myip4.ipip.net", reg=IPV4_REG): # 公网IPV4地址
def public_v4(url="https://api-ipv4.ip.sb/ip ", reg=IPV4_REG): # 公网IPV4地址
return _open(url, reg)


def public_v6(url="https://myip6.ipip.net", reg=IPV6_REG): # 公网IPV6地址
def public_v6(url="https://api-ipv6.ip.sb/ip ", reg=IPV6_REG): # 公网IPV6地址
return _open(url, reg)


Expand All @@ -71,10 +77,10 @@ def _ip_regex_match(parrent_regex, match_regex):
ip_pattern = compile(parrent_regex)
matcher = compile(match_regex)

if os_name == 'nt': # windows:
cmd = 'ipconfig'
if os_name == "nt": # windows:
cmd = "ipconfig"
else:
cmd = 'ifconfig 2>/dev/null || ip address'
cmd = "ifconfig 2>/dev/null || ip address"

for s in popen(cmd).readlines():
addr = ip_pattern.search(s)
Expand All @@ -83,16 +89,16 @@ def _ip_regex_match(parrent_regex, match_regex):


def regex_v4(reg): # ipv4 正则提取
if os_name == 'nt': # Windows: IPv4 xxx: 192.168.1.2
regex_str = r'IPv4 .*: ((?:\d{1,3}\.){3}\d{1,3})\W'
if os_name == "nt": # Windows: IPv4 xxx: 192.168.1.2
regex_str = r"IPv4 .*: ((?:\d{1,3}\.){3}\d{1,3})\W"
else:
regex_str = r'inet (?:addr\:)?((?:\d{1,3}\.){3}\d{1,3})[\s/]'
regex_str = r"inet (?:addr\:)?((?:\d{1,3}\.){3}\d{1,3})[\s/]"
return _ip_regex_match(regex_str, reg)


def regex_v6(reg): # ipv6 正则提取
if os_name == 'nt': # Windows: IPv4 xxx: ::1
regex_str = r'IPv6 .*: ([\:\dabcdef]*)?\W'
if os_name == "nt": # Windows: IPv4 xxx: ::1
regex_str = r"IPv6 .*: ([\:\dabcdef]*)?\W"
else:
regex_str = r'inet6 (?:addr\:\s*)?([\:\dabcdef]*)?[\s/%]'
regex_str = r"inet6 (?:addr\:\s*)?([\:\dabcdef]*)?[\s/%]"
return _ip_regex_match(regex_str, reg)
Loading