-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
258 lines (245 loc) · 12.9 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import configparser
import json
import sys
from enums import *
from structures import Fallback4, Real4, Virtual4, GlobalConfig
def __illegal_config_value(section, key, value, allowed):
print("Illegal configuration value '%s' for %s in section '%s'. Allowed values are: %s"
% (value, key, section, allowed), file=sys.stderr)
sys.exit(1)
def __parse_host(hoststring):
tmp1 = hoststring.rsplit(" ", 1)
tmp2 = tmp1[0].rsplit(":", 1)
host = tmp2[0]
port = int(tmp2[1])
if tmp1[1] == "gate":
method = ForwardingMethod.gate
elif tmp1[1] == "masq":
method = ForwardingMethod.masq
elif tmp1[1] == "ipip":
method = ForwardingMethod.ipip
else:
raise ValueError
return host, port, method
def parse_config(file):
config = configparser.ConfigParser()
config.read(file)
global_config = None
virtuals = list()
sections = config.sections()
for section in sections:
# special 'global' section handling
if section == "global":
global_args = dict()
global_args["configfile"] = file
for key in config[section]:
if key == "autoreload":
try:
global_args["autoreload"] = cur_section.getboolean(key)
except ValueError:
__illegal_config_value(section, key, cur_section[key],
"'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'")
elif key == "supervised":
try:
global_args["supervised"] = cur_section.getboolean(key)
except ValueError:
__illegal_config_value(section, key, cur_section[key],
"'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'")
elif key == "smtp":
global_args["smtp"] = cur_section[key] # FIXME
elif key == "logfile":
global_args["logfile"] = cur_section[key]
elif key == "callback":
global_args["callback"] = cur_section[key]
elif key == "maintenancedir":
global_args["maintenancedir"] = cur_section[key]
elif key == "configfile":
global_args["configfile"] = cur_section[key]
global_config = GlobalConfig(**global_args)
else:
virtual_args = dict()
virtual_args["description"] = section
reals = list()
fallback = None
cur_section = config[section]
for key in config[section]:
if key == "real":
for hoststring in json.loads(cur_section[key]):
ip, port, method = __parse_host(hoststring)
real = Real4(ip=ip, port=port, method=method)
reals.append(real)
elif key == "fallback":
ip, port, method = __parse_host(cur_section[key])
fallback = Fallback4(ip=ip, port=port, method=method)
elif key == "host":
virtual_args["ip"] = cur_section[key] # TODO: handle hostnames
elif key == "port":
try:
virtual_args["port"] = int(cur_section[key])
if not 0 < virtual_args["port"] <= 65535:
__illegal_config_value(section, key, cur_section[key], "0 < port <= 65535")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < port <= 65535")
elif key == "checkport":
try:
virtual_args["checkport"] = int(cur_section[key])
if not 0 < virtual_args["checkport"] <= 65535:
__illegal_config_value(section, key, cur_section[key], "0 < checkport <= 65535")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < checkport <= 65535")
elif key == "checktimeout":
try:
virtual_args["checktimeout"] = int(cur_section[key])
if not 0 < virtual_args["checktimeout"]:
__illegal_config_value(section, key, cur_section[key], "0 < checktimeout")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < checktimeout")
elif key == "negotiatetimeout":
try:
virtual_args["negotiatetimeout"] = int(cur_section[key])
if not 0 < virtual_args["negotiatetimeout"]:
__illegal_config_value(section, key, cur_section[key], "0 < negotiatetimeout")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < negotiatetimeout")
elif key == "checkinterval":
try:
virtual_args["checkinterval"] = int(cur_section[key])
if not 0 < virtual_args["checkinterval"]:
__illegal_config_value(section, key, cur_section[key], "0 < checkinterval")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < checkinterval")
elif key == "failurecount":
try:
virtual_args["failurecount"] = int(cur_section[key])
if not 0 <= virtual_args["failurecount"]:
__illegal_config_value(section, key, cur_section[key], "0 <= failurecount")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 <= failurecount")
elif key == "cleanstop":
try:
virtual_args["cleanstop"] = cur_section.getboolean("cleanstop")
except ValueError:
__illegal_config_value(section, key, cur_section[key],
"'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'")
elif key == "quiescent":
try:
virtual_args["quiescent"] = cur_section.getboolean("quiescent")
except ValueError:
__illegal_config_value(section, key, cur_section[key],
"'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'")
elif key == "readdquiescent":
try:
virtual_args["readdquiescent"] = cur_section.getboolean("readdquiescent")
except ValueError:
__illegal_config_value(section, key, cur_section[key],
"'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'")
elif key == "persistent":
try:
virtual_args["persistent"] = int(cur_section[key])
if not 0 < virtual_args["persistent"]:
__illegal_config_value(section, key, cur_section[key], "0 < persistent")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 < persistent")
elif key == "protocol":
raw = cur_section[key]
if raw == "tcp":
virtual_args["protocol"] = Protocol.tcp
elif raw == "udp":
virtual_args["protocol"] = Protocol.udp
elif raw == "fwm":
virtual_args["protocol"] = Protocol.fwm
else:
__illegal_config_value(section, key, cur_section[key], "tcp, udp, fwm")
elif key == "checktype":
raw = cur_section[key]
if raw == "connect":
virtual_args["checktype"] = Checktype.connect
elif raw == "external" or raw == "external-perl":
virtual_args["checktype"] = Checktype.external
elif raw == "negotiate":
virtual_args["checktype"] = Checktype.negotiate
elif raw == "off":
virtual_args["checktype"] = Checktype.off
elif raw == "on":
virtual_args["checktype"] = Checktype.on
elif raw == "ping":
virtual_args["checktype"] = Checktype.ping
elif raw == "negotiate_connect":
virtual_args["checktype"] = Checktype.negotiate_connect
else:
__illegal_config_value(section, key, cur_section[key],
"connect, external, negotiate, off, on, ping, negotiate_connect")
elif key == "scheduler":
raw = cur_section[key]
if raw == "rr":
virtual_args["scheduler"] = Scheduler.rr
elif raw == "wrr":
virtual_args["scheduler"] = Scheduler.wrr
elif raw == "lc":
virtual_args["scheduler"] = Scheduler.lc
elif raw == "wlc":
virtual_args["scheduler"] = Scheduler.wlc
elif raw == "lblc":
virtual_args["scheduler"] = Scheduler.lblc
elif raw == "lblcr":
virtual_args["scheduler"] = Scheduler.lblcr
elif raw == "dh":
virtual_args["scheduler"] = Scheduler.dh
elif raw == "sh":
virtual_args["scheduler"] = Scheduler.sh
elif raw == "sed":
virtual_args["scheduler"] = Scheduler.sed
elif raw == "nq":
virtual_args["scheduler"] = Scheduler.nq
else:
__illegal_config_value(section, key, cur_section[key],
"rr, wrr, lc, wlc, lblc, lblcr, dh, sh, sed, nq")
elif key == "httpmethod":
raw = cur_section[key]
if raw == "get":
virtual_args["httpmethod"] = HTTPMethod.get
elif raw == "head":
virtual_args["httpmethod"] = HTTPMethod.head
else:
__illegal_config_value(section, key, cur_section[key], "get, head")
elif key == "emailalert":
virtual_args["emailalert"] = cur_section[key]
elif key == "emailalertfrom":
virtual_args["emailalertfrom"] = cur_section[key]
elif key == "emailalertfreq":
try:
virtual_args["emailalertfreq"] = int(cur_section[key])
if not 0 <= virtual_args["emailalertfreq"]:
__illegal_config_value(section, key, cur_section[key], "0 <= emailalertfreq")
except ValueError:
__illegal_config_value(section, key, cur_section[key], "0 <= emailalertfreq")
elif key == "service":
virtual_args["service"] = cur_section[key]
elif key == "checkcommand":
virtual_args["checkcommand"] = cur_section[key]
elif key == "hostname":
virtual_args["hostname"] = cur_section[key]
elif key == "login":
virtual_args["login"] = cur_section[key]
elif key == "passwd":
virtual_args["passwd"] = cur_section[key]
elif key == "database":
virtual_args["database"] = cur_section[key]
elif key == "secret":
virtual_args["secret"] = cur_section[key]
elif key == "fingerprint":
virtual_args["fingerprint"] = cur_section[key]
elif key == "request":
virtual_args["request"] = cur_section[key]
elif key == "receive":
virtual_args["receive"] = cur_section[key]
else:
print("CUSTOM ATTRIBUTE: %s = %s" % (key, cur_section[key]))
virtual_args[key] = cur_section[key]
# create virtual server and add real servers as well as the fallback server
virtual = Virtual4(**virtual_args)
virtual.set_fallback(fallback)
for real in reals:
virtual.add_real(real)
virtuals.append(virtual)
return global_config, virtuals