1
0
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:
Felipe Martins Diel 2020-09-14 22:33:59 -03:00 committed by Matthew Garrett
parent 086fd1cd75
commit 5af3a81264
2 changed files with 20 additions and 5 deletions

View File

@ -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
View 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]