-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastd-verify.py
executable file
·77 lines (63 loc) · 2.95 KB
/
fastd-verify.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
#!/usr/bin/python3
'''
Created on Apr 29, 2021
@author: roland
'''
import os
import time
import argparse
import logging
import logging.handlers
import json
my_logger = logging.getLogger('fastd.verify')
my_logger.setLevel(logging.DEBUG)
log_handler = logging.handlers.SysLogHandler(address = '/dev/log')
my_logger.addHandler(log_handler)
DEFAULT_PREFERENCE = 50
MAX_DELAY = 8.0
OVERLOAD_DELAY = 9.0
def IsValidKey(FilePath, FastdKey):
for FileName in os.listdir(FilePath):
try:
with open(os.path.join(FilePath, FileName), encoding = 'utf-8') as KeyFile:
KeyData = KeyFile.read()
except:
my_logger.error('fastd-verify: ** Error while reading KeyFile %s/%s' % (FilePath, FileName))
else:
if FastdKey in KeyData:
# my_logger.debug('fastd-verify: FastdKey %s found in KeyFile %s/%s' % (FastdKey, FilePath, FileName))
return True
my_logger.debug('fastd-verify: ** Key %s not found in %s' % (FastdKey, FilePath))
return False
def GetGwPreference(StatusFile, Segment):
try:
with open(StatusFile) as JsonFile:
StatusDict = json.load(JsonFile)
Preference = int(StatusDict['segments'][str(Segment)]['preference'])
except:
my_logger.error('fastd-verify: ** Error while checking GW StatusFile %s for Segment %d' % (StatusFile, Segment))
Preference = DEFAULT_PREFERENCE
return Preference
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Verification of Fastd Connection Requests',
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-k', '--keyfolder', dest='keyfolder', action='store', required=True, help='path to keyfiles')
parser.add_argument('-g', '--gwstatus', dest='gwstatus', action='store', required=True, help='path to gwstatus.json')
args = parser.parse_args()
if 'INTERFACE' not in os.environ or 'PEER_KEY' not in os.environ:
my_logger.error('fastd-verify: ** Error - Environment Variables not set')
else:
if IsValidKey(args.keyfolder, os.environ['PEER_KEY'].lower()):
Preference = GetGwPreference(args.gwstatus, int(os.environ['INTERFACE'][3:]))
my_logger.debug('fastd-verify: %s / %s / %d will be delayed...' % (os.environ['INTERFACE'], os.environ['PEER_KEY'], Preference))
if Preference < 5:
time.sleep(OVERLOAD_DELAY)
elif Preference <= 10:
time.sleep(MAX_DELAY)
elif Preference <= 30: # 10 .. 30
time.sleep((1.00 - (Preference-10)/20.0 * 0.75) * MAX_DELAY) # (10|8.0) .. (30|2.0)
elif Preference <= 50: # 30 .. 50
time.sleep((0.25 - (Preference-30)/20.0 * 0.25) * MAX_DELAY) # (30|2.0) .. (50|0.0)
my_logger.debug('fastd-verify: %s / %s / %d ok.' % (os.environ['INTERFACE'], os.environ['PEER_KEY'], Preference))
exit(0)
exit(1)