Browse Source

feat: string list transfer

dev
Dnomd343 2 years ago
parent
commit
11767d7345
  1. 6
      include/utils/assets.h
  2. 6
      src/CMakeLists.txt
  3. 9
      src/assets/Cargo.lock
  4. 5
      src/assets/Cargo.toml
  5. 3
      src/assets/cbindgen.toml
  6. 93
      src/assets/src/fetch.rs
  7. 65
      src/assets/src/lib.rs
  8. 22
      src/cleardns.c

6
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

6
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)

9
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]]

5
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"] }

3
src/assets/cbindgen.toml

@ -0,0 +1,3 @@
language = "C"
pragma_once = true
include_version = true

93
src/assets/src/main.rs → 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<String>, // http url or file path
}
/// Cut text line by line and remove invisible characters on both sides.
fn asset_tidy(data: &str) -> Vec<String> {
@ -54,10 +55,10 @@ async fn http_fetch(url: &str, timeout: u64) -> Result<Vec<String>, String> {
}
/// Read the specified text file and organize it into a String array.
async fn local_fetch(path: &str) -> Result<Vec<String>, String> {
async fn local_fetch(path: &str, workdir: &str) -> Result<Vec<String>, 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<Vec<String>, String> {
}
/// Get multiple resource data and merge them.
async fn asset_fetch(name: &str, sources: Vec<&str>) -> Vec<String> {
pub(crate) async fn asset_fetch(info: &Asset) -> Vec<String> {
let is_remote = |src: &str| {
src.starts_with("http://") || src.starts_with("https://")
};
let mut contents: Vec<Vec<String>> = 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<String> {
.into_iter()
.flatten()
.collect::<Vec<String>>());
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");
}

65
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");
//
// }

22
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();

Loading…
Cancel
Save