From 4e618d091544903e705989a0ca4b27e8ba8cc117 Mon Sep 17 00:00:00 2001 From: kerbalwzy Date: Mon, 8 Jul 2019 12:43:21 +0800 Subject: [PATCH] add address check functions --- .gitignore | 1 + lightsocks/utils/addr_check.py | 49 +++++++++++++++++++++++++++++ lightsocks/utils/net.py | 3 +- lightsocks/utils/test_addr_check.py | 22 +++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 lightsocks/utils/addr_check.py create mode 100644 lightsocks/utils/test_addr_check.py diff --git a/.gitignore b/.gitignore index 32b21fd..838d212 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Byte-compiled / optimized / DLL files __pycache__/ +.idea/ *.py[cod] *$py.class diff --git a/lightsocks/utils/addr_check.py b/lightsocks/utils/addr_check.py new file mode 100644 index 0000000..7ff9ead --- /dev/null +++ b/lightsocks/utils/addr_check.py @@ -0,0 +1,49 @@ +# coding:utf-8 +""" +Check the availability of IP and port. +Author: kerbalwzy@gmail.com +Date: 2019-07-08 +""" + +import socket + + +def _host_addresses() -> set: + """ + Get the all addresses of the host, just include IPV4 + :return: address + """ + address = socket.getaddrinfo(socket.gethostname(), None) + address = [item[4][0] for item in address if ':' not in item[4][0]] + address.extend(["127.0.0.1", "0.0.0.0"]) + return set(address) + + +def ip_validate(ip: str) -> bool: + """ + The ip is require be one of the host addresses + :param ip: string, the ip need to be check + :return: boolean value + """ + return ip in _host_addresses() + + +def _port_is_free(port: int) -> bool: + """ + Try connect a local port. If success meanings the port is using. + :param port: int, the port need be check. + :return: boolean value. + """ + with socket.socket() as s: + s.settimeout(0.5) + # s.connect_ex return 0 means port is open and using + return s.connect_ex(('localhost', port)) != 0 + + +def port_validate(port: int) -> bool: + """ + The port is required in range 1024 to 65535, and spare. + :param port: int, the port need be check + :return: boolean value + """ + return 1023 < port < 65536 and _port_is_free(port) diff --git a/lightsocks/utils/net.py b/lightsocks/utils/net.py index 36c263a..17bb23e 100644 --- a/lightsocks/utils/net.py +++ b/lightsocks/utils/net.py @@ -1,4 +1,3 @@ from collections import namedtuple - -Address = namedtuple('Address', 'ip port') +Address = namedtuple('Address', 'ip port') \ No newline at end of file diff --git a/lightsocks/utils/test_addr_check.py b/lightsocks/utils/test_addr_check.py new file mode 100644 index 0000000..a2debc1 --- /dev/null +++ b/lightsocks/utils/test_addr_check.py @@ -0,0 +1,22 @@ +# coding:utf-8 +""" +Check the availability of IP and port. +Author: kerbalwzy@gmail.com +Date: 2019-07-08 +Note: 单元测试运行会把 "函数注解" 识别为语法错误,如果你要运行,请先在代码中删除函数注解 +""" + +import unittest +from lightsocks.utils.addr_check import ip_validate, port_validate + + +class UtilsAddrCheckTest(unittest.TestCase): + def test_ip_validate(self): + self.assertTrue(ip_validate("127.0.0.1")) + self.assertTrue(ip_validate("0.0.0.0")) + self.assertFalse(ip_validate("abc")) + self.assertFalse(ip_validate("127.0.0.256")) + + def test_port_validate(self): + self.assertFalse(port_validate(22)) + self.assertFalse(port_validate(1023))