diff --git a/.github/workflows/dagger.yml b/.github/workflows/dagger.yml new file mode 100644 index 0000000..a4581aa --- /dev/null +++ b/.github/workflows/dagger.yml @@ -0,0 +1,23 @@ +name: dagger +on: + push: + branches: [main] + pull_request: + +jobs: + build: + name: build + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v3 + with: + go-version: '1.20' + - uses: actions/checkout@v3 + # - name: Setup tmate session + # uses: mxschmitt/action-tmate@v3 + - name: print env var + run: | + env + - name: run debug + run: | + go run . diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 03bd4da..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -.env -target -*/target diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index bb6e26b..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,935 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "addr2line" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - -[[package]] -name = "aho-corasick" -version = "0.7.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" -dependencies = [ - "memchr", -] - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - -[[package]] -name = "backtrace" -version = "0.3.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11a17d453482a265fd5f8479f2a3f405566e6ca627837aaddb85af8b1ab8ef61" -dependencies = [ - "addr2line", - "cc", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", -] - -[[package]] -name = "base64" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "cc" -version = "1.0.73" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time", - "winapi", -] - -[[package]] -name = "clap" -version = "3.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53da17d37dba964b9b3ecb5c5a1f193a2762c700e6829201e645b9381c99dc7" -dependencies = [ - "atty", - "bitflags", - "clap_derive", - "clap_lex", - "indexmap", - "once_cell", - "strsim", - "termcolor", - "textwrap", -] - -[[package]] -name = "clap_derive" -version = "3.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c11d40217d16aee8508cc8e5fde8b4ff24639758608e5374e731b53f85749fb9" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_lex" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5538cd660450ebeb4234cfecf8f2284b844ffc4c50531e66d584ad5b91293613" -dependencies = [ - "os_str_bytes", -] - -[[package]] -name = "core-foundation" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" - -[[package]] -name = "daemonize" -version = "0.1.0" -dependencies = [ - "chrono", - "dotenv", - "lettre", - "libc", -] - -[[package]] -name = "dotenv" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424bad868b0ffe6ae351ee463526ba625bbca817978293bbe6bb7dc1804a175" -dependencies = [ - "failure", - "lazy_static", - "regex", -] - -[[package]] -name = "email-encoding" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "827e1fb86d24d558ab0454ca3fa084f8a6144ade1e3e6982f697c586bf96b41b" -dependencies = [ - "base64", - "memchr", -] - -[[package]] -name = "email_address" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8684b7c9cb4857dfa1e5b9629ef584ba618c9b93bae60f58cb23f4f271d0468e" - -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "synstructure", -] - -[[package]] -name = "fastrand" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf" -dependencies = [ - "instant", -] - -[[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - -[[package]] -name = "futures-core" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" - -[[package]] -name = "futures-io" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" - -[[package]] -name = "futures-task" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a" - -[[package]] -name = "futures-util" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" -dependencies = [ - "futures-core", - "futures-io", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - -[[package]] -name = "gimli" -version = "0.26.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" - -[[package]] -name = "hashbrown" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3" - -[[package]] -name = "heck" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" - -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - -[[package]] -name = "hostname" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" -dependencies = [ - "libc", - "match_cfg", - "winapi", -] - -[[package]] -name = "httpdate" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" - -[[package]] -name = "idna" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" -dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", -] - -[[package]] -name = "indexmap" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c6392766afd7964e2531940894cffe4bd8d7d17dbc3c1c4857040fd4b33bdb3" -dependencies = [ - "autocfg", - "hashbrown", -] - -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "lettre" -version = "0.10.0-rc.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f7e87d9d44162eea7abd87b1a7540fcb10d5e58e8bb4f173178f3dc6e453944" -dependencies = [ - "base64", - "email-encoding", - "email_address", - "fastrand", - "futures-util", - "hostname", - "httpdate", - "idna", - "mime", - "native-tls", - "nom", - "once_cell", - "quoted_printable", - "socket2", -] - -[[package]] -name = "libc" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" - -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "match_cfg" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" - -[[package]] -name = "matches" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" - -[[package]] -name = "matt_daemon" -version = "0.1.0" -dependencies = [ - "clap", - "daemonize", - "dotenv", - "libc", - "strum", - "strum_macros", -] - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "mime" -version = "0.3.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" - -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - -[[package]] -name = "miniz_oxide" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc" -dependencies = [ - "adler", -] - -[[package]] -name = "native-tls" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" -dependencies = [ - "lazy_static", - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", -] - -[[package]] -name = "nom" -version = "7.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" -dependencies = [ - "memchr", - "minimal-lexical", -] - -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] - -[[package]] -name = "object" -version = "0.28.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e42c982f2d955fac81dd7e1d0e1426a7d702acd9c98d19ab01083a6a0328c424" -dependencies = [ - "memchr", -] - -[[package]] -name = "once_cell" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" - -[[package]] -name = "openssl" -version = "0.10.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb81a6430ac911acb25fe5ac8f1d2af1b4ea8a4fdfda0f1ee4292af2e2d8eb0e" -dependencies = [ - "bitflags", - "cfg-if", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "835363342df5fba8354c5b453325b110ffd54044e588c539cf2f20a8014e4cb1" -dependencies = [ - "autocfg", - "cc", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "os_str_bytes" -version = "6.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa" - -[[package]] -name = "pin-project-lite" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - -[[package]] -name = "pkg-config" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro2" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "quoted_printable" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fee2dce59f7a43418e3382c766554c614e06a552d53a8f07ef499ea4b332c0f" - -[[package]] -name = "redox_syscall" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" -dependencies = [ - "bitflags", -] - -[[package]] -name = "regex" -version = "1.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" - -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - -[[package]] -name = "rustc-demangle" -version = "0.1.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" - -[[package]] -name = "rustversion" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0a5f7c728f5d284929a1cccb5bc19884422bfe6ef4d6c409da2c41838983fcf" - -[[package]] -name = "schannel" -version = "0.1.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" -dependencies = [ - "lazy_static", - "windows-sys", -] - -[[package]] -name = "security-framework" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dc14f172faf8a0194a3aded622712b0de276821addc574fa54fc0a1167e10dc" -dependencies = [ - "bitflags", - "core-foundation", - "core-foundation-sys", - "libc", - "security-framework-sys", -] - -[[package]] -name = "security-framework-sys" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" -dependencies = [ - "core-foundation-sys", - "libc", -] - -[[package]] -name = "slab" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" - -[[package]] -name = "socket2" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" -dependencies = [ - "libc", - "winapi", -] - -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - -[[package]] -name = "strum" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" -dependencies = [ - "strum_macros", -] - -[[package]] -name = "strum_macros" -version = "0.24.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6878079b17446e4d3eba6192bb0a2950d5b14f0ed8424b852310e5a94345d0ef" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "rustversion", - "syn", -] - -[[package]] -name = "syn" -version = "1.0.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "unicode-xid", -] - -[[package]] -name = "tempfile" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", -] - -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "textwrap" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" - -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi", - "winapi", -] - -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - -[[package]] -name = "unicode-bidi" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" - -[[package]] -name = "unicode-ident" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" - -[[package]] -name = "unicode-normalization" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "unicode-xid" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" -dependencies = [ - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" - -[[package]] -name = "windows_i686_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" - -[[package]] -name = "windows_i686_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index 089d9d6..0000000 --- a/Cargo.toml +++ /dev/null @@ -1,3 +0,0 @@ -[workspace] - -members = [ "daemonize", "usage"] diff --git a/Makefile b/Makefile deleted file mode 100644 index 8238e09..0000000 --- a/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -NAME = matt_daemon - -# recursive wildcard function -# ($1): list of directories -# ($2) is a list of patterns to match. -# source: https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html -rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d)) - -# list of directories to search for source files -SRC := $(call rwildcard,./daemonize,*.rs *.toml) -SRC += $(call rwildcard,./usage,*.rs *.toml) - -all: $(NAME) - -$(NAME): $(SRC) - cargo build - cp ./target/debug/matt_daemon . - -clean: - cargo clean - -fclean: clean - rm -rf matt_daemon - -re: fclean all \ No newline at end of file diff --git a/daemonize/Cargo.toml b/daemonize/Cargo.toml deleted file mode 100644 index 1255625..0000000 --- a/daemonize/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "daemonize" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -chrono = "0.4" -dotenv = "0.14" -lettre = "0.10.0-rc.7" -libc = "0.2" diff --git a/daemonize/README.md b/daemonize/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/daemonize/src/error.rs b/daemonize/src/error.rs deleted file mode 100644 index a99bb83..0000000 --- a/daemonize/src/error.rs +++ /dev/null @@ -1,151 +0,0 @@ -type Errno = libc::c_int; -pub type Result = std::result::Result; -use std::{fmt::Display, io}; - -#[derive(Debug)] -pub enum Error { - AcceptClient(std::io::Error), - ChangeDir(Errno), - CloseFd(Errno), - ConnectionShutdown(std::io::Error), - CreateDir(std::io::Error), - ClientErrorBinding(std::io::Error), - ClientGetter, - CommandFailed(std::io::Error), - ConvertToUTF8, - DeleteLock(Errno), - DotEnv(dotenv::Error), - DotEnvUsername(std::env::VarError), - DotEnvPassword(std::env::VarError), - DotEnvRelay(std::env::VarError), - FileAlreadyLocked(Errno), - Fork(Errno), - GetPid(Errno), - GetPgid(Errno), - GetSid(Errno), - InvalidFd { fd: i32, expected: i32 }, - IssueLockFile(Errno), - Log(std::io::Error), - LogOpen(std::io::Error), - MailBuilder(lettre::error::Error), - MailSend(lettre::transport::smtp::Error), - MailSmtpTransport(lettre::transport::smtp::Error), - MaxFdTooBig, - NoArgumentProvided, - Open(Errno), - ParseError, - ParseDstError, - Quit, - Read(Errno), - ReadFile(std::io::Error), - RedirectStream(Errno), - Rlmit(Errno), - SetSid(Errno), - SetSig(Errno), - ShellModeOverflow, - SigMask(Errno), - SignalSetting(Errno), - Sysconf(Errno), - Unlock(Errno), - WriteToStream(std::io::Error), - WrongFd, -} - -impl Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::AcceptClient(e) => write!(f, "Error accepting a new client : {e}")?, - Error::ChangeDir(e) => write!(f, "Error changing directory : {e}")?, - Error::CloseFd(e) => write!(f, "Error closing fd : {e}")?, - Error::ClientErrorBinding(e) => write!(f, "Cannot bind to address : {e}")?, - Error::ClientGetter => write!(f, "Cannot retrieve client")?, - Error::CommandFailed(e) => write!(f, "The command failed : {e}")?, - Error::ConnectionShutdown(e) => write!(f, "Cannot shutdown the TCP connection : {e}")?, - Error::CreateDir(e) => write!(f, "Error creating dir : {e}")?, - Error::ConvertToUTF8 => write!(f, "Cannot convert the logfile name to UTF8")?, - Error::DeleteLock(e) => write!(f, "Error deleting lock file: {e}")?, - Error::DotEnv(e) => write!(f, "Error getting .env file : {e}")?, - Error::DotEnvUsername(e) => write!(f, "SMTPUSERNAME not found in .env file : {e}")?, - Error::DotEnvPassword(e) => write!(f, "SMTPUSERNAME not found in .env file : {e}")?, - Error::DotEnvRelay(e) => write!(f, "SMTPUSERNAME not found in .env file : {e}")?, - Error::InvalidFd { fd, expected } => { - write!(f, "Opening fd {fd}, it should be {expected}")? - } - Error::FileAlreadyLocked(e) => { - write!(f, "The lock file is locked by another process: {e}")? - } - Error::Fork(e) => write!(f, "Error forking : {e}")?, - Error::GetPid(e) => write!(f, "Can't retrieve pid : {e}")?, - Error::GetPgid(e) => write!(f, "Can't retrieve pid : {e}")?, - Error::GetSid(e) => write!(f, "Can't retrieve pid : {e}")?, - Error::IssueLockFile(e) => write!(f, "Issue with lock file : {e}")?, - Error::Log(e) => write!(f, "Error while logging : {e}")?, - Error::LogOpen(e) => write!(f, "Error trying to open logfile : {e}")?, - Error::MaxFdTooBig => write!(f, "Max fd retrieved with sysconf is too big")?, - Error::MailBuilder(e) => write!(f, "Cannot create the email : {e}")?, - Error::MailSend(e) => write!(f, "Cannot send the mail : {e}")?, - Error::MailSmtpTransport(e) => write!(f, "Cannot create the smtp transporter: {e}")?, - Error::NoArgumentProvided => write!(f, "No argument provided while in shell mode")?, - Error::Open(e) => write!(f, "Error opening file : {e}")?, - Error::ParseError => write!(f, "Cannot parse email from string")?, - Error::ParseDstError => write!(f, "Cannot parse recipients's mail from string")?, - Error::Read(e) => write!(f, "Error reading file : {e}")?, - Error::ReadFile(e) => write!(f, "Error reading file : {e}")?, - Error::RedirectStream(e) => write!(f, "Error redirecting stream : {e}")?, - Error::Rlmit(e) => write!(f, "Error getting rlimit : {e}")?, - Error::SetSid(e) => write!(f, "Error setting sid : {e}")?, - Error::SetSig(e) => write!(f, "Error getting signal set : {e}")?, - Error::ShellModeOverflow => write!(f, "Error changing shell mode")?, - Error::SigMask(e) => write!(f, "Error setting signal mask : {e}")?, - Error::SignalSetting(e) => write!(f, "Error setting signla handler : {e}")?, - Error::Sysconf(e) => write!(f, "Error getting value of sysconf : {e}")?, - Error::Unlock(e) => write!(f, "Error unlocking lock file : {e}")?, - Error::Quit => write!(f, "Quitting the daemon")?, - Error::WriteToStream(e) => write!(f, "Error writing to stream : {e}")?, - Error::WrongFd => write!(f, "No stream corresponding to this fd")?, - }; - Ok(()) - } -} - -pub trait IsErr { - fn is_err(&self) -> bool; -} -impl IsErr for i32 { - fn is_err(&self) -> bool { - *self == -1 - } -} -impl IsErr for i64 { - fn is_err(&self) -> bool { - *self == -1 - } -} -impl IsErr for isize { - fn is_err(&self) -> bool { - *self == -1 - } -} -impl IsErr for usize { - fn is_err(&self) -> bool { - *self == usize::MAX - } -} - -pub fn get_err(value: V, f: F) -> Result -where - V: IsErr, - F: FnOnce(Errno) -> Error, -{ - if value.is_err() { - Err(f(get_errno())) - } else { - Ok(value) - } -} - -pub fn get_errno() -> Errno { - io::Error::last_os_error() - .raw_os_error() - .expect("Errno expected") -} diff --git a/daemonize/src/file_handler.rs b/daemonize/src/file_handler.rs deleted file mode 100644 index e672433..0000000 --- a/daemonize/src/file_handler.rs +++ /dev/null @@ -1,97 +0,0 @@ -use crate::error::{get_err, Error, Result}; - -pub(crate) fn redirect_stream() -> Result<()> { - unsafe { - get_err(libc::close(libc::STDIN_FILENO), Error::CloseFd)?; - let null_fd = get_err( - libc::open(b"/dev/null\0" as *const [u8; 10] as _, libc::O_RDWR), - Error::Open, - )?; - if null_fd != 0 { - return Err(Error::InvalidFd { - fd: null_fd, - expected: libc::STDIN_FILENO, - }); - } - let out_fd = get_err( - libc::dup2(libc::STDIN_FILENO, libc::STDOUT_FILENO), - Error::RedirectStream, - )?; - if out_fd != libc::STDOUT_FILENO { - return Err(Error::InvalidFd { - fd: out_fd, - expected: libc::STDOUT_FILENO, - }); - } - let err_fd = get_err( - libc::dup2(libc::STDIN_FILENO, libc::STDERR_FILENO), - Error::RedirectStream, - )?; - if err_fd != libc::STDERR_FILENO { - return Err(Error::InvalidFd { - fd: err_fd, - expected: libc::STDERR_FILENO, - }); - } - } - Ok(()) -} -fn get_rlimit() -> Result { - let mut rlim = libc::rlimit { - rlim_cur: 0, - rlim_max: u32::MAX.into(), - }; - unsafe { - get_err( - libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim), - Error::Rlmit, - ) - } -} - -fn get_max_fd() -> Result { - unsafe { - let ret = get_err(libc::sysconf(libc::_SC_OPEN_MAX), Error::Sysconf); - if let Ok(max_fd) = ret { - return max_fd.try_into().map_err(|_| Error::MaxFdTooBig); - } - } - - get_rlimit() -} - -pub(crate) unsafe fn close_fds() -> Result<()> { - let fds = 3..get_max_fd()?; - get_rlimit()?; - - fds.for_each(|fd| { - libc::close(fd); - }); - Ok(()) -} - -pub(crate) fn lock(file: String) -> Result<()> { - unsafe { - let fd = libc::open( - (file + "\0").as_ptr() as _, - libc::O_RDONLY | libc::O_CREAT | libc::O_EXCL, - ); - - get_err(libc::flock(fd, libc::LOCK_EX), Error::IssueLockFile).map_err(|e| match e { - Error::IssueLockFile(libc::EBADF) => Error::FileAlreadyLocked(libc::EWOULDBLOCK), - Error::IssueLockFile(_) => e, - _ => unreachable!(), - })?; - } - Ok(()) -} -pub(crate) fn unlock(file: String) -> Result<()> { - unsafe { - let fd = libc::open((file.clone() + "\0").as_ptr() as _, libc::O_RDONLY); - - get_err(libc::flock(fd, libc::LOCK_UN), Error::Unlock)?; - - get_err(libc::remove((file + "\0").as_ptr() as _), Error::DeleteLock)?; - } - Ok(()) -} diff --git a/daemonize/src/fork.rs b/daemonize/src/fork.rs deleted file mode 100644 index c11b1e7..0000000 --- a/daemonize/src/fork.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::error::{get_err, Error, Result}; - -pub(crate) enum ForkResult { - Child, - Parent(libc::pid_t), -} - -pub(crate) unsafe fn execute_fork() -> Result { - let pid = get_err(libc::fork(), Error::Fork)?; - if pid == 0 { - Ok(ForkResult::Child) - } else { - Ok(ForkResult::Parent(pid)) - } -} diff --git a/daemonize/src/lib.rs b/daemonize/src/lib.rs deleted file mode 100644 index 2436bc2..0000000 --- a/daemonize/src/lib.rs +++ /dev/null @@ -1,148 +0,0 @@ -mod error; -mod file_handler; -mod fork; -mod logger; -mod signal; - -use file_handler::{lock, redirect_stream, unlock}; -use fork::{execute_fork, ForkResult}; -use libc::exit; -use std::path; - -pub use error::{get_err, get_errno, Error, Result}; -pub use logger::{LogInfo, TintinReporter}; -use signal::set_sig_handlers; - -use crate::file_handler::close_fds; - -struct Mask { - inner: libc::mode_t, -} - -impl From for Mask { - fn from(mask: u32) -> Self { - Mask { - inner: mask as libc::mode_t, - } - } -} -fn get_pid() -> Result { - unsafe { - let pid = get_err(libc::getpid(), Error::GetPid)?; - Ok(format!("Starting with pid {pid}\n")) - } -} - -pub struct Daemon { - debug: bool, - lock_file: String, - logger: TintinReporter, - umask: Mask, - func: fn(TintinReporter) -> Result<()>, -} - -impl Drop for Daemon { - fn drop(&mut self) { - if self - .logger - .log("deleting lock file\n", LogInfo::Info, self.debug) - .is_err() - { - eprintln!("Exiting daemon : Could not log the deletion of the lock file"); - } - - if unlock(self.lock_file.clone()).is_err() { - eprintln!("Unable to delete lock file"); - } - - if self - .logger - .log("Daemon quitted\n", LogInfo::Info, self.debug) - .is_err() - { - eprintln!("Could not log the exit of the daemon"); - } - } -} - -impl Daemon { - pub fn new( - logger: TintinReporter, - f: fn(TintinReporter) -> Result<()>, - debug: bool, - ) -> Result { - let file = "/var/lock/matt_daemon.lock"; - if path::Path::new(file).exists() { - Err(Error::FileAlreadyLocked(0)) - } else { - Ok(Daemon { - debug, - lock_file: file.to_string(), - logger, - umask: 0.into(), - func: f, - }) - } - } - - pub fn umask(mut self, mask: u32) -> Self { - self.umask = mask.into(); - self - } - - pub fn start(self) -> Result<()> { - unsafe { - self.logger - .log("Entering daemon mode\n", LogInfo::Info, self.debug)?; - - match execute_fork()? { - ForkResult::Child => (), - ForkResult::Parent(_) => exit(libc::EXIT_SUCCESS), - } - - get_err(libc::setsid(), Error::SetSid)?; - - match execute_fork()? { - ForkResult::Child => (), - ForkResult::Parent(_) => exit(libc::EXIT_SUCCESS), - } - self.logger.log(get_pid()?, LogInfo::Info, self.debug)?; - - - self.logger - .log("Creating lock file\n", LogInfo::Debug, self.debug)?; - lock(self.lock_file.clone())?; - - self.logger - .log("Changing file mode creation\n", LogInfo::Debug, self.debug)?; - libc::umask(self.umask.inner); - - self.logger - .log("Changing working directory\n", LogInfo::Debug, self.debug)?; - get_err(libc::chdir(b"/\0" as *const u8 as _), Error::ChangeDir)?; - - self.logger - .log("Closing all open files\n", LogInfo::Debug, self.debug)?; - close_fds()?; - - self.logger - .log("Seting signal handlers\n", LogInfo::Debug, self.debug)?; - set_sig_handlers()?; - - redirect_stream()?; - self.logger.log( - "Redirecting standard streams to /dev/null\n", - LogInfo::Debug, - self.debug, - )?; - - self.logger - .log("Daemon started properly\n", LogInfo::Info, self.debug)?; - - (self.func)(self.logger.clone())?; - - self.logger.send_mail()?; - } - Ok(()) - } -} diff --git a/daemonize/src/logger.rs b/daemonize/src/logger.rs deleted file mode 100644 index 88e1aa2..0000000 --- a/daemonize/src/logger.rs +++ /dev/null @@ -1,155 +0,0 @@ -use std::{fmt::Display, fs, io::Write, path::PathBuf}; - -use crate::error::{Error, Result}; -use chrono::offset::Local; -use lettre::{ - message::{header::ContentType, Attachment, MessageBuilder}, - transport::smtp::authentication::Credentials, - Message, SmtpTransport, Transport, -}; - -// const LOGFILE: PathBuf = PathBuf::from("/var/log/matt_daemon/matt_daemon.log"); - -pub enum LogInfo { - Debug, - Error, - Info, - Warn, -} - -impl Display for LogInfo { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - LogInfo::Debug => write!(f, "\x1B[34mDEBUG\x1B[0m"), - LogInfo::Error => write!(f, "\x1B[31mERROR\x1B[0m"), - LogInfo::Info => write!(f, "\x1B[33mINFO\x1B[0m"), - LogInfo::Warn => write!(f, "\x1B[35mWarn\x1B[0m"), - } - } -} - -impl LogInfo { - fn is_debug(&self) -> bool { - matches!(self, LogInfo::Debug) - } -} - -#[derive(Clone, Debug)] -pub struct MailConfig { - username: String, - password: String, - relay: String, - dst: MessageBuilder, - mail_addr: String, -} - -#[derive(Clone)] -pub struct TintinReporter { - pub logfile: PathBuf, - pub mail: Option, -} - -impl Default for TintinReporter { - fn default() -> Self { - Self { - logfile: PathBuf::from("/var/log/matt_daemon/matt_daemon.log"), - mail: None, - } - } -} - -impl TintinReporter { - pub fn smtp( - &mut self, - username: String, - password: String, - relay: String, - dst: String, - ) -> Result<()> { - let mail = Message::builder() - .from( - "Matt Daemon " - .parse() - .map_err(|_| Error::ParseError)?, - ) - .to(dst.parse().map_err(|_| Error::ParseDstError)?); - self.mail = Some(MailConfig { - username, - password, - relay, - dst: mail, - mail_addr: dst, - }); - - Ok(()) - } - - pub fn logfile(mut self, logfile: String) -> Self { - self.logfile = PathBuf::from(logfile); - self - } - - pub fn send_mail(&self) -> Result<()> { - if self.mail.is_none() { - return Ok(()); - } - - let mail_config = match &self.mail { - None => return Ok(()), - Some(config) => config, - }; - - let smtp_credentials = - Credentials::new(mail_config.username.clone(), mail_config.password.clone()); - let client = SmtpTransport::relay(&mail_config.relay) - .map_err(Error::MailSmtpTransport)? - .credentials(smtp_credentials) - .build(); - - let filebody = fs::read(self.logfile.clone()).map_err(Error::ReadFile)?; - let attachment = Attachment::new( - self.logfile - .to_str() - .ok_or(Error::ConvertToUTF8)? - .to_string(), - ) - .body(filebody, ContentType::TEXT_PLAIN); - - let mail = &mail_config - .dst - .clone() - .subject("Recap Matt Daemon") - .singlepart(attachment) - .map_err(Error::MailBuilder)?; - let mail_addr = &mail_config.mail_addr; - - self.log( - format!("Sending a recap mail to {mail_addr}\n"), - LogInfo::Info, - false, - )?; - client.send(mail).map_err(Error::MailSend)?; - - Ok(()) - } - pub fn log(&self, msg: S, info: LogInfo, debug: bool) -> Result<()> - where - S: Display, - { - if !debug && info.is_debug() { - return Ok(()); - } - fs::create_dir_all("/var/log/matt_daemon").map_err(Error::CreateDir)?; - let mut f = fs::OpenOptions::new() - .create(true) - .write(true) - .append(true) - .open(&self.logfile) - .map_err(Error::LogOpen)?; - - let now = Local::now().format("%d / %m / %Y - %H : %M : %S"); - f.write(format!("[{now:}] - {info:5} : {msg}").as_bytes()) - .map_err(Error::Log)?; - Ok(()) - } -} diff --git a/daemonize/src/signal.rs b/daemonize/src/signal.rs deleted file mode 100644 index 3f85d11..0000000 --- a/daemonize/src/signal.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::{fs, io::Write, process}; - -use chrono::Local; - -use crate::{ - error::{get_err, Error, Result}, - file_handler::unlock, - LogInfo, -}; - -/// Can't find it in libc, this value has been taken from nyx::sys::signal, but it's the same as in -/// signal.h -const NSIG: libc::c_int = 32; - -pub fn handle_sig(value: i32) { - if let Err(e) = fs::create_dir_all("/var/log/matt_daemon").map_err(Error::CreateDir) { - eprintln!("Cannot create dir to log signal input : {e}"); - return; - } - let mut f = match fs::OpenOptions::new() - .create(true) - .write(true) - .append(true) - .open("/var/log/matt_daemon/matt_daemon.log") - .map_err(Error::LogOpen) - { - Ok(f) => f, - Err(e) => { - eprintln!("Cannot open the log file to log signal input : {e}"); - return; - } - }; - - let now = Local::now().format("%d / %m / %Y - %H : %M : %S"); - let info = LogInfo::Warn; - let msg = format!("Received signal {value}. Exiting the daemon\n"); - if let Err(e) = f - .write(format!("[{now:}] - {info:5} : {msg}").as_bytes()) - .map_err(Error::Log) - { - eprintln!("Could not log the signal input : {e}"); - return; - } - - if let Err(e) = unlock("/var/lock/matt_daemon.lock".to_string()) { - eprintln!("The lock file should be set to `/var/lock/matt_daemon.lock` : {e}"); - return; - } - - process::exit(0); -} - -pub fn set_sig_handlers() -> Result<()> { - unsafe { - for i in 1..NSIG { - // Can't overwrite SIGKILL or SIGSTOP - // SIGCHLD is up whenever I run a command in remote shell - if i == libc::SIGKILL || i == libc::SIGSTOP || i == libc::SIGCHLD { - continue; - } - get_err(libc::signal(i, handle_sig as _), Error::SignalSetting)?; - } - } - Ok(()) -} diff --git a/env_example.env b/env_example.env deleted file mode 100644 index 339877e..0000000 --- a/env_example.env +++ /dev/null @@ -1,3 +0,0 @@ -SMTPUSERNAME="toto" -SMTPPASSWORD="toto" -SMTPRELAY="smtp.gmail.com" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e6bb608 --- /dev/null +++ b/go.mod @@ -0,0 +1,33 @@ +module test + +go 1.21.1 + +require ( + github.com/go-git/go-git v4.7.0+incompatible + github.com/go-git/go-git/v5 v5.11.0 +) + +require ( + dario.cat/mergo v1.0.0 // indirect + github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect + github.com/cloudflare/circl v1.3.3 // indirect + github.com/cyphar/filepath-securejoin v0.2.4 // indirect + github.com/emirpasic/gods v1.18.1 // indirect + github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-git/go-billy/v5 v5.5.0 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect + github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/sergi/go-diff v1.1.0 // indirect + github.com/skeema/knownhosts v1.2.1 // indirect + github.com/xanzy/ssh-agent v0.3.3 // indirect + golang.org/x/crypto v0.16.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/tools v0.13.0 // indirect + gopkg.in/src-d/go-git.v4 v4.13.1 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..16ff9e4 --- /dev/null +++ b/go.sum @@ -0,0 +1,182 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg= +github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= +github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= +github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= +github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= +github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-git v4.7.0+incompatible h1:+W9rgGY4DOKKdX2x6HxSR7HNeTxqiKrOvKnuittYVdA= +github.com/go-git/go-git v4.7.0+incompatible/go.mod h1:6+421e08gnZWn30y26Vchf7efgYLe4dl5OQbBSUXShE= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= +github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= +github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= +github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= +github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= +github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2AQ= +github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4= +github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= +github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= +golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg= +gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98= +gopkg.in/src-d/go-git-fixtures.v3 v3.5.0 h1:ivZFOIltbce2Mo8IjzUHAFoq/IylO9WHhNOAJK+LsJg= +gopkg.in/src-d/go-git-fixtures.v3 v3.5.0/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g= +gopkg.in/src-d/go-git.v4 v4.13.1 h1:SRtFyV8Kxc0UP7aCHcijOMQGPxHSmMOPrzulQWolkYE= +gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQbYDu2z8= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/main.go b/main.go new file mode 100644 index 0000000..752a387 --- /dev/null +++ b/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "strings" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" +) + +func fetchFromFork(repo *git.Repository, branch string) (*object.Commit, error) { + // Get the username of the person who initiated the workflow run + username := os.Getenv("GITHUB_ACTOR") + + // Get the repository name (owner/repo) + repository := os.Getenv("GITHUB_REPOSITORY") + parts := strings.Split(repository, "/") + if len(parts) < 2 { + return nil, fmt.Errorf("invalid repository format: %s", repository) + } + + forkURL := "https://github.com/" + username + "/" + parts[1] + + cmd := exec.Command("git", "remote", "add", "fork", forkURL) + err := cmd.Run() + if err != nil { + return nil, fmt.Errorf("error adding fork as remote: %w", err) + } + + cmd = exec.Command("git", "fetch", "--depth", "1", "fork", branch) + err = cmd.Run() + if err != nil { + return nil, fmt.Errorf("error fetching branch from fork: %w", err) + } + + // Get the reference of the fetched branch + refName := plumbing.ReferenceName(fmt.Sprintf("refs/remotes/fork/%s", branch)) + ref, err := repo.Reference(refName, true) + if err != nil { + return nil, fmt.Errorf("error getting reference: %w", err) + } + + // Get the commit object of the fetched branch + branchCommit, err := repo.CommitObject(ref.Hash()) + if err != nil { + return nil, fmt.Errorf("error getting commit: %w", err) + } + + return branchCommit, nil +} + +func main() { + branch := os.Getenv("GITHUB_HEAD_REF") + + workdir, err := os.Getwd() + if err != nil { + fmt.Printf("Error getting working directory: %s", err.Error()) + return + } + + repo, err := git.PlainOpen(workdir) + if err != nil { + fmt.Printf("Error opening repo: %s", err.Error()) + return + } + + commit, err := fetchFromFork(repo, branch) + if err != nil { + fmt.Printf("Error fetching from fork: %s", err.Error()) + return + } + + fmt.Printf("Commit: %v\n", commit) +} diff --git a/usage/Cargo.toml b/usage/Cargo.toml deleted file mode 100644 index 26a984f..0000000 --- a/usage/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "matt_daemon" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -clap = { version = "3.1.18", features = ["derive"] } -daemonize = {path = "../daemonize/"} -dotenv = "0.14" -libc= "0.2" -strum = { version = "0.24", features = ["derive"] } -strum_macros = "0.24" diff --git a/usage/src/connections.rs b/usage/src/connections.rs deleted file mode 100644 index e9d95a4..0000000 --- a/usage/src/connections.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::ops::{Deref, DerefMut}; - -const POLLFLAGS: i16 = libc::POLLIN | libc::POLLRDHUP; - -pub struct Connections { - inner: Vec, -} -impl Deref for Connections { - type Target = Vec; - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl DerefMut for Connections { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -} - -impl Connections { - pub fn new() -> Self { - Self { inner: vec![] } - } - pub fn push_from_fd(&mut self, fd: i32) { - self.inner.push(libc::pollfd { - fd, - events: POLLFLAGS, - revents: 0, - }); - } -} diff --git a/usage/src/error.rs b/usage/src/error.rs deleted file mode 100644 index e69de29..0000000 diff --git a/usage/src/main.rs b/usage/src/main.rs deleted file mode 100644 index 8878649..0000000 --- a/usage/src/main.rs +++ /dev/null @@ -1,45 +0,0 @@ -use clap::Parser; -use daemonize::{Daemon, LogInfo, Result, TintinReporter}; -use smtp::get_smtp; - -mod connections; -mod smtp; - -mod usage; -use usage::MattDaemonArgs; - -mod server; -use server::server; - -fn main() -> Result<()> { - let mut reporter = TintinReporter::default(); - let args = MattDaemonArgs::parse(); - - match args.mail_send { - Some(addr) => { - if let Err(e) = get_smtp(&mut reporter, addr) { - eprintln!("{e}"); - return Err(e); - } - } - None => (), - } - let daemon = match Daemon::new(reporter.clone(), server, false) { - Ok(d) => d, - Err(e) => { - eprintln!("{e}"); - reporter.log(format!("{e}\n"), LogInfo::Error, false)?; - return Err(e); - } - }; - - match daemon.start() { - Ok(_) => Ok(()), - Err(e) => { - if let Err(e) = reporter.log(format!("Error : {e}\n"), LogInfo::Error, false) { - eprintln!("Failed to log error in daemon : {e}"); - } - Err(e) - } - } -} diff --git a/usage/src/server.rs b/usage/src/server.rs deleted file mode 100644 index 06806e1..0000000 --- a/usage/src/server.rs +++ /dev/null @@ -1,292 +0,0 @@ -use crate::connections::Connections; -use daemonize::{get_err, Error, LogInfo, Result, TintinReporter}; -use std::collections::HashMap; -use std::env; -use std::io::Write; -use std::net::{SocketAddr, TcpListener, TcpStream}; -use std::os::unix::prelude::AsRawFd; -use std::process::Command; - -use strum::FromRepr; - -const ADDRESS: &str = "127.0.0.1:4242"; -const SIZE_BUFF: usize = 400; - -const EXIT: &str = "exit"; -const QUIT: &str = "quit"; -const SHELL: &str = "shell"; - -#[derive(FromRepr, PartialEq, PartialOrd, Clone)] -enum ShellMode { - None = 1, - Shell = 2, - Bash = 3, -} - -pub struct Client { - stream: TcpStream, - addr: SocketAddr, - shell_mode: ShellMode, -} - -impl Client { - fn new(stream: TcpStream, addr: SocketAddr) -> Self { - Self { - stream, - addr, - shell_mode: ShellMode::None, - } - } - fn change_shell_mode(&mut self, nb: isize) -> Result<()> { - let tmp = ShellMode::from_repr((self.shell_mode.clone() as isize + nb) as usize) - .ok_or(Error::ShellModeOverflow)?; - self.shell_mode = tmp; - Ok(()) - } - - fn get_addr(&self) -> String { - self.addr.to_string() - } - - fn print_prompt(&mut self) -> Result<()> { - match self.shell_mode { - ShellMode::None => Ok(()), - ShellMode::Shell => self - .stream - .write_all(b"matt_daemon>") - .map_err(Error::WriteToStream), - ShellMode::Bash => self - .stream - .write_all(b"bash>") - .map_err(Error::WriteToStream), - } - } -} - -fn handle_remote_shell(msg: String, client: &mut Client, logger: &TintinReporter) -> Result<()> { - let addr = client.get_addr(); - if msg.is_empty() { - logger.log(format!("{addr} empty command\n"), LogInfo::Warn, false)?; - client.print_prompt()?; - return Ok(()); - } - if msg == EXIT || msg == QUIT { - let shell_type = match client.shell_mode { - ShellMode::Bash => "bash", - ShellMode::Shell => "shell mode", - _ => unreachable!(), - }; - - logger.log( - format!("{addr} exiting {shell_type}\n"), - LogInfo::Info, - false, - )?; - client.change_shell_mode(-1)?; - client.print_prompt()?; - return Ok(()); - } - if msg == SHELL && client.shell_mode < ShellMode::Bash { - logger.log(format!("{addr} now in bash mode\n"), LogInfo::Info, false)?; - client.change_shell_mode(1)?; - client.print_prompt()?; - return Ok(()); - } - - let (cmd, args) = match client.shell_mode { - ShellMode::Bash => { - let cmd = "/bin/sh"; - let args = vec!["-c", &*msg]; - (cmd, args) - } - ShellMode::Shell => { - let mut arguments = msg.split_whitespace(); - let cmd = arguments.next().ok_or(Error::NoArgumentProvided)?; - let args = arguments.collect(); - (cmd, args) - } - _ => unreachable!(), - }; - - let env: HashMap = env::vars().collect(); - let mut to_exec = Command::new(cmd); - let to_exec = to_exec.current_dir("/").envs(env).args(args); - - let pg = to_exec.get_program().to_str().ok_or(Error::ConvertToUTF8)?; - let mut args = format!("{pg} "); - to_exec - .get_args() - .filter_map(|arg| arg.to_str()) - .for_each(|arg| args.push_str(&*format!("{arg} "))); - let args = args.trim(); - - logger.log( - format!("{addr} is running `{args}`\n"), - LogInfo::Info, - false, - )?; - - match to_exec.output() { - Ok(res) => { - client - .stream - .write_all(&res.stdout) - .map_err(Error::WriteToStream)?; - } - Err(e) => { - let err = Error::CommandFailed(e); - return logger.log(format!("{err}\n"), LogInfo::Warn, false); - } - } - - client.print_prompt()?; - Ok(()) -} - -fn handle_client(msg: String, client: &mut Client, logger: &TintinReporter) -> Result { - let addr = client.get_addr(); - if msg == EXIT || msg == QUIT { - logger.log( - format!("{addr} asked to exit the daemon\n"), - LogInfo::Info, - false, - )?; - return Ok(true); - } - if msg == SHELL { - logger.log( - format!("{addr} is now in remote shell mode\n"), - LogInfo::Info, - false, - )?; - client.change_shell_mode(1)?; - client.print_prompt()?; - return Ok(false); - } - logger.log(format!("Read from {addr} : {msg}\n"), LogInfo::Info, false)?; - Ok(false) -} - -fn read_from_fd( - fd: i32, - idx: usize, - clients: &mut [Client], - logger: &TintinReporter, -) -> Result { - let mut data = [0_u8; SIZE_BUFF]; - let client = clients.get_mut(idx - 1).ok_or(Error::ClientGetter)?; - unsafe { - let nb = get_err( - libc::read(fd, data.as_mut_ptr() as _, SIZE_BUFF), - Error::Read, - )?; - - let msg = - String::from_utf8(data[0..(nb as usize)].to_vec()).map_err(|_| Error::ConvertToUTF8)?; - let msg = msg.trim().to_string(); - if client.shell_mode > ShellMode::None { - let res = handle_remote_shell(msg, client, logger); - match res { - Err(Error::CommandFailed(_)) => { - eprintln!("Wrong Command"); - Ok(false) - } - Ok(_) => Ok(false), - Err(e) => Err(e), - } - } else { - handle_client(msg, client, logger) - } - } -} - -fn handle_revent_error(a: i16, addr: String, logger: &TintinReporter) -> Result<()> { - if a & libc::POLLHUP != 0 { - logger.log(format!("Hanging up from {addr}\n"), LogInfo::Warn, false)?; - } else if a & libc::POLLERR != 0 { - logger.log( - format!("Error condition from {addr}\n"), - LogInfo::Warn, - false, - )?; - } else if a & libc::POLLNVAL != 0 { - logger.log( - format!("Invalid request: fd not open from {addr}\n"), - LogInfo::Warn, - false, - )?; - } else { - logger.log( - format!("Stream socket peer closed connection from {addr}\n"), - LogInfo::Warn, - false, - )?; - } - Ok(()) -} - -fn add_client( - fds: &mut Connections, - listener: &TcpListener, - streams: &mut Vec, - logger: &TintinReporter, -) -> Result<()> { - let (stream, addr) = listener.accept().map_err(Error::AcceptClient)?; - if fds.len() >= 3 { - logger.log("Already 2 clients connected\n", LogInfo::Warn, false)?; - return Ok(()); - } - fds.push_from_fd(stream.as_raw_fd()); - logger.log( - format!("Connecting to new address : {addr}\n"), - LogInfo::Info, - false, - )?; - streams.push(Client::new(stream, addr)); - Ok(()) -} - -pub fn server(logger: TintinReporter) -> Result<()> { - let listener = TcpListener::bind(ADDRESS).map_err(Error::ClientErrorBinding)?; - - let listener_fd = listener.as_raw_fd(); - let mut fds = Connections::new(); - fds.push_from_fd(listener_fd); - - let mut clients: Vec = vec![]; - loop { - unsafe { - let _ = libc::poll(fds.as_mut_ptr(), fds.len() as u64, -1); - for (i, poll_fd) in fds.clone().iter().enumerate() { - let fd = poll_fd.fd; - match poll_fd.revents { - 0 => (), - a => { - if a & libc::POLLIN != 0 && a & libc::POLLRDHUP == 0 { - if fd == listener_fd { - add_client(&mut fds, &listener, &mut clients, &logger)?; - } else if read_from_fd(fd, i, &mut clients, &logger)? { - return Ok(()); - } - } - if a & libc::POLLRDHUP != 0 - || a & libc::POLLHUP != 0 - || a & libc::POLLERR != 0 - { - eprintln!("{a}"); - let addr = clients - .get(i - 1) - .map(|client| client.get_addr()) - .unwrap_or_else(|| "Can't find address".to_string()); - - let stream = clients.remove(i - 1); - drop(stream); - fds.remove(i); - handle_revent_error(a, addr, &logger)?; - } - } - } - } - } - } -} diff --git a/usage/src/smtp.rs b/usage/src/smtp.rs deleted file mode 100644 index 1a257a2..0000000 --- a/usage/src/smtp.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::env; - -use daemonize::{Error, Result, TintinReporter}; - -pub fn get_smtp(reporter: &mut TintinReporter, dst: String) -> Result<()> { - match dotenv::dotenv() { - Ok(_) => (), - Err(e) => { - return Err(Error::DotEnv(e)); - } - }; - let username = match env::var("SMTPUSERNAME") { - Ok(v) => v, - Err(e) => { - return Err(Error::DotEnvUsername(e)); - } - }; - let password = match env::var("SMTPPASSWORD") { - Ok(v) => v, - Err(e) => { - return Err(Error::DotEnvPassword(e)); - } - }; - let relay = match env::var("SMTPRELAY") { - Ok(v) => v, - Err(e) => { - return Err(Error::DotEnvRelay(e)); - } - }; - reporter.smtp(username, password, relay, dst)?; - Ok(()) -} diff --git a/usage/src/usage.rs b/usage/src/usage.rs deleted file mode 100644 index 4e48d8c..0000000 --- a/usage/src/usage.rs +++ /dev/null @@ -1,9 +0,0 @@ -use clap::Parser; - -#[derive(Parser, Debug)] -pub struct MattDaemonArgs { - /// Tell the email adress you want to send the logfile - /// Must have the following format : "name " - #[clap(short)] - pub mail_send: Option, -}