This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
forked from ffnord/mesh-announce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
109 lines (93 loc) · 4.11 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import socket
from configparser import ConfigParser
class GlobalOptions():
''' Container class for global options
'''
def __init__(self, port, default_domain):
self.port = port
self.default_domain = default_domain
class DomainOptions():
''' Base container class for per domain options
'''
@classmethod
def from_parser(cls, section, parser, globals):
''' Builds a DomainOptions object from a config section
Handles domain type specific options automatically
'''
from domain import DomainType
domain_type = parser.get(section, 'DomainType', fallback='simple')
# Get DomainOptions subclass for type and instantiate
return DomainType.get(domain_type.lower()).options(section, parser, globals)
def __init__(self, name, parser, globals):
''' Initialize common options
'''
from domain import Domain
self.contact = parser.get(name, 'Contact', fallback=None)
self.hardware_model = parser.get(name, 'Hardware-Model', fallback=None)
self.hostname = parser.get(name, 'Hostname', fallback=socket.gethostname())
self.name = name
self.interfaces = list(map(str.strip, parser.get(name, 'Interfaces', fallback='').split(',')))
self.is_gateway = parser.getboolean(name, 'VPN', fallback=True)
self.longitude = parser.getfloat(name, 'Longitude', fallback=None)
self.latitude = parser.getfloat(name, 'Latitude', fallback=None)
self.mcast_link = parser.get(name, 'MulticastLinkAddress', fallback='ff02::2:1001')
self.mcast_site = parser.get(name, 'MulticastSiteAddress', fallback='ff05::2:1001')
self.fastd_pubkey = parser.get(name, 'FastdPublicKey', fallback=None)
self.vpn_protos = frozenset(parser.get(name, 'VPNProtocols', fallback='fastd').split(','))
self.wireguard_pubkey = parser.get(name, 'WireGuardPublicKey', fallback=None)
self.ipv4_gateway = parser.get(name, 'IPv4Gateway', fallback=None)
self.domain_code = parser.get(name, 'DomainCode', fallback=name)
self.domain_type = Domain
class BatmanDomainOptions(DomainOptions):
''' Container for batman specific options
'''
def __init__(self, name, parser, globals):
''' Initialize common and batman-specific options
'''
from domain import BatadvDomain
# Parse common options
super().__init__(name, parser, globals)
# Parse batman specific options
self.batman_iface = parser.get(name, 'BatmanInterface', fallback='bat-' + name)
self.domain_type = BatadvDomain
class Config():
''' Represents a parsed config file
'''
@classmethod
def from_file(cls, fname):
''' Load config from file
'''
parser = ConfigParser(empty_lines_in_values=False, default_section='Defaults')
with open(fname) as file:
parser.read_file(file)
return cls(parser)
def __init__(self, parser):
''' load config from a config parser
'''
self._initialize_global_options(parser)
self.domains = { }
for domain in parser.sections():
self._initialize_domain_options(parser, domain)
if not self.globals.default_domain:
self.globals.default_domain = self.domains[domain]
def _initialize_global_options(self, parser):
''' Set all global options
'''
self.globals = GlobalOptions(
parser.getint('Defaults', 'Port', fallback=1001),
parser.get('Defaults', 'DefaultDomain', fallback=None)
)
def _initialize_domain_options(self, parser, domain):
''' Populate options for domain from config parser
'''
self.domains[domain] = DomainOptions.from_parser(domain, parser, self.globals)
def get_domain_names(self):
''' Get list of all domain names listed in the config
'''
return self.domains.keys()
def get_port(self):
return self.globals.port
def get_default_domain(self):
return self.globals.default_domain
def get_domain_config(self, domain):
return self.domains[domain]