bambulab-wireguard-proxy/announce.py

43 lines
1.2 KiB
Python
Executable File

#!/usr/bin/python3
import socket
import time
import subprocess
UDP_PORT = 2021
MESSAGE = b"""NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
Server: https://git.f3l.de/asdil12/bambulab-wireguard-proxy/
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"):
UDP_IP = ip[:-3]
print(f"{UDP_IP}:{UDP_PORT} {MESSAGE=}" % MESSAGE)
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
sock.close()
time.sleep(10)