-serverIPv4 = 'ipv4.ns.ralfj.de' # Only needed if haveIPv4 is True. This server should NOT have an AAAA record!
-serverIPv6 = 'ipv6.ns.ralfj.de' # Only needed if haveIPv6 is True. This server should NOT have an A record!
-server = 'ns.ralfj.de'
-# END of configuration variables
+def getMyIP(family, config, methods = {}, verbose = 0):
+ '''Returns our current IP address (<family> can be "IPv4" or "IPv6"), detected as given by the configuration.
+ Additional detection methods can be supplied via <methods>.'''
+ method = config[family]['method']
+ if method == 'none':
+ return None
+ elif method == 'web':
+ server = config[family].get('server', config['DEFAULT']['server'])
+ url = 'https://'+server+'/checkip'
+ try:
+ ip = urlopen(url, config)
+ except urllib.error.URLError:
+ raise Exception("Error fetching {}, make sure the URL is correct and the internet connection actually works.".format(url))
+ if verbose >= VERBOSE_FULL:
+ print("Server",server,"says my",family,"is",ip)
+ return ip
+ elif method in methods:
+ return methods[method]()
+ else:
+ raise Exception("Unsupported "+family+" detection method: "+method)