From 6c436af5b2da4dd56c6e330c09f0cae6bc36743c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 01:14:08 +0800 Subject: [PATCH 01/12] feat: rust ffi demo --- .gitignore | 1 + src/demo.c | 18 ++++++++++++++++++ src/to-json/Cargo.lock | 7 +++++++ src/to-json/Cargo.toml | 11 +++++++++++ src/to-json/src/lib.rs | 15 +++++++++++++++ src/to-json/to_json.h | 8 ++++++++ 6 files changed, 60 insertions(+) create mode 100644 src/demo.c create mode 100644 src/to-json/Cargo.lock create mode 100644 src/to-json/Cargo.toml create mode 100644 src/to-json/src/lib.rs create mode 100644 src/to-json/to_json.h diff --git a/.gitignore b/.gitignore index 927f10c..fd1065e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /to-json/target/ /cmake-build-debug/ /cmake-build-release/ +/src/to-json/target/ diff --git a/src/demo.c b/src/demo.c new file mode 100644 index 0000000..4cc3f16 --- /dev/null +++ b/src/demo.c @@ -0,0 +1,18 @@ +#include +#include "to_json.h" + +int main() { + printf("start demo\n"); + + char raw_string[] = "hello"; + + const char *json_content = to_json_rust(raw_string); + + printf("return content -> `%s`\n", json_content); + + free_rust_string(json_content); + + printf("rust string free success\n"); + + return 0; +} diff --git a/src/to-json/Cargo.lock b/src/to-json/Cargo.lock new file mode 100644 index 0000000..3efbc0d --- /dev/null +++ b/src/to-json/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "to-json" +version = "0.1.0" diff --git a/src/to-json/Cargo.toml b/src/to-json/Cargo.toml new file mode 100644 index 0000000..a8c79b7 --- /dev/null +++ b/src/to-json/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "to-json" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs new file mode 100644 index 0000000..1ece23a --- /dev/null +++ b/src/to-json/src/lib.rs @@ -0,0 +1,15 @@ +use std::ffi::{c_char, CStr, CString}; + +#[no_mangle] +pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { + let content: &str = CStr::from_ptr(content).to_str().unwrap(); + + println!("Raw content -> {}", content); + + CString::new(content).unwrap().into_raw() +} + +#[no_mangle] +pub unsafe extern "C" fn free_rust_string(string: *const c_char) { + let _ = CString::from_raw(string as *mut _); +} diff --git a/src/to-json/to_json.h b/src/to-json/to_json.h new file mode 100644 index 0000000..5dff661 --- /dev/null +++ b/src/to-json/to_json.h @@ -0,0 +1,8 @@ +#include +#include +#include +#include + +const char *to_json_rust(const char *content); + +void free_rust_string(const char *string); From bf34eba3388d94589f7d043782fa4925358d98d8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 01:22:57 +0800 Subject: [PATCH 02/12] update: single ffi rs file --- src/to-json/src/ffi.rs | 18 ++++++++++++++++++ src/to-json/src/lib.rs | 16 +--------------- 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 src/to-json/src/ffi.rs diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs new file mode 100644 index 0000000..fef3e3a --- /dev/null +++ b/src/to-json/src/ffi.rs @@ -0,0 +1,18 @@ +use std::ffi::{c_char, CStr, CString}; + +#[no_mangle] +pub unsafe extern "C" fn free_rust_string(string: *const c_char) { + let _ = CString::from_raw(string as *mut _); +} + +#[no_mangle] +pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { + let content: &str = CStr::from_ptr(content).to_str().unwrap(); + + // TODO: remove test code + println!("Raw content -> {}", content); + + // TODO: convert to JSON format + + CString::new(content).unwrap().into_raw() +} diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 1ece23a..57ae9b9 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1,15 +1 @@ -use std::ffi::{c_char, CStr, CString}; - -#[no_mangle] -pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { - let content: &str = CStr::from_ptr(content).to_str().unwrap(); - - println!("Raw content -> {}", content); - - CString::new(content).unwrap().into_raw() -} - -#[no_mangle] -pub unsafe extern "C" fn free_rust_string(string: *const c_char) { - let _ = CString::from_raw(string as *mut _); -} +mod ffi; From 586cf9cfa6f16a5304f14461e2fab7ef0ebe046a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 10:00:50 +0800 Subject: [PATCH 03/12] feat: convert yaml or toml to json format --- src/to-json/Cargo.lock | 84 +++++++++++++++++++++++++++++++++++++++++ src/to-json/Cargo.toml | 3 ++ src/to-json/src/ffi.rs | 2 + src/to-json/src/json.rs | 32 ++++++++++++++++ src/to-json/src/lib.rs | 1 + 5 files changed, 122 insertions(+) create mode 100644 src/to-json/src/json.rs diff --git a/src/to-json/Cargo.lock b/src/to-json/Cargo.lock index 3efbc0d..71b6233 100644 --- a/src/to-json/Cargo.lock +++ b/src/to-json/Cargo.lock @@ -2,6 +2,90 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" + +[[package]] +name = "serde_json" +version = "1.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.9.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "to-json" version = "0.1.0" +dependencies = [ + "serde_json", + "serde_yaml", + "toml", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unsafe-libyaml" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" diff --git a/src/to-json/Cargo.toml b/src/to-json/Cargo.toml index a8c79b7..4e6f0af 100644 --- a/src/to-json/Cargo.toml +++ b/src/to-json/Cargo.toml @@ -9,3 +9,6 @@ crate-type = ["cdylib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde_json = "1.0.87" +serde_yaml = "0.9.14" +toml = "0.5.9" diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs index fef3e3a..fc0b297 100644 --- a/src/to-json/src/ffi.rs +++ b/src/to-json/src/ffi.rs @@ -1,4 +1,5 @@ use std::ffi::{c_char, CStr, CString}; +use crate::json::to_json; #[no_mangle] pub unsafe extern "C" fn free_rust_string(string: *const c_char) { @@ -13,6 +14,7 @@ pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { println!("Raw content -> {}", content); // TODO: convert to JSON format + let content: String = to_json(content); // may return empty string CString::new(content).unwrap().into_raw() } diff --git a/src/to-json/src/json.rs b/src/to-json/src/json.rs new file mode 100644 index 0000000..83efc4c --- /dev/null +++ b/src/to-json/src/json.rs @@ -0,0 +1,32 @@ +use serde_json as json; +use serde_yaml as yaml; + +enum Format { + JSON(json::Value), + YAML(yaml::Value), + TOML(toml::Value), +} + +fn parser(content: &str) -> Option { + if let Ok(data) = json::from_str::(content) { // try JSON format + return Some(Format::JSON(data)); + } + if let Ok(data) = toml::from_str::(content) { // try TOML format + return Some(Format::TOML(data)); + } + if let Ok(data) = yaml::from_str::(content) { // try YAML format + return Some(Format::YAML(data)); + } + return None; // parse failed +} + +pub fn to_json(content: &str) -> String { // convert to JSON format + match parser(content) { + Some(data) => match data { + Format::JSON(dat) => json::to_string(&dat).unwrap(), + Format::YAML(dat) => json::to_string(&dat).unwrap(), + Format::TOML(dat) => json::to_string(&dat).unwrap(), + }, + None => String::from(""), // failed -> empty string + } +} diff --git a/src/to-json/src/lib.rs b/src/to-json/src/lib.rs index 57ae9b9..4f1b9a0 100644 --- a/src/to-json/src/lib.rs +++ b/src/to-json/src/lib.rs @@ -1 +1,2 @@ mod ffi; +mod json; From 6b4b9390a091defb595d64c280f8659defadaa9a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 10:09:41 +0800 Subject: [PATCH 04/12] update: demo for to json library --- src/demo.c | 18 ++++++++++++------ src/to-json/to_json.h | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/demo.c b/src/demo.c index 4cc3f16..93096f8 100644 --- a/src/demo.c +++ b/src/demo.c @@ -1,18 +1,24 @@ #include +#include #include "to_json.h" int main() { printf("start demo\n"); - char raw_string[] = "hello"; + char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n"; + printf("----------------\n"); + printf("%s", yaml_string); + printf("----------------\n"); - const char *json_content = to_json_rust(raw_string); + const char *raw_json_string = to_json_rust(yaml_string); + char *json_string = strdup(raw_json_string); - printf("return content -> `%s`\n", json_content); + printf("----------------\n"); + printf("%s\n", json_string); + printf("----------------\n"); - free_rust_string(json_content); - - printf("rust string free success\n"); + free_rust_string(raw_json_string); + printf("rust string free complete\n"); return 0; } diff --git a/src/to-json/to_json.h b/src/to-json/to_json.h index 5dff661..ccde887 100644 --- a/src/to-json/to_json.h +++ b/src/to-json/to_json.h @@ -3,6 +3,6 @@ #include #include -const char *to_json_rust(const char *content); - void free_rust_string(const char *string); + +const char *to_json_rust(const char *content); From 32d80701b2bce099ca5554550430028553f77004 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 10:24:43 +0800 Subject: [PATCH 05/12] feat: cbindgen configure file --- src/to-json/cbindgen.toml | 3 +++ src/to-json/to_json.h | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 src/to-json/cbindgen.toml diff --git a/src/to-json/cbindgen.toml b/src/to-json/cbindgen.toml new file mode 100644 index 0000000..cb8d3fd --- /dev/null +++ b/src/to-json/cbindgen.toml @@ -0,0 +1,3 @@ +language = "C" +pragma_once = true +include_version = true diff --git a/src/to-json/to_json.h b/src/to-json/to_json.h index ccde887..c93c353 100644 --- a/src/to-json/to_json.h +++ b/src/to-json/to_json.h @@ -1,3 +1,7 @@ +#pragma once + +/* Generated with cbindgen:0.23.0 */ + #include #include #include From c89d10c57d7462cf447c2a1644311b24ed78c890 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 10:25:44 +0800 Subject: [PATCH 06/12] remove: to-json program --- to-json/Cargo.lock | 91 --------------------------------------------- to-json/Cargo.toml | 11 ------ to-json/src/main.rs | 57 ---------------------------- 3 files changed, 159 deletions(-) delete mode 100644 to-json/Cargo.lock delete mode 100644 to-json/Cargo.toml delete mode 100644 to-json/src/main.rs diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock deleted file mode 100644 index 71b6233..0000000 --- a/to-json/Cargo.lock +++ /dev/null @@ -1,91 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - -[[package]] -name = "indexmap" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" -dependencies = [ - "autocfg", - "hashbrown", -] - -[[package]] -name = "itoa" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" - -[[package]] -name = "ryu" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" - -[[package]] -name = "serde" -version = "1.0.147" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" - -[[package]] -name = "serde_json" -version = "1.0.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "serde_yaml" -version = "0.9.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da" -dependencies = [ - "indexmap", - "itoa", - "ryu", - "serde", - "unsafe-libyaml", -] - -[[package]] -name = "to-json" -version = "0.1.0" -dependencies = [ - "serde_json", - "serde_yaml", - "toml", -] - -[[package]] -name = "toml" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" -dependencies = [ - "serde", -] - -[[package]] -name = "unsafe-libyaml" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" diff --git a/to-json/Cargo.toml b/to-json/Cargo.toml deleted file mode 100644 index 2fddb92..0000000 --- a/to-json/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "to-json" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -serde_json = "1.0.86" -serde_yaml = "0.9.14" -toml = "0.5.9" diff --git a/to-json/src/main.rs b/to-json/src/main.rs deleted file mode 100644 index 07bbabf..0000000 --- a/to-json/src/main.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::io::Read; -use serde_json as json; -use serde_yaml as yaml; - -const FILE_ERROR: i32 = 1; // file open error -const PARSE_ERROR: i32 = 2; // parser error - -enum Format { - JSON(json::Value), - YAML(yaml::Value), - TOML(toml::Value), -} - -fn parser(content: &str) -> Option { - if let Ok(data) = json::from_str::(content) { // try JSON format - return Some(Format::JSON(data)); - } - if let Ok(data) = toml::from_str::(content) { // try TOML format - return Some(Format::TOML(data)); - } - if let Ok(data) = yaml::from_str::(content) { // try YAML format - return Some(Format::YAML(data)); - } - return None; // parse failed -} - -fn to_json(content: &str) -> String { // convert to JSON format - match parser(content) { - Some(data) => match data { - Format::JSON(dat) => json::to_string(&dat).unwrap(), - Format::YAML(dat) => json::to_string(&dat).unwrap(), - Format::TOML(dat) => json::to_string(&dat).unwrap(), - }, - None => std::process::exit(PARSE_ERROR), // error exit - } -} - -fn read_file(path: &str) -> String { // read file content - match std::fs::File::open(path) { - Ok(mut file) => { // file open success - let mut content = String::new(); - file.read_to_string(&mut content).unwrap(); // get file content - content - }, - Err(_) => std::process::exit(FILE_ERROR), // read failed - } -} - -fn main() { - let args: Vec = std::env::args().collect(); // input arguments - if args.len() < 2 { // missing arguments - println!("usage: toJSON [file]"); - std::process::exit(0); - } - let content = read_file(&args[1].clone()[..]); // read file content - println!("{}", to_json(&content[..])); // convert to JSON format -} From 2eed5d674b56864932b350b43265a3b5d12e25bf Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 10:41:55 +0800 Subject: [PATCH 07/12] feat: add to-json for cleardns --- {src/to-json => include}/to_json.h | 0 src/CMakeLists.txt | 2 ++ src/cleardns.c | 21 ++++++++++++-- src/common/CMakeLists.txt | 1 + src/common/json.c | 46 +++++++++++++++++++----------- 5 files changed, 51 insertions(+), 19 deletions(-) rename {src/to-json => include}/to_json.h (100%) diff --git a/src/to-json/to_json.h b/include/to_json.h similarity index 100% rename from src/to-json/to_json.h rename to include/to_json.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 08534e5..afa07b3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,8 @@ include_directories(${PROJECT_SOURCE_DIR}/include/bcrypt) include_directories(${PROJECT_SOURCE_DIR}/include/common) include_directories(${PROJECT_SOURCE_DIR}/include/loader) +link_directories(${PROJECT_SOURCE_DIR}/src/to-json/target/release) + add_subdirectory(utils) add_subdirectory(applet) add_subdirectory(bcrypt) diff --git a/src/cleardns.c b/src/cleardns.c index 3ad366a..6439ea1 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -114,9 +114,24 @@ void cleardns() { // cleardns service process_list_daemon(); // daemon all process } +#include "json.h" + int main(int argc, char *argv[]) { - init(argc, argv); - log_info("ClearDNS server start (%s)", VERSION); - cleardns(); + + char error_string[] = "\f233\a"; + char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n"; + + log_info("Test yaml content ->\n%s", yaml_string); + char *json_result = to_json(yaml_string); + log_info("Return json content -> %s", json_result); + free(json_result); + + log_info("Test error content"); + char *ret = to_json(error_string); + log_info("Return -> %s", ret); + +// init(argc, argv); +// log_info("ClearDNS server start (%s)", VERSION); +// cleardns(); return 0; } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 2e7dff9..d74b0be 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,3 +1,4 @@ cmake_minimum_required(VERSION 2.8.12) add_library(common json.c sundry.c system.c structure.c) +target_link_libraries(common to_json) diff --git a/src/common/json.c b/src/common/json.c index 95f33e7..f9e6912 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -4,6 +4,7 @@ #include "logger.h" #include "sundry.h" #include "system.h" +#include "to_json.h" #include "constant.h" #include "structure.h" @@ -17,26 +18,39 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `. return FALSE; } -char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) - char flag[9]; - for (int i = 0; i < 8; ++i) { // generate 8-bits flag - flag[i] = (char)gen_rand_num(10); - flag[i] += 48; - } - flag[8] = '\0'; +char* to_json(const char *raw) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) + + const char *json_string = to_json_rust(raw); // convert to json format - char *output_file = string_join("/tmp/to-json-", flag); - char *to_json_cmd = string_load("toJSON %s > %s", file, output_file); - int to_json_ret = run_command(to_json_cmd); - free(to_json_cmd); + char *json_content = strdup(json_string); // load string into owner heap - char *json_content = NULL; - if (!to_json_ret) { // toJSON return zero code (convert fine) - json_content = read_file(output_file); + free_rust_string(json_string); // free rust string + + if (strlen(json_content) == 0) { // empty string -> convert error + free(json_content); + return NULL; } - remove_file(output_file); - free(output_file); return json_content; + +// char flag[9]; +// for (int i = 0; i < 8; ++i) { // generate 8-bits flag +// flag[i] = (char)gen_rand_num(10); +// flag[i] += 48; +// } +// flag[8] = '\0'; +// +// char *output_file = string_join("/tmp/to-json-", flag); +// char *to_json_cmd = string_load("toJSON %s > %s", file, output_file); +// int to_json_ret = run_command(to_json_cmd); +// free(to_json_cmd); +// +// char *json_content = NULL; +// if (!to_json_ret) { // toJSON return zero code (convert fine) +// json_content = read_file(output_file); +// } +// remove_file(output_file); +// free(output_file); +// return json_content; } cJSON* json_field_get(cJSON *entry, const char *key) { // fetch key from json map (create when key not exist) From 04aacd0fd3a9ffa8738539f6d050788ea239ab4c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 11:13:02 +0800 Subject: [PATCH 08/12] update: interface of to_json function --- include/common/json.h | 2 +- src/applet/adguard.c | 10 +++++++--- src/cleardns.c | 21 +++------------------ src/common/json.c | 29 ++--------------------------- src/demo.c | 24 ------------------------ src/loader/default.c | 8 ++------ src/loader/parser.c | 11 +++++++---- 7 files changed, 22 insertions(+), 83 deletions(-) delete mode 100644 src/demo.c diff --git a/include/common/json.h b/include/common/json.h index 912463c..345f408 100644 --- a/include/common/json.h +++ b/include/common/json.h @@ -4,7 +4,7 @@ #include #include "cJSON.h" -char* to_json(const char *config_file); +char* to_json(const char *content); uint8_t is_json_suffix(const char *file_name); cJSON* json_field_get(cJSON *entry, const char *key); void json_field_replace(cJSON *entry, const char *key, cJSON *content); diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 328d114..91f652e 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -92,9 +92,13 @@ process* adguard_load(adguard *info, const char *dir) { // load adguard options log_info("Create AdGuardHome configure"); adguard_config_ret = adguard_config(info, "{}"); // begin with empty json } else { // configure exist -> modify - char *adguard_config_raw = to_json(adguard_config_file); - adguard_config_ret = adguard_config(info, adguard_config_raw); - free(adguard_config_raw); + char *adguard_config_content = read_file(adguard_config_file); + log_debug("AdGuardHome raw configure ->\n%s", adguard_config_content); + char *adguard_config_json = to_json(adguard_config_content); + log_debug("AdGuardHome json configure ->\n%s", adguard_config_json); + adguard_config_ret = adguard_config(info, adguard_config_json); + free(adguard_config_content); + free(adguard_config_json); } save_file(adguard_config_file, adguard_config_ret); // save modified configure free(adguard_config_file); diff --git a/src/cleardns.c b/src/cleardns.c index 6439ea1..3ad366a 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -114,24 +114,9 @@ void cleardns() { // cleardns service process_list_daemon(); // daemon all process } -#include "json.h" - int main(int argc, char *argv[]) { - - char error_string[] = "\f233\a"; - char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n"; - - log_info("Test yaml content ->\n%s", yaml_string); - char *json_result = to_json(yaml_string); - log_info("Return json content -> %s", json_result); - free(json_result); - - log_info("Test error content"); - char *ret = to_json(error_string); - log_info("Return -> %s", ret); - -// init(argc, argv); -// log_info("ClearDNS server start (%s)", VERSION); -// cleardns(); + init(argc, argv); + log_info("ClearDNS server start (%s)", VERSION); + cleardns(); return 0; } diff --git a/src/common/json.c b/src/common/json.c index f9e6912..49cd85b 100644 --- a/src/common/json.c +++ b/src/common/json.c @@ -3,7 +3,6 @@ #include "cJSON.h" #include "logger.h" #include "sundry.h" -#include "system.h" #include "to_json.h" #include "constant.h" #include "structure.h" @@ -18,39 +17,15 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `. return FALSE; } -char* to_json(const char *raw) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) - - const char *json_string = to_json_rust(raw); // convert to json format - +char* to_json(const char *content) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) + const char *json_string = to_json_rust(content); // convert to json format char *json_content = strdup(json_string); // load string into owner heap - free_rust_string(json_string); // free rust string - if (strlen(json_content) == 0) { // empty string -> convert error free(json_content); return NULL; } return json_content; - -// char flag[9]; -// for (int i = 0; i < 8; ++i) { // generate 8-bits flag -// flag[i] = (char)gen_rand_num(10); -// flag[i] += 48; -// } -// flag[8] = '\0'; -// -// char *output_file = string_join("/tmp/to-json-", flag); -// char *to_json_cmd = string_load("toJSON %s > %s", file, output_file); -// int to_json_ret = run_command(to_json_cmd); -// free(to_json_cmd); -// -// char *json_content = NULL; -// if (!to_json_ret) { // toJSON return zero code (convert fine) -// json_content = read_file(output_file); -// } -// remove_file(output_file); -// free(output_file); -// return json_content; } cJSON* json_field_get(cJSON *entry, const char *key) { // fetch key from json map (create when key not exist) diff --git a/src/demo.c b/src/demo.c deleted file mode 100644 index 93096f8..0000000 --- a/src/demo.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include "to_json.h" - -int main() { - printf("start demo\n"); - - char yaml_string[] = "test: ok\narray:\n - 123\n - 234\n - 345\n"; - printf("----------------\n"); - printf("%s", yaml_string); - printf("----------------\n"); - - const char *raw_json_string = to_json_rust(yaml_string); - char *json_string = strdup(raw_json_string); - - printf("----------------\n"); - printf("%s\n", json_string); - printf("----------------\n"); - - free_rust_string(raw_json_string); - printf("rust string free complete\n"); - - return 0; -} diff --git a/src/loader/default.c b/src/loader/default.c index 2480f9b..e964484 100644 --- a/src/loader/default.c +++ b/src/loader/default.c @@ -57,12 +57,8 @@ void load_default_config(const char *config_file) { log_info("Loading default configure file"); char *config_content = NULL; if (is_json_suffix(config_file)) { // convert to json format - char temp_file[] = "temp.yml"; - save_file(temp_file, DEFAULT_CONFIG); - config_content = to_json(temp_file); - remove_file(temp_file); - } - if (config_content == NULL) { + config_content = to_json(DEFAULT_CONFIG); + } else { config_content = strdup(DEFAULT_CONFIG); } save_file(config_file, config_content); diff --git a/src/loader/parser.c b/src/loader/parser.c index 74b0e72..b03c657 100644 --- a/src/loader/parser.c +++ b/src/loader/parser.c @@ -177,17 +177,20 @@ void cleardns_parser(cleardns_config *config, const char *config_content) { // J } void config_parser(cleardns_config *config, const char *config_file) { - char *config_content; + char *config_content = read_file(config_file); + if (is_json_suffix(config_file)) { // JSON format log_info("Start JSON configure parser"); - config_content = read_file(config_file); } else { // YAML or TOML format log_info("Start configure parser"); - config_content = to_json(config_file); // convert to json format - if (config_content == NULL) { + char *convert_ret = to_json(config_content); + if (convert_ret == NULL) { // convert failed log_fatal("Configure parser error"); } + free(config_content); + config_content = convert_ret; } + cleardns_parser(config, config_content); // configure parser free(config_content); log_info("Configure parser success"); From 327ea4c0b4dd0e7de25b6adb9f6e45887388cae1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 11:14:30 +0800 Subject: [PATCH 09/12] remove: rust test code --- .gitignore | 1 - src/to-json/src/ffi.rs | 6 ------ 2 files changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index fd1065e..a591128 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ /build/ /.idea/ /assets/*.txt -/to-json/target/ /cmake-build-debug/ /cmake-build-release/ /src/to-json/target/ diff --git a/src/to-json/src/ffi.rs b/src/to-json/src/ffi.rs index fc0b297..c9569be 100644 --- a/src/to-json/src/ffi.rs +++ b/src/to-json/src/ffi.rs @@ -9,12 +9,6 @@ pub unsafe extern "C" fn free_rust_string(string: *const c_char) { #[no_mangle] pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char { let content: &str = CStr::from_ptr(content).to_str().unwrap(); - - // TODO: remove test code - println!("Raw content -> {}", content); - - // TODO: convert to JSON format let content: String = to_json(content); // may return empty string - CString::new(content).unwrap().into_raw() } From 7cbec1531f43b9550a297e30905cc5bc3804965b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 11:32:54 +0800 Subject: [PATCH 10/12] update: avoid repeat log output --- src/applet/adguard.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/applet/adguard.c b/src/applet/adguard.c index 91f652e..cf70a7c 100644 --- a/src/applet/adguard.c +++ b/src/applet/adguard.c @@ -93,7 +93,6 @@ process* adguard_load(adguard *info, const char *dir) { // load adguard options adguard_config_ret = adguard_config(info, "{}"); // begin with empty json } else { // configure exist -> modify char *adguard_config_content = read_file(adguard_config_file); - log_debug("AdGuardHome raw configure ->\n%s", adguard_config_content); char *adguard_config_json = to_json(adguard_config_content); log_debug("AdGuardHome json configure ->\n%s", adguard_config_json); adguard_config_ret = adguard_config(info, adguard_config_json); From eb064041153febf7dea1708e1bd30b4254b966a3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 16:28:13 +0800 Subject: [PATCH 11/12] update: to-json use staticlib --- src/to-json/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/to-json/Cargo.toml b/src/to-json/Cargo.toml index 4e6f0af..70e5be9 100644 --- a/src/to-json/Cargo.toml +++ b/src/to-json/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [lib] -crate-type = ["cdylib"] +crate-type = ["staticlib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 4c1d1bdeb3f85a553ec8c45e9b073b5c5e51fcc1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 2 Nov 2022 16:51:03 +0800 Subject: [PATCH 12/12] build: link rust library --- Dockerfile | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index aef7928..2c3bd78 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,16 +38,11 @@ RUN env CGO_ENABLED=0 go build -v -trimpath -ldflags "-X main.VersionString=${DN COPY --from=upx /tmp/upx /usr/bin/ RUN upx -9 /tmp/dnsproxy -FROM ${RUST} AS to-json -COPY ./to-json/ /to-json/ -WORKDIR /to-json/ -RUN cargo build --release && mv ./target/release/to-json /tmp/toJSON -COPY --from=upx /tmp/upx /usr/bin/ -RUN upx -9 /tmp/toJSON - -FROM ${ALPINE} AS cleardns +FROM ${RUST} AS cleardns RUN apk add build-base cmake COPY ./ /ClearDNS/ +WORKDIR /ClearDNS/src/to-json/ +RUN cargo build --release WORKDIR /ClearDNS/bin/ RUN cmake -DCMAKE_EXE_LINKER_FLAGS=-static -DCMAKE_BUILD_TYPE=Release .. && make RUN strip cleardns && mv cleardns /tmp/ @@ -63,7 +58,6 @@ COPY --from=adguard /tmp/AdGuardHome /release/usr/bin/ COPY --from=overture /tmp/overture /release/usr/bin/ COPY --from=dnsproxy /tmp/dnsproxy /release/usr/bin/ COPY --from=cleardns /tmp/cleardns /release/usr/bin/ -COPY --from=to-json /tmp/toJSON /release/usr/bin/ FROM ${ALPINE} COPY --from=build /release/ /