From e7f62a95a63079273e8067977509948aa4453535 Mon Sep 17 00:00:00 2001 From: syui Date: Sun, 18 Feb 2024 23:33:41 +0900 Subject: [PATCH] fix refresh --- README.md | 20 ------------------- docs/wiki.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- src/data.rs | 29 ++++++++++++++++++++++++--- src/main.rs | 4 +++- src/refresh.rs | 15 ++++++++++---- 5 files changed, 92 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 1aa3a0f..23bd1aa 100644 --- a/README.md +++ b/README.md @@ -84,23 +84,3 @@ $ cargo install --force cargo-make $ cargo make build ``` -### docker - -```sh -$ docker run -it syui/aios ai -$ docker run -it -d syui/aios zsh -c "ai login -p && ai bot" -``` - -```sh -$ cp -rf ~/.config/ai ./.config/ - -$ docker compose up -``` - -### cron - -```sh -$ sudo pacman -S fcron -$ fcrontab -e -* * * * * $HOME/bot/test/ai.zsh c -``` diff --git a/docs/wiki.md b/docs/wiki.md index ba38a01..cd2152b 100644 --- a/docs/wiki.md +++ b/docs/wiki.md @@ -1 +1,52 @@ -## +### docker + +```sh +$ docker run -it syui/aios ai +$ docker run -it -d syui/aios zsh -c "ai login -p && ai bot" +``` + +```sh +$ cp -rf ~/.config/ai ./.config/ + +$ docker compose up +``` + +### cron + +```sh +$ sudo pacman -S fcron +$ fcrontab -e +* * * * * $HOME/bot/test/ai.zsh c +``` + +### ssh + +```sh +$ ssh-keygen -f /.ssh/diffusers.key -t ed25519 +``` + +```sh +FROM syui/aios +ADD .ssh /root/.ssh +``` + +```sh +Host diffusers + HostName localhost + User root + IdentityFile ~/.ssh/diffusers.key + StrictHostKeyChecking no + UserKnownHostsFile /dev/null +``` + +```sh +services: + aios: + #image: syui/aios + build: + context: . + restart: always + volumes: + - ./.config:/root/.config + command: ai bot -a syui.syu.is +``` diff --git a/src/data.rs b/src/data.rs index ba83651..ddd875e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -209,8 +209,34 @@ pub fn data_toml(s: &str) -> String { } } +pub fn c_refresh(access: &str, refresh: &str) { + let ff = data_file(&"refresh"); + let mut ff = fs::File::create(ff.clone()).unwrap(); + let refreshs = Refresh { + access: access.to_string(), + refresh: refresh.to_string(), + }; + let toml = toml::to_string(&refreshs).unwrap(); + ff.write_all(&toml.as_bytes()).unwrap(); +} + pub fn data_refresh(s: &str) -> String { let s = String::from(s); + + let data = Data::new().unwrap(); + let data = Data { + host: data.host, + handle: data.handle, + did: data.did, + access: data.access, + refresh: data.refresh, + }; + + let mut _file = match Refresh::new() + { + Err(_why) => c_refresh(&data.access, &data.refresh), + Ok(_) => println!(""), + }; let refresh = Refresh::new().unwrap(); let refresh = Refresh { access: refresh.access, @@ -509,11 +535,8 @@ pub fn w_cfg(h: &str, res: &str) { } pub fn w_refresh(res: &str) { - let f = data_file(&"json"); let ff = data_file(&"refresh"); - let mut f = fs::File::create(f.clone()).unwrap(); let mut ff = fs::File::create(ff.clone()).unwrap(); - f.write_all(&res.as_bytes()).unwrap(); let json: Token = serde_json::from_str(&res).unwrap(); let refreshs = Refresh { access: json.accessJwt.to_string(), diff --git a/src/main.rs b/src/main.rs index bea1c57..72382e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -303,7 +303,9 @@ fn refresh(_c: &Context) { if session == "err" { let res = refresh::post_request().await; println!("{}", res); - w_refresh(&res) + if res == "err" { + w_refresh(&res) + } } }; let res = tokio::runtime::Runtime::new().unwrap().block_on(h); diff --git a/src/refresh.rs b/src/refresh.rs index bbed80c..06fe322 100644 --- a/src/refresh.rs +++ b/src/refresh.rs @@ -12,10 +12,17 @@ pub async fn post_request() -> String { .header("Authorization", "Bearer ".to_owned() + &refresh) .send() .await - .unwrap() - .text() - .await .unwrap(); - return res; + let status_ref = res.error_for_status_ref(); + + match status_ref { + Ok(_) => { + return res.text().await.unwrap(); + } + Err(_e) => { + let e = "err".to_string(); + return e; + } + } }