2022-03-08 19:42:28 +01:00
|
|
|
import __main__
|
|
|
|
from secrets import token_urlsafe as _token_urlsafe, token_hex as _token_hex
|
|
|
|
from string import ascii_letters as _ascii_letters
|
|
|
|
from random import choice as _choice
|
|
|
|
from os.path import dirname as _dirname, abspath as _abspath, join as _join, isfile as _isfile, isdir as _isdir
|
|
|
|
from os import makedirs as _makedirs
|
|
|
|
|
|
|
|
|
2022-06-29 11:44:01 +02:00
|
|
|
from risotto.utils import ZONES_SERVER
|
2022-06-24 19:00:16 +02:00
|
|
|
|
|
|
|
|
2022-03-15 12:12:56 +01:00
|
|
|
_HERE = _dirname(_abspath(__main__.__file__))
|
|
|
|
_PASSWORD_DIR = _join(_HERE, 'password')
|
2022-03-08 19:42:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_password(server_name: str,
|
|
|
|
username: str,
|
|
|
|
description: str,
|
|
|
|
type: str,
|
2022-06-24 19:00:16 +02:00
|
|
|
hide: bool,
|
2022-03-08 19:42:28 +01:00
|
|
|
length: int=20,
|
|
|
|
temporary: bool=True,
|
|
|
|
) -> str:
|
2022-06-24 19:00:16 +02:00
|
|
|
if hide:
|
|
|
|
return "XXXXX"
|
2022-03-08 19:42:28 +01:00
|
|
|
def gen_password():
|
|
|
|
return _token_urlsafe(length)[:length]
|
|
|
|
return _set_password(server_name,
|
|
|
|
username,
|
|
|
|
description,
|
|
|
|
gen_password,
|
|
|
|
temporary,
|
2022-04-28 21:48:16 +02:00
|
|
|
type,
|
2022-03-08 19:42:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_password_alpha_num(server_name,
|
|
|
|
username: str,
|
|
|
|
description: str,
|
|
|
|
length,
|
2022-06-24 19:00:16 +02:00
|
|
|
hide: bool,
|
2022-03-08 19:42:28 +01:00
|
|
|
starts_with_char=False,
|
|
|
|
):
|
2022-06-24 19:00:16 +02:00
|
|
|
if hide:
|
|
|
|
return "XXXXX"
|
2022-03-08 19:42:28 +01:00
|
|
|
def gen_password():
|
|
|
|
password = _token_hex()
|
|
|
|
if starts_with_char:
|
|
|
|
password = _choice(_ascii_letters) + password
|
|
|
|
return password[:length]
|
|
|
|
return _set_password(server_name,
|
|
|
|
username,
|
|
|
|
description,
|
|
|
|
gen_password,
|
|
|
|
True,
|
2022-04-28 21:48:16 +02:00
|
|
|
'cleartext',
|
2022-03-08 19:42:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _set_password(server_name: str,
|
|
|
|
username: str,
|
|
|
|
description: str,
|
|
|
|
gen_password,
|
|
|
|
temporary,
|
2022-04-28 21:48:16 +02:00
|
|
|
type,
|
2022-03-08 19:42:28 +01:00
|
|
|
) -> str:
|
2022-04-28 21:48:16 +02:00
|
|
|
if type != 'cleartext':
|
|
|
|
raise Exception('only cleartext is supported')
|
2022-03-08 19:42:28 +01:00
|
|
|
if not server_name or not username:
|
|
|
|
return
|
2022-03-15 12:12:56 +01:00
|
|
|
dir_name = _join(_PASSWORD_DIR, server_name, description)
|
2022-03-08 19:42:28 +01:00
|
|
|
if not _isdir(dir_name):
|
|
|
|
_makedirs(dir_name)
|
|
|
|
file_name = _join(dir_name, username)
|
|
|
|
if not _isfile(file_name):
|
|
|
|
password = gen_password()
|
|
|
|
with open(file_name, 'w') as fh:
|
|
|
|
fh.write(password)
|
|
|
|
with open(file_name, 'r') as fh:
|
2022-04-28 21:48:16 +02:00
|
|
|
file_content = fh.read().strip()
|
|
|
|
return file_content
|
2022-03-08 19:42:28 +01:00
|
|
|
|
|
|
|
|
2022-06-24 19:00:16 +02:00
|
|
|
def get_range(lst):
|
|
|
|
return list(range(max(1, len(lst))))
|
2022-03-08 19:42:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_zone_name(zones: list,
|
|
|
|
index: str,
|
|
|
|
):
|
|
|
|
if zones is not None:
|
|
|
|
return zones[int(index)]
|
|
|
|
|
|
|
|
|
|
|
|
def get_domain_name(server_name: str,
|
|
|
|
extra_domainnames: list,
|
|
|
|
suffix: str,
|
|
|
|
) -> str:
|
|
|
|
index = int(suffix)
|
|
|
|
if index == 0:
|
|
|
|
return server_name
|
|
|
|
return extra_domainnames[index - 1]
|
2022-06-24 19:00:16 +02:00
|
|
|
|
|
|
|
|
2022-06-25 08:11:37 +02:00
|
|
|
def get_provider_name(network_name: str,
|
|
|
|
provider: str,
|
|
|
|
) -> str:
|
|
|
|
if network_name not in ZONES_SERVER['providers'] or provider not in ZONES_SERVER['providers'][network_name]:
|
|
|
|
return
|
|
|
|
return ZONES_SERVER['providers'][network_name][provider][0]
|