-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d0227a
commit f342d94
Showing
2 changed files
with
30 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import socket | ||
import asyncio | ||
import netifaces | ||
|
||
if __name__ == '__main__': | ||
DISCOVERY_IP = "0.0.0.0" | ||
DISCOVERY_PORT = 48899 | ||
DISCOVERY_MESSAGE = ["WIFIKIT-214028-READ".encode(), "HF-A11ASSISTHREAD".encode()] | ||
|
||
ifaces = netifaces.ifaddresses(netifaces.gateways()['default'][2][1]) | ||
iface_inet = ifaces[netifaces.AF_INET][0] | ||
iface_link = ifaces[netifaces.AF_LINK][0] | ||
|
||
class EchoServerProtocol: | ||
def connection_made(self, transport): | ||
self.transport = transport | ||
|
||
ifaces = netifaces.ifaddresses(netifaces.gateways()['default'][2][1]) | ||
iface_inet = ifaces[netifaces.AF_INET][0] | ||
iface_link = ifaces[netifaces.AF_LINK][0] | ||
def datagram_received(self, data, addr): | ||
if data == DISCOVERY_MESSAGE[0]: | ||
print(f"DiscoveryProtocol: Received {data} from {addr}") | ||
self.transport.sendto(f"{iface_inet["addr"]},{iface_link["addr"].replace(':', '').upper()},1234567890".encode(), addr) | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | ||
s.settimeout(1) | ||
s.bind(('', 48899)) | ||
async def main(): | ||
loop = asyncio.get_running_loop() | ||
transport, _ = await loop.create_datagram_endpoint(EchoServerProtocol, local_addr = (DISCOVERY_IP, DISCOVERY_PORT), family = socket.AF_INET, allow_broadcast = True) | ||
|
||
while True: | ||
try: | ||
m = s.recvfrom(1024) | ||
d = m[0].decode() | ||
if d == "WIFIKIT-214028-READ": | ||
print(f"{m[0]} from {m[1]}") | ||
data = f"{iface_inet["addr"]},{iface_link["addr"].replace(':', '').upper()},1234567890" | ||
s.sendto(data.encode(), m[1]) | ||
except (TimeoutError, socket.timeout): | ||
continue | ||
try: | ||
await asyncio.sleep(3600) | ||
finally: | ||
transport.close() | ||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |