|
@@ -1,80 +1,46 @@
|
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
|
-
|
|
|
|
|
import socket
|
|
import socket
|
|
|
-import platform
|
|
|
|
|
-from subprocess import check_output, Popen
|
|
|
|
|
-
|
|
|
|
|
-INVALID = "INVALID"
|
|
|
|
|
-WAKE = "WAKE"
|
|
|
|
|
-SLEEP = "SLEEP"
|
|
|
|
|
-OTHERS = "OTHERS"
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def parse_magic_packet(packet):
|
|
|
|
|
- frame = packet[0:6].hex()
|
|
|
|
|
- macs = set()
|
|
|
|
|
- for _ in range(0, 16):
|
|
|
|
|
- packet = packet[6:]
|
|
|
|
|
- mac = packet[:6].hex()
|
|
|
|
|
- macs.add(mac)
|
|
|
|
|
-
|
|
|
|
|
- if frame != "ff"*6 or len(macs) != 1:
|
|
|
|
|
- return INVALID
|
|
|
|
|
-
|
|
|
|
|
- mac = list(macs)[0].lower()
|
|
|
|
|
- if mac in MACS:
|
|
|
|
|
- return WAKE
|
|
|
|
|
-
|
|
|
|
|
- if mac[::-1] in MACS:
|
|
|
|
|
- return SLEEP
|
|
|
|
|
-
|
|
|
|
|
- return OTHERS
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def get_windows_macs():
|
|
|
|
|
- import re
|
|
|
|
|
- out = check_output(["ipconfig", "/all"]).decode("ascii").split("\n")
|
|
|
|
|
- match_mac = r"\s(?P<mac>[0-9a-f-]{17})\s"
|
|
|
|
|
- ret = []
|
|
|
|
|
- for line in out:
|
|
|
|
|
- matches = re.search(match_mac, line, flags=re.IGNORECASE)
|
|
|
|
|
- if matches:
|
|
|
|
|
- ret.append(matches.group("mac").replace("-", "").lower())
|
|
|
|
|
- return ret
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def get_linux_macs():
|
|
|
|
|
- import netifaces
|
|
|
|
|
- ret = []
|
|
|
|
|
- ifaces = netifaces.interfaces()
|
|
|
|
|
- ifaces.remove('lo')
|
|
|
|
|
- for i in ifaces:
|
|
|
|
|
- addresses = netifaces.ifaddresses(i)[netifaces.AF_LINK]
|
|
|
|
|
- ret.append(addresses[0]['addr'].replace(":", "")).lower()
|
|
|
|
|
- return ret
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def get_macs():
|
|
|
|
|
- if platform.system() == 'Windows':
|
|
|
|
|
- return get_windows_macs()
|
|
|
|
|
- else:
|
|
|
|
|
- return get_linux_macs()
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def suspend():
|
|
|
|
|
- if platform.system() == 'Windows':
|
|
|
|
|
- Popen(["rundll32.exe", "powrprof.dll,SetSuspendState", "0,1,0"])
|
|
|
|
|
- else:
|
|
|
|
|
- Popen(["systemctl", "suspend"])
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
-sock.bind(('', 9))
|
|
|
|
|
-MACS = get_macs()
|
|
|
|
|
-
|
|
|
|
|
-while True:
|
|
|
|
|
- msg, addr = sock.recvfrom(1024)
|
|
|
|
|
- res = parse_magic_packet(msg)
|
|
|
|
|
- print(res)
|
|
|
|
|
- if res == "SLEEP":
|
|
|
|
|
- suspend()
|
|
|
|
|
|
|
+import myutil
|
|
|
|
|
+from multiprocessing import Process
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_data(data):
|
|
|
|
|
+ print(data)
|
|
|
|
|
+ if data == "SLEEP":
|
|
|
|
|
+ # myutil.suspend()
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def listen_udp():
|
|
|
|
|
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
+ sock.bind(('', 9))
|
|
|
|
|
+ while True:
|
|
|
|
|
+ msg, _ = sock.recvfrom(1024)
|
|
|
|
|
+ res = myutil.parse_magic_packet(msg)
|
|
|
|
|
+ handle_data(res)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def listen_tcp():
|
|
|
|
|
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
+ sock.bind(('0.0.0.0', 7777))
|
|
|
|
|
+ sock.listen(1)
|
|
|
|
|
+ while True:
|
|
|
|
|
+ conn, _ = sock.accept()
|
|
|
|
|
+ data = conn.recv(4096)
|
|
|
|
|
+ if not data:
|
|
|
|
|
+ continue
|
|
|
|
|
+ data = data.decode("utf-8").split("\r\n")
|
|
|
|
|
+ path = data[0].split(" ")[1].lstrip("/")
|
|
|
|
|
+ conn.send(b"HTTP/1.1 200 OK\n\n\n")
|
|
|
|
|
+ conn.close()
|
|
|
|
|
+ handle_data(path)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ p = Process(target=listen_udp)
|
|
|
|
|
+ p.start()
|
|
|
|
|
+ q = Process(target=listen_tcp)
|
|
|
|
|
+ q.start()
|
|
|
|
|
+ p.join()
|
|
|
|
|
+ q.join()
|