Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
67f925f160 | |||
7a22af7598 | |||
73435ae70a | |||
9bf2103c47 | |||
1179840114 | |||
5e3ec48259 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
/.env.ps1
|
/.env.ps1
|
||||||
/.vscode
|
/.vscode
|
||||||
|
/.idea
|
||||||
|
|
||||||
|
557
Cargo.lock
generated
557
Cargo.lock
generated
@ -92,9 +92,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.91"
|
version = "1.0.90"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8"
|
checksum = "37bf3594c4c988a53154954629820791dde498571819ae4ca50ca811e060cc95"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-stream"
|
name = "async-stream"
|
||||||
@ -145,12 +145,37 @@ version = "2.6.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "block-buffer"
|
||||||
|
version = "0.10.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
||||||
|
dependencies = [
|
||||||
|
"generic-array",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bstr"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.16.0"
|
version = "3.16.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
@ -186,6 +211,28 @@ dependencies = [
|
|||||||
"windows-targets",
|
"windows-targets",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chrono-tz"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"chrono-tz-build",
|
||||||
|
"phf",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "chrono-tz-build"
|
||||||
|
version = "0.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1"
|
||||||
|
dependencies = [
|
||||||
|
"parse-zoneinfo",
|
||||||
|
"phf",
|
||||||
|
"phf_codegen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "4.5.20"
|
version = "4.5.20"
|
||||||
@ -238,6 +285,66 @@ version = "0.8.7"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpufeatures"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-deque"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-epoch",
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-epoch"
|
||||||
|
version = "0.9.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-utils"
|
||||||
|
version = "0.8.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crypto-common"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
|
||||||
|
dependencies = [
|
||||||
|
"generic-array",
|
||||||
|
"typenum",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deunicode"
|
||||||
|
version = "1.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "digest"
|
||||||
|
version = "0.10.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
||||||
|
dependencies = [
|
||||||
|
"block-buffer",
|
||||||
|
"crypto-common",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "encoding"
|
name = "encoding"
|
||||||
version = "0.2.33"
|
version = "0.2.33"
|
||||||
@ -406,12 +513,57 @@ dependencies = [
|
|||||||
"slab",
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "generic-array"
|
||||||
|
version = "0.14.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
||||||
|
dependencies = [
|
||||||
|
"typenum",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gimli"
|
name = "gimli"
|
||||||
version = "0.31.1"
|
version = "0.31.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "globset"
|
||||||
|
version = "0.4.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"bstr",
|
||||||
|
"log",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "globwalk"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"ignore",
|
||||||
|
"walkdir",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@ -424,6 +576,15 @@ version = "0.3.9"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humansize"
|
||||||
|
version = "2.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
|
||||||
|
dependencies = [
|
||||||
|
"libm",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iana-time-zone"
|
name = "iana-time-zone"
|
||||||
version = "0.1.61"
|
version = "0.1.61"
|
||||||
@ -447,12 +608,34 @@ dependencies = [
|
|||||||
"cc",
|
"cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ignore"
|
||||||
|
version = "0.4.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-deque",
|
||||||
|
"globset",
|
||||||
|
"log",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"same-file",
|
||||||
|
"walkdir",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "is_terminal_polyfill"
|
name = "is_terminal_polyfill"
|
||||||
version = "1.70.1"
|
version = "1.70.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.72"
|
version = "0.3.72"
|
||||||
@ -474,6 +657,12 @@ version = "0.2.161"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
|
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libm"
|
||||||
|
version = "0.2.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.22"
|
version = "0.4.22"
|
||||||
@ -570,7 +759,16 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "peazyrsa-srv"
|
name = "parse-zoneinfo"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24"
|
||||||
|
dependencies = [
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "peazyrsa"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
@ -584,9 +782,99 @@ dependencies = [
|
|||||||
"lazy_static",
|
"lazy_static",
|
||||||
"openssl",
|
"openssl",
|
||||||
"regex",
|
"regex",
|
||||||
|
"tera",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "percent-encoding"
|
||||||
|
version = "2.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pest"
|
||||||
|
version = "2.7.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "879952a81a83930934cbf1786752d6dedc3b1f29e8f8fb2ad1d0a36f377cf442"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"thiserror",
|
||||||
|
"ucd-trie",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pest_derive"
|
||||||
|
version = "2.7.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d214365f632b123a47fd913301e14c946c61d1c183ee245fa76eb752e59a02dd"
|
||||||
|
dependencies = [
|
||||||
|
"pest",
|
||||||
|
"pest_generator",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pest_generator"
|
||||||
|
version = "2.7.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eb55586734301717aea2ac313f50b2eb8f60d2fc3dc01d190eefa2e625f60c4e"
|
||||||
|
dependencies = [
|
||||||
|
"pest",
|
||||||
|
"pest_meta",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pest_meta"
|
||||||
|
version = "2.7.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b75da2a70cf4d9cb76833c990ac9cd3923c9a8905a8929789ce347c84564d03d"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
"pest",
|
||||||
|
"sha2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "phf"
|
||||||
|
version = "0.11.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
|
||||||
|
dependencies = [
|
||||||
|
"phf_shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "phf_codegen"
|
||||||
|
version = "0.11.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a"
|
||||||
|
dependencies = [
|
||||||
|
"phf_generator",
|
||||||
|
"phf_shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "phf_generator"
|
||||||
|
version = "0.11.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
|
||||||
|
dependencies = [
|
||||||
|
"phf_shared",
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "phf_shared"
|
||||||
|
version = "0.11.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
|
||||||
|
dependencies = [
|
||||||
|
"siphasher",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pin-project-lite"
|
name = "pin-project-lite"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
@ -605,6 +893,15 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
|
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.88"
|
version = "1.0.88"
|
||||||
@ -623,6 +920,36 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex"
|
name = "regex"
|
||||||
version = "1.11.0"
|
version = "1.11.0"
|
||||||
@ -658,6 +985,64 @@ version = "0.1.24"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.213"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.213"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.132"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sha2"
|
||||||
|
version = "0.10.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"cpufeatures",
|
||||||
|
"digest",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shlex"
|
name = "shlex"
|
||||||
version = "1.3.0"
|
version = "1.3.0"
|
||||||
@ -673,6 +1058,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "siphasher"
|
||||||
|
version = "0.3.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slab"
|
name = "slab"
|
||||||
version = "0.4.9"
|
version = "0.4.9"
|
||||||
@ -682,6 +1073,16 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "slug"
|
||||||
|
version = "0.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "882a80f72ee45de3cc9a5afeb2da0331d58df69e4e7d8eeb5d3c7784ae67e724"
|
||||||
|
dependencies = [
|
||||||
|
"deunicode",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
@ -699,6 +1100,48 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tera"
|
||||||
|
version = "1.20.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ab9d851b45e865f178319da0abdbfe6acbc4328759ff18dafc3a41c16b4cd2ee"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"chrono-tz",
|
||||||
|
"globwalk",
|
||||||
|
"humansize",
|
||||||
|
"lazy_static",
|
||||||
|
"percent-encoding",
|
||||||
|
"pest",
|
||||||
|
"pest_derive",
|
||||||
|
"rand",
|
||||||
|
"regex",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"slug",
|
||||||
|
"unic-segment",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.65"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.65"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.41.0"
|
version = "1.41.0"
|
||||||
@ -726,6 +1169,68 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "typenum"
|
||||||
|
version = "1.17.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ucd-trie"
|
||||||
|
version = "0.1.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-char-property"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a8c57a407d9b6fa02b4795eb81c5b6652060a15a7903ea981f3d723e6c0be221"
|
||||||
|
dependencies = [
|
||||||
|
"unic-char-range",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-char-range"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0398022d5f700414f6b899e10b8348231abf9173fa93144cbc1a43b9793c1fbc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-common"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "80d7ff825a6a654ee85a63e80f92f054f904f21e7d12da4e22f9834a4aaa35bc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-segment"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e4ed5d26be57f84f176157270c112ef57b86debac9cd21daaabbe56db0f88f23"
|
||||||
|
dependencies = [
|
||||||
|
"unic-ucd-segment",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-ucd-segment"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2079c122a62205b421f499da10f3ee0f7697f012f55b675e002483c73ea34700"
|
||||||
|
dependencies = [
|
||||||
|
"unic-char-property",
|
||||||
|
"unic-char-range",
|
||||||
|
"unic-ucd-version",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unic-ucd-version"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "96bd2f2237fe450fcd0a1d2f5f4e91711124f7857ba2e964247776ebeeb7b0c4"
|
||||||
|
dependencies = [
|
||||||
|
"unic-common",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.13"
|
version = "1.0.13"
|
||||||
@ -744,6 +1249,22 @@ version = "0.2.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "walkdir"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||||
|
dependencies = [
|
||||||
|
"same-file",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
@ -805,6 +1326,15 @@ version = "0.2.95"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
|
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-core"
|
name = "windows-core"
|
||||||
version = "0.52.0"
|
version = "0.52.0"
|
||||||
@ -886,3 +1416,24 @@ name = "windows_x86_64_msvc"
|
|||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
|
dependencies = [
|
||||||
|
"byteorder",
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "peazyrsa-srv"
|
name = "peazyrsa"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "peazysrv"
|
|
||||||
path = "src/main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.91"
|
anyhow = "1.0.90"
|
||||||
async-stream = "0.3.6"
|
async-stream = "0.3.6"
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
clap = { version = "4.5.20", features = ["derive"] }
|
clap = { version = "4.5.20", features = ["derive"] }
|
||||||
@ -19,6 +15,7 @@ futures-util = "0.3.31"
|
|||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
openssl = { version="0.10.68" }
|
openssl = { version="0.10.68" }
|
||||||
regex = "1.11.0"
|
regex = "1.11.0"
|
||||||
|
tera = "1.20.0"
|
||||||
tokio = { version = "1.41.0", features = ["fs", "rt", "process", "macros", "io-util"] }
|
tokio = { version = "1.41.0", features = ["fs", "rt", "process", "macros", "io-util"] }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
69
src/arcstr.rs
Normal file
69
src/arcstr.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
use std::ffi::OsStr;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Ord, PartialOrd, Eq)]
|
||||||
|
pub(crate) struct ArcStr {
|
||||||
|
inner: Arc<str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for ArcStr {
|
||||||
|
type Target = str;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.inner.deref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for ArcStr {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::from(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(suspicious_double_ref_op)]
|
||||||
|
impl From<&&str> for ArcStr {
|
||||||
|
fn from(value: &&str) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::from(value.deref()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<OsStr> for ArcStr {
|
||||||
|
fn as_ref(&self) -> &OsStr {
|
||||||
|
self.inner.as_ref().as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<Path> for ArcStr {
|
||||||
|
fn as_ref(&self) -> &Path {
|
||||||
|
self.inner.as_ref().as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<ArcStr> for ArcStr {
|
||||||
|
fn as_ref(&self) -> &ArcStr {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ArcStr {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.deref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ArcStr {
|
||||||
|
pub(crate) fn as_path(&self) -> &Path {
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn as_str(&self) -> &str {
|
||||||
|
self.inner.deref()
|
||||||
|
}
|
||||||
|
}
|
35
src/certs.rs
35
src/certs.rs
@ -1,15 +1,15 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
use crate::common::AppConfig;
|
use crate::common::AppConfig;
|
||||||
use crate::crypto_provider::ICryptoProvider;
|
use crate::crypto_provider::ICryptoProvider;
|
||||||
use crate::openssl::internal::OpenSSLInternalProvider;
|
|
||||||
|
|
||||||
pub(crate) struct Certs<T>
|
pub(crate) struct Certs<T>
|
||||||
where
|
where
|
||||||
T: ICryptoProvider,
|
T: ICryptoProvider,
|
||||||
{
|
{
|
||||||
|
pub(crate) ca_file: PathBuf,
|
||||||
pub(crate) key_file: PathBuf,
|
pub(crate) key_file: PathBuf,
|
||||||
pub(crate) cert_file: PathBuf,
|
pub(crate) cert_file: PathBuf,
|
||||||
pub(crate) provider: Arc<T>,
|
pub(crate) provider: Arc<T>,
|
||||||
@ -20,14 +20,13 @@ where
|
|||||||
T: ICryptoProvider,
|
T: ICryptoProvider,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(cfg: &AppConfig, provider: T) -> Self {
|
pub(crate) fn new(cfg: &AppConfig, provider: T) -> Self {
|
||||||
let base_dir = PathBuf::from(&cfg.base_directory);
|
let provider = Arc::new(provider);
|
||||||
let keys_dir = base_dir.clone();
|
|
||||||
let name = cfg.name.clone();
|
|
||||||
|
|
||||||
Certs {
|
Certs {
|
||||||
key_file: keys_dir.join(format!("{}.key", &name)),
|
ca_file: cfg.ca_filepath.clone(),
|
||||||
cert_file: keys_dir.join(format!("{}.crt", &name)),
|
key_file: cfg.key_filepath.clone(),
|
||||||
provider: Arc::new(provider),
|
cert_file: cfg.cert_filepath.clone(),
|
||||||
|
provider,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,23 +37,11 @@ where
|
|||||||
pub(crate) async fn sign(&self) -> Result<()> {
|
pub(crate) async fn sign(&self) -> Result<()> {
|
||||||
self.provider.sign().await
|
self.provider.sign().await
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn build_cert(config: &AppConfig) -> Result<()> {
|
pub(crate) async fn build_all(&self) -> Result<()> {
|
||||||
let certs = Certs::new(config, OpenSSLInternalProvider::from_cfg(config));
|
self.request().await.context("request")?;
|
||||||
certs.request().await?;
|
self.sign().await.context("sign")?;
|
||||||
certs.sign().await?;
|
|
||||||
|
|
||||||
let key_file = certs
|
|
||||||
.key_file
|
|
||||||
.to_str()
|
|
||||||
.ok_or(anyhow!("key_file PathBuf to str convert error"))?;
|
|
||||||
|
|
||||||
let cert_file = certs
|
|
||||||
.cert_file
|
|
||||||
.to_str()
|
|
||||||
.ok_or(anyhow!("req_file PathBuf to str convert error"))?;
|
|
||||||
|
|
||||||
println!("created: \n key: {key_file},\n cert: {cert_file}");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
239
src/common.rs
239
src/common.rs
@ -1,24 +1,32 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use async_stream::stream;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use encoding::label::encoding_from_whatwg_label;
|
use encoding::{label::encoding_from_whatwg_label, EncoderTrap};
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::FromStr,
|
|
||||||
};
|
};
|
||||||
use tokio::fs;
|
use tokio::{
|
||||||
|
fs::{self, File},
|
||||||
|
io::{AsyncBufReadExt, BufReader},
|
||||||
|
};
|
||||||
|
|
||||||
|
use futures_core::stream::Stream;
|
||||||
|
|
||||||
|
pub(crate) const UTF8_STR: &str = "utf8";
|
||||||
|
pub(crate) const DEFAULT_ENCODING: &str = UTF8_STR;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum OpenSSLProviderArg {
|
pub enum OpenSSLProviderArg {
|
||||||
Internal,
|
Internal,
|
||||||
|
ExternalBin(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for OpenSSLProviderArg {
|
impl From<Option<&String>> for OpenSSLProviderArg {
|
||||||
type Err = anyhow::Error;
|
fn from(value: Option<&String>) -> Self {
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
match value {
|
||||||
match s.to_ascii_lowercase().as_str() {
|
Some(x) => OpenSSLProviderArg::ExternalBin(x.clone()),
|
||||||
"internal" => Ok(OpenSSLProviderArg::Internal),
|
_ => OpenSSLProviderArg::Internal,
|
||||||
&_ => todo!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,7 +34,8 @@ impl FromStr for OpenSSLProviderArg {
|
|||||||
impl Display for OpenSSLProviderArg {
|
impl Display for OpenSSLProviderArg {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
OpenSSLProviderArg::Internal => write!(f, "internal"),
|
OpenSSLProviderArg::ExternalBin(x) => write!(f, "{}", x),
|
||||||
|
_ => write!(f, "internal"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,80 +58,78 @@ pub(crate) struct Args {
|
|||||||
#[arg(short = 'c', long)]
|
#[arg(short = 'c', long)]
|
||||||
pub(crate) encoding: Option<String>,
|
pub(crate) encoding: Option<String>,
|
||||||
|
|
||||||
|
/// keys subdir
|
||||||
|
#[arg(long, default_value = "keys")]
|
||||||
|
pub(crate) keys_dir: String,
|
||||||
|
|
||||||
|
/// config subdir
|
||||||
|
#[arg(long, default_value = "config")]
|
||||||
|
pub(crate) config_dir: String,
|
||||||
|
|
||||||
/// valid days
|
/// valid days
|
||||||
#[arg(long, default_value = "3650")]
|
#[arg(long, default_value = "3650")]
|
||||||
pub(crate) days: u32,
|
pub(crate) days: u32,
|
||||||
|
|
||||||
/// dns name
|
/// use openssl binary
|
||||||
#[arg(short = 'n', long)]
|
#[arg(long = "with-openssl", short)]
|
||||||
pub(crate) dns: Vec<String>,
|
pub(crate) openssl: Option<String>,
|
||||||
|
|
||||||
/// IP address
|
/// template file
|
||||||
#[arg(short, long)]
|
#[arg(long, default_value = "template.ovpn")]
|
||||||
pub(crate) ip: Vec<String>,
|
pub(crate) template_file: String,
|
||||||
|
|
||||||
/// Country
|
|
||||||
#[arg(long)]
|
|
||||||
pub(crate) country: Option<String>,
|
|
||||||
|
|
||||||
/// Province
|
|
||||||
#[arg(long)]
|
|
||||||
pub(crate) province: Option<String>,
|
|
||||||
|
|
||||||
/// Location
|
|
||||||
#[arg(long)]
|
|
||||||
pub(crate) loc: Option<String>,
|
|
||||||
|
|
||||||
/// Organization
|
|
||||||
#[arg(long)]
|
|
||||||
pub(crate) org: Option<String>,
|
|
||||||
|
|
||||||
/// Organization Unit
|
|
||||||
#[arg(long)]
|
|
||||||
pub(crate) ou: Option<String>,
|
|
||||||
|
|
||||||
/// Key size
|
|
||||||
#[arg(long, default_value = "2048")]
|
|
||||||
pub(crate) key_size: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct AppConfig {
|
pub(crate) struct AppConfig {
|
||||||
pub(crate) encoding: String,
|
pub(crate) encoding: String,
|
||||||
pub(crate) req_days: u32,
|
pub(crate) req_days: u32,
|
||||||
pub(crate) ca_filename: String,
|
pub(crate) template_file: PathBuf,
|
||||||
|
pub(crate) openssl_default_cnf: String,
|
||||||
|
pub(crate) openssl_cnf_env: String,
|
||||||
|
pub(crate) ca_filepath: PathBuf,
|
||||||
|
pub(crate) ca_key_filepath: PathBuf,
|
||||||
pub(crate) default_email_domain: String,
|
pub(crate) default_email_domain: String,
|
||||||
#[allow(unused)]
|
pub(crate) openssl: OpenSSLProviderArg,
|
||||||
pub(crate) base_directory: String,
|
pub(crate) base_directory: PathBuf,
|
||||||
pub(crate) email: String,
|
pub(crate) email: String,
|
||||||
pub(crate) name: String,
|
pub(crate) name: String,
|
||||||
pub(crate) dns: Vec<String>,
|
pub(crate) conf_filepath: PathBuf,
|
||||||
pub(crate) ip: Vec<String>,
|
pub(crate) req_filepath: PathBuf,
|
||||||
pub(crate) country: Option<String>,
|
pub(crate) key_filepath: PathBuf,
|
||||||
pub(crate) province: Option<String>,
|
pub(crate) cert_filepath: PathBuf,
|
||||||
pub(crate) loc: Option<String>,
|
|
||||||
pub(crate) org: Option<String>,
|
|
||||||
pub(crate) ou: Option<String>,
|
|
||||||
pub(crate) key_size: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppConfig {
|
impl Default for AppConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let name = String::from("user");
|
||||||
|
let base_directory: PathBuf = ".".into();
|
||||||
|
let keys_directory = base_directory.join("keys");
|
||||||
|
let config_directory = base_directory.join("config");
|
||||||
|
let template_file = base_directory.join("template.ovpn");
|
||||||
|
let openssl_default_cnf = String::from("openssl-1.0.0.cnf");
|
||||||
|
let ca_filepath = keys_directory.join("ca.crt");
|
||||||
|
let ca_key_filepath = keys_directory.join("ca.key");
|
||||||
|
let req_filepath = keys_directory.join(format!("{}.csr", &name));
|
||||||
|
let key_filepath = keys_directory.join(format!("{}.key", &name));
|
||||||
|
let cert_filepath = keys_directory.join(format!("{}.crt", &name));
|
||||||
|
let conf_filepath = config_directory.join(format!("{}.ovpn", &name));
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
encoding: "cp866".into(),
|
encoding: DEFAULT_ENCODING.into(),
|
||||||
req_days: 30650,
|
req_days: 30650,
|
||||||
ca_filename: "ca.crt".into(),
|
template_file,
|
||||||
|
conf_filepath,
|
||||||
|
req_filepath,
|
||||||
|
key_filepath,
|
||||||
|
cert_filepath,
|
||||||
|
openssl_default_cnf,
|
||||||
|
openssl_cnf_env: "KEY_CONFIG".into(),
|
||||||
|
ca_filepath,
|
||||||
|
ca_key_filepath,
|
||||||
default_email_domain: "example.com".into(),
|
default_email_domain: "example.com".into(),
|
||||||
base_directory: ".".into(),
|
openssl: OpenSSLProviderArg::Internal,
|
||||||
|
base_directory,
|
||||||
email: "name@example.com".into(),
|
email: "name@example.com".into(),
|
||||||
name: "user".into(),
|
name,
|
||||||
dns: Vec::new(),
|
|
||||||
ip: Vec::new(),
|
|
||||||
country: None,
|
|
||||||
province: None,
|
|
||||||
loc: None,
|
|
||||||
org: None,
|
|
||||||
ou: None,
|
|
||||||
key_size: 2048,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,11 +138,25 @@ impl From<&Args> for AppConfig {
|
|||||||
fn from(args: &Args) -> Self {
|
fn from(args: &Args) -> Self {
|
||||||
let defaults = Self::default();
|
let defaults = Self::default();
|
||||||
|
|
||||||
|
let name = args.name.clone();
|
||||||
let base_directory = args
|
let base_directory = args
|
||||||
.directory
|
.directory
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap_or(&defaults.base_directory)
|
.map(PathBuf::from)
|
||||||
|
.unwrap_or(defaults.base_directory)
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
|
let keys_directory = base_directory.join(&args.keys_dir);
|
||||||
|
let config_directory = base_directory.join(&args.config_dir);
|
||||||
|
let template_file = base_directory.join(&args.template_file);
|
||||||
|
let openssl_default_cnf = String::from("openssl-1.0.0.cnf");
|
||||||
|
let ca_filepath = keys_directory.join("ca.crt");
|
||||||
|
let ca_key_filepath = keys_directory.join("ca.key");
|
||||||
|
let req_filepath = keys_directory.join(format!("{}.csr", &name));
|
||||||
|
let key_filepath = keys_directory.join(format!("{}.key", &name));
|
||||||
|
let cert_filepath = keys_directory.join(format!("{}.crt", &name));
|
||||||
|
let conf_filepath = config_directory.join(format!("{}.ovpn", &name));
|
||||||
|
|
||||||
let email = args.email.clone().unwrap_or(format!(
|
let email = args.email.clone().unwrap_or(format!(
|
||||||
"{}@{}",
|
"{}@{}",
|
||||||
&args.name,
|
&args.name,
|
||||||
@ -147,62 +168,88 @@ impl From<&Args> for AppConfig {
|
|||||||
defaults.encoding.clone()
|
defaults.encoding.clone()
|
||||||
};
|
};
|
||||||
let name = args.name.clone();
|
let name = args.name.clone();
|
||||||
|
let openssl: OpenSSLProviderArg = args.openssl.as_ref().into();
|
||||||
let req_days = args.days;
|
let req_days = args.days;
|
||||||
let (dns, ip, country, province, loc, org, ou) = (
|
|
||||||
args.dns.clone(),
|
|
||||||
args.ip.clone(),
|
|
||||||
args.country.clone(),
|
|
||||||
args.province.clone(),
|
|
||||||
args.loc.clone(),
|
|
||||||
args.org.clone(),
|
|
||||||
args.ou.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
base_directory,
|
base_directory,
|
||||||
|
template_file,
|
||||||
|
openssl_default_cnf,
|
||||||
|
ca_filepath,
|
||||||
|
ca_key_filepath,
|
||||||
|
req_filepath,
|
||||||
|
key_filepath,
|
||||||
|
cert_filepath,
|
||||||
|
conf_filepath,
|
||||||
email,
|
email,
|
||||||
encoding,
|
encoding,
|
||||||
name,
|
name,
|
||||||
|
openssl,
|
||||||
req_days,
|
req_days,
|
||||||
dns,
|
|
||||||
ip,
|
|
||||||
country,
|
|
||||||
province,
|
|
||||||
loc,
|
|
||||||
org,
|
|
||||||
ou,
|
|
||||||
key_size: args.key_size,
|
|
||||||
..defaults
|
..defaults
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn is_file_exist(filepath: &PathBuf) -> bool {
|
pub(crate) async fn is_file_exist(filepath: &PathBuf) -> bool {
|
||||||
let metadata = tokio::fs::metadata(&filepath).await;
|
match tokio::fs::metadata(&filepath).await {
|
||||||
if metadata.is_err() {
|
Ok(x) => x.is_file(),
|
||||||
return false;
|
_ => false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !metadata.unwrap().is_file() {
|
pub(crate) async fn read_file<P>(filepath: P, encoding: &str) -> Result<String>
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn read_file<'a, S, P>(filepath: P, encoding: S) -> Result<String>
|
|
||||||
where
|
where
|
||||||
S: AsRef<str> + std::cmp::PartialEq<&'a str>,
|
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
let filepath = PathBuf::from(filepath.as_ref());
|
let filepath = PathBuf::from(filepath.as_ref());
|
||||||
if encoding == "utf8" {
|
if encoding == UTF8_STR {
|
||||||
return Ok(fs::read_to_string(filepath).await?);
|
return Ok(fs::read_to_string(filepath).await?);
|
||||||
}
|
}
|
||||||
|
|
||||||
let enc = encoding_from_whatwg_label(encoding.as_ref()).ok_or(anyhow!("encoding not found"))?;
|
let enc = encoding_from_whatwg_label(encoding).ok_or(anyhow!("encoding not found"))?;
|
||||||
|
|
||||||
let bytes = fs::read(filepath).await?;
|
let bytes = fs::read(filepath).await?;
|
||||||
enc.decode(&bytes, encoding::DecoderTrap::Ignore)
|
enc.decode(&bytes, encoding::DecoderTrap::Ignore)
|
||||||
.map_err(|_| anyhow!("could not read file"))
|
.map_err(|_| anyhow!("could not read file"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn write_file<P: AsRef<Path>>(
|
||||||
|
filepath: P,
|
||||||
|
text: &str,
|
||||||
|
encoding: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
if encoding == UTF8_STR {
|
||||||
|
return Ok(fs::write(filepath, text).await?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let enc = encoding_from_whatwg_label(encoding).ok_or(anyhow!("encoding not found"))?;
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
enc.encode_to(text, EncoderTrap::Ignore, &mut bytes)
|
||||||
|
.map_err(|e| anyhow!("can't encode: {:?}", e))?;
|
||||||
|
|
||||||
|
fs::write(filepath, bytes).await.context("can't write file")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn read_file_by_lines<P: AsRef<Path>>(
|
||||||
|
filepath: P,
|
||||||
|
encoding: &str,
|
||||||
|
) -> Result<Box<dyn Stream<Item = String>>> {
|
||||||
|
Ok(if encoding == UTF8_STR {
|
||||||
|
let f = File::open(filepath).await?;
|
||||||
|
let reader = BufReader::new(f);
|
||||||
|
let mut lines = reader.lines();
|
||||||
|
Box::new(stream! {
|
||||||
|
while let Ok(Some(line)) = lines.next_line().await {
|
||||||
|
yield line
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
let text = read_file(filepath, encoding).await?;
|
||||||
|
Box::new(stream! {
|
||||||
|
for line in text.lines() {
|
||||||
|
yield line.to_string()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
59
src/main.rs
59
src/main.rs
@ -1,18 +1,71 @@
|
|||||||
use anyhow::Result;
|
use anyhow::{anyhow, Context, Result};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use common::{is_file_exist, OpenSSLProviderArg};
|
||||||
|
use crypto_provider::ICryptoProvider;
|
||||||
|
|
||||||
|
mod arcstr;
|
||||||
mod certs;
|
mod certs;
|
||||||
mod common;
|
mod common;
|
||||||
mod crypto_provider;
|
mod crypto_provider;
|
||||||
mod openssl;
|
mod openssl;
|
||||||
|
mod ovpn;
|
||||||
|
mod vars;
|
||||||
|
|
||||||
use crate::certs::build_cert;
|
use crate::certs::Certs;
|
||||||
use crate::common::{AppConfig, Args};
|
use crate::common::{AppConfig, Args};
|
||||||
|
use crate::openssl::{external::OpenSSLExternalProvider, internal::OpenSSLInternalProvider};
|
||||||
|
use crate::ovpn::OvpnConfig;
|
||||||
|
use crate::vars::{VarsFile, VarsMap};
|
||||||
|
|
||||||
|
async fn build_client_with<T: ICryptoProvider>(config: &AppConfig, provider: T) -> Result<String> {
|
||||||
|
let config_file = config.conf_filepath.clone();
|
||||||
|
let config_file_str = config_file
|
||||||
|
.to_str()
|
||||||
|
.ok_or(anyhow!("config file exist err"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
if is_file_exist(&config_file).await {
|
||||||
|
return Err(anyhow!("Config file exist: {}", &config_file_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
let certs = Certs::new(config, provider);
|
||||||
|
OvpnConfig::try_from_certs(certs, config)
|
||||||
|
.await?
|
||||||
|
.render()?
|
||||||
|
.to_file(&config_file)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(config_file_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn build_client_config(config: &AppConfig, vars: VarsMap) -> Result<String> {
|
||||||
|
match config.openssl {
|
||||||
|
OpenSSLProviderArg::Internal => {
|
||||||
|
let provider = OpenSSLInternalProvider::try_from_cfg(config, vars)?;
|
||||||
|
build_client_with(config, provider).await
|
||||||
|
}
|
||||||
|
OpenSSLProviderArg::ExternalBin(_) => {
|
||||||
|
let provider = OpenSSLExternalProvider::from_cfg(config, vars);
|
||||||
|
build_client_with(config, provider).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let config = AppConfig::from(&args);
|
let config = AppConfig::from(&args);
|
||||||
|
let vars = VarsFile::from_config(&config)
|
||||||
|
.await
|
||||||
|
.context("vars from config")?
|
||||||
|
.parse()
|
||||||
|
.await
|
||||||
|
.context("parse vars")?;
|
||||||
|
|
||||||
build_cert(&config).await
|
println!("loaded: {:#?}", &vars);
|
||||||
|
|
||||||
|
let config_file = build_client_config(&config, vars).await?;
|
||||||
|
println!("created: {}", &config_file);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
136
src/openssl/external.rs
Normal file
136
src/openssl/external.rs
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use tokio::process::Command;
|
||||||
|
|
||||||
|
use crate::common::{is_file_exist, AppConfig};
|
||||||
|
use crate::crypto_provider::ICryptoProvider;
|
||||||
|
use crate::vars::{IStrMap, VarsMap};
|
||||||
|
|
||||||
|
pub(crate) struct OpenSSLExternalProvider {
|
||||||
|
vars: VarsMap,
|
||||||
|
base_dir: PathBuf,
|
||||||
|
openssl_cnf: PathBuf,
|
||||||
|
openssl: String,
|
||||||
|
ca_file: PathBuf,
|
||||||
|
req_file: PathBuf,
|
||||||
|
key_file: PathBuf,
|
||||||
|
cert_file: PathBuf,
|
||||||
|
req_days: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OpenSSLExternalProvider {
|
||||||
|
async fn is_ca_exists(&self) -> bool {
|
||||||
|
is_file_exist(&self.ca_file).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn is_cert_exists(&self) -> bool {
|
||||||
|
is_file_exist(&self.cert_file).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn is_req_exists(&self) -> bool {
|
||||||
|
is_file_exist(&self.req_file).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn from_cfg(cfg: &AppConfig, vars: VarsMap) -> Self {
|
||||||
|
let base_dir = PathBuf::from(&cfg.base_directory);
|
||||||
|
let mut vars = vars;
|
||||||
|
|
||||||
|
vars.insert("KEY_CN", &cfg.name);
|
||||||
|
vars.insert("KEY_NAME", &cfg.name);
|
||||||
|
vars.insert("KEY_EMAIL", &cfg.email);
|
||||||
|
|
||||||
|
let openssl_cnf = base_dir.join(
|
||||||
|
std::env::var(&cfg.openssl_cnf_env)
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_or(&cfg.openssl_default_cnf),
|
||||||
|
);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
vars,
|
||||||
|
base_dir,
|
||||||
|
openssl_cnf,
|
||||||
|
openssl: cfg.openssl.to_string(),
|
||||||
|
ca_file: cfg.ca_filepath.clone(),
|
||||||
|
req_file: cfg.req_filepath.clone(),
|
||||||
|
key_file: cfg.key_filepath.clone(),
|
||||||
|
cert_file: cfg.cert_filepath.clone(),
|
||||||
|
req_days: cfg.req_days,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ICryptoProvider for OpenSSLExternalProvider {
|
||||||
|
async fn request(&self) -> Result<()> {
|
||||||
|
if self.is_req_exists().await {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.is_ca_exists().await {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"ca file not found: {}",
|
||||||
|
&self.ca_file.to_str().unwrap()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let status = Command::new(&self.openssl)
|
||||||
|
.args([
|
||||||
|
"req",
|
||||||
|
"-nodes",
|
||||||
|
"-new",
|
||||||
|
"-keyout",
|
||||||
|
self.key_file.to_str().unwrap(),
|
||||||
|
"-out",
|
||||||
|
self.req_file.to_str().unwrap(),
|
||||||
|
"-config",
|
||||||
|
self.openssl_cnf.to_str().unwrap(),
|
||||||
|
"-batch",
|
||||||
|
])
|
||||||
|
.current_dir(&self.base_dir)
|
||||||
|
.envs(self.vars.deref())
|
||||||
|
.status()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match status.success() {
|
||||||
|
true => Ok(()),
|
||||||
|
false => Err(anyhow!("openssl req execution failed")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sign(&self) -> Result<()> {
|
||||||
|
if self.is_cert_exists().await {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.is_ca_exists().await {
|
||||||
|
return Err(anyhow!(
|
||||||
|
"ca file not found: {}",
|
||||||
|
&self.ca_file.to_str().unwrap()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let status = Command::new(&self.openssl)
|
||||||
|
.args([
|
||||||
|
"ca",
|
||||||
|
"-days",
|
||||||
|
format!("{}", self.req_days).as_str(),
|
||||||
|
"-out",
|
||||||
|
self.cert_file.to_str().unwrap(),
|
||||||
|
"-in",
|
||||||
|
self.req_file.to_str().unwrap(),
|
||||||
|
"-config",
|
||||||
|
self.openssl_cnf.to_str().unwrap(),
|
||||||
|
"-batch",
|
||||||
|
])
|
||||||
|
.current_dir(&self.base_dir)
|
||||||
|
.envs(self.vars.deref())
|
||||||
|
.status()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match status.success() {
|
||||||
|
true => Ok(()),
|
||||||
|
false => Err(anyhow!("ssl ca execution failed")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,8 +20,25 @@ use crate::{
|
|||||||
crypto_provider::ICryptoProvider,
|
crypto_provider::ICryptoProvider,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::vars::{IStrMap, VarsMap};
|
||||||
use chrono::{Datelike, Days, Timelike, Utc};
|
use chrono::{Datelike, Days, Timelike, Utc};
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref KEYMAP: HashMap<&'static str, &'static str> = {
|
||||||
|
let mut m = HashMap::new();
|
||||||
|
m.insert("C", "KEY_COUNTRY");
|
||||||
|
m.insert("ST", "KEY_PROVINCE");
|
||||||
|
m.insert("O", "KEY_ORG");
|
||||||
|
m.insert("OU", "KEY_OU");
|
||||||
|
m.insert("CN", "KEY_CN");
|
||||||
|
m.insert("name", "KEY_NAME");
|
||||||
|
m
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
trait ToPemX {
|
trait ToPemX {
|
||||||
fn to_pem_x(&self) -> Result<Vec<u8>>;
|
fn to_pem_x(&self) -> Result<Vec<u8>>;
|
||||||
}
|
}
|
||||||
@ -76,6 +93,7 @@ fn get_time_str_x509(days: u32) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct OpenSSLInternalProvider {
|
pub(crate) struct OpenSSLInternalProvider {
|
||||||
|
vars: VarsMap,
|
||||||
ca_file: PathBuf,
|
ca_file: PathBuf,
|
||||||
ca_key_file: PathBuf,
|
ca_key_file: PathBuf,
|
||||||
req_file: PathBuf,
|
req_file: PathBuf,
|
||||||
@ -84,15 +102,6 @@ pub(crate) struct OpenSSLInternalProvider {
|
|||||||
req_days: u32,
|
req_days: u32,
|
||||||
key_size: u32,
|
key_size: u32,
|
||||||
encoding: String,
|
encoding: String,
|
||||||
name: String,
|
|
||||||
email: String,
|
|
||||||
dns: Vec<String>,
|
|
||||||
ip: Vec<String>,
|
|
||||||
country: Option<String>,
|
|
||||||
province: Option<String>,
|
|
||||||
location: Option<String>,
|
|
||||||
org: Option<String>,
|
|
||||||
ou: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OpenSSLInternalProvider {
|
impl OpenSSLInternalProvider {
|
||||||
@ -108,38 +117,29 @@ impl OpenSSLInternalProvider {
|
|||||||
is_file_exist(&self.req_file).await
|
is_file_exist(&self.req_file).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_cfg(cfg: &AppConfig) -> Self {
|
pub(crate) fn try_from_cfg(cfg: &AppConfig, vars: VarsMap) -> Result<Self> {
|
||||||
let base_dir = PathBuf::from(&cfg.base_directory);
|
let mut vars = vars;
|
||||||
let keys_dir = base_dir.clone();
|
|
||||||
let name = cfg.name.clone();
|
|
||||||
|
|
||||||
let ca_file = keys_dir.join(cfg.ca_filename.clone());
|
vars.insert("KEY_CN", &cfg.name);
|
||||||
let ca_key_file = ca_file.with_extension("key");
|
vars.insert("KEY_NAME", &cfg.name);
|
||||||
let req_file = keys_dir.join(format!("{}.csr", &name));
|
vars.insert("KEY_EMAIL", &cfg.email);
|
||||||
let key_file = keys_dir.join(format!("{}.key", &name));
|
|
||||||
let cert_file = keys_dir.join(format!("{}.crt", &name));
|
let key_size_s = vars.get(&"KEY_SIZE").unwrap_or("2048");
|
||||||
|
let key_size: u32 = key_size_s.parse().context("parse key size error")?;
|
||||||
|
|
||||||
let encoding = cfg.encoding.clone();
|
let encoding = cfg.encoding.clone();
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
ca_file,
|
vars,
|
||||||
ca_key_file,
|
ca_file: cfg.ca_filepath.clone(),
|
||||||
req_file,
|
ca_key_file: cfg.ca_key_filepath.clone(),
|
||||||
key_file,
|
req_file: cfg.req_filepath.clone(),
|
||||||
cert_file,
|
key_file: cfg.key_filepath.clone(),
|
||||||
|
cert_file: cfg.cert_filepath.clone(),
|
||||||
req_days: cfg.req_days,
|
req_days: cfg.req_days,
|
||||||
key_size: cfg.key_size,
|
key_size,
|
||||||
encoding,
|
encoding,
|
||||||
name,
|
})
|
||||||
email: cfg.email.clone(),
|
|
||||||
dns: cfg.dns.clone(),
|
|
||||||
ip: cfg.ip.clone(),
|
|
||||||
country: cfg.country.clone(),
|
|
||||||
province: cfg.province.clone(),
|
|
||||||
location: cfg.loc.clone(),
|
|
||||||
org: cfg.org.clone(),
|
|
||||||
ou: cfg.ou.clone(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_key_pair(&self) -> Result<(Rsa<Private>, PKey<Private>)> {
|
fn generate_key_pair(&self) -> Result<(Rsa<Private>, PKey<Private>)> {
|
||||||
@ -149,24 +149,24 @@ impl OpenSSLInternalProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_ca_cert(&self) -> Result<X509> {
|
async fn get_ca_cert(&self) -> Result<X509> {
|
||||||
let text = read_file(self.ca_file.clone(), &self.encoding).await?;
|
let text = read_file(&self.ca_file, &self.encoding).await?;
|
||||||
Ok(X509::from_pem(text.as_bytes())?)
|
Ok(X509::from_pem(text.as_bytes())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_ca_key(&self) -> Result<PKey<Private>> {
|
async fn get_ca_key(&self) -> Result<PKey<Private>> {
|
||||||
let text = read_file(self.ca_key_file.clone(), &self.encoding).await?;
|
let text = read_file(&self.ca_key_file, &self.encoding).await?;
|
||||||
Ok(PKey::from_rsa(Rsa::private_key_from_pem(text.as_bytes())?)?)
|
Ok(PKey::from_rsa(Rsa::private_key_from_pem(text.as_bytes())?)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_key(&self) -> Result<(Rsa<Private>, PKey<Private>)> {
|
async fn get_key(&self) -> Result<(Rsa<Private>, PKey<Private>)> {
|
||||||
let text = read_file(self.key_file.clone(), &self.encoding).await?;
|
let text = read_file(&self.key_file, &self.encoding).await?;
|
||||||
let rsa = Rsa::private_key_from_pem(text.as_bytes())?;
|
let rsa = Rsa::private_key_from_pem(text.as_bytes())?;
|
||||||
let pkey = PKey::from_rsa(rsa.clone())?;
|
let pkey = PKey::from_rsa(rsa.clone())?;
|
||||||
Ok((rsa, pkey))
|
Ok((rsa, pkey))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_req(&self) -> Result<X509Req> {
|
async fn get_req(&self) -> Result<X509Req> {
|
||||||
let text = read_file(self.req_file.clone(), &self.encoding).await?;
|
let text = read_file(&self.req_file, &self.encoding).await?;
|
||||||
Ok(X509Req::from_pem(text.as_bytes())?)
|
Ok(X509Req::from_pem(text.as_bytes())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,43 +186,32 @@ impl OpenSSLInternalProvider {
|
|||||||
fn build_x509_name(&self) -> Result<X509Name> {
|
fn build_x509_name(&self) -> Result<X509Name> {
|
||||||
let mut name_builder =
|
let mut name_builder =
|
||||||
X509NameBuilder::new().context("Failed to create X509 name builder")?;
|
X509NameBuilder::new().context("Failed to create X509 name builder")?;
|
||||||
name_builder.append_entry_by_text("name", &self.name)?;
|
for (&key, &var) in KEYMAP.iter() {
|
||||||
name_builder.append_entry_by_text("CN", &self.name)?;
|
let value = self
|
||||||
if let Some(country) = self.country.clone() {
|
.vars
|
||||||
name_builder.append_entry_by_text("C", &country)?;
|
.get(&var)
|
||||||
}
|
.ok_or(anyhow!("variable not set: {}", var))?;
|
||||||
if let Some(province) = self.province.clone() {
|
name_builder.append_entry_by_text(key, value)?;
|
||||||
name_builder.append_entry_by_text("ST", &province)?;
|
|
||||||
}
|
|
||||||
if let Some(location) = self.location.clone() {
|
|
||||||
name_builder.append_entry_by_text("L", &location)?;
|
|
||||||
}
|
|
||||||
if let Some(org) = self.org.clone() {
|
|
||||||
name_builder.append_entry_by_text("O", &org)?;
|
|
||||||
}
|
|
||||||
if let Some(ou) = self.ou.clone() {
|
|
||||||
name_builder.append_entry_by_text("OU", &ou)?;
|
|
||||||
}
|
}
|
||||||
Ok(name_builder.build())
|
Ok(name_builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_x509_extensions(
|
fn gen_x509_extensions(
|
||||||
&self,
|
|
||||||
context: &openssl::x509::X509v3Context,
|
context: &openssl::x509::X509v3Context,
|
||||||
|
vars: &VarsMap,
|
||||||
) -> Result<Vec<X509Extension>> {
|
) -> Result<Vec<X509Extension>> {
|
||||||
let key_usage = KeyUsage::new()
|
let key_usage = KeyUsage::new()
|
||||||
|
.key_agreement()
|
||||||
.digital_signature()
|
.digital_signature()
|
||||||
.data_encipherment()
|
|
||||||
.build()?;
|
.build()?;
|
||||||
let key_extended_ext = ExtendedKeyUsage::new().server_auth().build()?;
|
let key_extended_ext = ExtendedKeyUsage::new().client_auth().build()?;
|
||||||
|
|
||||||
let mut san_extension = SubjectAlternativeName::new();
|
let mut san_extension = SubjectAlternativeName::new();
|
||||||
san_extension.email(self.email.as_str());
|
if let Some(name) = vars.get(&"KEY_NAME") {
|
||||||
for dns in self.dns.iter() {
|
san_extension.dns(name);
|
||||||
san_extension.dns(dns);
|
|
||||||
}
|
}
|
||||||
for ip in self.ip.iter() {
|
if let Some(email) = vars.get(&"KEY_EMAIL") {
|
||||||
san_extension.ip(ip);
|
san_extension.email(email);
|
||||||
}
|
}
|
||||||
let san_ext = san_extension.build(context).context("build san")?;
|
let san_ext = san_extension.build(context).context("build san")?;
|
||||||
|
|
||||||
@ -230,11 +219,11 @@ impl OpenSSLInternalProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gen_x509_extensions_stack(
|
fn gen_x509_extensions_stack(
|
||||||
&self,
|
|
||||||
context: &openssl::x509::X509v3Context,
|
context: &openssl::x509::X509v3Context,
|
||||||
|
vars: &VarsMap,
|
||||||
) -> Result<Stack<X509Extension>> {
|
) -> Result<Stack<X509Extension>> {
|
||||||
let mut stack = Stack::new()?;
|
let mut stack = Stack::new()?;
|
||||||
for extension in self.gen_x509_extensions(context)?.into_iter() {
|
for extension in Self::gen_x509_extensions(context, vars)?.into_iter() {
|
||||||
stack.push(extension).context("push ext")?;
|
stack.push(extension).context("push ext")?;
|
||||||
}
|
}
|
||||||
Ok(stack)
|
Ok(stack)
|
||||||
@ -267,7 +256,7 @@ impl ICryptoProvider for OpenSSLInternalProvider {
|
|||||||
.set_subject_name(&name)
|
.set_subject_name(&name)
|
||||||
.context("set subject name")?;
|
.context("set subject name")?;
|
||||||
let context = csr_builder.x509v3_context(Some(&conf));
|
let context = csr_builder.x509v3_context(Some(&conf));
|
||||||
let extensions = self.gen_x509_extensions_stack(&context)?;
|
let extensions = Self::gen_x509_extensions_stack(&context, &self.vars)?;
|
||||||
csr_builder.add_extensions(&extensions)?;
|
csr_builder.add_extensions(&extensions)?;
|
||||||
csr_builder.sign(&pkey, MessageDigest::sha512())?;
|
csr_builder.sign(&pkey, MessageDigest::sha512())?;
|
||||||
let csr = csr_builder.build();
|
let csr = csr_builder.build();
|
||||||
@ -324,7 +313,7 @@ impl ICryptoProvider for OpenSSLInternalProvider {
|
|||||||
.context("set_subject_name")?;
|
.context("set_subject_name")?;
|
||||||
|
|
||||||
let context = builder.x509v3_context(Some(&ca_cert), None);
|
let context = builder.x509v3_context(Some(&ca_cert), None);
|
||||||
for extension in self.gen_x509_extensions(&context)? {
|
for extension in Self::gen_x509_extensions(&context, &self.vars)? {
|
||||||
builder.append_extension(extension).context("append ext")?;
|
builder.append_extension(extension).context("append ext")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1 +1,2 @@
|
|||||||
|
pub(crate) mod external;
|
||||||
pub(crate) mod internal;
|
pub(crate) mod internal;
|
||||||
|
183
src/ovpn.rs
Normal file
183
src/ovpn.rs
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
certs::Certs,
|
||||||
|
common::{read_file, write_file, AppConfig, DEFAULT_ENCODING},
|
||||||
|
crypto_provider::ICryptoProvider,
|
||||||
|
};
|
||||||
|
use anyhow::{anyhow, Context, Ok, Result};
|
||||||
|
use tera::Tera;
|
||||||
|
|
||||||
|
pub(crate) struct OvpnConfig {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) encoding: String,
|
||||||
|
pub(crate) email: Option<String>,
|
||||||
|
pub(crate) template: String,
|
||||||
|
pub(crate) template_filename: Option<String>,
|
||||||
|
pub(crate) ca_cert: String,
|
||||||
|
pub(crate) cert: String,
|
||||||
|
pub(crate) key: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&OvpnConfig> for tera::Context {
|
||||||
|
fn from(value: &OvpnConfig) -> Self {
|
||||||
|
let mut context = tera::Context::new();
|
||||||
|
context.insert("name", &value.name);
|
||||||
|
context.insert("encoding", &value.encoding);
|
||||||
|
context.insert("email", &value.email);
|
||||||
|
context.insert("template_filename", &value.template_filename);
|
||||||
|
context.insert("ca", &value.ca_cert);
|
||||||
|
context.insert("cert", &value.cert);
|
||||||
|
context.insert("key", &value.key);
|
||||||
|
context
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub(crate) struct OvpnConfigBuilder {
|
||||||
|
name: String,
|
||||||
|
encoding: Option<String>,
|
||||||
|
email: Option<String>,
|
||||||
|
template_file: PathBuf,
|
||||||
|
ca_cert_file: Option<PathBuf>,
|
||||||
|
cert_file: Option<PathBuf>,
|
||||||
|
key_file: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OvpnConfigBuilder {
|
||||||
|
pub(crate) fn new<P: Into<PathBuf>>(name: String, template_file: P) -> Self {
|
||||||
|
Self {
|
||||||
|
name,
|
||||||
|
template_file: template_file.into(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) fn with_encoding(&mut self, encoding: String) -> &mut Self {
|
||||||
|
self.encoding = Some(encoding);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub(crate) fn with_email(&mut self, email: String) -> &mut Self {
|
||||||
|
self.email = Some(email);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub(crate) fn with_ca_cert_file<P: Into<PathBuf>>(&mut self, ca_cert_file: P) -> &mut Self {
|
||||||
|
self.ca_cert_file = Some(ca_cert_file.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub(crate) fn with_cert_file<P: Into<PathBuf>>(&mut self, cert_file: P) -> &mut Self {
|
||||||
|
self.cert_file = Some(cert_file.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub(crate) fn with_key_file<P: Into<PathBuf>>(&mut self, key_file: P) -> &mut Self {
|
||||||
|
self.key_file = Some(key_file.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn build(self) -> Result<OvpnConfig> {
|
||||||
|
let name = self.name;
|
||||||
|
let encoding = self.encoding.unwrap_or(DEFAULT_ENCODING.into());
|
||||||
|
let template = read_file(&self.template_file, &encoding)
|
||||||
|
.await
|
||||||
|
.context("template file read error")?;
|
||||||
|
|
||||||
|
let ca_cert_file = self.ca_cert_file.ok_or(anyhow!("ca cert file not set"))?;
|
||||||
|
let cert_file = self.cert_file.ok_or(anyhow!("cert file not set"))?;
|
||||||
|
let key_file = self.key_file.ok_or(anyhow!("key file not set"))?;
|
||||||
|
|
||||||
|
let ca_cert = read_file(ca_cert_file, &encoding)
|
||||||
|
.await
|
||||||
|
.context("ca cert file read error")?
|
||||||
|
.trim_end()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let cert = read_file(cert_file, &encoding)
|
||||||
|
.await
|
||||||
|
.context("cert file read error")?
|
||||||
|
.trim_end()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let key = read_file(key_file, &encoding)
|
||||||
|
.await
|
||||||
|
.context("key file read error")?
|
||||||
|
.trim_end()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let template_filename = self
|
||||||
|
.template_file
|
||||||
|
.file_name()
|
||||||
|
.ok_or(anyhow!("template_file filename"))?
|
||||||
|
.to_str()
|
||||||
|
.ok_or(anyhow!("template_file to_str"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(OvpnConfig {
|
||||||
|
name,
|
||||||
|
encoding,
|
||||||
|
email: self.email,
|
||||||
|
template,
|
||||||
|
ca_cert,
|
||||||
|
cert,
|
||||||
|
key,
|
||||||
|
template_filename: Some(template_filename),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct OvpnConfigRendered {
|
||||||
|
content: String,
|
||||||
|
encoding: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OvpnConfigRendered {
|
||||||
|
pub async fn to_file<P: AsRef<Path>>(&self, filepath: P) -> Result<()> {
|
||||||
|
write_file(filepath, &self.content, &self.encoding)
|
||||||
|
.await
|
||||||
|
.context("ovpn config file write error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<OvpnConfig> for OvpnConfigRendered {
|
||||||
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
|
fn try_from(value: OvpnConfig) -> Result<Self> {
|
||||||
|
let mut tera = Tera::default();
|
||||||
|
tera.add_raw_template("template", &value.template)
|
||||||
|
.context("raw template add error")?;
|
||||||
|
|
||||||
|
let context: tera::Context = (&value).into();
|
||||||
|
let content = tera
|
||||||
|
.render("template", &context)
|
||||||
|
.context("config render error")?;
|
||||||
|
|
||||||
|
Ok(OvpnConfigRendered {
|
||||||
|
content,
|
||||||
|
encoding: value.encoding,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OvpnConfig {
|
||||||
|
pub(crate) fn render(self) -> Result<OvpnConfigRendered> {
|
||||||
|
OvpnConfigRendered::try_from(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn try_from_certs<T: ICryptoProvider>(
|
||||||
|
certs: Certs<T>,
|
||||||
|
config: &AppConfig,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let base_dir = PathBuf::from(&config.base_directory);
|
||||||
|
let template_file = base_dir.join(&config.template_file);
|
||||||
|
|
||||||
|
certs.build_all().await.context("certs build all error")?;
|
||||||
|
|
||||||
|
let mut builder = OvpnConfigBuilder::new(config.name.clone(), template_file);
|
||||||
|
builder
|
||||||
|
.with_encoding(config.encoding.clone())
|
||||||
|
.with_ca_cert_file(certs.ca_file.clone())
|
||||||
|
.with_email(config.email.clone())
|
||||||
|
.with_cert_file(certs.cert_file.clone())
|
||||||
|
.with_key_file(certs.key_file.clone());
|
||||||
|
|
||||||
|
Ok(builder.build().await.context("ovpn config build error")?)
|
||||||
|
}
|
||||||
|
}
|
130
src/vars.rs
Normal file
130
src/vars.rs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use regex::Regex;
|
||||||
|
use std::{path::PathBuf, pin::Pin};
|
||||||
|
use tokio::pin;
|
||||||
|
|
||||||
|
use crate::arcstr::ArcStr;
|
||||||
|
use crate::common::{read_file_by_lines, AppConfig};
|
||||||
|
use futures_util::stream::StreamExt;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::fmt::{Debug, Formatter};
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
pub(crate) type ArcVarsMap = BTreeMap<ArcStr, ArcStr>;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Ord, PartialOrd, Eq)]
|
||||||
|
pub(crate) struct VarsMap(ArcVarsMap);
|
||||||
|
|
||||||
|
pub(crate) trait IStrMap {
|
||||||
|
fn get<S: AsRef<str> + Clone>(&self, key: &S) -> Option<&str>;
|
||||||
|
fn insert<S: AsRef<str> + Clone>(&mut self, key: S, value: S) -> Option<S>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IStrMap for VarsMap {
|
||||||
|
fn get<S: AsRef<str> + Clone>(&self, key: &S) -> Option<&str> {
|
||||||
|
self.0.get(&ArcStr::from(key.as_ref())).map(|x| x.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert<S: AsRef<str> + Clone>(&mut self, key: S, value: S) -> Option<S> {
|
||||||
|
let key = ArcStr::from(key.as_ref());
|
||||||
|
let value2 = ArcStr::from(value.clone().as_ref());
|
||||||
|
self.0.insert(key, value2).and(Some(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for VarsMap {
|
||||||
|
type Target = BTreeMap<ArcStr, ArcStr>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for VarsMap {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let debug_map: BTreeMap<_, _> =
|
||||||
|
self.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
|
||||||
|
debug_map.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct VarsFile {
|
||||||
|
pub(crate) filepath: ArcStr,
|
||||||
|
pub(crate) encoding: ArcStr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VarsFile {
|
||||||
|
async fn from_file<P>(filepath: P, encoding: ArcStr) -> Result<Self>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let filepath_str = filepath
|
||||||
|
.as_ref()
|
||||||
|
.to_str()
|
||||||
|
.ok_or(anyhow!("filepath to str"))?;
|
||||||
|
let metadata = tokio::fs::metadata(filepath_str)
|
||||||
|
.await
|
||||||
|
.context(format!("config file not found: {}", &filepath_str))?;
|
||||||
|
if !metadata.is_file() {
|
||||||
|
Err(anyhow!("config is not a file {}", &filepath_str))?;
|
||||||
|
}
|
||||||
|
Ok(VarsFile {
|
||||||
|
filepath: ArcStr::from(filepath_str),
|
||||||
|
encoding,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn from_dir<P>(dir: P, encoding: ArcStr) -> Result<Self>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let filepath = dir.as_ref().join("vars");
|
||||||
|
let err_context = "vars or vars.bat file not found";
|
||||||
|
|
||||||
|
match Self::from_file(&filepath, encoding.clone()).await {
|
||||||
|
Ok(res) => Ok(res),
|
||||||
|
Err(_) => Self::from_file(&filepath.with_extension("bat"), encoding.clone())
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.context(err_context)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn from_config(config: &AppConfig) -> Result<Self> {
|
||||||
|
Self::from_dir(
|
||||||
|
PathBuf::from(&config.base_directory),
|
||||||
|
ArcStr::from(config.encoding.as_str()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn parse(&self) -> Result<VarsMap> {
|
||||||
|
let mut result = ArcVarsMap::new();
|
||||||
|
result.insert("__file__".into(), self.filepath.clone());
|
||||||
|
result.insert("__encoding__".into(), self.encoding.clone());
|
||||||
|
|
||||||
|
let lines = read_file_by_lines(&self.filepath.as_path(), &self.encoding)
|
||||||
|
.await
|
||||||
|
.context("vars read error")?;
|
||||||
|
let lines = Pin::from(lines);
|
||||||
|
pin!(lines);
|
||||||
|
|
||||||
|
let re_v2 = Regex::new(r#"^(export|set)\s\b(?P<key>[\w\d_]+)\b=\s?"?(?P<value>[^#]+?)"?$"#)
|
||||||
|
.context("regex v2")
|
||||||
|
.context("re_v2 error")?;
|
||||||
|
let re_v3 = Regex::new(r"^set_var\s(?P<key1>[\w\d_]+)\s+(?P<value1>[^#]+?)$")
|
||||||
|
.context("regex v3")
|
||||||
|
.context("re_v3 error")?;
|
||||||
|
|
||||||
|
while let Some(line) = lines.next().await {
|
||||||
|
for re in [&re_v2, &re_v3].iter() {
|
||||||
|
if let Some(caps) = re.captures(line.as_str()) {
|
||||||
|
let [key, value] = [&caps["key"], &caps["value"]].map(ArcStr::from);
|
||||||
|
result.insert(key, value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(VarsMap(result))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user