Skip to content

Commit 74f5d25

Browse files
bjesterclaude
andcommitted
Fall back to localhost when the client IP probe is denied
_get_client_ip_for_server opens a UDP socket to learn which local address routes to the server. AppArmor or a sandbox can refuse to create that socket, which raised outside the existing try block. It now logs a warning and returns 127.0.0.1, as it already did when the connect failed. The bare except is narrowed to OSError. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 9c14262 commit 74f5d25

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎morango/sync/syncsession.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,25 @@ def _get_server_ip(hostname):
6969

7070

7171
def _get_client_ip_for_server(server_host, server_port):
72-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
72+
default_ip = "127.0.0.1"
73+
74+
# strict apparmor rules or sandboxing could prevent this
75+
try:
76+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
77+
except PermissionError:
78+
logger.warning(
79+
f"Could not create socket to determine external IP address to reach {server_host}"
80+
)
81+
return default_ip
82+
7383
try:
7484
s.connect((server_host, server_port))
75-
IP = s.getsockname()[0]
76-
except: # noqa: E722
77-
IP = "127.0.0.1"
85+
return s.getsockname()[0]
86+
except OSError:
87+
logger.warning(f"Could not determine external IP address to reach {server_host}")
88+
return default_ip
7889
finally:
7990
s.close()
80-
return IP
8191

8292

8393
def ignore_404(target):

0 commit comments

Comments
 (0)