From 96a9e2a498296effa6f5e5d14aa3a1f0ca884bfd Mon Sep 17 00:00:00 2001 From: ppc Date: Mon, 5 Jan 2026 21:10:05 +0000 Subject: [PATCH] initial rust commit --- amnet-server/Cargo.lock | 810 +++++++++++++++++++++++++++++++++++ amnet-server/Cargo.toml | 16 + amnet-server/src/amcard.rs | 87 ++++ amnet-server/src/amserver.rs | 108 +++++ amnet-server/src/config.rs | 121 ++++++ amnet-server/src/lib.rs | 10 + amnet-server/src/metrics.rs | 23 + amnet-server/src/utils.rs | 38 ++ 8 files changed, 1213 insertions(+) create mode 100644 amnet-server/Cargo.lock create mode 100644 amnet-server/Cargo.toml create mode 100644 amnet-server/src/amcard.rs create mode 100644 amnet-server/src/amserver.rs create mode 100644 amnet-server/src/config.rs create mode 100644 amnet-server/src/lib.rs create mode 100644 amnet-server/src/metrics.rs create mode 100644 amnet-server/src/utils.rs diff --git a/amnet-server/Cargo.lock b/amnet-server/Cargo.lock new file mode 100644 index 0000000..a6d7605 --- /dev/null +++ b/amnet-server/Cargo.lock @@ -0,0 +1,810 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "amnet-server" +version = "2.0.0" +dependencies = [ + "axum", + "hostname", + "log", + "rust-ini", + "serde", + "tokio", + "tower-http", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "axum" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b52af3cb4058c895d37317bb27508dccc8e5f2d39454016b297bf4a400597b8" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "const-random" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" +dependencies = [ + "const-random-macro", +] + +[[package]] +name = "const-random-macro" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" +dependencies = [ + "getrandom", + "once_cell", + "tiny-keccak", +] + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "dlv-list" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "hostname" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617aaa3557aef3810a6369d0a99fac8a080891b68bd9f9812a1eeda0c0730cbd" +dependencies = [ + "cfg-if", + "libc", + "windows-link", +] + +[[package]] +name = "http" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.4.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "727805d60e7938b76b826a6ef209eb70eaa1812794f9424d4a4e2d740662df5f" +dependencies = [ + "bytes", + "futures-core", + "http 1.4.0", + "http-body 1.0.1", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" + +[[package]] +name = "libc" +version = "0.2.179" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5a2d376baa530d1238d133232d15e239abad80d05838b4b59354e5268af431f" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "ordered-multimap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" +dependencies = [ + "dlv-list", + "hashbrown", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9695f8df41bb4f3d222c95a67532365f569318332d03d5f3f67f37b20e6ebdf0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.10.0", +] + +[[package]] +name = "rust-ini" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "796e8d2b6696392a43bea58116b667fb4c29727dc5abd27d6acf338bb4f688c7" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + +[[package]] +name = "ryu" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] +name = "syn" +version = "2.0.113" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678faa00651c9eb72dd2020cbdf275d92eccb2400d568e419efdd64838145cb4" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + +[[package]] +name = "tokio" +version = "1.49.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tower" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f873044bf02dd1e8239e9c1293ea39dad76dc594ec16185d0a1bf31d8dc8d858" +dependencies = [ + "bitflags 1.3.2", + "bytes", + "futures-core", + "futures-util", + "http 0.2.12", + "http-body 0.4.6", + "http-range-header", + "pin-project-lite", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "zmij" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30e0d8dffbae3d840f64bda38e28391faef673a7b5a6017840f2a106c8145868" diff --git a/amnet-server/Cargo.toml b/amnet-server/Cargo.toml new file mode 100644 index 0000000..d1111d8 --- /dev/null +++ b/amnet-server/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "amnet-server" +version = "2.0.0" +edition = "2024" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +axum = "0.8.8" +hostname = "0.4" +log = "0.4" +rust-ini = "0.21" +serde = { version = "1.0", features = ["derive"] } +tokio = { version = "1", features = ["full"] } +tower-http = { version = "0.3", features = ["cors"] } \ No newline at end of file diff --git a/amnet-server/src/amcard.rs b/amnet-server/src/amcard.rs new file mode 100644 index 0000000..68cfac8 --- /dev/null +++ b/amnet-server/src/amcard.rs @@ -0,0 +1,87 @@ +use std::{sync::{Mutex, OnceLock}, time::{Duration, Instant}}; + +use crate::utils::{parse_access_code, parse_idm_hex}; + +#[derive(Debug, Clone)] +pub (in crate) struct StoredCard { + pub access_code: [u8; 10], + pub card_idm: Option<[u8; 8]>, + + pub expiry_time: Instant +} + +impl StoredCard { + pub fn is_expired(&self) -> bool { + Instant::now() > self.expiry_time + } +} + +pub enum CardPresentResult { + Accepted, + Rejected(String /* reason */), + SlotUnavailable(Instant /* release time */), +} + +static PRESENTER: OnceLock = OnceLock::new(); + +pub (in crate) struct CardPresenter { + current_card: Mutex>, +} + +impl Default for CardPresenter { + fn default() -> Self { + Self { + current_card: Mutex::new(None), + } + } +} + +impl CardPresenter { + pub fn instance() -> &'static CardPresenter { + PRESENTER.get_or_init(|| CardPresenter::default()) + } + + pub fn request_card(&mut self, require_idm: Option) -> Option { + let mut guard = self.current_card.lock().unwrap(); + let card = guard.as_ref()?; + + if let Some(require) = require_idm { + if card.card_idm.is_some() != require { + return None; + } + } + + guard.take() + } + + pub fn present_card(&self, access_code: &String, idm_hex: Option<&String>, validity: Duration) -> CardPresentResult { + let mut guard = self.current_card.lock().unwrap(); + + if let Some(card) = guard.as_ref() && !card.is_expired() { + return CardPresentResult::SlotUnavailable(card.expiry_time); + } + + let access_code_bytes = match parse_access_code(access_code) { + Some(bytes) => bytes, + None => return CardPresentResult::Rejected("Invalid access code format".to_string()), + }; + + let mut card_idm_bytes = None; + if let Some(idm_hex) = idm_hex { + match parse_idm_hex(idm_hex) { + Some(bytes) => card_idm_bytes = Some(bytes), + None => return CardPresentResult::Rejected("Invalid IDm format".to_string()), + } + } + + let stored_card = StoredCard { + access_code: access_code_bytes, + card_idm: card_idm_bytes, + expiry_time: Instant::now() + validity, + }; + + *guard = Some(stored_card); + + CardPresentResult::Accepted + } +} \ No newline at end of file diff --git a/amnet-server/src/amserver.rs b/amnet-server/src/amserver.rs new file mode 100644 index 0000000..96cf034 --- /dev/null +++ b/amnet-server/src/amserver.rs @@ -0,0 +1,108 @@ +use std::time::Instant; + +use axum::{Json, http::{StatusCode, header}, response::{IntoResponse, Response}}; +use serde::{Deserialize, Serialize}; + +use crate::{amcard::{CardPresentResult, CardPresenter}, config::Config, metrics::SystemMetrics}; + +pub const API_VERSION: u8 = 1; + +#[derive(Serialize)] +struct ServerError { + error: String, +} + +#[derive(Serialize)] +struct ServerState { + #[serde(rename = "apiVersion")] + api_version: u8, + + #[serde(rename = "gameId")] + game_id: Option, + + #[serde(rename = "serverName")] + server_name: String, + + #[serde(rename = "sessionUptime")] + session_uptime: u64, + + #[serde(rename = "timeSinceLastPoll")] + time_since_last_poll: u64, +} + +pub fn server_state_handler() -> Json { + let config = Config::instance(); + let metrics = SystemMetrics::instance(); + + let state = ServerState { + api_version: API_VERSION, + game_id: config.game_id.clone(), + server_name: config.server_name.clone(), + session_uptime: metrics.start_time.map_or(0, |start| start.elapsed().as_secs()), + time_since_last_poll: metrics.last_poll_time.map_or(0, |last_poll| last_poll.elapsed().as_secs()), + }; + + Json(state) +} + +#[derive(Deserialize)] +struct CardSubmissionRequest { + #[serde(rename = "cardId")] + access_code: String, + + #[serde(rename = "physicalCardIDm")] + card_idm_hex: Option, +} + +pub fn card_submission_handler(Json(payload): Json) -> impl IntoResponse { + if payload.access_code.len() > 20 || payload.access_code.len() == 0 { + return build_error_response(StatusCode::BAD_REQUEST, "Access code is required"); + } + + if payload.access_code.chars().any(|c| !c.is_ascii_digit()) { + return build_error_response(StatusCode::UNPROCESSABLE_ENTITY, "Invalid access code provided"); + } + + if payload.access_code.chars().all(|c| c == '0') { + return build_error_response(StatusCode::FORBIDDEN, "All-zero access codes are forbidden."); + } + + if payload.card_idm_hex.as_ref().is_some_and(|idm| idm.chars().any(|c| !c.is_ascii_hexdigit())) { + return build_error_response(StatusCode::UNPROCESSABLE_ENTITY, "Invalid IDm provided"); + } + + let presenter = CardPresenter::instance().present_card( + &payload.access_code, + payload.card_idm_hex.as_ref(), + std::time::Duration::from_secs(5) + ); + + match presenter { + CardPresentResult::Accepted => StatusCode::ACCEPTED.into_response(), + CardPresentResult::Rejected(_) => StatusCode::FORBIDDEN.into_response(), + CardPresentResult::SlotUnavailable(release_time) => { + let now = Instant::now(); + let wait_secs = if release_time > now { + (release_time - now).as_secs() + } else { + 0 + }; + + let mut response = build_error_response(StatusCode::TOO_MANY_REQUESTS, "A card has already been submitted. Please wait before submitting another."); + response.headers_mut().insert( + header::RETRY_AFTER, + wait_secs.to_string().parse().unwrap(), + ); + + response + } + } +} + +fn build_error_response(status: StatusCode, message: &str) -> Response { + let error = ServerError { + error: message.to_string(), + }; + + (status, Json(error)).into_response() +} \ No newline at end of file diff --git a/amnet-server/src/config.rs b/amnet-server/src/config.rs new file mode 100644 index 0000000..62e6c34 --- /dev/null +++ b/amnet-server/src/config.rs @@ -0,0 +1,121 @@ +use ini::Ini; +use log::error; +use std::env; +use std::path::PathBuf; +use std::sync::OnceLock; + +const DEFAULT_CONFIG_FILE: &str = "./segatools.ini"; +const CONFIG_FILE_OVERRIDE_ENV: &str = "SEGATOOLS_CONFIG_PATH"; + +const DEFAULT_SERVER_NAME: &str = "AMNET-PC"; + +const DEFAULT_AIME_FILE: &str = r"DEVICE\aime.txt"; +const DEFAULT_AIME_KEYBOARD_ENABLED: bool = true; + +const DEFAULT_INCLUDE_IDM_FOR_PHYSICAL_CARDS: bool = false; + +const GAME_ID_EXACT_LEN: usize = 4; +const SERVER_NAME_MAX_LEN: usize = 16; + +static CONFIG: OnceLock = OnceLock::new(); + +pub (in crate) struct Config { + /// Game ID (4 characters or none) + pub game_id: Option, + + /// Server name shown on devices (max 16 chars). Defaults to the machine hostname. + pub server_name: String, + + /// Whether physically scanned cards should have their IDm forwarded to the game + /// instead of the access code. + pub physical_card_use_idm: bool, + + /// Whether holding the enter key should trigger a card scan using the code + /// from the aime.txt file + pub enable_aime_txt: bool, + + /// Path to the aime.txt file + pub aime_txt_path: PathBuf, +} + +impl Config { + pub fn instance() -> &'static Config { + CONFIG.get_or_init(|| { + Config::load().unwrap_or_else(|e| { + log::warn!("Failed to load config: {}. Using defaults.", e); + Config::default() + }) + }) + } + + fn load() -> Result> { + let config_path = env::var(CONFIG_FILE_OVERRIDE_ENV).unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string()); + let ini = Ini::load_from_file(&config_path).map_err(|e| format!("Failed to load config file '{}': {}", config_path, e))?; + + let io_section = ini.section(Some("aimeio")).ok_or("Missing [aimeio] section")?; + let aime_section = ini.section(Some("aime")); + + // gameId must be exactly 4 ascii letters or empty + let game_id = io_section.get("gameId").unwrap_or(""); + let game_id = if game_id.is_empty() { + None + } else if game_id.chars().count() == GAME_ID_EXACT_LEN && game_id.chars().all(|c| c.is_ascii_alphabetic()) { + Some(game_id.to_ascii_uppercase().to_string()) + } else { + error!("Invalid gameId '{game_id}': must be exactly {GAME_ID_EXACT_LEN} characters or empty"); + None + }; + + let default_hostname = hostname::get() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or_else(|| DEFAULT_SERVER_NAME.to_string()); + + let server_name = io_section.get("serverName") + .unwrap_or(&default_hostname) + .chars() + .take(SERVER_NAME_MAX_LEN) + .collect(); + + // Read boolean flags + let physical_card_use_idm = io_section.get("useAimeDBForPhysicalCards") + .and_then(|v| v.parse::().ok()) + .map(|v| v > 0) + .unwrap_or(false); + + let enable_aime_txt = io_section.get("enableKeyboardMode") + .and_then(|v| v.parse::().ok()) + .map(|v| v > 0) + .unwrap_or(DEFAULT_AIME_KEYBOARD_ENABLED); + + let aime_txt_path = aime_section + .and_then(|s| s.get("aimePath")) + .unwrap_or(DEFAULT_AIME_FILE) + .into(); + + Ok(Config { + game_id, + server_name, + physical_card_use_idm, + enable_aime_txt, + aime_txt_path, + }) + } +} + +impl Default for Config { + fn default() -> Self { + let default_hostname = hostname::get() + .ok() + .and_then(|h| h.into_string().ok()) + .unwrap_or_else(|| DEFAULT_SERVER_NAME.to_string()); + + Config { + game_id: None, + server_name: default_hostname.chars().take(SERVER_NAME_MAX_LEN).collect(), + physical_card_use_idm: DEFAULT_INCLUDE_IDM_FOR_PHYSICAL_CARDS, + enable_aime_txt: DEFAULT_AIME_KEYBOARD_ENABLED, + aime_txt_path: PathBuf::from(DEFAULT_AIME_FILE), + } + } +} diff --git a/amnet-server/src/lib.rs b/amnet-server/src/lib.rs new file mode 100644 index 0000000..ff2409e --- /dev/null +++ b/amnet-server/src/lib.rs @@ -0,0 +1,10 @@ +mod config; +mod amcard; +mod amserver; +mod metrics; +mod utils; + +#[unsafe(no_mangle)] +pub extern "C" fn say_hello() { + println!("Hello, world!"); +} diff --git a/amnet-server/src/metrics.rs b/amnet-server/src/metrics.rs new file mode 100644 index 0000000..0b90eda --- /dev/null +++ b/amnet-server/src/metrics.rs @@ -0,0 +1,23 @@ +use std::sync::OnceLock; + +static METRICS: OnceLock = OnceLock::new(); + +pub (in crate) struct SystemMetrics { + pub start_time: Option, + pub last_poll_time: Option, +} + +impl Default for SystemMetrics { + fn default() -> Self { + Self { + start_time: None, + last_poll_time: None, + } + } +} + +impl SystemMetrics { + pub fn instance() -> &'static SystemMetrics { + METRICS.get_or_init(|| SystemMetrics::default()) + } +} diff --git a/amnet-server/src/utils.rs b/amnet-server/src/utils.rs new file mode 100644 index 0000000..e1c5771 --- /dev/null +++ b/amnet-server/src/utils.rs @@ -0,0 +1,38 @@ +pub (in crate) fn format_hex_bytes(bytes: &[u8]) -> String { + bytes.chunks(2) + .map(|chunk| chunk.iter().map(|b| format!("{:02X}", b)).collect::()) + .collect::>() + .join(" ") +} + +pub (in crate) fn parse_access_code(code: &String) -> Option<[u8; 10]> { + let matrix_code = format!("{:0>20}", code.replace(" ", "")); + let mut bytes: [u8; 10] = [0; 10]; + + for i in 0..bytes.len() { + let slice_str = &matrix_code[i*2..i*2+2]; + let slice_val = u8::from_str_radix(slice_str, 16).ok()?; + + bytes[i] = slice_val; + } + + Some(bytes) +} + +pub (in crate) fn parse_idm_hex(idm_hex: &String) -> Option<[u8; 8]> { + if idm_hex.len() > 16 || !idm_hex.chars().all(|c| c.is_ascii_hexdigit()) { + return None; + } + + let padded_idm = format!("{:0>16}", idm_hex); + let mut idm_bytes: [u8; 8] = [0; 8]; + + for i in 0..idm_bytes.len() { + let slice_str = &padded_idm[i*2..i*2+2]; + let slice_val = u8::from_str_radix(slice_str, 16).ok()?; + + idm_bytes[i] = slice_val; + } + + Some(idm_bytes) +}