Added sync feature

This commit is contained in:
2023-12-17 17:06:30 +02:00
parent 96fc572bd0
commit 66a6c3e6d6
6 changed files with 94 additions and 58 deletions

21
lib.py
View File

@ -8,10 +8,9 @@ logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%d-%m-%Y %H:%M:%S')
CFG_PATH = '/usr/local/etc/outfleet/config.yaml'
class ServerDict(TypedDict):
server_id: str
name: str
url: str
cert: str
@ -30,9 +29,12 @@ class Server:
url: str,
cert: str,
comment: str,
# read from config. not the same as real server id you can get from api
local_server_id: str,
):
self.client = OutlineVPN(api_url=url, cert_sha256=cert)
self.data: ServerDict = {
'local_server_id': local_server_id,
'name': self.client.get_server_information()["name"],
'url': url,
'cert': cert,
@ -49,8 +51,21 @@ class Server:
def info(self) -> ServerDict:
return self.data
def check_client(self, name):
# Looking for any users with provided name. len(result) != 1 is a problem.
result = []
for key in self.client.get_keys():
if key.name == name:
result.append(name)
self.log.info(f"check_client found client `{name}` config is correct.")
if len(result) != 1:
self.log.warning(f"check_client found client `{name}` inconsistent. Found {len(result)} keys.")
return False
else:
return True
def apply_config(self, config):
def apply_config(self, config, CFG_PATH):
if config.get("name"):
self.client.set_server_name(config.get("name"))
self.log.info("Changed %s name to '%s'", self.data["server_id"], config.get("name"))