mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 22:51:41 +01:00
Restore VPN support
This commit is contained in:
parent
086fd1cd75
commit
5af3a81264
@ -13,6 +13,7 @@ from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
|
||||
from .exceptions import check_error, exception
|
||||
from .helpers import get_local_ip
|
||||
|
||||
|
||||
def get_devices():
|
||||
@ -116,11 +117,7 @@ def discover(
|
||||
discover_ip_address='255.255.255.255',
|
||||
discover_ip_port=80
|
||||
):
|
||||
if local_ip_address is None:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.connect(('8.8.8.8', 53)) # connecting to a UDP address doesn't send packets
|
||||
local_ip_address = s.getsockname()[0]
|
||||
|
||||
local_ip_address = local_ip_address or get_local_ip()
|
||||
address = local_ip_address.split('.')
|
||||
cs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
18
broadlink/helpers.py
Normal file
18
broadlink/helpers.py
Normal file
@ -0,0 +1,18 @@
|
||||
"""Helper functions."""
|
||||
import socket
|
||||
|
||||
|
||||
def get_local_ip() -> str:
|
||||
"""Try to determine the local IP address of the machine."""
|
||||
# Useful for VPNs.
|
||||
try:
|
||||
local_ip_address = socket.gethostbyname(socket.gethostname())
|
||||
if not local_ip_address.startswith('127.'):
|
||||
return local_ip_address
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# Connecting to UDP address does not send packets.
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.connect(('8.8.8.8', 53))
|
||||
return s.getsockname()[0]
|
Loading…
Reference in New Issue
Block a user