bambulab-wireguard-proxy/announce.py

43 lines
1.2 KiB
Python
Raw Normal View History

2023-11-03 23:07:32 +01:00
#!/usr/bin/python3
import socket
import time
import subprocess
UDP_PORT = 2021
MESSAGE = b"""NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
2023-11-03 23:39:29 +01:00
Server: https://git.f3l.de/asdil12/bambulab-wireguard-proxy/
2023-11-03 23:07:32 +01:00
Location: 10.73.37.29
NT: urn:bambulab-com:device:3dprinter:1
USN: 01S00C321501449
Cache-Control: max-age=1800
DevModel.bambu.com: C11
DevName.bambu.com: Nerdberg Bambu P1P
DevSignal.bambu.com: -41
DevConnect.bambu.com: lan
DevBind.bambu.com: free
Devseclink.bambu.com: secure
""".replace(b"\n", b"\r\n")
while True:
for pl in subprocess.check_output(["wg", "show", "wg-firezone", "dump"]).decode().strip().split("\n")[1:]:
ips = pl.split("\t")[3]
for ip in ips.split(","):
if ip.endswith("/32"):
2023-11-04 00:04:15 +01:00
UDP_IP = ip[:-3]
print(f"{UDP_IP}:{UDP_PORT} {MESSAGE=}" % MESSAGE)
2023-11-03 23:07:32 +01:00
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.bind(('0.0.0.0', 1900)) # 1900 is SSDP src port
try:
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
except OSError:
# OSError: [Errno 89] Destination address required
# this error means that wireguard doesn't know an endpoint for this peer
# so we can ignore this error
pass
2023-11-03 23:07:32 +01:00
sock.close()
time.sleep(10)