Implement migration

This commit is contained in:
Ethan O'Brien 2024-03-30 11:11:43 -05:00
parent 751e37e02e
commit 5bf5b8333d
3 changed files with 293 additions and 10 deletions

View file

@ -90,6 +90,21 @@ async fn login_bonus(req: HttpRequest, body: String) -> HttpResponse { router::l
#[get("/api/notice/reward")]
async fn reward(req: HttpRequest) -> HttpResponse { router::notice::reward(req) }
#[post("/api/user/getmigrationcode")]
async fn getmigrationcode(req: HttpRequest, body: String) -> HttpResponse { router::user::get_migration_code(req, body) }
#[post("/api/user/registerpassword")]
async fn registerpassword(req: HttpRequest, body: String) -> HttpResponse { router::user::register_password(req, body) }
#[post("/api/user/migration")]
async fn migration(req: HttpRequest, body: String) -> HttpResponse { router::user::migration(req, body) }
#[post("/api/user/gglrequestmigrationcode")]
async fn gglrequestmigrationcode(req: HttpRequest, body: String) -> HttpResponse { router::user::request_migration_code(req, body) }
#[post("/api/user/gglverifymigrationcode")]
async fn gglverifymigrationcode(req: HttpRequest, body: String) -> HttpResponse { router::user::verify_migration_code(req, body) }
async fn log_unknown_request(req: HttpRequest, body: String) -> HttpResponse {
if body != String::new() {
println!("{}", encryption::decrypt_packet(&body).unwrap());
@ -134,6 +149,11 @@ async fn main() -> std::io::Result<()> {
.service(user_post)
.service(user_deck)
.service(dummy_login)
.service(getmigrationcode)
.service(registerpassword)
.service(migration)
.service(gglrequestmigrationcode)
.service(gglverifymigrationcode)
.default_service(web::route().to(log_unknown_request)))
.bind(("0.0.0.0", 8080))?
.run();

View file

@ -78,6 +78,139 @@ pub fn user_post(req: HttpRequest, body: String) -> HttpResponse {
global::send(resp)
}
fn uid_to_code(uid: String) -> String {
//just replace uid with numbers because im too lazy to have a real database and this is close enough anyways
return uid
.replace("1", "A")
.replace("2", "G")
.replace("3", "W")
.replace("4", "Q")
.replace("5", "Y")
.replace("6", "6")
.replace("7", "I")
.replace("8", "P")
.replace("9", "U")
.replace("0", "M")
+ "7";
}
fn code_to_uid(code: String) -> String {
//just replace uid with numbers because im too lazy to have a real database and this is close enough anyways
return code
.replace("7", "")
.replace("A", "1")
.replace("G", "2")
.replace("W", "3")
.replace("Q", "4")
.replace("Y", "5")
.replace("6", "6")
.replace("I", "7")
.replace("P", "8")
.replace("U", "9")
.replace("M", "0");
}
pub fn get_migration_code(_req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
let code = uid_to_code(body["user_id"].to_string());
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"migrationCode": code
}
};
global::send(resp)
}
pub fn register_password(req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
let key = global::get_login(req.headers());
let user = userdata::get_acc(&key);
let code = uid_to_code(user["user"]["id"].to_string());
userdata::save_acc_transfer(&code, &body["pass"].to_string());
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": []
};
global::send(resp)
}
pub fn verify_migration_code(_req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
let uid = code_to_uid(body["migrationCode"].to_string()).parse::<i64>().unwrap_or(0);
let user = userdata::get_acc_transfer(uid, &body["migrationCode"].to_string(), &body["pass"].to_string());
if user["success"].as_bool().unwrap() == false || uid == 0 {
let resp = object!{
"code": 2,
"server_time": global::timestamp(),
"message": ""
};
return global::send(resp);
}
let data_user = userdata::get_acc(&user["login_token"].to_string());
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"user_id": uid,
"uuid": format!("ecd0d830-{}-25ec5f34f7f8", user["login_token"].to_string()),
"charge": data_user["gem"]["charge"].clone(),
"free": data_user["gem"]["free"].clone()
}
};
global::send(resp)
}
pub fn request_migration_code(_req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
let uid = code_to_uid(body["migrationCode"].to_string()).parse::<i64>().unwrap_or(0);
let user = userdata::get_acc_transfer(uid, &body["migrationCode"].to_string(), &body["pass"].to_string());
if user["success"].as_bool().unwrap() == false || uid == 0 {
let resp = object!{
"code": 2,
"server_time": global::timestamp(),
"message": ""
};
return global::send(resp);
}
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"twxuid": format!("ecd0d830-{}-25ec5f34f7f8", user["login_token"].to_string())
}
};
global::send(resp)
}
pub fn migration(_req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
//UYWMGAMIMQQUPG67
let user = userdata::get_name_and_rank(body["user_id"].to_string().parse::<i64>().unwrap());
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": user
};
global::send(resp)
}
pub fn initialize(req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();

View file

@ -22,12 +22,11 @@ fn create_token_store(conn: &Connection) {
"CREATE TABLE tokens (
jsondata TEXT NOT NULL
)",
(), // empty list of parameters.
(),
).unwrap();
init_data(conn, "tokens", array![{}]);
init_data(conn, "tokens", object!{});
}
}
//store_data(conn, "tokens", array![{}]);
}
fn create_uid_store(conn: &Connection) {
match conn.prepare("SELECT jsondata FROM uids") {
@ -37,12 +36,26 @@ fn create_uid_store(conn: &Connection) {
"CREATE TABLE uids (
jsondata TEXT NOT NULL
)",
(), // empty list of parameters.
(),
).unwrap();
init_data(conn, "uids", array![]);
}
}
//store_data(conn, "uids", array![]);
}
fn create_migration_store(conn: &Connection) {
match conn.prepare("SELECT jsondata FROM migrationdata") {
Ok(_) => {}
Err(_) => {
conn.execute(
"CREATE TABLE migrationdata (
jsondata TEXT NOT NULL
)",
(),
).unwrap();
init_data(conn, "migrationdata", object!{});
}
}
}
fn acc_exists(conn: &Connection, key: i64) -> bool {
conn.prepare(&format!("SELECT jsondata FROM _{}_", key)).is_ok()
@ -106,17 +119,28 @@ fn create_acc(conn: &Connection, uid: i64, login: &str) {
create_token_store(conn);
let mut tokens = get_tokens(conn);
tokens[0][login] = uid.into();
tokens[login] = uid.into();
store_data(conn, "tokens", tokens);
}
fn get_uid(conn: &Connection, uid: &str) -> i64 {
create_token_store(conn);
let tokens = get_tokens(conn);
if tokens[0][uid].is_null() {
if tokens[uid].is_null() {
return 0;
}
return tokens[0][uid].as_i64().unwrap();
return tokens[uid].as_i64().unwrap();
}
fn get_login_token(conn: &Connection, uid: i64) -> String {
create_token_store(conn);
let tokens = get_tokens(conn);
for (_i, data) in tokens.entries().enumerate() {
if uid == data.1.as_i64().unwrap() {
return data.0.to_string();
}
}
String::new()
}
fn get_data(a6573cbe: &str) -> JsonValue {
@ -162,7 +186,7 @@ pub fn get_acc_home(a6573cbe: &str) -> JsonValue {
return get_data(a6573cbe)["home"].clone();
}
pub fn save_acc(a6573cbe: &str, data: JsonValue) {
pub fn save_data(a6573cbe: &str, data: JsonValue, id: &str) {
loop {
match ENGINE.lock() {
Ok(mut result) => {
@ -188,7 +212,7 @@ pub fn save_acc(a6573cbe: &str, data: JsonValue) {
let mut rv = json::parse(&result.unwrap()).unwrap();
rv["userdata"] = data;
rv[id] = data;
store_data(conn, &format!("_{}_", key), rv);
break;
}
@ -198,3 +222,109 @@ pub fn save_acc(a6573cbe: &str, data: JsonValue) {
}
}
}
pub fn save_acc(a6573cbe: &str, data: JsonValue) {
save_data(a6573cbe, data, "userdata");
}
pub fn get_acc_transfer(uid: i64, token: &str, password: &str) -> JsonValue {
loop {
match ENGINE.lock() {
Ok(mut result) => {
if result.is_none() {
init(&mut result);
}
let conn = result.as_ref().unwrap();
create_migration_store(conn);
let mut stmt = conn.prepare("SELECT jsondata FROM migrationdata").unwrap();
let result: Result<String, rusqlite::Error> = stmt.query_row([], |row| row.get(0));
let data = json::parse(&result.unwrap()).unwrap();
if data[token].is_empty() {
return object!{success: false};
}
if data[token].to_string() == password.to_string() {
let login_token = get_login_token(conn, uid);
if login_token == String::new() {
return object!{success: false};
}
return object!{success: true, login_token: login_token};
}
return object!{success: false};
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
}
}
pub fn save_acc_transfer(token: &str, password: &str) {
loop {
match ENGINE.lock() {
Ok(mut result) => {
if result.is_none() {
init(&mut result);
}
let conn = result.as_ref().unwrap();
create_migration_store(conn);
let mut stmt = conn.prepare("SELECT jsondata FROM migrationdata").unwrap();
let result: Result<String, rusqlite::Error> = stmt.query_row([], |row| row.get(0));
let mut data = json::parse(&result.unwrap()).unwrap();
data[token] = password.into();
store_data(conn, "migrationdata", data);
break;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
}
}
pub fn get_name_and_rank(uid: i64) -> JsonValue {
loop {
match ENGINE.lock() {
Ok(mut result) => {
if result.is_none() {
init(&mut result);
}
let conn = result.as_ref().unwrap();
create_migration_store(conn);
let login_token = get_login_token(conn, uid);
if login_token == String::new() {
return object!{
user_name: "",
user_rank: 1
}
}
let uid = get_uid(conn, &login_token);
if uid == 0 || !acc_exists(conn, uid) {
return object!{
user_name: "",
user_rank: 1
}
}
let mut stmt = conn.prepare(&format!("SELECT jsondata FROM _{}_", uid)).unwrap();
let result: Result<String, rusqlite::Error> = stmt.query_row([], |row| row.get(0));
let data = json::parse(&result.unwrap()).unwrap();
return object!{
user_name: data["userdata"]["user"]["name"].clone(),
user_rank: 1 //todo
}
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
}
}