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
use base64::encode;
use regex::Regex;
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use std::env::consts::OS;
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Stdio};
pub struct LeagueClientConnector {}
impl LeagueClientConnector {
pub fn parse_lockfile() -> Result<RiotLockFile> {
let mut path = PathBuf::from(Self::get_path()?);
path.push("lockfile");
let lockfile = match path.to_str() {
Some(l) => l,
None => {
return Err(LeagueConnectorError::EmptyPath {});
}
};
let contents = fs::read_to_string(lockfile).context(UnableToRead)?;
let pieces: Vec<&str> = contents.split(":").collect();
let username = "riot".to_string();
let address = "127.0.0.1".to_string();
let process = pieces[0].to_string();
let pid = pieces[1].parse().context(NumberParse { name: "pid" })?;
let port = pieces[2].parse().context(NumberParse { name: "port" })?;
let password = pieces[3].to_string();
let protocol = pieces[4].to_string();
let b64_auth = encode(format!("{}:{}", username, password).as_bytes());
Ok(RiotLockFile {
process,
pid,
port,
password,
protocol,
username,
address,
b64_auth,
})
}
pub fn get_path() -> Result<String> {
let raw_info: String = match OS {
"windows" => Self::get_raw_league_info_in_windows()?,
"macos" => Self::get_raw_league_info_in_macos()?,
_ => unimplemented!(),
};
let pattern = Regex::new(r"--install-directory=(?P<dir>[[:alnum:][:space:]:\./\\]+)")
.context(RegexParse)?;
let caps = match pattern.captures(&raw_info) {
Some(c) => c,
None => {
return Err(LeagueConnectorError::NoInstallationPath {});
}
};
let path = caps["dir"].to_string().trim().to_string();
Ok(path)
}
fn get_raw_league_info_in_windows() -> Result<String> {
let output_child = Command::new("WMIC")
.args(&[
"PROCESS",
"WHERE",
"name='LeagueClientUx.exe'",
"GET",
"commandline",
])
.output()
.context(GetRawPath)?;
let res = String::from_utf8(output_child.stdout).context(Utf8Parse)?;
Ok(res)
}
fn get_raw_league_info_in_macos() -> Result<String> {
let mut ps_output_child = Command::new("ps")
.args(&["x", "-o", "args"])
.stdout(Stdio::piped())
.spawn()
.context(GetRawPath)?;
let ps_output = if let Some(ps_output) = ps_output_child.stdout.take() {
ps_output
} else {
return Err(LeagueConnectorError::EmptyStdout {});
};
let output_child = Command::new("grep")
.args(&["LeagueClientUx"])
.stdin(ps_output)
.stdout(Stdio::piped())
.spawn()
.context(GetRawPath)?;
let output = output_child.wait_with_output().context(GetRawPath)?;
ps_output_child.wait().context(GetRawPath)?;
let res = String::from_utf8(output.stdout).context(Utf8Parse)?;
Ok(res)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct RiotLockFile {
pub process: String,
pub pid: u32,
pub port: u32,
pub password: String,
pub protocol: String,
pub username: String,
pub address: String,
pub b64_auth: String,
}
pub type Result<T, E = LeagueConnectorError> = std::result::Result<T, E>;
#[derive(Debug, Snafu)]
pub enum LeagueConnectorError {
#[snafu(display("Could not get raw path: {}", source))]
GetRawPath { source: std::io::Error },
#[snafu(display("Process didn't return any stdout"))]
EmptyStdout {},
#[snafu(display("Unable to parse from utf8: {}", source))]
Utf8Parse { source: std::string::FromUtf8Error },
#[snafu(display("Unable to parse Regex: {}", source))]
RegexParse { source: regex::Error },
#[snafu(display("No installation path found for League"))]
NoInstallationPath {},
#[snafu(display("Path is empty"))]
EmptyPath {},
#[snafu(display("Unable to read file: {}", source))]
UnableToRead { source: std::io::Error },
#[snafu(display("Unable to parse to number {}: {}", name, source))]
NumberParse {
source: std::num::ParseIntError,
name: &'static str,
},
}