feat(ck): 成功获取 ck,只需两步确认

Co-authored-by: Moke <jerry765@users.noreply.github.com>
This commit is contained in:
BTMuli
2023-04-09 17:17:58 +08:00
parent 4c0041b3e4
commit b5b1c318ea
4 changed files with 818 additions and 98 deletions

View File

@@ -1,6 +1,49 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// Tauri
use tauri::{AppHandle, Manager, WindowBuilder, WindowUrl};
// Rocket
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response};
#[macro_use]
extern crate rocket;
static mut COOKIE: String = String::new();
// Rocket 接受 get 请求,发来的 cookie 会被打印在控制台
#[rocket::get("/login?<cookie>")]
fn emit_cookie(cookie: String) -> String {
unsafe {
COOKIE = cookie.parse().unwrap();
}
cookie
}
pub struct Cors;
#[rocket::async_trait]
impl Fairing for Cors {
fn info(&self) -> Info {
Info {
name: "Cross-Origin-Resource-Sharing Fairing",
kind: Kind::Response,
}
}
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new(
"Access-Control-Allow-Methods",
"POST, PATCH, PUT, DELETE, HEAD, OPTIONS, GET",
));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}
#[tauri::command]
async fn mys_login(handle: AppHandle) {
// 创建新窗口
@@ -13,22 +56,32 @@ async fn mys_login(handle: AppHandle) {
.unwrap();
// 执行 js 代码
let js_code = "
var cookie = document.cookie;
const cookie = document.cookie;
if(cookie == null || cookie == ''){
alert('请在该网页登录后关闭窗口再试');
} else {
prompt('请复制以下内容', `mys-login:${cookie}`);
fetch('http://localhost:8000/login?cookie=' + cookie);
alert('成功获取 cookie!请关闭窗口');
}
";
mys_window.eval(js_code).ok().unwrap();
}
use tauri::{AppHandle, Manager, WindowBuilder, WindowUrl};
#[tauri::command]
fn read_cookie() -> String {
unsafe { COOKIE.clone() }
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![mys_login])
.invoke_handler(tauri::generate_handler![mys_login, read_cookie])
.setup(|app| {
tauri::async_runtime::spawn(
rocket::build()
.attach(Cors)
.mount("/", routes![emit_cookie])
.launch(),
);
#[cfg(debug_assertions)] // only include this code on debug builds
{
let window = app.get_window("tauri-genshin").unwrap();