ozelot 0.3.0

Handles everything network related to MCMODERN.
Documentation
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
//! For interacting with the various Mojang APIs
//!
//! Remember that requests are rate limited, avoid sending too many requests,
//! and cache what you can.
//!
//! In general, you may want to read [wiki.vg/Mojang
//! API](http://wiki.vg/Mojang_API),
//! [wiki.vg/Authentication](http://wiki.vg/Authentication) and
//! [wiki.vg/Protocol
//! Encryption](http://wiki.vg/Protocol_Encryption#Authentication) for further
//! documentation about the
//! requests and their responses.
//!
//! Also contains some helper functions used for authentication.

pub use json::*;
use errors::Result;
use utils;

use curl::easy::{Easy, List};

use serde_json;

/// Make a request to check the status of the Mojang APIs
#[derive(Debug)]
pub struct APIStatus();
impl APIStatus {
    pub fn perform(&self) -> Result<APIStatusResponse> {
        let res = get_request(&Self::get_endpoint())?;
        /* Flatten the list, and turn it into an object.
         * For some reason this response is given in a really weird way, and
         * this fixes it so that it can be parsed more easily */
        let res = res.replace(|c| match c {
                                  '{' | '}' => true,
                                  _ => false,
                              },
                              "")
            .replace('[', "{")
            .replace(']', "}");
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new() -> Self {
        APIStatus {}
    }
    fn get_endpoint() -> String {
        "https://status.mojang.com/check".to_string()
    }
}

/// Make a Username -> UUID (at time) request
///
/// Returns information about which account had the given name at the point in
/// time, where the time is specified as epoch time. If at is not specified, it
/// will default to the current time.
///
/// If unable to find the player at the given point in time, will return an
/// error.
#[derive(Debug)]
pub struct NameToUUID {
    username: String,
    at: Option<i64>,
}
impl NameToUUID {
    pub fn perform(&self) -> Result<NameUUID> {
        let url = match self.at {
            Some(x) => {
                format!("https://api.mojang.com/users/profiles/minecraft/{}?at={}",
                        self.username,
                        x)
            },
            None => {
                format!("https://api.mojang.com/users/profiles/minecraft/{}",
                        self.username)
            },
        };
        let res = get_request(&url)?;
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(username: String, at: Option<i64>) -> Self {
        NameToUUID {
            username: username,
            at: at,
        }
    }
}

/// A UUID -> Username history request
///
/// The UUID must be given as a string without hyphens.
#[derive(Debug)]
pub struct UUIDToHistory {
    uuid: String,
}
impl UUIDToHistory {
    pub fn perform(&self) -> Result<Vec<NameHistory>> {
        let url = format!("https://api.mojang.com/user/profiles/{}/names",
                          self.uuid);
        let res = get_request(&url)?;
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(uuid: String) -> Self {
        UUIDToHistory {
            uuid: uuid,
        }
    }
}

/// A Playernames -> UUIDs request.
///
/// Can request up to 100 UUIDs at a time.
#[derive(Debug)]
pub struct PlayernamesToUUIDs {
    usernames: Vec<String>,
}
impl PlayernamesToUUIDs {
    fn get_endpoint() -> String {
        "https://api.mojang.com/profiles/minecraft".to_string()
    }
    pub fn perform(&self) -> Result<Vec<NameUUID>> {
        let body = serde_json::to_string(&self.usernames)?;
        println!("body: {}", body);
        let res = post_request(&Self::get_endpoint(), &body)?;
        Ok(serde_json::from_str(&res)?)
    }
    /// Create a new instance of this request.
    ///
    /// # Panics
    ///
    /// Panics if usernames.len() > 100. The API limits this request to 100
    /// usernames.
    pub fn new(usernames: Vec<String>) -> Self {
        if usernames.len() > 100 {
            panic!("PlayernamesToUUIDs got more than 100 usernames");
        }
        PlayernamesToUUIDs {
            usernames: usernames,
        }
    }
}

/// Represents a UUID -> Profile + Skin and Cape request
#[derive(Debug)]
pub struct UUIDToProfile {
    uuid: String,
    /// Whether you want the response signed by the yggdrasil private key
    signed: bool,
}
impl UUIDToProfile {
    pub fn perform(&self) -> Result<Profile> {
        let url = if self.signed {
            format!("https://sessionserver.mojang.com/session/minecraft/profile/{}?unsigned=false",
                    self.uuid)
        } else {
            format!("https://sessionserver.mojang.com/session/minecraft/profile/{}",
                    self.uuid)
        };
        let res = get_request(&url)?;
        println!("res: {}", res);
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(uuid: String, signed: bool) -> Self {
        UUIDToProfile {
            uuid: uuid,
            signed: signed,
        }
    }
}

/// Get the blocked server's hashes
#[derive(Debug)]
pub struct BlockedServers();
impl BlockedServers {
    fn get_endpoint() -> String {
        "https://sessionserver.mojang.com/blockedservers".to_string()
    }
    pub fn perform(&self) -> Result<Vec<String>> {
        let res: String = get_request(&Self::get_endpoint())?;
        Ok(res.split('\n')
               .filter_map(|e| if !e.is_empty() {
                               Some(e.to_string())
                           } else {
                               None
                           })
               .collect())
    }
    pub fn new() -> Self {
        BlockedServers {}
    }
}

/// Get the orders statistics
///
/// The API will respond with the sum of sales for the selected types. E.g. by
/// setting item_sold_minecraft and prepaid_card_redeemed_minecraft to true,
/// you'll get the sum of sales for those two types.
#[derive(Debug)]
pub struct Statistics {
    item_sold_minecraft: bool,
    prepaid_card_redeemed_minecraft: bool,
    item_sold_cobalt: bool,
    item_sold_scrolls: bool,
}
impl Statistics {
    fn get_endpoint() -> String {
        "https://api.mojang.com/orders/statistics".to_string()
    }
    pub fn perform(&self) -> Result<StatisticsResponse> {
        let mut query: Vec<&str> = Vec::new();
        if self.item_sold_minecraft {
            query.push("item_sold_minecraft");
        }
        if self.prepaid_card_redeemed_minecraft {
            query.push("prepaid_card_redeemed_minecraft");
        }
        if self.item_sold_cobalt {
            query.push("item_sold_cobalt");
        }
        if self.item_sold_scrolls {
            query.push("item_sold_scrolls");
        }
        let payload = json!({
                                "metricKeys": query
                            });
        let res = post_request(&Self::get_endpoint(), &payload.to_string())?;
        Ok(serde_json::from_str(&res)?)
    }
    /// Create a new request for requesting the sum of sales of the specified
    /// types.
    ///
    /// # Panics
    ///
    /// Panics if not at least one of the values is true.
    pub fn new(item_sold_minecraft: bool,
               prepaid_card_redeemed_minecraft: bool,
               item_sold_cobalt: bool,
               item_sold_scrolls: bool)
               -> Self {
        if !(item_sold_minecraft | prepaid_card_redeemed_minecraft |
             item_sold_cobalt | item_sold_scrolls) {
            panic!("You must specify at least one type of sale in the Statistics request");
        }
        Statistics {
            item_sold_minecraft: item_sold_minecraft,
            prepaid_card_redeemed_minecraft: prepaid_card_redeemed_minecraft,
            item_sold_cobalt: item_sold_cobalt,
            item_sold_scrolls: item_sold_scrolls,
        }
    }
    /// Get the sum of everything
    pub fn all() -> Self {
        Statistics {
            item_sold_minecraft: true,
            prepaid_card_redeemed_minecraft: true,
            item_sold_cobalt: true,
            item_sold_scrolls: true,
        }
    }
    /// Get just the amount of Minecraft sales
    pub fn minecraft() -> Self {
        Statistics {
            item_sold_minecraft: true,
            prepaid_card_redeemed_minecraft: true,
            item_sold_cobalt: false,
            item_sold_scrolls: false,
        }
    }
}

/* Here begins the authentication requests */

/// Authenticate with Mojang
#[derive(Debug)]
pub struct Authenticate {
    username: String,
    password: String,
    clientToken: Option<String>,
    requestUser: bool,
}
impl Authenticate {
    fn get_endpoint() -> String {
        "https://authserver.mojang.com/authenticate".to_string()
    }
    pub fn perform(&self) -> Result<AuthenticationResponse> {
        let payload = json!({
            "agent": {
                "name": "Minecraft",
                "version": 1
            },
            "username": self.username,
            "password": self.password,
            "clientToken": self.clientToken,
            "requestUser": self.requestUser
        });
        let res = post_request(&Self::get_endpoint(), &payload.to_string())?;
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(username: String, password: String) -> Self {
        Authenticate {
            username: username,
            password: password,
            clientToken: None,
            requestUser: false,
        }
    }
}

/// Refresh a valid accessToken
#[derive(Debug, Serialize)]
pub struct AuthenticateRefresh {
    accessToken: String,
    clientToken: String,
    requestUser: bool,
}
impl AuthenticateRefresh {
    fn get_endpoint() -> String {
        "https://authserver.mojang.com/refresh".to_string()
    }
    pub fn perform(&self) -> Result<AuthenticationResponse> {
        let payload = serde_json::to_string(self)?;
        let res = post_request(&Self::get_endpoint(), &payload)?;
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(accessToken: String,
               clientToken: String,
               requestUser: bool)
               -> Self {
        AuthenticateRefresh {
            accessToken: accessToken,
            clientToken: clientToken,
            requestUser: requestUser,
        }
    }
}

/// Validate an existing access token
#[derive(Debug, Serialize)]
pub struct AuthenticateValidate {
    accessToken: String,
    clientToken: Option<String>,
}
impl AuthenticateValidate {
    fn get_endpoint() -> String {
        "https://authserver.mojang.com/validate".to_string()
    }
    pub fn perform(&self) -> Result<()> {
        let payload = serde_json::to_string(self)?;
        let _ = post_request(&Self::get_endpoint(), &payload)?;
        Ok(())
    }
    pub fn new(accessToken: String, clientToken: Option<String>) -> Self {
        AuthenticateValidate {
            accessToken: accessToken,
            clientToken: clientToken,
        }
    }
}

/// Invalidate an accessToken, using the client username/password
#[derive(Debug, Serialize)]
pub struct AuthenticateSignout {
    username: String,
    password: String,
}
impl AuthenticateSignout {
    fn get_endpoint() -> String {
        "https://authserver.mojang.com/signout".to_string()
    }
    pub fn perform(&self) -> Result<()> {
        let payload = serde_json::to_string(self)?;
        let _ = post_request(&Self::get_endpoint(), &payload)?;
        Ok(())
    }
    pub fn new(username: String, password: String) -> Self {
        AuthenticateSignout {
            username: username,
            password: password,
        }
    }
}

/// Invalidate an accessToken, using the accessToken and a clientToken
#[derive(Debug, Serialize)]
pub struct AuthenticateInvalidate {
    accessToken: String,
    clientToken: String,
}
impl AuthenticateInvalidate {
    fn get_endpoint() -> String {
        "https://authserver.mojang.com/invalidate".to_string()
    }
    pub fn perform(&self) -> Result<()> {
        let payload = serde_json::to_string(self)?;
        let _ = post_request(&Self::get_endpoint(), &payload)?;
        Ok(())
    }
    pub fn new(accessToken: String, clientToken: String) -> Self {
        AuthenticateInvalidate {
            accessToken: accessToken,
            clientToken: clientToken,
        }
    }
}

/// Send a session join message to Mojang, used by clients when connecting to
/// online servers
#[derive(Debug, Serialize)]
pub struct SessionJoin {
    accessToken: String,
    /// The player's uuid
    selectedProfile: String,
    serverId: String,
}
impl SessionJoin {
    fn get_endpoint() -> String {
        "https://sessionserver.mojang.com/session/minecraft/join".to_string()
    }
    pub fn perform(&self) -> Result<()> {
        let payload = serde_json::to_string(self)?;
        let _ = post_request(&Self::get_endpoint(), &payload)?;
        Ok(())
    }
    pub fn new(access_token: String,
               uuid: String,
               server_id: &str,
               shared_secret: &[u8],
               server_public_key: &[u8])
               -> Self {
        let hash =
            utils::post_sha1(server_id, shared_secret, server_public_key);
        SessionJoin {
            accessToken: access_token,
            selectedProfile: uuid,
            serverId: hash,
        }
    }
}

/// Check whether a client has posted a SessionJoin to Mojang, used by servers
/// for authenticating connecting clients.
#[derive(Debug)]
pub struct SessionHasJoined {
    username: String,
    serverId: String,
}
impl SessionHasJoined {
    pub fn perform(&self) -> Result<SessionHasJoinedResponse> {
        let url = format!("https://sessionserver.mojang.com/session/minecraft/hasJoined?username={}&serverId={}", self.username, self.serverId);
        let res = get_request(&url)?;
        println!("session has joined response: {}", &res);
        Ok(serde_json::from_str(&res)?)
    }
    pub fn new(username: String,
               server_id: &str,
               shared_secret: &[u8],
               public_key: &[u8])
               -> Self {
        let hash = utils::post_sha1(server_id, shared_secret, public_key);
        SessionHasJoined {
            username: username,
            serverId: hash,
        }
    }
}

/// Helper function for performing a GET request to the given URL, returning
/// the response content
fn get_request(url: &str) -> Result<String> {
    let mut handle = Easy::new();
    handle.url(url)?;
    handle.fail_on_error(true)?;
    let mut response = Vec::new();
    {
        let mut transfer = handle.transfer();
        transfer
            .write_function(|data| {
                                response.extend_from_slice(data);
                                Ok(data.len())
                            })?;
        transfer.perform()?;
    }
    Ok(String::from_utf8(response)?)
}

/// Helper function for performing a POST request to the given URL,
/// posting the given data to it, and returning the response content.
fn post_request(url: &str, post: &str) -> Result<String> {
    let mut handle = Easy::new();
    handle.url(url)?;
    handle.fail_on_error(true)?;
    let mut headers = List::new();
    headers.append("Content-Type: application/json")?;
    handle.http_headers(headers)?;
    handle.post_fields_copy(post.as_bytes())?;
    handle.post(true)?;
    let mut response = Vec::new();
    {
        let mut transfer = handle.transfer();
        transfer
            .write_function(|data| {
                                response.extend_from_slice(data);
                                Ok(data.len())
                            })?;
        transfer.perform()?;
    }
    Ok(String::from_utf8(response)?)
}