2323 # Python 3
2424 basestring = str
2525 import configparser as ConfigParser
26+ from typing import Any # noqa: F401
2627
2728CONFIG_FILE = "config.ini"
2829LOG_FOLDER = "logs"
@@ -221,6 +222,7 @@ def get_config(name, config_file=CONFIG_FILE):
221222 config .read (config_file )
222223 return {
223224 "IP" : config .get (name , "IP" ),
225+ "ExternalIP" : config .get (name , "ExternalIP" ),
224226 "Port" : config .getint (name , "Port" ),
225227 "Name" : config .get (name , "Name" ),
226228 "MaxThread" : config .getint (name , "MaxThread" ),
@@ -239,19 +241,31 @@ def get_config(name, config_file=CONFIG_FILE):
239241
240242
241243def get_default_ip ():
244+ # type: () -> str
242245 """Get the default IP address"""
243246 s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
244247 s .connect (("8.8.8.8" , 80 ))
245- ip = s .getsockname ()[0 ]
248+ ip = s .getsockname ()[0 ] # type: str
246249 s .close ()
247250 return ip
248251
249252
250253def get_ip (ip ):
254+ # type: (str) -> str
251255 """Return the IP address that will be used."""
252256 return get_default_ip () if ip == "0.0.0.0" else ip
253257
254258
259+ def get_external_ip (config ):
260+ # type: (dict[str, Any]) -> str
261+ """Return the IP address advertised by the server.
262+
263+ It's useful when the public IP address can't easily be retrieved.
264+ For instance, when behind a NAT or some cloud infrastructures.
265+ """
266+ return config ["ExternalIP" ] or get_ip (config ["IP" ])
267+
268+
255269def argparse_from_config (config ):
256270 """Argument parser from config."""
257271 import argparse
0 commit comments