-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from anyproto/fixQuicListen
Fix QUIC listening
- Loading branch information
Showing
2 changed files
with
54 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import sys | ||
import re | ||
import yaml | ||
import json | ||
|
||
arguments = sys.argv[1:] | ||
yamlFile = arguments[0] | ||
listenHosts = arguments[1:] | ||
# load .env vars | ||
envVars = dict() | ||
if os.path.exists('.env') and os.path.getsize('.env') > 0: | ||
with open('.env') as file: | ||
for line in file: | ||
if line.startswith('#') or not line.strip(): | ||
continue | ||
key, value = line.strip().split('=', 1) | ||
value = value.replace('"', '') | ||
if key in envVars: | ||
print(f"WARNING: dublicate key={key} in env file='.env'") | ||
envVars[key] = value | ||
else: | ||
print(f"ERROR: file='.env' not found or size=0") | ||
exit(1) | ||
|
||
with open(yamlFile, 'r') as file: | ||
#print(f"DEBUG: envVars={json.dumps(envVars,indent=4)}") | ||
|
||
inputYamlFile = sys.argv[1] | ||
outputYamlFile = sys.argv[2] | ||
listenHosts = envVars['EXTERNAL_LISTEN_HOSTS'].split() | ||
if 'EXTERNAL_LISTEN_HOST' in envVars: | ||
listenHosts.append(envVars['EXTERNAL_LISTEN_HOST']) | ||
|
||
print(f"DEBUG: listenHosts={listenHosts}") | ||
|
||
# read input yaml file | ||
with open(inputYamlFile, 'r') as file: | ||
config = yaml.load(file,Loader=yaml.Loader) | ||
|
||
# processing addresses for nodes | ||
for index, nodes in enumerate(config['nodes']): | ||
addresses = nodes['addresses'] | ||
port = addresses[0].split(':')[1] | ||
for listenHost in listenHosts: | ||
listenAddress = listenHost +':'+ port | ||
if listenAddress not in addresses: | ||
addresses.append(listenAddress) | ||
|
||
with open(yamlFile, 'w') as file: | ||
listenHost = nodes['addresses'][0].split(':')[0] | ||
listenPort = nodes['addresses'][0].split(':')[1] | ||
nodeListenHosts = [listenHost] + listenHosts | ||
for nodeListenHost in nodeListenHosts: | ||
listenAddress = nodeListenHost +':'+ str(listenPort) | ||
if listenAddress not in nodes['addresses']: | ||
nodes['addresses'].append(listenAddress) | ||
# add "quic" listen address | ||
for name,value in envVars.items(): | ||
if re.match(r"^(ANY_SYNC_.*_PORT)$", name) and value == listenPort: | ||
quicPortKey = name.replace('_PORT', '_QUIC_PORT') | ||
quicPortValue = envVars[quicPortKey] | ||
quicListenAddress = 'quic://'+ nodeListenHost +':'+ str(quicPortValue) | ||
if ( quicPortValue ) and ( quicListenAddress not in nodes['addresses']): | ||
nodes['addresses'].append(quicListenAddress) | ||
|
||
# write output yaml file | ||
with open(outputYamlFile, 'w') as file: | ||
yaml.dump(config, file) |