From 9271de25fbb5e66e7ee7a3ee7bc0b11f583d99a0 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Fri, 10 Mar 2023 00:09:04 +0100 Subject: [PATCH] updated _modules state --- states/_modules/net.py | 47 +++++++++++++++++++++++++++++++++++- states/_modules/nextcloud.py | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/states/_modules/net.py b/states/_modules/net.py index 7d5f660..1b30195 100644 --- a/states/_modules/net.py +++ b/states/_modules/net.py @@ -1,3 +1,48 @@ #!/usr/bin/python3 -ip_cidr_regex="\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}\/(\d+)" +import re +import json +import urllib +from urllib.request import urlopen, Request +from urllib.parse import urljoin +from urllib.error import HTTPError + +MIN_CIDR=32 +CIDR_REGEX = re.compile(r"(.*)/(.*)") +POSTFIX_FORMAT = lambda ip: "[{}]/{}".format(CIDR_REGEX.findall(ip)[0][0], + CIDR_REGEX.findall(ip)[0][1]) + +def __virtual__(): + return True + +def get_public_ip(url="https://ipinfo.io"): + req = Request(url=url,) + req.add_header("Accept", "*/*") + + res = urlopen(req) + + ret = json.loads(res.read()) + if res.status == 200: + return "{}/{}".format(ret["ip"],MIN_CIDR) + + return None + + +def get_internal_networks(tgt="*", fmt="std"): + res = [] + res.extend([net for k, net in __salt__["mine.get"](tgt=tgt, fun='public_ip', tgt_type='compound').items()]) + res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks', tgt_type='compound').items() for net in nets if __salt__['network.is_private'](net.split("/")[0])]) + if fmt == "std": + res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks6', tgt_type='compound').items() for net in nets]) + elif fmt == "postfix": + res.extend([POSTFIX_FORMAT(net) for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks6', tgt_type='compound').items() for net in nets]) + res = list(set(res)) + res.sort() + return res + +def get_nat_networks(tgt="*"): + res = [] + res.extend([net for k, nets in __salt__["mine.get"](tgt=tgt, fun='ip_networks', tgt_type='compound').items() for net in nets if __salt__['network.is_private'](net.split("/")[0])]) + res = list(set(res)) + res.sort() + return res diff --git a/states/_modules/nextcloud.py b/states/_modules/nextcloud.py index f0c7831..1692c57 100644 --- a/states/_modules/nextcloud.py +++ b/states/_modules/nextcloud.py @@ -11,4 +11,4 @@ def get_version(configfile=None): res = regex.match(l.strip("\n")) if res: return ".".join(res.groups()) - return "" + return None