2018-07-25 04:32:16 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from configparser import ConfigParser as Config
|
|
|
|
import netifaces
|
|
|
|
import ipaddress
|
|
|
|
import os
|
|
|
|
|
|
|
|
def yield_public_addresses():
|
|
|
|
for ifname in netifaces.interfaces():
|
|
|
|
addrs = netifaces.ifaddresses(ifname)
|
|
|
|
if netifaces.AF_INET in addrs:
|
|
|
|
for addr in addrs[netifaces.AF_INET]:
|
|
|
|
ip = addr['addr']
|
|
|
|
if not ipaddress.ip_address(ip).is_private:
|
|
|
|
yield ifname, ip
|
|
|
|
|
|
|
|
def genconf(rootdir):
|
|
|
|
conf = Config()
|
|
|
|
conf['router'] = {
|
|
|
|
'threads': '2',
|
|
|
|
'net-threads': '1',
|
|
|
|
'contact-file': os.path.join(rootdir, 'self.signed'),
|
|
|
|
'transport-privkey': os.path.join(rootdir, 'transport.key'),
|
|
|
|
'identity-privkey': os.path.join(rootdir, 'identity.key')
|
|
|
|
}
|
|
|
|
conf['netdb'] = {
|
|
|
|
'dir': os.path.join(rootdir, 'netdb')
|
|
|
|
}
|
|
|
|
conf['bind'] = {}
|
2018-07-25 04:34:28 +00:00
|
|
|
found = False
|
2018-07-25 04:32:16 +00:00
|
|
|
for ifname, ip in yield_public_addresses():
|
|
|
|
conf['bind'][ifname] = '1090'
|
|
|
|
print("using public address {}".format(ip))
|
2018-07-25 04:42:33 +00:00
|
|
|
break
|
2018-07-25 04:32:16 +00:00
|
|
|
else:
|
2018-07-25 04:34:28 +00:00
|
|
|
print("This machine has no public network addresses")
|
2018-07-25 04:42:33 +00:00
|
|
|
return conf
|
2018-07-25 04:32:16 +00:00
|
|
|
|
2018-07-25 04:42:33 +00:00
|
|
|
def main(args):
|
|
|
|
fname = 'daemon.ini'
|
|
|
|
if len(args) == 1:
|
|
|
|
fname = args[0]
|
2018-07-25 04:32:16 +00:00
|
|
|
conf = genconf(os.path.realpath('.'))
|
|
|
|
if conf:
|
2018-07-25 04:42:33 +00:00
|
|
|
with open(fname, 'w') as f:
|
2018-07-25 04:32:16 +00:00
|
|
|
conf.write(f)
|
2018-07-25 04:42:33 +00:00
|
|
|
print("wrote config to {}".format(fname))
|
2018-07-25 04:32:16 +00:00
|
|
|
if __name__ == '__main__':
|
2018-07-25 04:42:33 +00:00
|
|
|
import sys
|
|
|
|
main(sys.argv[1:])
|
2018-07-25 04:32:16 +00:00
|
|
|
|