From 9e256557e44c09aed7da1420bbd066d434a10951 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 07:38:14 +0200 Subject: Adding upstream version 0.7.1. Signed-off-by: Daniel Baumann --- port_for/__init__.py | 29 ++ port_for/_download_ranges.py | 95 ++++++ port_for/_ranges.py | 689 +++++++++++++++++++++++++++++++++++++++++++ port_for/api.py | 207 +++++++++++++ port_for/cmd.py | 66 +++++ port_for/docopt.py | 471 +++++++++++++++++++++++++++++ port_for/ephemeral.py | 72 +++++ port_for/exceptions.py | 3 + port_for/py.typed | 0 port_for/store.py | 87 ++++++ port_for/utils.py | 29 ++ 11 files changed, 1748 insertions(+) create mode 100644 port_for/__init__.py create mode 100644 port_for/_download_ranges.py create mode 100644 port_for/_ranges.py create mode 100644 port_for/api.py create mode 100644 port_for/cmd.py create mode 100644 port_for/docopt.py create mode 100644 port_for/ephemeral.py create mode 100644 port_for/exceptions.py create mode 100644 port_for/py.typed create mode 100644 port_for/store.py create mode 100644 port_for/utils.py (limited to 'port_for') diff --git a/port_for/__init__.py b/port_for/__init__.py new file mode 100644 index 0000000..15c664d --- /dev/null +++ b/port_for/__init__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +"""port_for package.""" +__version__ = "0.7.1" + +from ._ranges import UNASSIGNED_RANGES +from .api import ( + available_good_ports, + available_ports, + is_available, + good_port_ranges, + port_is_used, + select_random, + get_port, +) +from .store import PortStore +from .exceptions import PortForException + +__all__ = ( + "UNASSIGNED_RANGES", + "available_good_ports", + "available_ports", + "is_available", + "good_port_ranges", + "port_is_used", + "select_random", + "get_port", + "PortStore", + "PortForException", +) diff --git a/port_for/_download_ranges.py b/port_for/_download_ranges.py new file mode 100644 index 0000000..5e6a8fb --- /dev/null +++ b/port_for/_download_ranges.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- +""" +This module/script is for updating port_for._ranges with recent information +from IANA and Wikipedia. +""" +import sys +import os +import re +import datetime +from urllib.request import Request, urlopen +from xml.etree import ElementTree +from typing import Set, Iterator, Iterable, Tuple + +from port_for.utils import to_ranges, ranges_to_set + +name = os.path.abspath( + os.path.normpath(os.path.join(os.path.dirname(__file__), "..")) +) +sys.path.insert(0, name) + +IANA_DOWNLOAD_URL = ( + "https://www.iana.org/assignments" + "/service-names-port-numbers/service-names-port-numbers.xml" +) +IANA_NS = "http://www.iana.org/assignments" +WIKIPEDIA_PAGE = "http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers" + + +def _write_unassigned_ranges(out_filename: str) -> None: + """ + Downloads ports data from IANA & Wikipedia and converts + it to a python module. This function is used to generate _ranges.py. + """ + with open(out_filename, "wt") as f: + f.write( + "# auto-generated by port_for._download_ranges (%s)\n" + % datetime.date.today() + ) + f.write("UNASSIGNED_RANGES = [\n") + for range in to_ranges(sorted(list(_unassigned_ports()))): + f.write(" (%d, %d),\n" % range) + f.write("]\n") + + +def _unassigned_ports() -> Set[int]: + """Return a set of all unassigned ports (according to IANA and Wikipedia)""" + free_ports = ranges_to_set(_parse_ranges(_iana_unassigned_port_ranges())) + known_ports = ranges_to_set(_wikipedia_known_port_ranges()) + return free_ports.difference(known_ports) + + +def _wikipedia_known_port_ranges() -> Iterator[Tuple[int, int]]: + """ + Returns used port ranges according to Wikipedia page. + This page contains unofficial well-known ports. + """ + req = Request(WIKIPEDIA_PAGE, headers={"User-Agent": "Magic Browser"}) + page = urlopen(req).read().decode("utf8") + + # just find all numbers in table cells + ports = re.findall(r"((\d+)(\W(\d+))?)", page, re.U) + return ((int(p[1]), int(p[3] if p[3] else p[1])) for p in ports) + + +def _iana_unassigned_port_ranges() -> Iterator[str]: + """ + Returns unassigned port ranges according to IANA. + """ + page = urlopen(IANA_DOWNLOAD_URL).read() + xml = ElementTree.fromstring(page) + records = xml.findall("{%s}record" % IANA_NS) + for record in records: + description_el = record.find("{%s}description" % IANA_NS) + assert description_el is not None + description = description_el.text + if description == "Unassigned": + number_el = record.find("{%s}number" % IANA_NS) + assert number_el is not None + numbers = number_el.text + assert numbers is not None + yield numbers + + +def _parse_ranges(ranges: Iterable[str]) -> Iterator[Tuple[int, int]]: + """Converts a list of string ranges to a list of [low, high] tuples.""" + for txt in ranges: + if "-" in txt: + low, high = txt.split("-") + else: + low, high = txt, txt + yield int(low), int(high) + + +if __name__ == "__main__": + _write_unassigned_ranges("_ranges.py") diff --git a/port_for/_ranges.py b/port_for/_ranges.py new file mode 100644 index 0000000..abe5435 --- /dev/null +++ b/port_for/_ranges.py @@ -0,0 +1,689 @@ +# auto-generated by port_for._download_ranges (2017-07-19) +UNASSIGNED_RANGES = [ + (28, 28), + (30, 30), + (32, 32), + (34, 34), + (36, 36), + (60, 60), + (258, 258), + (272, 279), + (285, 285), + (288, 299), + (301, 307), + (325, 332), + (334, 343), + (703, 703), + (708, 708), + (717, 728), + (732, 740), + (743, 743), + (745, 746), + (755, 757), + (766, 766), + (768, 768), + (778, 779), + (781, 781), + (784, 799), + (803, 807), + (809, 809), + (811, 827), + (834, 842), + (844, 846), + (849, 852), + (855, 859), + (863, 872), + (874, 885), + (889, 896), + (899, 899), + (904, 909), + (1002, 1007), + (1009, 1009), + (1491, 1491), + (2194, 2194), + (2259, 2259), + (2378, 2378), + (2693, 2693), + (2794, 2794), + (2873, 2873), + (3092, 3092), + (3126, 3126), + (3546, 3546), + (3694, 3694), + (3994, 3994), + (4048, 4048), + (4120, 4120), + (4144, 4144), + (4194, 4196), + (4315, 4315), + (4317, 4319), + (4332, 4332), + (4337, 4339), + (4363, 4365), + (4367, 4367), + (4380, 4388), + (4397, 4399), + (4424, 4424), + (4434, 4440), + (4459, 4483), + (4489, 4499), + (4501, 4501), + (4539, 4544), + (4561, 4562), + (4564, 4565), + (4571, 4572), + (4574, 4589), + (4606, 4609), + (4641, 4657), + (4693, 4699), + (4705, 4710), + (4712, 4712), + (4714, 4724), + (4734, 4736), + (4748, 4748), + (4757, 4773), + (4775, 4783), + (4792, 4799), + (4805, 4826), + (4828, 4836), + (4852, 4866), + (4872, 4875), + (4886, 4893), + (4895, 4898), + (4903, 4911), + (4916, 4935), + (4938, 4939), + (4943, 4948), + (4954, 4968), + (4971, 4979), + (4981, 4983), + (4992, 4998), + (5016, 5019), + (5035, 5036), + (5038, 5041), + (5076, 5077), + (5088, 5089), + (5095, 5098), + (5108, 5110), + (5113, 5113), + (5118, 5119), + (5122, 5123), + (5126, 5132), + (5138, 5144), + (5147, 5149), + (5158, 5160), + (5169, 5171), + (5173, 5189), + (5204, 5208), + (5210, 5214), + (5216, 5220), + (5238, 5241), + (5244, 5244), + (5255, 5263), + (5266, 5268), + (5273, 5279), + (5283, 5297), + (5311, 5311), + (5316, 5316), + (5319, 5319), + (5322, 5342), + (5345, 5348), + (5365, 5393), + (5395, 5396), + (5438, 5442), + (5444, 5444), + (5446, 5449), + (5451, 5452), + (5460, 5460), + (5466, 5467), + (5469, 5469), + (5476, 5479), + (5482, 5494), + (5496, 5497), + (5508, 5516), + (5518, 5549), + (5551, 5552), + (5558, 5564), + (5570, 5572), + (5576, 5578), + (5587, 5596), + (5606, 5617), + (5619, 5626), + (5640, 5645), + (5647, 5655), + (5657, 5665), + (5668, 5669), + (5685, 5686), + (5690, 5692), + (5694, 5695), + (5697, 5699), + (5701, 5704), + (5706, 5712), + (5731, 5740), + (5749, 5749), + (5751, 5754), + (5756, 5756), + (5758, 5765), + (5772, 5776), + (5778, 5779), + (5788, 5792), + (5795, 5799), + (5801, 5812), + (5815, 5840), + (5843, 5858), + (5860, 5862), + (5864, 5867), + (5869, 5880), + (5882, 5882), + (5884, 5899), + (5901, 5909), + (5914, 5930), + (5932, 5937), + (5939, 5962), + (5964, 5967), + (5970, 5983), + (5994, 5998), + (6067, 6067), + (6078, 6079), + (6089, 6098), + (6119, 6120), + (6125, 6128), + (6131, 6132), + (6134, 6135), + (6137, 6139), + (6150, 6158), + (6164, 6199), + (6202, 6208), + (6210, 6221), + (6223, 6224), + (6226, 6226), + (6228, 6229), + (6231, 6239), + (6245, 6250), + (6254, 6254), + (6256, 6256), + (6258, 6259), + (6261, 6261), + (6263, 6266), + (6270, 6299), + (6302, 6305), + (6307, 6314), + (6318, 6319), + (6323, 6323), + (6327, 6342), + (6345, 6345), + (6348, 6349), + (6351, 6354), + (6356, 6359), + (6361, 6362), + (6364, 6369), + (6371, 6378), + (6380, 6381), + (6383, 6388), + (6391, 6399), + (6411, 6416), + (6422, 6431), + (6433, 6435), + (6438, 6441), + (6447, 6454), + (6457, 6463), + (6465, 6470), + (6472, 6479), + (6490, 6499), + (6504, 6504), + (6512, 6512), + (6516, 6521), + (6524, 6540), + (6545, 6546), + (6552, 6555), + (6557, 6557), + (6559, 6559), + (6562, 6565), + (6569, 6570), + (6572, 6578), + (6584, 6599), + (6603, 6618), + (6630, 6631), + (6637, 6639), + (6641, 6652), + (6654, 6654), + (6658, 6659), + (6674, 6677), + (6680, 6686), + (6691, 6695), + (6698, 6698), + (6700, 6702), + (6707, 6713), + (6717, 6766), + (6772, 6776), + (6779, 6782), + (6792, 6800), + (6802, 6816), + (6818, 6830), + (6832, 6840), + (6843, 6849), + (6851, 6867), + (6870, 6880), + (10011, 10019), + (10021, 10023), + (10026, 10041), + (10043, 10049), + (10052, 10054), + (10056, 10079), + (10082, 10099), + (10105, 10106), + (10108, 10109), + (10112, 10112), + (10118, 10124), + (10126, 10127), + (10130, 10159), + (10163, 10171), + (10173, 10199), + (10205, 10211), + (10213, 10251), + (10254, 10259), + (10262, 10287), + (10289, 10300), + (10303, 10307), + (10309, 10320), + (10322, 10438), + (10440, 10479), + (10481, 10499), + (10501, 10504), + (10506, 10513), + (10515, 10539), + (10545, 10547), + (10549, 10630), + (10632, 10799), + (10801, 10804), + (10806, 10808), + (10811, 10822), + (10824, 10859), + (10861, 10879), + (10881, 10890), + (10892, 10932), + (10934, 10989), + (10991, 10999), + (11002, 11022), + (11024, 11094), + (11096, 11102), + (11107, 11107), + (11113, 11154), + (11156, 11160), + (11166, 11170), + (11176, 11200), + (11203, 11207), + (11209, 11210), + (11212, 11213), + (11216, 11234), + (11236, 11310), + (11312, 11318), + (11322, 11366), + (11368, 11370), + (11372, 11429), + (11431, 11488), + (11490, 11575), + (11577, 11599), + (11601, 11622), + (11624, 11719), + (11721, 11722), + (11724, 11750), + (11752, 11752), + (11754, 11795), + (11797, 11875), + (11878, 11949), + (11952, 11966), + (11968, 11996), + (12014, 12029), + (12033, 12108), + (12110, 12120), + (12122, 12167), + (12169, 12171), + (12173, 12200), + (12202, 12221), + (12224, 12299), + (12301, 12301), + (12303, 12320), + (12323, 12344), + (12346, 12442), + (12444, 12488), + (12490, 12752), + (12754, 12864), + (12866, 12974), + (12976, 13000), + (13002, 13007), + (13009, 13074), + (13076, 13159), + (13161, 13194), + (13197, 13215), + (13219, 13222), + (13225, 13399), + (13401, 13719), + (13723, 13723), + (13725, 13781), + (13784, 13784), + (13787, 13817), + (13824, 13893), + (13895, 13928), + (13931, 13999), + (14003, 14032), + (14035, 14140), + (14144, 14144), + (14146, 14148), + (14151, 14153), + (14155, 14249), + (14251, 14413), + (14415, 14499), + (14501, 14549), + (14551, 14566), + (14568, 14899), + (14901, 14935), + (14938, 14999), + (15001, 15001), + (15003, 15117), + (15119, 15344), + (15346, 15362), + (15364, 15554), + (15557, 15566), + (15568, 15659), + (15661, 15739), + (15741, 15997), + (16004, 16019), + (16022, 16079), + (16081, 16160), + (16163, 16199), + (16201, 16224), + (16226, 16249), + (16251, 16260), + (16262, 16299), + (16301, 16308), + (16312, 16359), + (16362, 16366), + (16369, 16383), + (16388, 16392), + (16473, 16481), + (16483, 16566), + (16568, 16618), + (16620, 16664), + (16667, 16788), + (16790, 16899), + (16901, 16949), + (16951, 16990), + (16996, 17006), + (17008, 17010), + (17012, 17183), + (17186, 17218), + (17226, 17233), + (17236, 17499), + (17501, 17554), + (17556, 17728), + (17730, 17753), + (17757, 17776), + (17778, 17999), + (18001, 18090), + (18093, 18103), + (18105, 18135), + (18137, 18180), + (18188, 18199), + (18202, 18205), + (18207, 18240), + (18244, 18261), + (18263, 18299), + (18302, 18305), + (18307, 18332), + (18334, 18399), + (18402, 18462), + (18464, 18504), + (18507, 18604), + (18607, 18633), + (18636, 18667), + (18669, 18768), + (18770, 18880), + (18882, 18887), + (18889, 18999), + (19002, 19006), + (19008, 19019), + (19021, 19131), + (19133, 19149), + (19151, 19190), + (19192, 19193), + (19195, 19219), + (19221, 19225), + (19227, 19282), + (19284, 19293), + (19296, 19301), + (19303, 19314), + (19316, 19397), + (19399, 19409), + (19413, 19538), + (19542, 19787), + (19789, 19811), + (19815, 19997), + (20004, 20004), + (20006, 20011), + (20015, 20033), + (20035, 20045), + (20047, 20047), + (20050, 20056), + (20058, 20166), + (20168, 20201), + (20203, 20221), + (20223, 20479), + (20481, 20559), + (20561, 20594), + (20596, 20669), + (20671, 20701), + (20703, 20719), + (20721, 20789), + (20791, 20807), + (20809, 20998), + (21001, 21009), + (21011, 21024), + (21026, 21220), + (21222, 21552), + (21555, 21589), + (21591, 21799), + (21801, 21844), + (21850, 21999), + (22006, 22124), + (22126, 22127), + (22129, 22135), + (22137, 22221), + (22223, 22272), + (22274, 22304), + (22306, 22334), + (22336, 22342), + (22344, 22346), + (22348, 22348), + (22352, 22536), + (22538, 22554), + (22556, 22762), + (22764, 22799), + (22801, 22950), + (22952, 22999), + (23006, 23052), + (23054, 23072), + (23074, 23271), + (23273, 23283), + (23285, 23293), + (23295, 23332), + (23334, 23398), + (23403, 23455), + (23458, 23512), + (23514, 23545), + (23547, 23999), + (24007, 24241), + (24243, 24248), + (24250, 24320), + (24323, 24385), + (24387, 24440), + (24442, 24443), + (24445, 24464), + (24466, 24553), + (24555, 24576), + (24578, 24665), + (24667, 24675), + (24679, 24679), + (24681, 24753), + (24755, 24799), + (24801, 24841), + (24843, 24849), + (24851, 24921), + (24923, 24999), + (25011, 25104), + (25106, 25470), + (25472, 25559), + (25561, 25564), + (25566, 25569), + (25571, 25574), + (25577, 25603), + (25605, 25792), + (25794, 25825), + (25827, 25827), + (25841, 25887), + (25889, 25899), + (25904, 25953), + (25956, 25998), + (26001, 26132), + (26134, 26207), + (26209, 26256), + (26258, 26259), + (26265, 26485), + (26488, 26488), + (26490, 26899), + (26902, 26949), + (26951, 26999), + (27051, 27344), + (27346, 27373), + (27375, 27441), + (27443, 27499), + (27911, 27949), + (27951, 27959), + (27970, 27998), + (28002, 28014), + (28016, 28118), + (28120, 28199), + (28201, 28239), + (28241, 28588), + (28590, 28769), + (28772, 28784), + (28787, 28851), + (28853, 28909), + (28911, 28959), + (28961, 28999), + (29001, 29069), + (29071, 29117), + (29119, 29166), + (29170, 29899), + (29902, 29919), + (29921, 29998), + (30005, 30099), + (30101, 30259), + (30261, 30563), + (30565, 30831), + (30833, 30998), + (31000, 31015), + (31017, 31019), + (31021, 31028), + (31030, 31336), + (31338, 31399), + (31401, 31415), + (31417, 31437), + (31439, 31456), + (31458, 31619), + (31621, 31684), + (31686, 31764), + (31766, 31947), + (31950, 32033), + (32035, 32136), + (32138, 32248), + (32250, 32399), + (32401, 32482), + (32484, 32634), + (32637, 32763), + (32765, 32766), + (32778, 32800), + (32802, 32810), + (32812, 32886), + (32888, 32895), + (32897, 32975), + (32977, 33059), + (33061, 33122), + (33124, 33330), + (33332, 33332), + (33335, 33433), + (33435, 33655), + (33657, 33847), + (33849, 33999), + (34001, 34248), + (34250, 34377), + (34380, 34566), + (34568, 34961), + (34965, 34979), + (34981, 34999), + (35007, 35099), + (35101, 35353), + (35358, 36000), + (36002, 36410), + (36413, 36421), + (36425, 36442), + (36445, 36461), + (36463, 36523), + (36525, 36601), + (36603, 36699), + (36701, 36864), + (36866, 37474), + (37476, 37482), + (37484, 37600), + (37602, 37653), + (37655, 37999), + (38003, 38200), + (38204, 38411), + (38413, 38421), + (38423, 38471), + (38473, 38799), + (38801, 38864), + (38866, 39680), + (39682, 39999), + (40001, 40022), + (40024, 40403), + (40405, 40840), + (40844, 40852), + (40854, 41110), + (41112, 41120), + (41122, 41229), + (41231, 41793), + (41798, 42507), + (42511, 42999), + (43001, 43593), + (43596, 44320), + (44323, 44404), + (44406, 44443), + (44445, 44543), + (44545, 44552), + (44554, 44599), + (44601, 44817), + (44819, 44899), + (44901, 44999), + (45003, 45044), + (45046, 45053), + (45055, 45513), + (45515, 45677), + (45679, 45823), + (45826, 45965), + (45967, 46335), + (46337, 46997), + (47002, 47099), + (47101, 47556), + (47558, 47623), + (47625, 47805), + (47807, 47807), + (47810, 47999), + (48006, 48048), + (48051, 48127), + (48130, 48555), + (48557, 48618), + (48620, 48652), + (48654, 48999), + (49002, 49150), +] diff --git a/port_for/api.py b/port_for/api.py new file mode 100644 index 0000000..4ecf9f8 --- /dev/null +++ b/port_for/api.py @@ -0,0 +1,207 @@ +# -*- coding: utf-8 -*- +import contextlib +import socket +import errno +import random +from itertools import chain +from typing import Optional, Set, List, Tuple, Iterable, TypeVar, Type, Union +from port_for import ephemeral, utils +from ._ranges import UNASSIGNED_RANGES +from .exceptions import PortForException + + +SYSTEM_PORT_RANGE = (0, 1024) + + +def select_random( + ports: Optional[Set[int]] = None, + exclude_ports: Optional[Iterable[int]] = None, +) -> int: + """ + Returns random unused port number. + """ + if ports is None: + ports = available_good_ports() + + if exclude_ports is None: + exclude_ports = set() + + ports.difference_update(set(exclude_ports)) + + for port in random.sample(tuple(ports), min(len(ports), 100)): + if not port_is_used(port): + return port + raise PortForException("Can't select a port") + + +def is_available(port: int) -> bool: + """ + Returns if port is good to choose. + """ + return port in available_ports() and not port_is_used(port) + + +def available_ports( + low: int = 1024, + high: int = 65535, + exclude_ranges: Optional[List[Tuple[int, int]]] = None, +) -> Set[int]: + """ + Returns a set of possible ports (excluding system, + ephemeral and well-known ports). + Pass ``high`` and/or ``low`` to limit the port range. + """ + if exclude_ranges is None: + exclude_ranges = [] + available = utils.ranges_to_set(UNASSIGNED_RANGES) + exclude = utils.ranges_to_set( + # Motivation behind excluding ephemeral port ranges: + # let's say you decided to use an ephemeral local port + # as a persistent port, and "reserve" it to your software. + # OS won't know about it, and still can try to use this port. + # This is not a problem if your service is always running and occupying + # this port (OS would pick next one). But if the service is temporarily + # not using the port (because of restart of other reason), + # OS might reuse the same port, + # which might prevent the service from starting. + ephemeral.port_ranges() + + exclude_ranges + + [SYSTEM_PORT_RANGE, (SYSTEM_PORT_RANGE[1], low), (high, 65536)] + ) + return available.difference(exclude) + + +def good_port_ranges( + ports: Optional[Set[int]] = None, min_range_len: int = 20, border: int = 3 +) -> List[Tuple[int, int]]: + """ + Returns a list of 'good' port ranges. + Such ranges are large and don't contain ephemeral or well-known ports. + Ranges borders are also excluded. + """ + min_range_len += border * 2 + if ports is None: + ports = available_ports() + ranges = utils.to_ranges(list(ports)) + lenghts = sorted([(r[1] - r[0], r) for r in ranges], reverse=True) + long_ranges = [ + length[1] for length in lenghts if length[0] >= min_range_len + ] + without_borders = [ + (low + border, high - border) for low, high in long_ranges + ] + return without_borders + + +def available_good_ports(min_range_len: int = 20, border: int = 3) -> Set[int]: + return utils.ranges_to_set( + good_port_ranges(min_range_len=min_range_len, border=border) + ) + + +def port_is_used(port: int, host: str = "127.0.0.1") -> bool: + """ + Returns if port is used. Port is considered used if the current process + can't bind to it or the port doesn't refuse connections. + """ + unused = _can_bind(port, host) and _refuses_connection(port, host) + return not unused + + +def _can_bind(port: int, host: str) -> bool: + sock = socket.socket() + with contextlib.closing(sock): + try: + sock.bind((host, port)) + except socket.error: + return False + return True + + +def _refuses_connection(port: int, host: str) -> bool: + sock = socket.socket() + with contextlib.closing(sock): + sock.settimeout(1) + err = sock.connect_ex((host, port)) + return err == errno.ECONNREFUSED + + +T = TypeVar("T") + + +def filter_by_type(lst: Iterable, type_of: Type[T]) -> List[T]: + """Returns a list of elements with given type.""" + return [e for e in lst if isinstance(e, type_of)] + + +PortType = Union[ + str, + int, + Tuple[int, int], + Set[int], + List[str], + List[int], + List[Tuple[int, int]], + List[Set[int]], + List[Union[Set[int], Tuple[int, int]]], + List[Union[str, int, Tuple[int, int], Set[int]]], +] + + +def get_port( + ports: Optional[PortType], + exclude_ports: Optional[Iterable[int]] = None, +) -> Optional[int]: + """ + Retuns a random available port. If there's only one port passed + (e.g. 5000 or '5000') function does not check if port is available. + If there's -1 passed as an argument, function returns None. + + :param ports: + exact port (e.g. '8000', 8000) + randomly selected port (None) - any random available port + [(2000,3000)] or (2000,3000) - random available port from a given range + [{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports + [(2000,3000), {4002,4003}] -random of given range and set + :param exclude_ports: A set of known ports that can not be selected. + :returns: a random free port + :raises: ValueError + """ + if ports == -1: + return None + elif not ports: + return select_random(None, exclude_ports) + + try: + return int(ports) # type: ignore[arg-type] + except TypeError: + pass + + ports_set: Set[int] = set() + + try: + if not isinstance(ports, list): + ports = [ports] + ranges: Set[int] = utils.ranges_to_set( + filter_by_type(ports, tuple) # type: ignore[arg-type] + ) + nums: Set[int] = set(filter_by_type(ports, int)) + sets: Set[int] = set( + chain( + *filter_by_type( + ports, (set, frozenset) # type: ignore[arg-type] + ) + ) + ) + ports_set = ports_set.union(ranges, sets, nums) + except ValueError: + raise PortForException( + "Unknown format of ports: %s.\n" + 'You should provide a ports range "[(4000,5000)]"' + 'or "(4000,5000)" or a comma-separated ports set' + '"[{4000,5000,6000}]" or list of ints "[400,5000,6000,8000]"' + 'or all of them "[(20000, 30000), {48889, 50121}, 4000, 4004]"' + % (ports,) + ) + + return select_random(ports_set, exclude_ports) diff --git a/port_for/cmd.py b/port_for/cmd.py new file mode 100644 index 0000000..be28902 --- /dev/null +++ b/port_for/cmd.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +""" +cmd.py is a command-line utility that helps with local TCP ports management. + +It finds 'good' unused TCP localhost port and remembers the association. + +Usage: + port-for + port-for --bind + port-for --bind --port + port-for --port + port-for --unbind + port-for --list + port-for --version + port-for --help + +Options: + -h --help Show this screen. + -v, --version Show version. + -b FOO, --bind FOO Find and return a port for FOO; this is an alias for + 'port-for FOO'. + -p PORT, --port PORT (Optional) specific port number for the --bind command. + -u FOO, --unbind FOO Remove association for FOO. + -l, --list List all associated ports. +""" + +import sys +from typing import Optional +import port_for +from port_for.docopt import docopt + +store = port_for.PortStore() + + +def _list() -> None: + for app, port in store.bound_ports(): + sys.stdout.write("%s: %s\n" % (app, port)) + + +def _bind(app: str, port: Optional[str] = None) -> None: + bound_port = store.bind_port(app, port) + sys.stdout.write("%s\n" % bound_port) + + +def _unbind(app: str) -> None: + store.unbind_port(app) + + +def main() -> None: + """port-for executable entrypoint.""" + args = docopt( + __doc__, + version="port-for %s" % port_for.__version__, + ) # type: ignore[no-untyped-call] + if args[""]: + _bind(args[""], args["--port"]) + elif args["--bind"]: + _bind(args["--bind"], args["--port"]) + elif args["--list"]: + _list() + elif args["--unbind"]: + _unbind(args["--unbind"]) + + +if __name__ == "__main__": + main() diff --git a/port_for/docopt.py b/port_for/docopt.py new file mode 100644 index 0000000..eff4e2f --- /dev/null +++ b/port_for/docopt.py @@ -0,0 +1,471 @@ +from copy import copy +import sys +import re + + +class DocoptLanguageError(Exception): + """Error in construction of usage-message by developer.""" + + +class DocoptExit(SystemExit): + """Exit in case user invoked program with incorrect arguments.""" + + usage = "" + + def __init__(self, message=""): + SystemExit.__init__(self, (message + "\n" + self.usage).strip()) + + +class Pattern(object): + def __init__(self, *children): + self.children = list(children) + + def __eq__(self, other): + return repr(self) == repr(other) + + def __hash__(self): + return hash(repr(self)) + + def __repr__(self): + return "%s(%s)" % ( + self.__class__.__name__, + ", ".join(repr(a) for a in self.children), + ) + + @property + def flat(self): + if not hasattr(self, "children"): + return [self] + return sum([c.flat for c in self.children], []) + + def fix(self): + self.fix_identities() + self.fix_list_arguments() + return self + + def fix_identities(self, uniq=None): + """Make pattern-tree tips point to same object if they are equal.""" + if not hasattr(self, "children"): + return self + uniq = list(set(self.flat)) if uniq is None else uniq + for i, c in enumerate(self.children): + if not hasattr(c, "children"): + assert c in uniq + self.children[i] = uniq[uniq.index(c)] + else: + c.fix_identities(uniq) + + def fix_list_arguments(self): + """Find arguments that should accumulate values and fix them.""" + either = [list(c.children) for c in self.either.children] + for case in either: + case = [c for c in case if case.count(c) > 1] + for a in [e for e in case if type(e) == Argument]: + a.value = [] + return self + + @property + def either(self): + """Transform pattern into an equivalent, with only top-level Either.""" + # Currently the pattern will not be equivalent, but more "narrow", + # although good enough to reason about list arguments. + if not hasattr(self, "children"): + return Either(Required(self)) + else: + ret = [] + groups = [[self]] + while groups: + children = groups.pop(0) + types = [type(c) for c in children] + if Either in types: + either = [c for c in children if type(c) is Either][0] + children.pop(children.index(either)) + for c in either.children: + groups.append([c] + children) + elif Required in types: + required = [c for c in children if type(c) is Required][0] + children.pop(children.index(required)) + groups.append(list(required.children) + children) + elif Optional in types: + optional = [c for c in children if type(c) is Optional][0] + children.pop(children.index(optional)) + groups.append(list(optional.children) + children) + elif OneOrMore in types: + oneormore = [c for c in children if type(c) is OneOrMore][0] + children.pop(children.index(oneormore)) + groups.append(list(oneormore.children) * 2 + children) + else: + ret.append(children) + return Either(*[Required(*e) for e in ret]) + + +class Argument(Pattern): + def __init__(self, name, value=None): + self.name = name + self.value = value + + def match(self, left, collected=None): + collected = [] if collected is None else collected + args = [arg_left for arg_left in left if type(arg_left) is Argument] + if not len(args): + return False, left, collected + left.remove(args[0]) + if type(self.value) is not list: + return True, left, collected + [Argument(self.name, args[0].value)] + same_name = [ + a for a in collected if type(a) is Argument and a.name == self.name + ] + if len(same_name): + same_name[0].value += [args[0].value] + return True, left, collected + else: + return ( + True, + left, + collected + [Argument(self.name, [args[0].value])], + ) + + def __repr__(self): + return "Argument(%r, %r)" % (self.name, self.value) + + +class Command(Pattern): + def __init__(self, name, value=False): + self.name = name + self.value = value + + def match(self, left, collected=None): + collected = [] if collected is None else collected + args = [arg_left for arg_left in left if type(arg_left) is Argument] + if not len(args) or args[0].value != self.name: + return False, left, collected + left.remove(args[0]) + return True, left, collected + [Command(self.name, True)] + + def __repr__(self): + return "Command(%r, %r)" % (self.name, self.value) + + +class Option(Pattern): + def __init__(self, short=None, long=None, argcount=0, value=False): + assert argcount in (0, 1) + self.short, self.long = short, long + self.argcount, self.value = argcount, value + self.value = None if not value and argcount else value # HACK + + @classmethod + def parse(class_, option_description): + short, long, argcount, value = None, None, 0, False + options, _, description = option_description.strip().partition(" ") + options = options.replace(",", " ").replace("=", " ") + for s in options.split(): + if s.startswith("--"): + long = s + elif s.startswith("-"): + short = s + else: + argcount = 1 + if argcount: + matched = re.findall(r"\[default: (.*)\]", description, flags=re.I) + value = matched[0] if matched else None + return class_(short, long, argcount, value) + + def match(self, left, collected=None): + collected = [] if collected is None else collected + left_ = [] + for arg_left in left: + # if this is so greedy, how to handle OneOrMore then? + if not ( + type(arg_left) is Option + and (self.short, self.long) == (arg_left.short, arg_left.long) + ): + left_.append(arg_left) + return (left != left_), left_, collected + + @property + def name(self): + return self.long or self.short + + def __repr__(self): + return "Option(%r, %r, %r, %r)" % ( + self.short, + self.long, + self.argcount, + self.value, + ) + + +class AnyOptions(Pattern): + def match(self, left, collected=None): + collected = [] if collected is None else collected + left_ = [opt_left for opt_left in left if not type(opt_left) == Option] + return (left != left_), left_, collected + + +class Required(Pattern): + def match(self, left, collected=None): + collected = [] if collected is None else collected + copied_left = copy(left) + c = copy(collected) + for p in self.children: + matched, copied_left, c = p.match(copied_left, c) + if not matched: + return False, left, collected + return True, copied_left, c + + +class Optional(Pattern): + def match(self, left, collected=None): + collected = [] if collected is None else collected + left = copy(left) + for p in self.children: + m, left, collected = p.match(left, collected) + return True, left, collected + + +class OneOrMore(Pattern): + def match(self, left, collected=None): + assert len(self.children) == 1 + collected = [] if collected is None else collected + pattern_left = copy(left) + c = copy(collected) + l_ = None + matched = True + times = 0 + while matched: + # could it be that something didn't match but + # changed pattern_left or c? + matched, pattern_left, c = self.children[0].match(pattern_left, c) + times += 1 if matched else 0 + if l_ == pattern_left: + break + l_ = copy(pattern_left) + if times >= 1: + return True, pattern_left, c + return False, left, collected + + +class Either(Pattern): + def match(self, left, collected=None): + collected = [] if collected is None else collected + outcomes = [] + for p in self.children: + matched, _, _ = outcome = p.match(copy(left), copy(collected)) + if matched: + outcomes.append(outcome) + if outcomes: + return min(outcomes, key=lambda outcome: len(outcome[1])) + return False, left, collected + + +class TokenStream(list): + def __init__(self, source, error): + self += source.split() if type(source) is str else source + self.error = error + + def move(self): + return self.pop(0) if len(self) else None + + def current(self): + return self[0] if len(self) else None + + +def parse_long(tokens, options): + raw, eq, value = tokens.move().partition("=") + value = None if eq == value == "" else value + opt = [o for o in options if o.long and o.long.startswith(raw)] + if len(opt) < 1: + if tokens.error is DocoptExit: + raise tokens.error("%s is not recognized" % raw) + else: + o = Option(None, raw, (1 if eq == "=" else 0)) + options.append(o) + return [o] + if len(opt) > 1: + raise tokens.error( + "%s is not a unique prefix: %s?" + % (raw, ", ".join("%s" % o.long for o in opt)) + ) + opt = copy(opt[0]) + if opt.argcount == 1: + if value is None: + if tokens.current() is None: + raise tokens.error("%s requires argument" % opt.name) + value = tokens.move() + elif value is not None: + raise tokens.error("%s must not have an argument" % opt.name) + opt.value = value or True + return [opt] + + +def parse_shorts(tokens, options): + raw = tokens.move()[1:] + parsed = [] + while raw != "": + opt = [ + o + for o in options + if o.short and o.short.lstrip("-").startswith(raw[0]) + ] + if len(opt) > 1: + raise tokens.error( + "-%s is specified ambiguously %d times" % (raw[0], len(opt)) + ) + if len(opt) < 1: + if tokens.error is DocoptExit: + raise tokens.error("-%s is not recognized" % raw[0]) + else: + o = Option("-" + raw[0], None) + options.append(o) + parsed.append(o) + raw = raw[1:] + continue + opt = copy(opt[0]) + raw = raw[1:] + if opt.argcount == 0: + value = True + else: + if raw == "": + if tokens.current() is None: + raise tokens.error("-%s requires argument" % opt.short[0]) + raw = tokens.move() + value, raw = raw, "" + opt.value = value + parsed.append(opt) + return parsed + + +def parse_pattern(source, options): + tokens = TokenStream( + re.sub(r"([\[\]\(\)\|]|\.\.\.)", r" \1 ", source), DocoptLanguageError + ) + result = parse_expr(tokens, options) + if tokens.current() is not None: + raise tokens.error("unexpected ending: %r" % " ".join(tokens)) + return Required(*result) + + +def parse_expr(tokens, options): + """expr ::= seq ( '|' seq )* ;""" + seq = parse_seq(tokens, options) + if tokens.current() != "|": + return seq + result = [Required(*seq)] if len(seq) > 1 else seq + while tokens.current() == "|": + tokens.move() + seq = parse_seq(tokens, options) + result += [Required(*seq)] if len(seq) > 1 else seq + return [Either(*result)] if len(result) > 1 else result + + +def parse_seq(tokens, options): + """seq ::= ( atom [ '...' ] )* ;""" + result = [] + while tokens.current() not in [None, "]", ")", "|"]: + atom = parse_atom(tokens, options) + if tokens.current() == "...": + atom = [OneOrMore(*atom)] + tokens.move() + result += atom + return result + + +def parse_atom(tokens, options): + """atom ::= '(' expr ')' | '[' expr ']' | 'options' + | long | shorts | argument | command ; + """ + token = tokens.current() + result = [] + if token == "(": + tokens.move() + result = [Required(*parse_expr(tokens, options))] + if tokens.move() != ")": + raise tokens.error("Unmatched '('") + return result + elif token == "[": + tokens.move() + result = [Optional(*parse_expr(tokens, options))] + if tokens.move() != "]": + raise tokens.error("Unmatched '['") + return result + elif token == "options": + tokens.move() + return [AnyOptions()] + elif token.startswith("--") and token != "--": + return parse_long(tokens, options) + elif token.startswith("-") and token not in ("-", "--"): + return parse_shorts(tokens, options) + elif token.startswith("<") and token.endswith(">") or token.isupper(): + return [Argument(tokens.move())] + else: + return [Command(tokens.move())] + + +def parse_args(source, options): + tokens = TokenStream(source, DocoptExit) + options = copy(options) + parsed = [] + while tokens.current() is not None: + if tokens.current() == "--": + return parsed + [Argument(None, v) for v in tokens] + elif tokens.current().startswith("--"): + parsed += parse_long(tokens, options) + elif tokens.current().startswith("-") and tokens.current() != "-": + parsed += parse_shorts(tokens, options) + else: + parsed.append(Argument(None, tokens.move())) + return parsed + + +def parse_doc_options(doc): + return [Option.parse("-" + s) for s in re.split("^ *-|\n *-", doc)[1:]] + + +def printable_usage(doc): + usage_split = re.split(r"([Uu][Ss][Aa][Gg][Ee]:)", doc) + if len(usage_split) < 3: + raise DocoptLanguageError('"usage:" (case-insensitive) not found.') + if len(usage_split) > 3: + raise DocoptLanguageError('More than one "usage:" (case-insensitive).') + return re.split(r"\n\s*\n", "".join(usage_split[1:]))[0].strip() + + +def formal_usage(printable_usage): + pu = printable_usage.split()[1:] # split and drop "usage:" + return " ".join("|" if s == pu[0] else s for s in pu[1:]) + + +def extras(help, version, options, doc): + if help and any((o.name in ("-h", "--help")) and o.value for o in options): + print(doc.strip()) + exit() + if version and any(o.name == "--version" and o.value for o in options): + print(version) + exit() + + +class Dict(dict): + """Dictionary with custom repr bbehaviour.""" + + def __repr__(self): + """Dictionary representation for docopt.""" + return "{%s}" % ",\n ".join("%r: %r" % i for i in sorted(self.items())) + + +def docopt(doc, argv=sys.argv[1:], help=True, version=None): + DocoptExit.usage = docopt.usage = usage = printable_usage(doc) + pot_options = parse_doc_options(doc) + formal_pattern = parse_pattern(formal_usage(usage), options=pot_options) + argv = parse_args(argv, options=pot_options) + extras(help, version, argv, doc) + matched, left, arguments = formal_pattern.fix().match(argv) + if matched and left == []: # better message if left? + options = [o for o in argv if type(o) is Option] + pot_arguments = [ + a for a in formal_pattern.flat if type(a) in [Argument, Command] + ] + return Dict( + (a.name, a.value) + for a in (pot_options + options + pot_arguments + arguments) + ) + raise DocoptExit() diff --git a/port_for/ephemeral.py b/port_for/ephemeral.py new file mode 100644 index 0000000..a3c03ec --- /dev/null +++ b/port_for/ephemeral.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +""" +This module provide utilities to find ephemeral port ranges for the current OS. +See http://www.ncftp.com/ncftpd/doc/misc/ephemeral_ports.html for more info +about ephemeral port ranges. + +Currently only Linux and BSD (including OS X) are supported. +""" +import subprocess +from typing import List, Tuple, Dict + +DEFAULT_EPHEMERAL_PORT_RANGE = (32768, 65535) + + +def port_ranges() -> List[Tuple[int, int]]: + """ + Returns a list of ephemeral port ranges for current machine. + """ + try: + return _linux_ranges() + except (OSError, IOError): # not linux, try BSD + try: + ranges = _bsd_ranges() + if ranges: + return ranges + except (OSError, IOError): + pass + + # fallback + return [DEFAULT_EPHEMERAL_PORT_RANGE] + + +def _linux_ranges() -> List[Tuple[int, int]]: + with open("/proc/sys/net/ipv4/ip_local_port_range") as f: + # use readline() instead of read() for linux + musl + low, high = f.readline().split() + return [(int(low), int(high))] + + +def _bsd_ranges() -> List[Tuple[int, int]]: + pp = subprocess.Popen( + ["sysctl", "net.inet.ip.portrange"], stdout=subprocess.PIPE + ) + stdout, stderr = pp.communicate() + lines = stdout.decode("ascii").split("\n") + out: Dict[str, str] = dict( + [ + [x.strip().rsplit(".")[-1] for x in line.split(":")] + for line in lines + if line + ] + ) + + ranges = [ + # FreeBSD & Mac + ("first", "last"), + ("lowfirst", "lowlast"), + ("hifirst", "hilast"), + # OpenBSD + ("portfirst", "portlast"), + ("porthifirst", "porthilast"), + ] + + res = [] + for rng in ranges: + try: + low, high = int(out[rng[0]]), int(out[rng[1]]) + if low <= high: + res.append((low, high)) + except KeyError: + pass + return res diff --git a/port_for/exceptions.py b/port_for/exceptions.py new file mode 100644 index 0000000..256a255 --- /dev/null +++ b/port_for/exceptions.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +class PortForException(Exception): + pass diff --git a/port_for/py.typed b/port_for/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/port_for/store.py b/port_for/store.py new file mode 100644 index 0000000..a07b737 --- /dev/null +++ b/port_for/store.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +import os +from configparser import ConfigParser, DEFAULTSECT +from typing import Optional, List, Tuple, Union + +from .api import select_random +from .exceptions import PortForException + + +DEFAULT_CONFIG_PATH = "/etc/port-for.conf" + + +class PortStore(object): + def __init__(self, config_filename: str = DEFAULT_CONFIG_PATH): + self._config = config_filename + + def bind_port( + self, app: str, port: Optional[Union[int, str]] = None + ) -> int: + if "=" in app or ":" in app: + raise Exception('invalid app name: "%s"' % app) + + requested_port: Optional[str] = None + if port is not None: + requested_port = str(port) + + parser = self._get_parser() + + # this app already use some port; return it + if parser.has_option(DEFAULTSECT, app): + actual_port = parser.get(DEFAULTSECT, app) + if requested_port is not None and requested_port != actual_port: + msg = ( + "Can't bind to port %s: %s is already associated " + "with port %s" % (requested_port, app, actual_port) + ) + raise PortForException(msg) + return int(actual_port) + + # port is already used by an another app + app_by_port = dict((v, k) for k, v in parser.items(DEFAULTSECT)) + bound_port_numbers = map(int, app_by_port.keys()) + + if requested_port is None: + requested_port = str( + select_random(exclude_ports=bound_port_numbers) + ) + + if requested_port in app_by_port: + binding_app = app_by_port[requested_port] + if binding_app != app: + raise PortForException( + "Port %s is already used by %s!" + % (requested_port, binding_app) + ) + + # new app & new port + parser.set(DEFAULTSECT, app, requested_port) + self._save(parser) + + return int(requested_port) + + def unbind_port(self, app: str) -> None: + parser = self._get_parser() + parser.remove_option(DEFAULTSECT, app) + self._save(parser) + + def bound_ports(self) -> List[Tuple[str, int]]: + return [ + (app, int(port)) + for app, port in self._get_parser().items(DEFAULTSECT) + ] + + def _ensure_config_exists(self) -> None: + if not os.path.exists(self._config): + with open(self._config, "wb"): + pass + + def _get_parser(self) -> ConfigParser: + self._ensure_config_exists() + parser = ConfigParser() + parser.read(self._config) + return parser + + def _save(self, parser: ConfigParser) -> None: + with open(self._config, "wt") as f: + parser.write(f) diff --git a/port_for/utils.py b/port_for/utils.py new file mode 100644 index 0000000..361d5c0 --- /dev/null +++ b/port_for/utils.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +import itertools +from typing import Iterable, Iterator, Tuple, Set + + +def ranges_to_set(lst: Iterable[Tuple[int, int]]) -> Set[int]: + """ + Convert a list of ranges to a set of numbers:: + + >>> ranges = [(1,3), (5,6)] + >>> sorted(list(ranges_to_set(ranges))) + [1, 2, 3, 5, 6] + + """ + return set(itertools.chain(*(range(x[0], x[1] + 1) for x in lst))) + + +def to_ranges(lst: Iterable[int]) -> Iterator[Tuple[int, int]]: + """ + Convert a list of numbers to a list of ranges:: + + >>> numbers = [1,2,3,5,6] + >>> list(to_ranges(numbers)) + [(1, 3), (5, 6)] + + """ + for a, b in itertools.groupby(enumerate(lst), lambda t: t[1] - t[0]): + c = list(b) + yield c[0][1], c[-1][1] -- cgit v1.2.3