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
#[cfg(target_os = "windows")]
mod wincred;

#[cfg(target_os = "macos")]
mod binarycookies;

use std::env;

use cookie::Cookie;
use log::{info, trace};

static COOKIE_NAME: &str = ".ROBLOSECURITY";

/// Returns the cookie as a formatted header ready to add to a request
pub fn get() -> Option<String> {
    let cookie = get_value()?;

    Some(
        Cookie::build(COOKIE_NAME, cookie)
            .domain(".roblox.com")
            .finish()
            .to_string(),
    )
}

/// Returns the raw cookie value
pub fn get_value() -> Option<String> {
    from_environment()
        .or_else(from_roblox_studio)
        .or_else(from_roblox_studio_legacy)
}

fn from_environment() -> Option<String> {
    trace!("Attempting to load cookie from ROBLOSECURITY environment variable.");
    match env::var("ROBLOSECURITY") {
        Ok(v) => {
            info!("Loaded cookie from ROBLOSECURITY environment variable.");
            Some(v)
        }
        Err(_) => None,
    }
}

#[cfg(target_os = "windows")]
fn from_roblox_studio() -> Option<String> {
    trace!("Attempting to load cookie from Windows Credentials.");

    let cookie = wincred::get(&format!(
        "https://www.roblox.com:RobloxStudioAuth{}",
        COOKIE_NAME
    ))
    .ok()?;

    info!("Loaded cookie from Windows Credentials.");

    Some(cookie)
}

#[cfg(target_os = "macos")]
fn from_roblox_studio() -> Option<String> {
    use std::fs;

    trace!("Attempting to load cookie from MacOS HTTPStorages.");

    let path = dirs::home_dir()?.join("Library/HTTPStorages/com.Roblox.RobloxStudio.binarycookies");

    let binary = fs::read(path).ok()?;

    let mut cookie_store = binarycookies::Cookies::new(false);
    cookie_store.parse_content(&binary).ok()?;

    if let Some(cookie) = cookie_store
        .cookies
        .iter()
        .find(|cookie| cookie.name == COOKIE_NAME)
    {
        info!("Loaded cookie from MacOS HTTPStorages.");
        Some(cookie.value.clone())
    } else {
        None
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn from_roblox_studio() -> Option<String> {
    None
}

#[cfg(target_os = "windows")]
fn from_roblox_studio_legacy() -> Option<String> {
    use winreg::{enums::HKEY_CURRENT_USER, RegKey};

    trace!("Attempting to load cookie from Windows Registry.");

    let hkcu = RegKey::predef(HKEY_CURRENT_USER);
    let key = hkcu
        .open_subkey("SOFTWARE\\Roblox\\RobloxStudioBrowser\\roblox.com")
        .ok()?;
    let value: String = key.get_value(COOKIE_NAME).ok()?;

    if let Some(cookie) = parse_roblox_studio_cookie(&value) {
        info!("Loaded cookie from Windows Registry.");
        Some(cookie)
    } else {
        None
    }
}

#[cfg(target_os = "macos")]
fn from_roblox_studio_legacy() -> Option<String> {
    trace!("Attempting to load cookie from MacOS plist.");

    let path = dirs::home_dir()?.join("Library/Preferences/com.roblox.RobloxStudioBrowser.plist");
    let list = plist::Value::from_file(path).ok()?;

    let value = list
        .as_dictionary()
        .and_then(|dict| {
            dict.into_iter().find_map(|(key, value)| {
                if key.ends_with("ROBLOSECURITY") {
                    Some(value)
                } else {
                    None
                }
            })
        })?
        .as_string()?;

    if let Some(cookie) = parse_roblox_studio_cookie(value) {
        info!("Loaded cookie from MacOS plist.");
        Some(cookie)
    } else {
        None
    }
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn from_roblox_studio_legacy() -> Option<String> {
    None
}

#[cfg(any(target_os = "windows", target_os = "macos"))]
fn parse_roblox_studio_cookie(value: &str) -> Option<String> {
    for item in value.split(',') {
        let parts = item.split("::").collect::<Vec<_>>();
        match &parts[..] {
            ["COOK", cookie] => {
                if !cookie.starts_with('<') || !cookie.ends_with('>') {
                    return None;
                }
                return Some(cookie[1..cookie.len() - 1].to_owned());
            }
            _ => continue,
        }
    }

    None
}