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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use byteorder::{LittleEndian, ReadBytesExt};
use bytes::{BufMut, BytesMut};
use serde_json::Value;
use std::io::Read;
#[cfg(unix)]
use std::os::unix::net::UnixStream;
#[cfg(windows)]
use std::fs::OpenOptions;
#[cfg(windows)]
use std::io::{BufReader, BufWriter};
use crate::error::{DiscordIpcError, ProtocolContext, Result};
use crate::ipc::protocol::{constants, Opcode};
/// Configuration for selecting which Discord IPC pipe to connect to
#[derive(Debug, Clone, Default)]
pub enum PipeConfig {
/// Automatically discover and connect to the first available pipe (default behavior)
#[default]
Auto,
/// Connect to a custom pipe path
///
/// # Examples
///
/// Unix: `/run/user/1000/discord-ipc-0` or `/run/user/1000/app/com.discordapp.Discord/discord-ipc-0`
///
/// Windows: `\\.\pipe\discord-ipc-0`
CustomPath(String),
}
/// Information about a discovered Discord IPC pipe
#[derive(Debug, Clone)]
pub struct DiscoveredPipe {
/// The pipe number (0-9)
pub pipe_number: u8,
/// The full path to the pipe
pub path: String,
}
#[cfg(unix)]
pub struct IpcConnection {
stream: UnixStream,
read_buf: BytesMut,
write_buf: BytesMut,
}
#[cfg(windows)]
pub struct IpcConnection {
reader: BufReader<std::fs::File>,
writer: BufWriter<std::fs::File>,
read_buf: BytesMut,
write_buf: BytesMut,
}
impl IpcConnection {
/// Initial capacity for read and write buffers (4KB)
const INITIAL_BUFFER_CAPACITY: usize = 4096;
/// Discover all available Discord IPC pipes
///
/// Returns a list of all Discord IPC pipes that are currently accessible
///
/// # Example
///
/// ```no_run
/// use presenceforge::ipc::IpcConnection;
///
/// let pipes = IpcConnection::discover_pipes();
/// for pipe in pipes {
/// println!("Found pipe {}: {}", pipe.pipe_number, pipe.path);
/// }
/// ```
pub fn discover_pipes() -> Vec<DiscoveredPipe> {
#[cfg(unix)]
{
Self::discover_pipes_unix()
}
#[cfg(windows)]
{
Self::discover_pipes_windows()
}
}
// Returns the current users UID on unix based systems
#[cfg(unix)]
/// Returns the current user's UID on Unix-based systems.
///
/// Safety: Calling `libc::getuid()` is always safe per POSIX.
fn current_uid() -> u32 {
unsafe { libc::getuid() }
}
/// Discovers potential base directories where IPC sockets may exist
/// Check environment variables
/// - `XDG_RUNTIME_DIR`
/// - `TMPDIR`
/// - `TMP`
/// - `TEMP`
/// - `tmp`
/// - `XDG_RUNTIME_DIR/app/com.discordapp.Discord` (Flatpak specific)
/// - `/run/user/{UID}` (if `XDG_RUNTIME_DIR` is not set)
/// - `/run/user/{UID}/app/com.discordapp.Discord` (Flatpak fallback)
#[cfg(unix)]
fn candidate_ipc_dir() -> Vec<String> {
let env_keys = ["XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP", "tmp"];
let mut directories = Vec::new();
for key in &env_keys {
if let Ok(dir) = std::env::var(key) {
directories.push(dir.clone());
// Also check Flatpak Discord path if XDG_RUNTIME_DIR is set
if key == &"XDG_RUNTIME_DIR" {
directories.push(format!("{}/app/com.discordapp.Discord", dir));
}
}
}
if directories.is_empty() {
let uid = Self::current_uid();
directories.push(format!("/run/user/{}", uid));
// Also try Flatpak path as fallback
directories.push(format!("/run/user/{}/app/com.discordapp.Discord", uid));
}
directories
}
#[cfg(unix)]
fn discover_pipes_unix() -> Vec<DiscoveredPipe> {
let mut pipes = Vec::new();
// Try each directory with each socket number
for dir in Self::candidate_ipc_dir() {
for i in 0..constants::MAX_IPC_SOCKETS {
let socket_path = format!("{}/{}{}", dir, constants::IPC_SOCKET_PREFIX, i);
// Check if we can connect to this socket
if let Ok(stream) = UnixStream::connect(&socket_path) {
drop(stream); // Close the test connection
pipes.push(DiscoveredPipe {
pipe_number: i,
path: socket_path,
});
}
}
}
pipes
}
#[cfg(windows)]
fn discover_pipes_windows() -> Vec<DiscoveredPipe> {
let mut pipes = Vec::new();
for i in 0..constants::MAX_IPC_SOCKETS {
let pipe_path = format!(r"\\?\pipe\discord-ipc-{}", i);
// Try to open the named pipe to check if it exists
if let Ok(file) = OpenOptions::new().read(true).write(true).open(&pipe_path) {
drop(file); // Close the test connection
pipes.push(DiscoveredPipe {
pipe_number: i,
path: pipe_path,
});
}
}
pipes
}
/// Create a new IPC connection with optional pipe configuration
///
/// # Arguments
///
/// * `config` - Optional pipe configuration. If `None`, auto-discovery is used.
pub fn new_with_config(config: Option<PipeConfig>) -> Result<Self> {
let config = config.unwrap_or_default();
#[cfg(unix)]
{
let stream = Self::connect_to_discord_unix_with_config(&config)?;
Ok(Self {
stream,
read_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
write_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
})
}
#[cfg(windows)]
{
let (reader, writer) = Self::connect_to_discord_windows_with_config(&config)?;
Ok(Self {
reader,
writer,
read_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
write_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
})
}
}
/// Create a new IPC connection (uses auto-discovery)
pub fn new() -> Result<Self> {
Self::new_with_config(None)
}
/// Create a new IPC connection with a timeout
pub fn new_with_timeout(timeout_ms: u64) -> Result<Self> {
Self::new_with_config_and_timeout(None, timeout_ms)
}
/// Create a new IPC connection with optional pipe configuration and timeout
///
/// # Arguments
///
/// * `config` - Optional pipe configuration. If `None`, auto-discovery is used.
/// * `timeout_ms` - Connection timeout in milliseconds
pub fn new_with_config_and_timeout(
config: Option<PipeConfig>,
timeout_ms: u64,
) -> Result<Self> {
use std::time::{Duration, Instant};
let start = Instant::now();
let timeout = Duration::from_millis(timeout_ms);
let config = config.unwrap_or_default();
let mut last_error_message = None;
// Keep trying to connect until we succeed or timeout
while start.elapsed() < timeout {
match Self::try_connect_with_config(&config) {
Ok(connection) => return Ok(connection),
Err(DiscordIpcError::NoValidSocket) => {
last_error_message = Some("No valid Discord socket found".to_string());
// Wait a bit before trying again
std::thread::sleep(Duration::from_millis(constants::DEFAULT_RETRY_INTERVAL_MS));
continue;
}
Err(DiscordIpcError::SocketDiscoveryFailed { ref source, .. }) => {
last_error_message = Some(format!("Socket discovery failed: {}", source));
// Wait a bit before trying again
std::thread::sleep(Duration::from_millis(constants::DEFAULT_RETRY_INTERVAL_MS));
continue;
}
Err(e) => {
// Non-recoverable error
return Err(e);
}
}
}
Err(DiscordIpcError::connection_timeout(
timeout_ms,
last_error_message,
))
}
/// Try to connect to Discord with configuration
fn try_connect_with_config(config: &PipeConfig) -> Result<Self> {
#[cfg(unix)]
{
let stream = Self::connect_to_discord_unix_with_config(config)?;
Ok(Self {
stream,
read_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
write_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
})
}
#[cfg(windows)]
{
let (reader, writer) = Self::connect_to_discord_windows_with_config(config)?;
Ok(Self {
reader,
writer,
read_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
write_buf: BytesMut::with_capacity(Self::INITIAL_BUFFER_CAPACITY),
})
}
}
#[cfg(unix)]
/// Connect to Discord IPC socket on Unix systems with configuration
fn connect_to_discord_unix_with_config(config: &PipeConfig) -> Result<UnixStream> {
match config {
PipeConfig::Auto => {
// Auto-discovery: try all possible pipes
Self::connect_to_discord_unix_auto()
}
PipeConfig::CustomPath(path) => {
// Connect to custom path
UnixStream::connect(path)
.and_then(|stream| {
stream.set_nonblocking(false)?;
Ok(stream)
})
.map_err(DiscordIpcError::ConnectionFailed)
}
}
}
#[cfg(unix)]
/// Connect to Discord IPC socket using auto-discovery
fn connect_to_discord_unix_auto() -> Result<UnixStream> {
// Try each directory with each socket number
let mut last_error = None;
let mut attempted_paths = Vec::new();
for dir in Self::candidate_ipc_dir() {
for i in 0..constants::MAX_IPC_SOCKETS {
let socket_path = format!("{}/{}{}", dir, constants::IPC_SOCKET_PREFIX, i);
attempted_paths.push(socket_path.clone());
match UnixStream::connect(&socket_path) {
Ok(stream) => {
// Configure socket
if let Err(err) = stream.set_nonblocking(false) {
last_error = Some(err);
continue;
}
return Ok(stream);
}
Err(err) => {
last_error = Some(err);
continue;
}
}
}
}
// If we got here, no valid socket was found
if let Some(err) = last_error {
// Return the last error we encountered for diagnostic purposes with all attempted paths
Err(DiscordIpcError::socket_discovery_failed(
err,
attempted_paths,
))
} else {
Err(DiscordIpcError::NoValidSocket)
}
}
#[cfg(windows)]
/// Connect to Discord IPC named pipe on Windows with configuration
fn connect_to_discord_windows_with_config(
config: &PipeConfig,
) -> Result<(BufReader<std::fs::File>, BufWriter<std::fs::File>)> {
match config {
PipeConfig::Auto => {
// Auto-discovery: try all possible pipes
Self::connect_to_discord_windows_auto()
}
PipeConfig::CustomPath(path) => {
// Connect to custom path
OpenOptions::new()
.read(true)
.write(true)
.open(path)
.and_then(|file| {
let reader_file = file.try_clone()?;
Ok((BufReader::new(reader_file), BufWriter::new(file)))
})
.map_err(DiscordIpcError::ConnectionFailed)
}
}
}
#[cfg(windows)]
/// Connect to Discord IPC named pipe on Windows using auto-discovery
fn connect_to_discord_windows_auto(
) -> Result<(BufReader<std::fs::File>, BufWriter<std::fs::File>)> {
let mut last_error = None;
let mut attempted_paths = Vec::new();
for i in 0..constants::MAX_IPC_SOCKETS {
let pipe_path = format!(r"\\?\pipe\discord-ipc-{}", i);
attempted_paths.push(pipe_path.clone());
// Try to open the named pipe
match OpenOptions::new().read(true).write(true).open(&pipe_path) {
Ok(file) => {
// Clone the file handle for reader and writer
match file.try_clone() {
Ok(reader_file) => {
let writer_file = file;
return Ok((BufReader::new(reader_file), BufWriter::new(writer_file)));
}
Err(err) => {
last_error = Some(err);
continue;
}
}
}
Err(err) => {
last_error = Some(err);
continue; // Try next pipe number
}
}
}
// If we got here, no valid pipe was found
if let Some(err) = last_error {
// Return the last error we encountered with all attempted paths
Err(DiscordIpcError::socket_discovery_failed(
err,
attempted_paths,
))
} else {
Err(DiscordIpcError::NoValidSocket)
}
}
/// Send data with opcode
pub fn send(&mut self, opcode: Opcode, payload: &Value) -> Result<()> {
let raw = serde_json::to_vec(payload)?;
// Clear and prepare write buffer
self.write_buf.clear();
self.write_buf.reserve(8 + raw.len());
// Write header and payload to buffer
self.write_buf.put_u32_le(opcode.into());
self.write_buf.put_u32_le(raw.len() as u32);
self.write_buf.extend_from_slice(&raw);
#[cfg(unix)]
{
use std::io::Write;
self.stream.write_all(&self.write_buf)?;
}
#[cfg(windows)]
{
use std::io::Write;
self.writer.write_all(&self.write_buf)?;
self.writer.flush()?;
}
Ok(())
}
/// Receive data and return opcode and payload
pub fn recv(&mut self) -> Result<(Opcode, Value)> {
// Read header into buffer
self.read_buf.clear();
self.read_buf.reserve(8);
let mut header = [0u8; 8];
#[cfg(unix)]
{
self.stream
.read_exact(&mut header)
.map_err(|_| DiscordIpcError::SocketClosed)?;
}
#[cfg(windows)]
{
self.reader
.read_exact(&mut header)
.map_err(|_| DiscordIpcError::SocketClosed)?;
}
let mut header_reader = &header[..];
let opcode_raw = header_reader.read_u32::<LittleEndian>()?;
let length = header_reader.read_u32::<LittleEndian>()?;
// Validate payload size to prevent excessive memory allocation
if length > constants::MAX_PAYLOAD_SIZE {
let context = ProtocolContext::with_payload(opcode_raw, length as usize);
return Err(DiscordIpcError::protocol_violation(
format!(
"Payload size {} exceeds maximum allowed size of {} bytes",
length,
constants::MAX_PAYLOAD_SIZE
),
context,
));
}
let opcode = Opcode::try_from(opcode_raw)?;
// Reuse read buffer for payload
self.read_buf.clear();
self.read_buf.resize(length as usize, 0);
#[cfg(unix)]
{
self.stream
.read_exact(&mut self.read_buf[..])
.map_err(|_| DiscordIpcError::SocketClosed)?;
}
#[cfg(windows)]
{
self.reader
.read_exact(&mut self.read_buf[..])
.map_err(|_| DiscordIpcError::SocketClosed)?;
}
let value: Value = serde_json::from_slice(&self.read_buf)?;
Ok((opcode, value))
}
/// Close the connection
pub fn close(&mut self) {
#[cfg(unix)]
{
let _ = self.stream.shutdown(std::net::Shutdown::Both);
}
#[cfg(windows)]
{
// Windows named pipes don't need explicit shutdown
// Files will be closed when dropped
}
}
}