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

feat(ip): support specify IP protocol version to initiate a request #426

Closed
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
34 changes: 25 additions & 9 deletions util/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
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
except ImportError:
# python3
from urllib.request import urlopen, Request
from urllib.request import urlopen, Request
from urllib.parse import urlparse

# 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])'
# 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}){1,3})|((:[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

dns_mapping = {}
_orig_getaddrinfo = getaddrinfo


def default_v4(): # 默认连接外网的ipv4
s = socket(AF_INET, SOCK_DGRAM)
Expand Down Expand Up @@ -46,9 +45,26 @@ def local_v4(i=0): # 本地ipv4地址
return info[int(i)][-1][0]


def _open(url, reg):
def _custom_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
debug("host: %s", host)
if host in dns_mapping:
ip = dns_mapping[host]
debug("ip: %s", ip)
return [(family, type, proto, '', (ip, port))]
else:
return _orig_getaddrinfo(host, port, family, type, proto, flags)


socket.getaddrinfo = _custom_getaddrinfo


def _open(url, reg, sock):
try:
debug("open: %s", url)
parse = urlparse(url)
ip = _orig_getaddrinfo(parse.hostname, None, sock)[0][4][0]
debug("ip address: %s", ip)
dns_mapping[parse.hostname] = ip
res = urlopen(
Request(url, headers={'User-Agent': 'curl/7.63.0-ddns'}), timeout=60
).read().decode('utf8', 'ignore')
Expand All @@ -59,11 +75,11 @@ def _open(url, reg):


def public_v4(url="https://myip.ipip.net", reg=IPV4_REG): # 公网IPV4地址
return _open(url, reg)
return _open(url, reg, AF_INET)


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


def _ip_regex_match(parrent_regex, match_regex):
Expand Down
Loading