diff --git a/include/utils/assets.h b/include/utils/assets.h index 5e1b47b..cf75abf 100644 --- a/include/utils/assets.h +++ b/include/utils/assets.h @@ -12,4 +12,10 @@ void assets_load(assets *info); void assets_extract(); +// RUST DEMO START + +void rust_test(const char *const *ptr); + +// RUST DEMO END + #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index afa07b3..6db7fa3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,9 @@ include_directories(${PROJECT_SOURCE_DIR}/include/loader) link_directories(${PROJECT_SOURCE_DIR}/src/to-json/target/release) +# TODO: just for test +link_directories(${PROJECT_SOURCE_DIR}/src/assets/target/debug) + add_subdirectory(utils) add_subdirectory(applet) add_subdirectory(bcrypt) @@ -17,3 +20,6 @@ add_subdirectory(loader) add_executable(cleardns cleardns.c) target_link_libraries(cleardns utils applet bcrypt common loader) + +# TODO: just for test +target_link_libraries(cleardns assets) diff --git a/src/assets/Cargo.lock b/src/assets/Cargo.lock index da04665..5e23ef0 100644 --- a/src/assets/Cargo.lock +++ b/src/assets/Cargo.lock @@ -251,12 +251,6 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608" -[[package]] -name = "futures-io" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfb8371b6fb2aeb2d280374607aeabfc99d95c72edfe51692e42d3d7f0d08531" - [[package]] name = "futures-sink" version = "0.3.26" @@ -276,12 +270,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1" dependencies = [ "futures-core", - "futures-io", "futures-task", - "memchr", "pin-project-lite", "pin-utils", - "slab", ] [[package]] diff --git a/src/assets/Cargo.toml b/src/assets/Cargo.toml index 83999fa..761f136 100644 --- a/src/assets/Cargo.toml +++ b/src/assets/Cargo.toml @@ -3,10 +3,13 @@ name = "assets" version = "0.1.0" edition = "2021" +[lib] +crate-type = ["staticlib"] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] env_logger = "0.10.0" log = "0.4.17" -reqwest = { version = "0.11.14", features = ["blocking", "deflate", "gzip", "brotli"] } +reqwest = { version = "0.11.14", features = ["deflate", "gzip", "brotli"] } tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] } diff --git a/src/assets/cbindgen.toml b/src/assets/cbindgen.toml new file mode 100644 index 0000000..cb8d3fd --- /dev/null +++ b/src/assets/cbindgen.toml @@ -0,0 +1,3 @@ +language = "C" +pragma_once = true +include_version = true diff --git a/src/assets/src/main.rs b/src/assets/src/fetch.rs similarity index 60% rename from src/assets/src/main.rs rename to src/assets/src/fetch.rs index 73381ba..a015399 100644 --- a/src/assets/src/main.rs +++ b/src/assets/src/fetch.rs @@ -1,17 +1,18 @@ -use std::collections::{HashMap, HashSet}; -use reqwest::Client; -use std::env::set_var; -use std::time::Duration; - use std::fs::File; use std::io::Read; +use reqwest::Client; use std::path::PathBuf; - +use std::time::Duration; use log::{debug, info, warn}; - -const TIMEOUT: u64 = 60; - -const ASSETS_DIR: &str = "/cleardns/assets/"; +use std::collections::HashSet; + +#[derive(Debug)] +pub(crate) struct Asset { + pub(crate) name: String, + pub(crate) timeout: u64, + pub(crate) workdir: String, // assets folder + pub(crate) sources: Vec, // http url or file path +} /// Cut text line by line and remove invisible characters on both sides. fn asset_tidy(data: &str) -> Vec { @@ -54,10 +55,10 @@ async fn http_fetch(url: &str, timeout: u64) -> Result, String> { } /// Read the specified text file and organize it into a String array. -async fn local_fetch(path: &str) -> Result, String> { +async fn local_fetch(path: &str, workdir: &str) -> Result, String> { let mut path = String::from(path); if !path.starts_with("/") { // relative path - let file_path = PathBuf::from(ASSETS_DIR).join(path); + let file_path = PathBuf::from(workdir).join(path); path = String::from(file_path.to_str().unwrap()); } match File::open(&path) { @@ -74,16 +75,16 @@ async fn local_fetch(path: &str) -> Result, String> { } /// Get multiple resource data and merge them. -async fn asset_fetch(name: &str, sources: Vec<&str>) -> Vec { +pub(crate) async fn asset_fetch(info: &Asset) -> Vec { let is_remote = |src: &str| { src.starts_with("http://") || src.starts_with("https://") }; let mut contents: Vec> = vec![]; - for source in sources { - contents.push(match if is_remote(source) { - http_fetch(source.trim(), TIMEOUT).await + for source in &info.sources { + contents.push(match if is_remote(&source) { + http_fetch(source.trim(), info.timeout).await } else { - local_fetch(source.trim()).await + local_fetch(source.trim(), &info.workdir).await } { Ok(data) => { debug!("Asset source `{}` fetch success with {} items", source.trim(), data.len()); @@ -99,62 +100,6 @@ async fn asset_fetch(name: &str, sources: Vec<&str>) -> Vec { .into_iter() .flatten() .collect::>()); - info!("Asset `{}` fetch complete with {} items", name, contents.len()); + info!("Asset `{}` fetch complete with {} items", info.name, contents.len()); contents } - -async fn demo() { - - println!("demo function start"); - - // match asset_fetch("https://res.343.re/Share/cleardns/gfwlist.txt", TIMEOUT).await { - // Ok(data) => { - // println!("{:?}", data); - // }, - // Err(err) => println!("error -> {}", err) - // } - - match local_fetch("../../tmp/gfwlist.txt").await { - Ok(data) => { - // println!("{:?}", data); - }, - Err(err) => println!("error -> `{}`", err) - }; - - println!("demo function exit"); - -} - -#[tokio::main] -async fn main() { - - set_var("RUST_LOG", "trace"); - env_logger::init(); - - let d = vec![ - "https://res.343.re/Share/cleardns/gfwlist.txt", - "/tmp/gfwlist.txt", - ]; - asset_fetch("test", d).await; - - // let demo = vec![ - // "baidu.com", - // "ip.343.re", - // "qq.com", - // "google.com", - // "res.343.re", - // "ip.343.re", - // "343.re", - // ]; - // let demo = demo.into_iter() - // .map(|x| String::from(x)) - // .collect(); - // remove_dup(&demo); - - // let data: &str = "dnomd343\n linjucong\n\nfuck\t\n7700\n"; - // println!("{}", data); - // tidy(data); - - println!("end demo"); - -} diff --git a/src/assets/src/lib.rs b/src/assets/src/lib.rs new file mode 100644 index 0000000..ac11b25 --- /dev/null +++ b/src/assets/src/lib.rs @@ -0,0 +1,65 @@ +// mod fetch; +// +// use std::env::set_var; +// +// use crate::fetch::Asset; + +use std::ffi::{c_char, CStr}; + +// const TIMEOUT: u64 = 60; +// +// const ASSETS_DIR: &str = "/cleardns/assets/"; + +#[no_mangle] +pub unsafe extern "C" fn rust_test(ptr: *const *const c_char) { + println!("enter rust function"); + + unsafe { + + println!("ptr: {:?}", ptr); + + + let mut index = 0; + while *ptr.offset(index) != std::ptr::null() { + + let p = *ptr.offset(index); + + println!("loop: {:?}", p); + + let s = String::from( + CStr::from_ptr(p).to_str().unwrap() + ); + + println!("content: {}", s); + + index += 1; + } + + + } + + println!("exit rust function"); +} + +// #[tokio::main] +// async fn main() { +// +// set_var("RUST_LOG", "trace"); +// env_logger::init(); +// +// let d = vec![ +// String::from("https://res.343.re/Share/cleardns/gfwlist.txt"), +// String::from("/tmp/gfwlist.txt"), +// ]; +// let info = Asset { +// name: String::from("demo"), +// timeout: TIMEOUT, +// workdir: String::from(ASSETS_DIR), +// sources: d, +// }; +// fetch::asset_fetch(&info).await; +// +// +// println!("end demo"); +// +// } diff --git a/src/cleardns.c b/src/cleardns.c index 31c85a3..1225691 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -118,6 +118,28 @@ void cleardns() { // cleardns service } int main(int argc, char *argv[]) { + + char **demo = string_list_init(); + string_list_append(&demo, "item 1"); + string_list_append(&demo, "item 2"); + string_list_append(&demo, "item 3"); + + char *tmp = string_list_dump(demo); + log_warn("dump -> %s", tmp); + + string_list_free(demo); + + log_info("string list -> %p", demo); + log_info("string list 1 -> %p", demo[0]); + log_info("string list 2 -> %p", demo[1]); + log_info("string list 3 -> %p", demo[2]); + log_info("string list 4 -> %p", demo[3]); + + rust_test(demo); + + log_warn("test end"); + exit(0); + init(argc, argv); log_info("ClearDNS server start (%s)", VERSION); cleardns();