From 3b1f75c14f8a4a3a11ef12b40d8e5f5c41a3163b Mon Sep 17 00:00:00 2001 From: TongZ <471205975@qq.com> Date: Thu, 1 Aug 2024 11:54:56 +0800 Subject: [PATCH] enhance: add readable log --- .github/workflows/main.yml | 2 +- Cargo.lock | 167 +++++++++++++++++++++++++++++++++++-- Cargo.toml | 4 +- README.md | 5 +- src/main.rs | 36 ++++---- src/utils.rs | 8 +- 6 files changed, 196 insertions(+), 26 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 65e4e6c..093e935 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,7 +38,7 @@ jobs: repo: context.repo.repo, tag_name: `athene_network_bot_v${{steps.read_toml.outputs.value}}`, name: `Athene Bot v${{steps.read_toml.outputs.value}}`, - body: 'Take a look at the assets to download and install this app.', + body: `${{ github.event.head_commit.message }}`, draft: true, prerelease: false }) diff --git a/Cargo.lock b/Cargo.lock index 8b98b61..959e05e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -32,13 +41,64 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "athene_bot" -version = "0.0.1" +version = "0.0.2" dependencies = [ "base64", "chrono", + "colog", "futures", + "log", "percent-encoding", "rand", "reqwest", @@ -126,9 +186,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.1" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" +checksum = "fca2be1d5c43812bae364ee3f30b3afcb7877cf59f4aeb94c66f313a41d2fac9" [[package]] name = "cc" @@ -156,6 +216,33 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "colog" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c426b7af8d5e0ad79de6713996632ce31f0d68ba84068fb0d654b396e519df0" +dependencies = [ + "colored", + "env_logger", + "log", +] + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -239,6 +326,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -478,6 +588,12 @@ version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.4.1" @@ -586,9 +702,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.2.6" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" dependencies = [ "equivalent", "hashbrown", @@ -600,6 +716,12 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.11" @@ -1004,6 +1126,35 @@ dependencies = [ "bitflags 2.6.0", ] +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "reqwest" version = "0.12.5" @@ -1574,6 +1725,12 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.10.0" diff --git a/Cargo.toml b/Cargo.toml index a3f59fd..6689cbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "athene_bot" -version = "0.0.1" +version = "0.0.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,7 +8,9 @@ edition = "2021" [dependencies] base64 = "0.22.1" chrono = "0.4.38" +colog = "1.3.0" futures = "0.3.30" +log = "0.4.22" percent-encoding = "2.3.1" rand = "0.8.5" reqwest = "0.12.5" diff --git a/README.md b/README.md index d02c9d7..951314e 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ A telegram mini GameFI project, you can earn **gem** \ **ATH token** by **gold** ## Usage +> **!!! use it in your commandline.** + 1. Download your app from release page. 2. Grab your `athene-network` URL from [Telegram Web APP](https://web.telegram.org/k/#@athene_official_bot). @@ -30,11 +32,12 @@ A telegram mini GameFI project, you can earn **gem** \ **ATH token** by **gold** 3. Make sure that `user.json` must be same directory with `athene_bot`. ### user.json +This bot use `user.json` in the same directory to login and claim your rewards, the schema like this: ```json { "{different account alias name}": { "link": "{your link}", - "access_token": "your access_token, most of time, generated by link after first run", + "access_token": "Your access_token, most of time, generated by link after first run.", "invite_code": "{your invite code}" } } diff --git a/src/main.rs b/src/main.rs index e92e411..d1299d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use base64::engine::general_purpose::STANDARD; use base64::Engine; +use colog; +use log::info; use reqwest::header::{HeaderMap, HeaderValue, COOKIE}; use reqwest::{StatusCode, Url}; use serde::{Deserialize, Serialize}; @@ -44,7 +46,7 @@ fn concat_str(s: i64, d: i64) -> String { format!("{s}-{ts}-{d}") } -async fn get_tap_earn(cookie: &str) -> Result> { +async fn get_tap_earn(cookie: &str, name: &str) -> Result> { let client = reqwest::Client::new(); // let client = reqwest::Client::builder().proxy(reqwest::Proxy::http("http://127.0.0.1:13453")?).build()?; let mut headers = HeaderMap::new(); @@ -79,11 +81,11 @@ async fn get_tap_earn(cookie: &str) -> Result Result<(), Box> { +async fn post_conver_gem(re: String, cookie: &str, name: &str) -> Result<(), Box> { let client = reqwest::Client::new(); let mut headers = HeaderMap::new(); utils::init_headers(&mut headers); @@ -104,13 +106,13 @@ async fn post_conver_gem(re: String, cookie: &str) -> Result<(), Box Result> { +async fn get_mining_time(cookie: &str, name: &str) -> Result> { let client = reqwest::Client::new(); let mut headers = HeaderMap::new(); utils::init_headers(&mut headers); @@ -127,19 +129,19 @@ async fn get_mining_time(cookie: &str) -> Result // let response_text = response.te.await?; // println!("{:?}", response_text); - println!("get_mining: {:?}", response.status()); let status = response.status(); + utils::format_println(name, &format!("get_mining: {:?}", status)); if status == StatusCode::OK { let val: serde_json::Value = serde_json::from_str(&response.text().await?).unwrap(); return Ok(val["data"]["remainTimeNextClaim"].as_i64().unwrap()); } - println!("get_mining_time_error: {:?}", status); + utils::format_error(name, "get_mining_time_error"); Err(Box::new(AthenaErr::GetMiningErr)) } async fn post_claim_gem(cookie: &str, name: &str) -> Result<(), Box> { - let rest_mining_time = get_mining_time(cookie).await?; + let rest_mining_time = get_mining_time(cookie, name).await?; utils::format_println(name, &format!("get_mining_time: {}", rest_mining_time)); if rest_mining_time <= 0i64 { @@ -200,7 +202,7 @@ async fn post_check_in(cookie: &str, name: &str) -> Result<(), Box Result<(), Box> { - let tap_data = get_tap_earn(cookie).await?; + let tap_data = get_tap_earn(cookie, name).await?; let total_tap = (((utils::get_current_timestamp() - tap_data.number_tap) / 100) | 0) - 100; utils::format_println(name, &format!("now tap count: {}", total_tap)); @@ -213,7 +215,7 @@ async fn post_convert_gem(cookie: &str, name: &str) -> Result<(), Box) { #[tokio::main] async fn main() -> Result<(), JobSchedulerError> { + colog::init(); + let sched = JobScheduler::new().await?; // read user token from file let file_path = path::PathBuf::from(std::env::current_dir().unwrap()).join("user.json"); - println!("file_path: {:?}", file_path); + info!("file_path: {:?}", file_path); let users = read_config_json(file_path.to_str().unwrap()); let mut copy_users: HashMap = HashMap::new(); @@ -330,7 +334,7 @@ async fn main() -> Result<(), JobSchedulerError> { let token1 = token.clone(); let name2 = name.clone(); let token2 = token.clone(); - println!("name: {}, start", &name); + info!("name: {}, start", &name); utils::format_println(&name, "post_check_in_start"); let _ = post_check_in(&token, &name).await.map_err(|err| { @@ -351,7 +355,7 @@ async fn main() -> Result<(), JobSchedulerError> { sleep(Duration::from_secs(1)).await; utils::format_println(&name, "post_check_in_start"); let _ = post_check_in(&token, &name).await.map_err(|err| { - utils::format_println( + utils::format_error( &name, &format!("post_check_in_error: {:?}", err), ); @@ -372,7 +376,7 @@ async fn main() -> Result<(), JobSchedulerError> { sleep(Duration::from_secs(3)).await; utils::format_println(&name, "post_claim_gem_start"); let _ = post_claim_gem(&token, &name).await.map_err(|err| { - utils::format_println( + utils::format_error( &name, &format!("post_claim_gem_error: {:?}", err), ); @@ -393,7 +397,7 @@ async fn main() -> Result<(), JobSchedulerError> { sleep(Duration::from_secs(5)).await; utils::format_println(&name, "post_convert_gem_start"); let _ = post_convert_gem(&token, &name).await.map_err(|err| { - utils::format_println( + utils::format_error( &name, &format!("post_convert_gem_error: {:?}", err), ); diff --git a/src/utils.rs b/src/utils.rs index e49ad5b..922b5e8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,7 @@ use base64::engine::general_purpose::STANDARD; use base64::Engine; use chrono::Local; +use log::{error, info}; use std::time::{SystemTime, UNIX_EPOCH}; use rsa::Oaep; @@ -12,7 +13,7 @@ use reqwest::header::{ REFERRER_POLICY, USER_AGENT, }; -// 加密算法 +// encrypt algorithm pub fn rsa_encrypt(t: &str) -> String { let key = "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwmkourU0WsNzc0mcb6a7 @@ -46,7 +47,10 @@ pub fn get_current_timestamp() -> i64 { } pub fn format_println(name: &str, msg: &str) { - println!("[{}] [{}]: {}", now(), name, msg); + info!("[{}] [{}]: {}", now(), name, msg); +} +pub fn format_error(name: &str, msg: &str) { + error!("[{}] [{}]: {}", now(), name, msg); } pub fn init_headers(h: &mut HeaderMap) -> &mut HeaderMap {