Skip to content

Commit 651080e

Browse files
authored
Merge pull request #4 from YaoerWu/master
add agent
2 parents 22729d7 + aac929b commit 651080e

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/main.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ struct AuthRequest<'a> {
2626
password: &'a str,
2727
request_user: bool,
2828
client_token: &'a str,
29+
agent: Agent<'a>,
30+
}
31+
impl Default for AuthRequest<'_> {
32+
fn default() -> Self {
33+
AuthRequest {
34+
username: "",
35+
password: "",
36+
request_user: true,
37+
client_token: "",
38+
agent: Agent::default(),
39+
}
40+
}
41+
}
42+
43+
#[derive(Serialize)]
44+
#[serde(rename_all = "camelCase")]
45+
struct Agent<'a> {
46+
name: &'a str,
47+
version: i32,
48+
}
49+
impl Default for Agent<'_> {
50+
fn default() -> Self {
51+
Agent {
52+
name: "Minecraft",
53+
version: 1,
54+
}
55+
}
2956
}
3057

3158
#[derive(Deserialize)]
@@ -35,19 +62,20 @@ struct AuthResponse {
3562
selected_profile: Profile,
3663
}
3764

38-
#[derive(Deserialize)]
65+
#[derive(Deserialize, Debug)]
3966
struct Profile {
4067
id: String,
4168
name: String,
4269
}
4370

71+
#[derive(Debug)]
4472
struct LoginResult {
4573
prefetched_data: String,
4674
access_token: String,
4775
selected_profile: Profile,
4876
}
4977

50-
fn validate_args(args: &Vec<String>) -> Result<()> {
78+
fn validate_args(args: &[String]) -> Result<()> {
5179
match args.len() {
5280
len if len < 4 => Err(MmcaiError::InvalidArgument(args[0].to_owned())),
5381
4 => Err(MmcaiError::CannotRunDirectly),
@@ -105,16 +133,16 @@ fn yggdrasil_login(
105133
let body = AuthRequest {
106134
username,
107135
password,
108-
request_user: true,
109136
client_token,
137+
..AuthRequest::default()
110138
};
111139

112-
Ok(client
140+
client
113141
.post(format!("{}/authserver/authenticate", api_url))
114142
.headers(headers)
115143
.json(&body)
116144
.send()?
117-
.json::<AuthResponse>()?)
145+
.json::<AuthResponse>()
118146
};
119147

120148
let prefetched_data = get_prefetched_data().map_err(MmcaiError::YggdrasilHelloFailed)?;
@@ -128,7 +156,7 @@ fn yggdrasil_login(
128156
}
129157

130158
fn modify_minecraft_params(
131-
minecraft_params: &mut Vec<String>,
159+
minecraft_params: &mut [String],
132160
access_token: &str,
133161
uuid: &str,
134162
playername: &str,
@@ -250,9 +278,7 @@ fn main() -> Result<()> {
250278
.spawn()
251279
.map_err(MmcaiError::SpawnProcessFailed)?;
252280

253-
let stdin = (&mut child.stdin)
254-
.as_mut()
255-
.ok_or(MmcaiError::StdinUnavailable)?;
281+
let stdin = child.stdin.as_mut().ok_or(MmcaiError::StdinUnavailable)?;
256282

257283
minecraft_params.iter().for_each(|line| {
258284
let _ = writeln!(stdin, "{}", line).map_err(MmcaiError::WriteMinecraftParamsFailed);
@@ -281,7 +307,7 @@ mod tests {
281307
1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282308
0, 0, 0, 0, 0,
283309
];
284-
let ref mut r = StdRng::from_seed(seed);
310+
let r = &mut StdRng::from_seed(seed);
285311
(0..length)
286312
.map(|_| Faker.fake_with_rng::<String, _>(r))
287313
.collect()

0 commit comments

Comments
 (0)