From c86afe29baa2adb3c3dcce33a02203810988bf17 Mon Sep 17 00:00:00 2001 From: shortcutme Date: Sun, 20 Jan 2019 03:21:27 +0100 Subject: [PATCH] GetIpType and createSocket helper functions --- src/util/helper.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/util/helper.py b/src/util/helper.py index 3f67f141..3e81843c 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -245,3 +245,20 @@ def isIp(ip): local_ip_pattern = re.compile(r"^(127\.)|(192\.168\.)|(10\.)|(172\.1[6-9]\.)|(172\.2[0-9]\.)|(172\.3[0-1]\.)|(::1$)|([fF][cCdD])") def isPrivateIp(ip): return local_ip_pattern.match(ip) + + +def getIpType(ip): + if ip.endswith(".onion"): + return "onion" + elif ":" in ip: + return "ipv6" + else: + return "ipv4" + + +def createSocket(ip): + ip_type = getIpType(ip) + if ip_type == "ipv6": + return socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + else: + return socket.socket(socket.AF_INET, socket.SOCK_STREAM)