Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local IP address determination #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions thespian/system/transport/IPBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def __init__(self, af, socktype, proto, baseaddr, port, external=False):
A "truthy" value of external can be an external address to
try. Using the address of the Convention Leader (if any)
is recommended to ensure that the address chosen is
appropriate for the network supporting the Convention. By
default, the address is Go Daddy's public webserver
address.
appropriate for the network supporting the Convention. By
default, the address is Google's public DNS server.
"""
self.af = af
self.socktype = socktype
Expand All @@ -114,13 +113,16 @@ def __init__(self, af, socktype, proto, baseaddr, port, external=False):
baseaddr = None
if baseaddr == '':
baseaddr = None if external else '127.0.0.1'
base2 = os.getenv('THESPIAN_BASE_IPADDR', None)
if base2:
if baseaddr is None and (base2 := os.getenv('THESPIAN_BASE_IPADDR', None)):
baseaddr = base2
if external and not baseaddr:
# Trick to get the "public" IP address... doesn't work so
# well if there are multiple routes, or if the public site
# is not accessible. (needs work)

# If external is a list, use just the first element
if isinstance(external, list):
external = external[0]
remoteAddr = (
external
if isinstance(external, tuple)
Expand Down
16 changes: 16 additions & 0 deletions thespian/system/transport/test/test_TCPAddresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,19 @@ def testMixedHashing(self):
raises(TypeError, hash, a3)
raises(TypeError, hash, a4)
raises(TypeError, hash, a5)

def testThespianBaseAddressOverride(self, monkeypatch):
monkeypatch.setenv('THESPIAN_BASE_IPADDR', '1.2.3.4')

a1 = ActorAddress(TCPv4ActorAddress('1.2.3.4', 1234, external=False))
loopback = ActorAddress(TCPv4ActorAddress('127.0.0.1', 1234, external=False))

# override base address when None
assert a1 == ActorAddress(TCPv4ActorAddress(None, 1234, external=False))
assert a1 == ActorAddress(TCPv4ActorAddress(None, 1234, external=True))
# override base address when empty string
assert loopback == ActorAddress(TCPv4ActorAddress('', 1234, external=False))
assert a1 == ActorAddress(TCPv4ActorAddress('', 1234, external=True))
# do not ovverride base address when not None or empty string
assert a1 != ActorAddress(TCPv4ActorAddress('4.3.2.1', 1234, external=False))
assert a1 != ActorAddress(TCPv4ActorAddress('4.3.2.1', 1234, external=True))