Browse Source

Merge branch 'dev'

dev
Dnomd343 2 years ago
parent
commit
5fc6105dd6
  1. 2
      .gitignore
  2. 12
      Dockerfile
  3. 2
      include/common/json.h
  4. 12
      include/to_json.h
  5. 2
      src/CMakeLists.txt
  6. 9
      src/applet/adguard.c
  7. 1
      src/common/CMakeLists.txt
  8. 27
      src/common/json.c
  9. 8
      src/loader/default.c
  10. 11
      src/loader/parser.c
  11. 0
      src/to-json/Cargo.lock
  12. 5
      src/to-json/Cargo.toml
  13. 3
      src/to-json/cbindgen.toml
  14. 14
      src/to-json/src/ffi.rs
  15. 32
      src/to-json/src/json.rs
  16. 2
      src/to-json/src/lib.rs
  17. 57
      to-json/src/main.rs

2
.gitignore

@ -2,6 +2,6 @@
/build/ /build/
/.idea/ /.idea/
/assets/*.txt /assets/*.txt
/to-json/target/
/cmake-build-debug/ /cmake-build-debug/
/cmake-build-release/ /cmake-build-release/
/src/to-json/target/

12
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/ COPY --from=upx /tmp/upx /usr/bin/
RUN upx -9 /tmp/dnsproxy RUN upx -9 /tmp/dnsproxy
FROM ${RUST} AS to-json FROM ${RUST} AS cleardns
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
RUN apk add build-base cmake RUN apk add build-base cmake
COPY ./ /ClearDNS/ COPY ./ /ClearDNS/
WORKDIR /ClearDNS/src/to-json/
RUN cargo build --release
WORKDIR /ClearDNS/bin/ WORKDIR /ClearDNS/bin/
RUN cmake -DCMAKE_EXE_LINKER_FLAGS=-static -DCMAKE_BUILD_TYPE=Release .. && make RUN cmake -DCMAKE_EXE_LINKER_FLAGS=-static -DCMAKE_BUILD_TYPE=Release .. && make
RUN strip cleardns && mv cleardns /tmp/ 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=overture /tmp/overture /release/usr/bin/
COPY --from=dnsproxy /tmp/dnsproxy /release/usr/bin/ COPY --from=dnsproxy /tmp/dnsproxy /release/usr/bin/
COPY --from=cleardns /tmp/cleardns /release/usr/bin/ COPY --from=cleardns /tmp/cleardns /release/usr/bin/
COPY --from=to-json /tmp/toJSON /release/usr/bin/
FROM ${ALPINE} FROM ${ALPINE}
COPY --from=build /release/ / COPY --from=build /release/ /

2
include/common/json.h

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include "cJSON.h" #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); uint8_t is_json_suffix(const char *file_name);
cJSON* json_field_get(cJSON *entry, const char *key); cJSON* json_field_get(cJSON *entry, const char *key);
void json_field_replace(cJSON *entry, const char *key, cJSON *content); void json_field_replace(cJSON *entry, const char *key, cJSON *content);

12
include/to_json.h

@ -0,0 +1,12 @@
#pragma once
/* Generated with cbindgen:0.23.0 */
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
void free_rust_string(const char *string);
const char *to_json_rust(const char *content);

2
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/common)
include_directories(${PROJECT_SOURCE_DIR}/include/loader) include_directories(${PROJECT_SOURCE_DIR}/include/loader)
link_directories(${PROJECT_SOURCE_DIR}/src/to-json/target/release)
add_subdirectory(utils) add_subdirectory(utils)
add_subdirectory(applet) add_subdirectory(applet)
add_subdirectory(bcrypt) add_subdirectory(bcrypt)

9
src/applet/adguard.c

@ -92,9 +92,12 @@ process* adguard_load(adguard *info, const char *dir) { // load adguard options
log_info("Create AdGuardHome configure"); log_info("Create AdGuardHome configure");
adguard_config_ret = adguard_config(info, "{}"); // begin with empty json adguard_config_ret = adguard_config(info, "{}"); // begin with empty json
} else { // configure exist -> modify } else { // configure exist -> modify
char *adguard_config_raw = to_json(adguard_config_file); char *adguard_config_content = read_file(adguard_config_file);
adguard_config_ret = adguard_config(info, adguard_config_raw); char *adguard_config_json = to_json(adguard_config_content);
free(adguard_config_raw); 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 save_file(adguard_config_file, adguard_config_ret); // save modified configure
free(adguard_config_file); free(adguard_config_file);

1
src/common/CMakeLists.txt

@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 2.8.12)
add_library(common json.c sundry.c system.c structure.c) add_library(common json.c sundry.c system.c structure.c)
target_link_libraries(common to_json)

27
src/common/json.c

@ -3,7 +3,7 @@
#include "cJSON.h" #include "cJSON.h"
#include "logger.h" #include "logger.h"
#include "sundry.h" #include "sundry.h"
#include "system.h" #include "to_json.h"
#include "constant.h" #include "constant.h"
#include "structure.h" #include "structure.h"
@ -17,25 +17,14 @@ uint8_t is_json_suffix(const char *file_name) { // whether file name end with `.
return FALSE; return FALSE;
} }
char* to_json(const char *file) { // convert JSON / TOML / YAML to json format (if failed -> return NULL) char* to_json(const char *content) { // convert JSON / TOML / YAML to json format (if failed -> return NULL)
char flag[9]; const char *json_string = to_json_rust(content); // convert to json format
for (int i = 0; i < 8; ++i) { // generate 8-bits flag char *json_content = strdup(json_string); // load string into owner heap
flag[i] = (char)gen_rand_num(10); free_rust_string(json_string); // free rust string
flag[i] += 48; if (strlen(json_content) == 0) { // empty string -> convert error
free(json_content);
return NULL;
} }
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; return json_content;
} }

8
src/loader/default.c

@ -57,12 +57,8 @@ void load_default_config(const char *config_file) {
log_info("Loading default configure file"); log_info("Loading default configure file");
char *config_content = NULL; char *config_content = NULL;
if (is_json_suffix(config_file)) { // convert to json format if (is_json_suffix(config_file)) { // convert to json format
char temp_file[] = "temp.yml"; config_content = to_json(DEFAULT_CONFIG);
save_file(temp_file, DEFAULT_CONFIG); } else {
config_content = to_json(temp_file);
remove_file(temp_file);
}
if (config_content == NULL) {
config_content = strdup(DEFAULT_CONFIG); config_content = strdup(DEFAULT_CONFIG);
} }
save_file(config_file, config_content); save_file(config_file, config_content);

11
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) { 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 if (is_json_suffix(config_file)) { // JSON format
log_info("Start JSON configure parser"); log_info("Start JSON configure parser");
config_content = read_file(config_file);
} else { // YAML or TOML format } else { // YAML or TOML format
log_info("Start configure parser"); log_info("Start configure parser");
config_content = to_json(config_file); // convert to json format char *convert_ret = to_json(config_content);
if (config_content == NULL) { if (convert_ret == NULL) { // convert failed
log_fatal("Configure parser error"); log_fatal("Configure parser error");
} }
free(config_content);
config_content = convert_ret;
} }
cleardns_parser(config, config_content); // configure parser cleardns_parser(config, config_content); // configure parser
free(config_content); free(config_content);
log_info("Configure parser success"); log_info("Configure parser success");

0
to-json/Cargo.lock → src/to-json/Cargo.lock

5
to-json/Cargo.toml → src/to-json/Cargo.toml

@ -3,9 +3,12 @@ name = "to-json"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[lib]
crate-type = ["staticlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde_json = "1.0.86" serde_json = "1.0.87"
serde_yaml = "0.9.14" serde_yaml = "0.9.14"
toml = "0.5.9" toml = "0.5.9"

3
src/to-json/cbindgen.toml

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

14
src/to-json/src/ffi.rs

@ -0,0 +1,14 @@
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) {
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();
let content: String = to_json(content); // may return empty string
CString::new(content).unwrap().into_raw()
}

32
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<Format> {
if let Ok(data) = json::from_str::<json::Value>(content) { // try JSON format
return Some(Format::JSON(data));
}
if let Ok(data) = toml::from_str::<toml::Value>(content) { // try TOML format
return Some(Format::TOML(data));
}
if let Ok(data) = yaml::from_str::<yaml::Value>(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
}
}

2
src/to-json/src/lib.rs

@ -0,0 +1,2 @@
mod ffi;
mod json;

57
to-json/src/main.rs

@ -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<Format> {
if let Ok(data) = json::from_str::<json::Value>(content) { // try JSON format
return Some(Format::JSON(data));
}
if let Ok(data) = toml::from_str::<toml::Value>(content) { // try TOML format
return Some(Format::TOML(data));
}
if let Ok(data) = yaml::from_str::<yaml::Value>(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<String> = 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
}
Loading…
Cancel
Save