-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.py
207 lines (143 loc) · 4.58 KB
/
ipc.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
import os
import sys
import json
import struct
import asyncio
import tempfile
import platform # OS detection.
import uuid # For generating nonse.
import time
from enum import IntEnum
# Global variables
reader: asyncio.StreamReader = None
writer: asyncio.StreamWriter = None
is_connected = asyncio.Event()
message_queue = asyncio.Queue()
class OpCode(IntEnum):
Handshake = 0,
Frame = 1,
Close = 2,
Ping = 3,
Pong = 4,
def pack_message(opcode:int, message:str) -> bytes:
"""
Pack message into IPC frame.
"""
# [UINT opcode][UINT length][payload]
header = struct.pack('<II', opcode, len(message))
return header + message.encode("utf-8")
def get_ipc_address(pipe_id=0) -> str:
"""
Get Discord instance IPC address.
"""
ipc_prexix = 'discord-ipc-'
ipc = f"{ipc_prexix}{pipe_id}"
# TODO: Scan IPC pipe id.
if platform.system() in ['Linux', 'Darwin']:
base_dir = os.environ.get('XDG_RUNTIME_DIR') or os.environ.get('TMP') or os.environ.get('TEMP') or os.environ.get('/tmp') or tempfile.gettempdir()
elif platform.system() == 'Windows':
base_dir = '\\\\?\\pipe'
else:
raise NotImplemented
return os.path.join(base_dir, ipc)
async def connect(appid:str, loop=None):
"""
Connect to Discord
"""
global reader, writer
is_connected.clear()
if not loop:
loop = asyncio.get_event_loop()
async def _connect():
global reader, writer
if platform.system() in ['Linux', 'Darwin']:
reader, writer = await asyncio.open_unix_connection(get_ipc_address(), loop=loop)
elif platform.system() == 'Windows':
reader = asyncio.StreamReader(loop=loop)
writer, _ = await loop.create_pipe_connection(
lambda: asyncio.StreamReaderProtocol(reader, loop=loop),
get_ipc_address())
await _connect()
# Send handshake.
frame = pack_message(OpCode.Handshake,
json.dumps({'v': 1, 'client_id': str(appid)})
)
writer.write(frame)
if platform.system() != "Windows": await writer.drain()
# Read response
read_header = await reader.read(8)
opcode, length = struct.unpack('<II', read_header)
read_frame = await reader.read(length)
if opcode != 2:
print("Connected.")
is_connected.set()
async def disconnect(appid:str, loop=None):
frame = pack_message(OpCode.Close,
json.dumps({'v': 1, 'client_id': str(appid)})
)
writer.write(frame)
if platform.system() != "Windows": await writer.drain()
async def writer_loop():
while True:
# Wait until connect.
await is_connected.wait()
# Send message
frame = await message_queue.get()
writer.write(frame)
if platform.system() != "Windows": await writer.drain()
async def reader_loop():
while True:
await is_connected.wait()
# Read response
read_header = await reader.read(8)
opcode, length = struct.unpack('<II', read_header)
print(opcode, length)
read_frame = await reader.read(length)
print(read_frame)
# TODO: Update loop.
def message_enqueue(opcode:int, message:dict):
nonce = str(uuid.uuid4())
message['nonce'] = nonce
message_queue.put_nowait(
pack_message(opcode,
json.dumps(message)
)
)
return
if __name__ == "__main__":
# python ipc.py <client_id> [<activity json file>]
appid = sys.argv[1]
if len(sys.argv) > 2:
with open(sys.argv[2], 'r', encoding="utf-8") as f:
activity = json.load(f)
else:
activity = {
"type": 0,
"details": "Sample detail field.",
"state": "Sample state field.",
}
# Append timestamp.
activity["timestamps"] = {
"start": int(time.time()),
#"end": end
}
pid = os.getpid()
loop = asyncio.get_event_loop()
loop.run_until_complete(connect(appid, loop=loop))
reader_task = loop.create_task(reader_loop())
writer_task = loop.create_task(writer_loop())
message_enqueue(OpCode.Ping, {}) #PING
payload_args = {}
payload_args["pid"] = pid
payload_args["activity"] = activity
payload = {}
payload["cmd"] = "SET_ACTIVITY"
payload["args"] = payload_args
message_enqueue(OpCode.Frame, payload)
try:
loop.run_forever()
except KeyboardInterrupt as ex:
print("Closing")
loop.run_until_complete(disconnect(appid, loop=loop))
reader_task.cancel()
writer_task.cancel()