forked from romansvozil/nostale_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
208 lines (156 loc) · 6.38 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import asyncio
import os.path
import queue
import threading
from typing import List, Dict, Tuple, Callable, Optional
import psutil
import win32con
import win32gui
import win32process
from injector import Injector
def get_nostale_windows() -> List[Dict[str, int]]:
def callback(hwnd, hwnds):
if not win32gui.IsWindowEnabled(hwnd):
return
if "NosTale" not in win32gui.GetWindowText(hwnd):
return
if "[BladeTiger12]" in win32gui.GetWindowText(hwnd):
return
_, pid = win32process.GetWindowThreadProcessId(hwnd)
hwnds.append({"pid": pid, "hwnd": hwnd})
windows = []
win32gui.EnumWindows(callback, windows)
return windows
def get_window_pid(window: int) -> int:
_, pid = win32process.GetWindowThreadProcessId(window)
return pid
def get_nostale_windows_wo_packet_logger() -> List[Dict[str, int]]:
windows = []
for window in get_nostale_windows():
process = psutil.Process(window["pid"])
for connection in process.connections():
if connection.laddr and connection.laddr.ip == "127.0.0.1":
break
else:
windows.append(window)
return windows
def get_packet_logger_path(nostale_pid: int) -> str:
process = psutil.Process(nostale_pid)
return os.path.dirname(process.exe()) + "/PacketLogger.dll"
def get_packet_logger_port(packet_logger: Dict[str, int]) -> int:
process = psutil.Process(packet_logger["pid"])
for connection in process.connections():
if connection.laddr and connection.laddr.ip == "127.0.0.1":
return connection.laddr.port
return 0
def get_packet_logger_windows() -> List[Dict[str, int]]:
def callback(hwnd, hwnds):
if not win32gui.IsWindowEnabled(hwnd):
return
if "PacketLogger" not in win32gui.GetWindowText(hwnd):
return
_, pid = win32process.GetWindowThreadProcessId(hwnd)
hwnds.append({"pid": pid, "hwnd": hwnd})
windows = []
win32gui.EnumWindows(callback, windows)
return windows
def inject_packet_logger(pid: int):
injector = Injector()
injector.load_from_pid(pid)
injector.inject_dll(get_packet_logger_path(pid))
def hide_window(window: Dict[str, int]):
style = win32gui.GetWindowLong(window["hwnd"], win32con.GWL_STYLE)
style &= ~(win32con.WS_VISIBLE)
style |= win32con.WS_EX_TOOLWINDOW
style &= ~(win32con.WS_EX_APPWINDOW)
win32gui.ShowWindow(window["hwnd"], win32con.SW_HIDE)
win32gui.SetWindowLong(window["hwnd"], win32con.GWL_STYLE, style)
win32gui.ShowWindow(window["hwnd"], win32con.SW_SHOW)
win32gui.ShowWindow(window["hwnd"], win32con.SW_HIDE)
def rename_nostale_window(window: Dict[str, int], packet_logger_port: int):
win32gui.SetWindowText(
window["hwnd"],
f"NosTale PL_PORT: {packet_logger_port}"
)
async def setup_client(window) -> Tuple[int, int]:
inject_packet_logger(window["pid"])
await asyncio.sleep(3) # wait for packet logger to start
packet_logger = [x for x in get_packet_logger_windows() if x["pid"] == window["pid"]][0]
hide_window(packet_logger)
rename_nostale_window(window, get_packet_logger_port(packet_logger))
return window["pid"], get_packet_logger_port(packet_logger)
async def setup_all_clients() -> List[Tuple[int, int]]:
windows = get_nostale_windows_wo_packet_logger()
return await asyncio.gather(*[setup_client(window) for window in windows])
class TCPClient:
IP: str = "127.0.0.1"
PACKET_SIZE: int = 4096
ENCODING: str = "windows-1252"
def __init__(self, port: int):
self._port = port
self._callbacks: List[Callable] = []
self._reader: Optional[asyncio.StreamReader] = None
self._writer: Optional[asyncio.StreamWriter] = None
self._send_queue: queue.Queue = queue.Queue()
async def _handle_packet(self, packet: List[str]):
for callback in self._callbacks:
callback(packet)
async def _receive_task(self):
while True:
data = await self._reader.read(self.PACKET_SIZE)
for packet in data.strip().decode(self.ENCODING).split("\r"):
await self._handle_packet(packet.split())
async def _send_task(self):
while True:
if not self._send_queue.empty():
self._writer.write((self._send_queue.get() + "\r").encode(self.ENCODING))
await self._writer.drain()
else:
await asyncio.sleep(0.01)
async def _serve(self):
self._reader, self._writer = await asyncio.open_connection(self.IP, self._port)
await asyncio.gather(self._receive_task(), self._send_task())
def serve(self):
thread = threading.Thread(target=lambda: asyncio.run(self._serve()))
thread.daemon = True
thread.start()
def send_raw(self, packet: str):
self._send_queue.put(packet)
def send(self, packet: str):
self.send_raw(" ".join(["1", packet]))
def recv(self, packet: str):
self.send_raw(" ".join(["0", packet]))
def add_callback(self, callback: Callable):
self._callbacks.append(callback)
def remove_callback(self, callback: Callable):
self._callbacks.remove(callback)
async def wait_for_packet(self, selectors,
timeout: float = None) -> Optional[List[str]]:
# selectors are functions that takes packet as parameter and returns bool
result = None
if not isinstance(selectors, list):
selectors = [selectors]
def callback(packet: List[str]):
nonlocal result
if all(selector(packet) for selector in selectors):
result = packet
self.remove_callback(callback)
self.add_callback(callback)
while result is None and (timeout is None or timeout > 0):
await asyncio.sleep(0.05)
timeout -= 0.05
return result
class Selector:
@classmethod
def header(cls, header: str) -> Callable:
def inner(packet: List[str]):
return len(packet) > 1 and packet[1] == header
return inner
@classmethod
def index_eq(cls, index: int, value: str) -> Callable:
def inner(packet: List[str]):
return len(packet) > index and packet[index] == value
return inner
if __name__ == '__main__':
ports = asyncio.run(setup_all_clients())
print(ports)