From b485d17e6da3882ad43ae31a2335b79bdcde1ac9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 12:48:31 +0800 Subject: [PATCH 01/16] style: constant define --- include/constant.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/constant.h b/include/constant.h index b2b09da..6f064a8 100644 --- a/include/constant.h +++ b/include/constant.h @@ -15,8 +15,6 @@ #define VERSION "1.3.2" #define CONFIG_FILE "cleardns.yml" -#define ADGUARD_USER "admin" -#define ADGUARD_PASSWD "cleardns" #define DNSPROXY_BIN "dnsproxy" #define OVERTURE_BIN "overture" @@ -27,8 +25,11 @@ #define ASSETS_DIR "/cleardns/assets/" #define ADGUARD_DIR "/cleardns/adguard/" -#define UPDATE_CRON "0 4 * * *" -#define ASSETS_PKG "/assets.tar.xz" +#define ADGUARD_USER "admin" +#define ADGUARD_PASSWD "cleardns" + +#define UPDATE_CRON "0 4 * * *" +#define ASSETS_PKG "/assets.tar.xz" #define ASSET_TTL "ttl.txt" #define ASSET_HOSTS "hosts.txt" @@ -36,7 +37,7 @@ #define ASSET_CHINA_IP "china-ip.txt" #define ASSET_CHINA_LIST "chinalist.txt" -#define EXIT_NORMAL 0 +#define EXIT_NORMAL 0 #define EXIT_FORK_ERROR 1 #define EXIT_EXEC_ERROR 2 #define EXIT_WAIT_ERROR 3 From 36aa97a93e1a9b60e05e3419e0e30690e02a953b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 19:58:08 +0800 Subject: [PATCH 02/16] feat: rust-based `toJSON` --- .gitignore | 3 ++- to-json/Cargo.lock | 7 +++++++ to-json/Cargo.toml | 8 ++++++++ to-json/src/main.rs | 3 +++ {toJSON => toJSON-legacy}/go.mod | 0 {toJSON => toJSON-legacy}/go.sum | 0 {toJSON => toJSON-legacy}/main.go | 0 7 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 to-json/Cargo.lock create mode 100644 to-json/Cargo.toml create mode 100644 to-json/src/main.rs rename {toJSON => toJSON-legacy}/go.mod (100%) rename {toJSON => toJSON-legacy}/go.sum (100%) rename {toJSON => toJSON-legacy}/main.go (100%) diff --git a/.gitignore b/.gitignore index e629ffb..927f10c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /bin/ /build/ -**/.idea/ +/.idea/ /assets/*.txt +/to-json/target/ /cmake-build-debug/ /cmake-build-release/ diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock new file mode 100644 index 0000000..3efbc0d --- /dev/null +++ b/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/to-json/Cargo.toml b/to-json/Cargo.toml new file mode 100644 index 0000000..4047338 --- /dev/null +++ b/to-json/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/to-json/src/main.rs b/to-json/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/to-json/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/toJSON/go.mod b/toJSON-legacy/go.mod similarity index 100% rename from toJSON/go.mod rename to toJSON-legacy/go.mod diff --git a/toJSON/go.sum b/toJSON-legacy/go.sum similarity index 100% rename from toJSON/go.sum rename to toJSON-legacy/go.sum diff --git a/toJSON/main.go b/toJSON-legacy/main.go similarity index 100% rename from toJSON/main.go rename to toJSON-legacy/main.go From 4325bee5bfd0037cc58310586f86a92a0824bf2b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 20:26:46 +0800 Subject: [PATCH 03/16] docs: update table --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8b78036..91e4463 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ ClearDNS 支持多种 DNS 协议,首先是常规 DNS ,即基于 UDP 或 TCP 当分流器接到请求时,若在 `chinalist.txt` 中有所匹配,则只请求国内组,若在 `gfwlist.txt` 匹配,则仅请求国外组;两者均未未匹配的情况下,将同时请求国内组与国外组,若国内组返回结果在 `china-ip.txt` 中,则证明 DNS 未被污染,采纳国内组结果,若返回国外 IP 地址,则可能已经被污染,将选取国外组结果。 -由于以上资源数据一直在变动,ClearDNS 内置了更新功能,支持自动获取新的资源文件;数据从多个上游项目收集,每天进行一次合并整理,整合数据的源码可见[此处](./assets/),您可以自由配置更新服务器,或者禁用更新。 +由于以上资源数据一直在变动,ClearDNS 内置了更新功能,支持自动获取新的资源文件;数据从多个上游项目收集,每天进行一次合并整理,整合数据的源码可见[此处](./assets),您可以自由配置更新服务器,或者禁用更新。 ## 配置格式 @@ -287,17 +287,17 @@ assets: ClearDNS 基于 Docker 网络有以下三种部署模式: -| | Host 模式 | Bridge 模式 | Macvlan 模式 | -| :-: | :-: | :-: | :-: | -| 网络原理 | 宿主机网络 | 桥接网络 | 虚拟独立 mac 网卡 | -| 服务 IP | 宿主机 IP | 宿主机 IP | 容器独立 IP | -| 宿主机 IP | 静态 IP 地址 | 静态 IP 地址 | 静态/动态 IP 地址 | -| 宿主机网络 | 无需改动网络配置 | Docker 自动适配 | 手动修改底层网络配置 | -| 宿主机端口 | 占用宿主机 53, 80, 4053, 5353, 6053 端口 | 占用宿主机 53 与 80 端口 | 不占用端口 | -| 管理完整性 | 完全 | 无法区分客户端 | 完全 | -| 宿主机耦合 | 强耦合 | 一般耦合 | 链路层以上完全分离 | -| 网络性能 | 相对较高 | 相对较低 | 相对适中 | -| 部署难度 | 简单 | 简单 | 复杂 | +| | Host 模式 | Bridge 模式 | Macvlan 模式 | +|:----------:|:----------------------------------------:|:------------------------:|:--------------------:| +| 网络原理 | 宿主机网络 | 桥接网络 | 虚拟独立 mac 网卡 | +| 服务 IP | 宿主机 IP | 宿主机 IP | 容器独立 IP | +| 宿主机 IP | 静态 IP 地址 | 静态 IP 地址 | 静态/动态 IP 地址 | +| 宿主机网络 | 无需改动网络配置 | Docker 自动适配 | 手动修改底层网络配置 | +| 宿主机端口 | 占用宿主机 53, 80, 4053, 5353, 6053 端口 | 占用宿主机 53 与 80 端口 | 不占用端口 | +| 管理完整性 | 完全 | 无法区分客户端 | 完全 | +| 宿主机耦合 | 强耦合 | 一般耦合 | 链路层以上完全分离 | +| 网络性能 | 相对较高 | 相对较低 | 相对适中 | +| 部署难度 | 简单 | 简单 | 复杂 | > 不熟悉 Linux 网络配置请勿使用 Macvlan 模式,新手建议选择 Bridge 或 Host 模式。 @@ -465,7 +465,7 @@ ClearDNS 会将数据持久化存储,以在重启 Docker 或宿主机后保留 > 国外组服务器切勿使用常规 DNS 服务,例如 `8.8.8.8` ,由于请求信息为明文,GFW 会抢答回复数据,导致内容仍然受到污染。 -在 `cleardns.yml` 中指定上游 DNS 服务器,国内组可指定国内公共 DNS 服务,国外组需指定可用的加密 DNS 服务,具体说明参考[关于DNS上游](#关于-dns-上游)部分。 +在 `cleardns.yml` 中指定上游 DNS 服务器,国内组可指定国内公共 DNS 服务,国外组需指定可用的加密 DNS 服务,具体说明参考[关于DNS上游](#关于 DNS 上游)部分。 > DNSCrypt 使用 `DNS Stamp` 封装,可以在[这里](https://dnscrypt.info/stamps)在线解析或生成链接内容。 From 6d8088274860f5f592a89d180f882a2b64790a72 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 20:32:19 +0800 Subject: [PATCH 04/16] docs: fix build-in tag --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91e4463..2265587 100644 --- a/README.md +++ b/README.md @@ -465,7 +465,7 @@ ClearDNS 会将数据持久化存储,以在重启 Docker 或宿主机后保留 > 国外组服务器切勿使用常规 DNS 服务,例如 `8.8.8.8` ,由于请求信息为明文,GFW 会抢答回复数据,导致内容仍然受到污染。 -在 `cleardns.yml` 中指定上游 DNS 服务器,国内组可指定国内公共 DNS 服务,国外组需指定可用的加密 DNS 服务,具体说明参考[关于DNS上游](#关于 DNS 上游)部分。 +在 `cleardns.yml` 中指定上游 DNS 服务器,国内组可指定国内公共 DNS 服务,国外组需指定可用的加密 DNS 服务,具体说明参考[关于DNS上游](#关于-DNS-上游)部分。 > DNSCrypt 使用 `DNS Stamp` 封装,可以在[这里](https://dnscrypt.info/stamps)在线解析或生成链接内容。 From e34ddf3d0ab4aa438e2988d2c902057d9ec5e723 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Oct 2022 20:53:13 +0800 Subject: [PATCH 05/16] feat: serde json demo --- to-json/Cargo.lock | 33 +++++++++++++++++++++++++++++++++ to-json/Cargo.toml | 2 ++ to-json/src/main.rs | 21 ++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock index 3efbc0d..e209a54 100644 --- a/to-json/Cargo.lock +++ b/to-json/Cargo.lock @@ -2,6 +2,39 @@ # It is not intended for manual editing. version = 3 +[[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.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" + +[[package]] +name = "serde_json" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "to-json" version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] diff --git a/to-json/Cargo.toml b/to-json/Cargo.toml index 4047338..8653059 100644 --- a/to-json/Cargo.toml +++ b/to-json/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = "1.0.145" +serde_json = "1.0.86" diff --git a/to-json/src/main.rs b/to-json/src/main.rs index e7a11a9..a8f5bef 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,3 +1,22 @@ +use serde_json::{Value}; + +fn json_str() -> String { + let json = r#"{ + "demo": "key_1", + "author": "dnomd343", + "test": [ + "123", + "234", + "345" + ] +}"#; + return String::from(json); +} + fn main() { - println!("Hello, world!"); + let raw = json_str(); + println!("JSON raw content ->\n{}", raw); + + let parsed: Value = serde_json::from_str(&raw[..]).unwrap(); + println!("Author -> {}", parsed["author"]); } From 2632b1bbf254b42f6a9e16e09512659cd3376cb1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 17:59:50 +0800 Subject: [PATCH 06/16] update: json decode and encode demo --- to-json/src/main.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/to-json/src/main.rs b/to-json/src/main.rs index a8f5bef..20144fe 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,7 +1,6 @@ use serde_json::{Value}; -fn json_str() -> String { - let json = r#"{ +const JSON_STR: &str = r#"{ "demo": "key_1", "author": "dnomd343", "test": [ @@ -10,13 +9,13 @@ fn json_str() -> String { "345" ] }"#; - return String::from(json); -} fn main() { - let raw = json_str(); - println!("JSON raw content ->\n{}", raw); + println!("JSON raw content ->\n{}", JSON_STR); + + let data: Value = serde_json::from_str(JSON_STR).unwrap(); + println!("{:#?}", data); - let parsed: Value = serde_json::from_str(&raw[..]).unwrap(); - println!("Author -> {}", parsed["author"]); + let ret = serde_json::to_string(&data).unwrap(); + println!("JSON output ->\n{}", ret); } From 0ae72622b47f0e5fa316e68a0944cc823715dbef Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 18:06:46 +0800 Subject: [PATCH 07/16] feat: add yaml decode demo --- to-json/Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ to-json/Cargo.toml | 1 + to-json/src/main.rs | 18 ++++++++++++++++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock index e209a54..a9f3d1a 100644 --- a/to-json/Cargo.lock +++ b/to-json/Cargo.lock @@ -2,6 +2,28 @@ # 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" @@ -31,10 +53,30 @@ dependencies = [ "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", "serde_json", + "serde_yaml", ] + +[[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 index 8653059..14bf0c0 100644 --- a/to-json/Cargo.toml +++ b/to-json/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] serde = "1.0.145" serde_json = "1.0.86" +serde_yaml = "0.9.14" diff --git a/to-json/src/main.rs b/to-json/src/main.rs index 20144fe..20908bd 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,4 +1,5 @@ -use serde_json::{Value}; +use serde_json::Value as JsonValue; +use serde_yaml::Value as YamlValue; const JSON_STR: &str = r#"{ "demo": "key_1", @@ -10,12 +11,25 @@ const JSON_STR: &str = r#"{ ] }"#; +const YAML_STR: &str = r#" +demo: key_1 +author: dnomd343 +test: + - 123 + - 234 + - 345 +"#; + fn main() { println!("JSON raw content ->\n{}", JSON_STR); + println!("YAML raw content ->\n{}", YAML_STR); - let data: Value = serde_json::from_str(JSON_STR).unwrap(); + // let data: JsonValue = serde_json::from_str(JSON_STR).unwrap(); + // println!("{:#?}", data); + let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap(); println!("{:#?}", data); + let ret = serde_json::to_string(&data).unwrap(); println!("JSON output ->\n{}", ret); } From aeece10839f96023267c99fac56d22a56337f77a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 18:14:25 +0800 Subject: [PATCH 08/16] feat: add toml decode demo --- to-json/Cargo.lock | 10 ++++++++++ to-json/Cargo.toml | 1 + to-json/src/main.rs | 27 +++++++++++++++++++++------ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock index a9f3d1a..b43b84a 100644 --- a/to-json/Cargo.lock +++ b/to-json/Cargo.lock @@ -73,6 +73,16 @@ dependencies = [ "serde", "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]] diff --git a/to-json/Cargo.toml b/to-json/Cargo.toml index 14bf0c0..fbc7b0d 100644 --- a/to-json/Cargo.toml +++ b/to-json/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" serde = "1.0.145" 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 index 20908bd..4252260 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,7 +1,9 @@ use serde_json::Value as JsonValue; use serde_yaml::Value as YamlValue; +use toml::Value as TomlValue; -const JSON_STR: &str = r#"{ +const JSON_STR: &str = r#" +{ "demo": "key_1", "author": "dnomd343", "test": [ @@ -9,7 +11,8 @@ const JSON_STR: &str = r#"{ "234", "345" ] -}"#; +} +"#; const YAML_STR: &str = r#" demo: key_1 @@ -20,16 +23,28 @@ test: - 345 "#; +const TOML_STR: &str = r#" +demo = "key_1" +author = "dnomd343" +test = [ 123, 234, 345 ] +"#; + fn main() { - println!("JSON raw content ->\n{}", JSON_STR); - println!("YAML raw content ->\n{}", YAML_STR); + println!("---------------------------------"); + println!("JSON raw content:\n{}", JSON_STR); + println!("---------------------------------"); + println!("YAML raw content:\n{}", YAML_STR); + println!("---------------------------------"); + println!("TOML raw content:\n{}", TOML_STR); + println!("---------------------------------"); // let data: JsonValue = serde_json::from_str(JSON_STR).unwrap(); // println!("{:#?}", data); - let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap(); + // let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap(); + // println!("{:#?}", data); + let data: TomlValue = toml::from_str(TOML_STR).unwrap(); println!("{:#?}", data); - let ret = serde_json::to_string(&data).unwrap(); println!("JSON output ->\n{}", ret); } From 8e1f7e5f9dfac4c79c6a4294e112c7882e783e09 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 22:26:49 +0800 Subject: [PATCH 09/16] update: framework of to-json --- to-json/src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/to-json/src/main.rs b/to-json/src/main.rs index 4252260..c6a2e85 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,6 +1,5 @@ -use serde_json::Value as JsonValue; -use serde_yaml::Value as YamlValue; -use toml::Value as TomlValue; +use serde_json as json; +use serde_yaml as yaml; const JSON_STR: &str = r#" { @@ -29,6 +28,37 @@ author = "dnomd343" test = [ 123, 234, 345 ] "#; +#[derive(Debug)] +enum Format { + JSON(json::Value), + YAML(yaml::Value), + TOML(toml::Value), +} + +fn parser(raw: &str) -> Option { + if let Ok(data) = json::from_str::(raw) { // try JSON format + return Some(Format::JSON(data)); + } + if let Ok(data) = toml::from_str::(raw) { // try TOML format + return Some(Format::TOML(data)); + } + if let Ok(data) = yaml::from_str::(raw) { // try YAML format + return Some(Format::YAML(data)); + } + return None; +} + +fn to_json(raw: &str) -> String { + match parser(raw) { + 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(1), + } +} + fn main() { println!("---------------------------------"); println!("JSON raw content:\n{}", JSON_STR); @@ -38,13 +68,37 @@ fn main() { println!("TOML raw content:\n{}", TOML_STR); println!("---------------------------------"); - // let data: JsonValue = serde_json::from_str(JSON_STR).unwrap(); - // println!("{:#?}", data); - // let data: YamlValue = serde_yaml::from_str(YAML_STR).unwrap(); + let temp = to_json(JSON_STR); + println!("{}", temp); + + // let a = String::from("ok"); + // match parser(JSON_STR) { + // Some(data) => { + // // println!("{:#?}", data); + // match data { + // Format::JSON(dat) => { + // return json::to_string(&data).unwrap() + // }, + // _ => println!("123"), + // // Format::JSON(dat) => println!("{:#?}", dat), + // // Format::YAML(dat) => println!("{:#?}", dat), + // // Format::TOML(dat) => println!("{:#?}", dat), + // } + // // let temp = data; + // // let ret = json::to_string(&data).unwrap(); + // // println!("JSON output ->\n{}", ret); + // }, + // None => println!("parser error"), + // } + + // let data = parser(JSON_STR); // println!("{:#?}", data); - let data: TomlValue = toml::from_str(TOML_STR).unwrap(); - println!("{:#?}", data); - let ret = serde_json::to_string(&data).unwrap(); - println!("JSON output ->\n{}", ret); + // decode(YAML_STR); + // decode(TOML_STR); + + + + // let ret = serde_json::to_string(&data).unwrap(); + // println!("JSON output ->\n{}", ret); } From 1ec94a51b240b734173b8223017d3ab82ab60737 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 22:57:16 +0800 Subject: [PATCH 10/16] feat: read target file content --- to-json/src/main.rs | 113 +++++++++++++++++--------------------------- to-json/test.json | 9 ++++ to-json/test.toml | 3 ++ to-json/test.yaml | 6 +++ 4 files changed, 62 insertions(+), 69 deletions(-) create mode 100644 to-json/test.json create mode 100644 to-json/test.toml create mode 100644 to-json/test.yaml diff --git a/to-json/src/main.rs b/to-json/src/main.rs index c6a2e85..4f6e808 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,104 +1,79 @@ +use std::fs::{read, read_to_string}; +use std::io::Read; use serde_json as json; use serde_yaml as yaml; -const JSON_STR: &str = r#" -{ - "demo": "key_1", - "author": "dnomd343", - "test": [ - "123", - "234", - "345" - ] -} -"#; - -const YAML_STR: &str = r#" -demo: key_1 -author: dnomd343 -test: - - 123 - - 234 - - 345 -"#; +const FILE_ERROR: i32 = 1; // file open error +const PARSE_ERROR: i32 = 2; // parser error -const TOML_STR: &str = r#" -demo = "key_1" -author = "dnomd343" -test = [ 123, 234, 345 ] -"#; - -#[derive(Debug)] enum Format { JSON(json::Value), YAML(yaml::Value), TOML(toml::Value), } -fn parser(raw: &str) -> Option { - if let Ok(data) = json::from_str::(raw) { // try JSON format +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::(raw) { // try TOML format + if let Ok(data) = toml::from_str::(content) { // try TOML format return Some(Format::TOML(data)); } - if let Ok(data) = yaml::from_str::(raw) { // try YAML format + if let Ok(data) = yaml::from_str::(content) { // try YAML format return Some(Format::YAML(data)); } - return None; + return None; // parse failed } -fn to_json(raw: &str) -> String { - match parser(raw) { +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(1), + 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() { - println!("---------------------------------"); - println!("JSON raw content:\n{}", JSON_STR); - println!("---------------------------------"); - println!("YAML raw content:\n{}", YAML_STR); - println!("---------------------------------"); - println!("TOML raw content:\n{}", TOML_STR); - println!("---------------------------------"); - let temp = to_json(JSON_STR); - println!("{}", temp); + let file_name = "test.json_"; - // let a = String::from("ok"); - // match parser(JSON_STR) { - // Some(data) => { - // // println!("{:#?}", data); - // match data { - // Format::JSON(dat) => { - // return json::to_string(&data).unwrap() - // }, - // _ => println!("123"), - // // Format::JSON(dat) => println!("{:#?}", dat), - // // Format::YAML(dat) => println!("{:#?}", dat), - // // Format::TOML(dat) => println!("{:#?}", dat), - // } - // // let temp = data; - // // let ret = json::to_string(&data).unwrap(); - // // println!("JSON output ->\n{}", ret); - // }, - // None => println!("parser error"), - // } + let content = read_file(file_name); - // let data = parser(JSON_STR); - // println!("{:#?}", data); + println!("{}", content); - // decode(YAML_STR); - // decode(TOML_STR); + // match std::fs::File::open(file_name) { + // Ok(file) => { + // read_to_string() + // }, + // Err(_) => std::process::exit(FILE_ERROR), + // } + // let mut file = std::fs::File::open(file_name).unwrap(); + // let mut contents = String::new(); + // file.read_to_string(&mut contents).unwrap(); + // println!("{}", contents); + // match std::fs::File::open(file_name) { + // Ok(mut file) => { + // let mut contents = String::new(); + // file.read_to_string(&mut contents).unwrap(); + // println!("{}", contents); + // }, + // Err(_) => std::process::exit(FILE_ERROR), + // } - // let ret = serde_json::to_string(&data).unwrap(); - // println!("JSON output ->\n{}", ret); } diff --git a/to-json/test.json b/to-json/test.json new file mode 100644 index 0000000..0873928 --- /dev/null +++ b/to-json/test.json @@ -0,0 +1,9 @@ +{ + "demo": "key_1", + "author": "dnomd343", + "test": [ + 123, + 234, + 345 + ] +} diff --git a/to-json/test.toml b/to-json/test.toml new file mode 100644 index 0000000..5e929d6 --- /dev/null +++ b/to-json/test.toml @@ -0,0 +1,3 @@ +demo = "key_1" +author = "dnomd343" +test = [ 123, 234, 345 ] diff --git a/to-json/test.yaml b/to-json/test.yaml new file mode 100644 index 0000000..09fa3e6 --- /dev/null +++ b/to-json/test.yaml @@ -0,0 +1,6 @@ +demo: key_1 +author: dnomd343 +test: + - 123 + - 234 + - 345 From c04201aab54aa968ffb5e333d687335eecb1fbed Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:07:07 +0800 Subject: [PATCH 11/16] bump: rust-based toJSON --- to-json/src/main.rs | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/to-json/src/main.rs b/to-json/src/main.rs index 4f6e808..07bbabf 100644 --- a/to-json/src/main.rs +++ b/to-json/src/main.rs @@ -1,4 +1,3 @@ -use std::fs::{read, read_to_string}; use std::io::Read; use serde_json as json; use serde_yaml as yaml; @@ -48,32 +47,11 @@ fn read_file(path: &str) -> String { // read file content } fn main() { - - let file_name = "test.json_"; - - let content = read_file(file_name); - - println!("{}", content); - - // match std::fs::File::open(file_name) { - // Ok(file) => { - // read_to_string() - // }, - // Err(_) => std::process::exit(FILE_ERROR), - // } - - // let mut file = std::fs::File::open(file_name).unwrap(); - // let mut contents = String::new(); - // file.read_to_string(&mut contents).unwrap(); - // println!("{}", contents); - - // match std::fs::File::open(file_name) { - // Ok(mut file) => { - // let mut contents = String::new(); - // file.read_to_string(&mut contents).unwrap(); - // println!("{}", contents); - // }, - // Err(_) => std::process::exit(FILE_ERROR), - // } - + 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 1f07e0ea9eeb258ed16291595ae0c241b7310ae2 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:12:13 +0800 Subject: [PATCH 12/16] remove: test file --- to-json/test.json | 9 --------- to-json/test.toml | 3 --- to-json/test.yaml | 6 ------ 3 files changed, 18 deletions(-) delete mode 100644 to-json/test.json delete mode 100644 to-json/test.toml delete mode 100644 to-json/test.yaml diff --git a/to-json/test.json b/to-json/test.json deleted file mode 100644 index 0873928..0000000 --- a/to-json/test.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "demo": "key_1", - "author": "dnomd343", - "test": [ - 123, - 234, - 345 - ] -} diff --git a/to-json/test.toml b/to-json/test.toml deleted file mode 100644 index 5e929d6..0000000 --- a/to-json/test.toml +++ /dev/null @@ -1,3 +0,0 @@ -demo = "key_1" -author = "dnomd343" -test = [ 123, 234, 345 ] diff --git a/to-json/test.yaml b/to-json/test.yaml deleted file mode 100644 index 09fa3e6..0000000 --- a/to-json/test.yaml +++ /dev/null @@ -1,6 +0,0 @@ -demo: key_1 -author: dnomd343 -test: - - 123 - - 234 - - 345 From 36ca25eaae40f84f882c2c7969af42c481461c76 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:14:04 +0800 Subject: [PATCH 13/16] remove: go-based toJSON --- toJSON-legacy/go.mod | 8 -------- toJSON-legacy/go.sum | 6 ------ toJSON-legacy/main.go | 40 ---------------------------------------- 3 files changed, 54 deletions(-) delete mode 100644 toJSON-legacy/go.mod delete mode 100644 toJSON-legacy/go.sum delete mode 100644 toJSON-legacy/main.go diff --git a/toJSON-legacy/go.mod b/toJSON-legacy/go.mod deleted file mode 100644 index 77fd623..0000000 --- a/toJSON-legacy/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module toJSON - -go 1.18 - -require ( - github.com/BurntSushi/toml v1.2.0 - gopkg.in/yaml.v3 v3.0.1 -) diff --git a/toJSON-legacy/go.sum b/toJSON-legacy/go.sum deleted file mode 100644 index 9eb3253..0000000 --- a/toJSON-legacy/go.sum +++ /dev/null @@ -1,6 +0,0 @@ -github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= -github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/toJSON-legacy/main.go b/toJSON-legacy/main.go deleted file mode 100644 index cb3d6ff..0000000 --- a/toJSON-legacy/main.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "github.com/BurntSushi/toml" - "gopkg.in/yaml.v3" - "os" -) - -func parser(raw []byte) (interface{}, interface{}) { - var object interface{} - if err := json.Unmarshal(raw, &object); err == nil { // try json - return object, nil // json format - } - if err := toml.Unmarshal(raw, &object); err == nil { // try toml - return object, nil // toml format - } - if err := yaml.Unmarshal(raw, &object); err == nil { // try yaml - return object, nil // yaml format - } - return nil, nil // parser error -} - -func main() { - if len(os.Args) < 2 { // without argument - fmt.Printf("usage: toJSON [file]\n") - os.Exit(0) - } - raw, err := os.ReadFile(os.Args[1]) - if err != nil { - os.Exit(2) // file open failed - } - if object, err := parser(raw); err == nil { - ret, _ := json.Marshal(object) - fmt.Println(string(ret)) - os.Exit(0) - } - os.Exit(1) // unmarshal failed -} From 33774aa841b00a32cf9bd45304874438eea96318 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:26:30 +0800 Subject: [PATCH 14/16] chore: update serde deps --- to-json/Cargo.lock | 9 ++++----- to-json/Cargo.toml | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/to-json/Cargo.lock b/to-json/Cargo.lock index b43b84a..71b6233 100644 --- a/to-json/Cargo.lock +++ b/to-json/Cargo.lock @@ -38,15 +38,15 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "serde" -version = "1.0.145" +version = "1.0.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" [[package]] name = "serde_json" -version = "1.0.86" +version = "1.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074" +checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" dependencies = [ "itoa", "ryu", @@ -70,7 +70,6 @@ dependencies = [ name = "to-json" version = "0.1.0" dependencies = [ - "serde", "serde_json", "serde_yaml", "toml", diff --git a/to-json/Cargo.toml b/to-json/Cargo.toml index fbc7b0d..2fddb92 100644 --- a/to-json/Cargo.toml +++ b/to-json/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = "1.0.145" serde_json = "1.0.86" serde_yaml = "0.9.14" toml = "0.5.9" From 1ce8303a34b3e9036e8f7b0ca3b083c43d4bc8d3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:38:13 +0800 Subject: [PATCH 15/16] build: rust compile of toJSON --- Dockerfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 304cbcd..e2a2205 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ ARG ALPINE="alpine:3.16" +ARG RUST="rust:1.64-alpine3.16" ARG GOLANG="golang:1.18-alpine3.16" FROM ${ALPINE} AS upx @@ -37,10 +38,10 @@ 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 ${GOLANG} AS toJSON -COPY ./toJSON/ /toJSON/ -WORKDIR /toJSON/ -RUN env CGO_ENABLED=0 go build -v -trimpath -ldflags "-s -w" && mv toJSON /tmp/ +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 @@ -62,7 +63,7 @@ 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=toJSON /tmp/toJSON /release/usr/bin/ +COPY --from=to-json /tmp/toJSON /release/usr/bin/ FROM ${ALPINE} COPY --from=build /release/ / From f941bacdc7e6e661d3f27d61c12ac9b40bb2fc96 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 31 Oct 2022 23:41:01 +0800 Subject: [PATCH 16/16] build: upx version `4.0.0` --- Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index e2a2205..fad9e21 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,10 +3,10 @@ ARG RUST="rust:1.64-alpine3.16" ARG GOLANG="golang:1.18-alpine3.16" FROM ${ALPINE} AS upx -RUN apk add build-base cmake git -RUN git clone https://github.com/dnomd343/upx.git --depth=1 -WORKDIR ./upx/ -RUN git submodule update --init && rm -rf ./.git/ +RUN apk add build-base cmake +ENV UPX="4.0.0" +RUN wget https://github.com/upx/upx/releases/download/v${UPX}/upx-${UPX}-src.tar.xz && tar xf upx-${UPX}-src.tar.xz +WORKDIR ./upx-${UPX}-src/ RUN make UPX_CMAKE_CONFIG_FLAGS=-DCMAKE_EXE_LINKER_FLAGS=-static WORKDIR ./build/release/ RUN strip upx && mv upx /tmp/