diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-20 22:54:17 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-20 22:57:39 +0200 |
commit | 7071dfe8259716375bb9f1373ea0b88a39102efa (patch) | |
tree | 32404c4af2324519ff09fc026dead77700d2ba86 | |
parent | 9ddb4d7786509bf5995bd5e254c611a05ea50eba (diff) | |
download | hurrycurry-7071dfe8259716375bb9f1373ea0b88a39102efa.tar hurrycurry-7071dfe8259716375bb9f1373ea0b88a39102efa.tar.bz2 hurrycurry-7071dfe8259716375bb9f1373ea0b88a39102efa.tar.zst |
server can register
-rw-r--r-- | Cargo.lock | 467 | ||||
-rw-r--r-- | server/Cargo.toml | 1 | ||||
-rw-r--r-- | server/protocol/src/registry.rs | 4 | ||||
-rw-r--r-- | server/registry/src/main.rs | 4 | ||||
-rw-r--r-- | server/src/lib.rs | 9 | ||||
-rw-r--r-- | server/src/main.rs | 33 | ||||
-rw-r--r-- | server/src/register.rs | 80 |
7 files changed, 584 insertions, 14 deletions
@@ -173,6 +173,12 @@ dependencies = [ ] [[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -754,6 +760,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[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 = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] name = "fs_extra" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -921,6 +951,25 @@ dependencies = [ ] [[package]] +name = "h2" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e8ac6999421f49a846c2d4411f337e53497d8ec55d67753beffa43c5d9205" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http 1.1.0", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] name = "half" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -997,6 +1046,29 @@ dependencies = [ ] [[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http 1.1.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "pin-project-lite", +] + +[[package]] name = "httparse" version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1101,6 +1173,7 @@ dependencies = [ "log", "pollster", "rand 0.9.0-alpha.2", + "reqwest", "serde", "serde_json", "serde_yml", @@ -1120,9 +1193,9 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", + "h2 0.3.26", "http 0.2.12", - "http-body", + "http-body 0.4.6", "httparse", "httpdate", "itoa", @@ -1135,6 +1208,89 @@ dependencies = [ ] [[package]] +name = "hyper" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2 0.4.6", + "http 1.1.0", + "http-body 1.0.1", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" +dependencies = [ + "futures-util", + "http 1.1.0", + "hyper 1.4.1", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper 1.4.1", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http 1.1.0", + "http-body 1.0.1", + "hyper 1.4.1", + "pin-project-lite", + "socket2", + "tokio", + "tower", + "tower-service", + "tracing", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] name = "image" version = "0.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1202,6 +1358,12 @@ dependencies = [ ] [[package]] +name = "ipnet" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" + +[[package]] name = "is-terminal" version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1249,6 +1411,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0" [[package]] +name = "js-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +dependencies = [ + "wasm-bindgen", +] + +[[package]] name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1458,6 +1629,23 @@ dependencies = [ ] [[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] name = "new_debug_unreachable" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1571,12 +1759,50 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] +name = "openssl" +version = "0.10.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9529f4786b70a3e8c61e11179af17ab6188ad8d0ded78c5529441ed39d4bd9c1" +dependencies = [ + "bitflags 2.6.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +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.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] name = "overload" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1641,6 +1867,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] +name = "pin-project" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "pin-project-lite" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1993,6 +2239,49 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] +name = "reqwest" +version = "0.12.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8f4955649ef5c38cc7f9e8aa41761d48fb9677197daea9984dc54f56aad5e63" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.4.6", + "http 1.1.0", + "http-body 1.0.1", + "http-body-util", + "hyper 1.4.1", + "hyper-rustls", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows-registry", +] + +[[package]] name = "rgb" version = "0.8.48" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2081,7 +2370,7 @@ dependencies = [ "either", "futures", "http 0.2.12", - "hyper", + "hyper 0.14.30", "indexmap", "log", "memchr", @@ -2303,6 +2592,18 @@ dependencies = [ ] [[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 = "serde_yml" version = "0.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2445,6 +2746,36 @@ dependencies = [ ] [[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +dependencies = [ + "futures-core", +] + +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.6.0", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] name = "system-deps" version = "6.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2549,6 +2880,21 @@ dependencies = [ ] [[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] name = "tokio" version = "1.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2578,6 +2924,16 @@ dependencies = [ ] [[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] name = "tokio-rustls" version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2675,6 +3031,27 @@ dependencies = [ ] [[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "pin-project", + "pin-project-lite", + "tokio", + "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" @@ -2794,12 +3171,27 @@ dependencies = [ ] [[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] name = "unicode-xid" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2812,6 +3204,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] name = "users" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2851,6 +3254,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" [[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] name = "version-compare" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2915,6 +3324,18 @@ dependencies = [ ] [[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] name = "wasm-bindgen-macro" version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2944,6 +3365,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" [[package]] +name = "web-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] name = "weezl" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2993,6 +3424,36 @@ dependencies = [ ] [[package]] +name = "windows-registry" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +dependencies = [ + "windows-result", + "windows-strings", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", +] + +[[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/server/Cargo.toml b/server/Cargo.toml index 70e36dc7..04b2fd8b 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -17,6 +17,7 @@ serde_yml = "0.0.11" rand = "0.9.0-alpha.2" shlex = "1.3.0" clap = { version = "4.5.15", features = ["derive"] } +reqwest = { version = "0.12.7", features = ["json"] } pollster = "0.3.0" bincode = "2.0.0-rc.3" xdg = "2.5.2" diff --git a/server/protocol/src/registry.rs b/server/protocol/src/registry.rs index f1e9a083..92f2c454 100644 --- a/server/protocol/src/registry.rs +++ b/server/protocol/src/registry.rs @@ -6,7 +6,7 @@ pub struct Entry { pub address: Vec<String>, pub players_online: usize, pub last_game: i64, - pub version: (usize, usize), + pub version: (u32, u32), } #[derive(Debug, Deserialize, Serialize)] @@ -15,6 +15,6 @@ pub struct Submission { pub name: String, pub players: usize, pub last_game: i64, - pub version: (usize, usize), + pub version: (u32, u32), pub uri: String, } diff --git a/server/registry/src/main.rs b/server/registry/src/main.rs index 9ba06e49..c24aecbe 100644 --- a/server/registry/src/main.rs +++ b/server/registry/src/main.rs @@ -31,7 +31,7 @@ fn main() { address: var("BIND_ADDR") .map(|a| IpAddr::from_str(&a).unwrap()) .unwrap_or(IpAddr::V4(Ipv4Addr::LOCALHOST)), - port: var("PORT").map(|p| p.parse().unwrap()).unwrap_or(8000), + port: var("PORT").map(|p| p.parse().unwrap()).unwrap_or(27033), ..Default::default() }) .manage(registry) @@ -105,7 +105,7 @@ struct InternalEntry { address: HashMap<String, Instant>, players_online: usize, last_game: i64, - version: (usize, usize), + version: (u32, u32), } impl Default for InternalEntry { diff --git a/server/src/lib.rs b/server/src/lib.rs index c35a2b5a..ea514d5b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -27,6 +27,7 @@ pub mod commands; pub mod data; pub mod entity; pub mod interaction; +pub mod register; pub mod scoreboard; pub mod server; pub mod state; @@ -72,7 +73,9 @@ macro_rules! trm_param { (t, $x:expr) => { hurrycurry_protocol::Message::Tile($x) }; - (m, $x:expr) => { $x }; + (m, $x:expr) => { + $x + }; } #[derive(Debug)] @@ -110,5 +113,7 @@ macro_rules! tre_param { (t, $x:expr) => { hurrycurry_protocol::Message::Tile($x) }; - (m, $x:expr) => { $x }; + (m, $x:expr) => { + $x + }; } diff --git a/server/src/main.rs b/server/src/main.rs index 20d9908d..4db43c64 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -21,6 +21,7 @@ use futures_util::{SinkExt, StreamExt}; use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION}; use hurrycurry_server::{ data::DATA_DIR, + register::Register, server::{GameServerExt, Server}, trm, ConnectionID, }; @@ -45,7 +46,7 @@ use tokio::{ use tokio_tungstenite::tungstenite::Message; #[derive(Parser)] -struct Args { +pub(crate) struct Args { /// Print the version, then exit #[arg(short, long)] version: bool, @@ -55,6 +56,18 @@ struct Args { /// Set the address on which the server should listen #[arg(short, long, default_value = "0.0.0.0:27032")] listen: SocketAddr, + /// Enables submissions to the public server registry + #[arg(long)] + register: bool, + /// Enables mDNS discoverability + #[arg(long)] + discoverable: bool, + /// Server name + #[arg(long, short = 'N', default_value = "A Hurry Curry! Server")] + server_name: String, + /// Uri for connecting remotely for registry submission + #[arg(long)] + register_uri: Option<String>, } fn main() -> Result<()> { @@ -70,7 +83,7 @@ fn main() -> Result<()> { exit(0); } - let data_dir = if let Some(d) = args.data_dir { + let data_dir = if let Some(d) = args.data_dir.clone() { d } else { let d = PathBuf::from_str( @@ -94,12 +107,12 @@ fn main() -> Result<()> { tokio::runtime::Builder::new_multi_thread() .enable_all() .build()? - .block_on(run(args.listen))?; + .block_on(run(args))?; Ok(()) } -async fn run(addr: SocketAddr) -> anyhow::Result<()> { - let ws_listener = TcpListener::bind(addr).await?; +async fn run(args: Args) -> anyhow::Result<()> { + let ws_listener = TcpListener::bind(args.listen).await?; info!("Listening for websockets on {}", ws_listener.local_addr()?); let (tx, rx) = broadcast::channel::<PacketC>(128 * 1024); @@ -108,6 +121,16 @@ async fn run(addr: SocketAddr) -> anyhow::Result<()> { state.load(state.index.generate("lobby").await?, None); let state = Arc::new(RwLock::new(state)); + if args.register { + let r = Register::new( + args.server_name.clone(), + args.listen.port(), + args.register_uri, + state.clone(), + ); + tokio::task::spawn(r.register_loop()); + } + { let state = state.clone(); spawn(async move { diff --git a/server/src/register.rs b/server/src/register.rs new file mode 100644 index 00000000..ef940fbf --- /dev/null +++ b/server/src/register.rs @@ -0,0 +1,80 @@ +use crate::server::Server; +use anyhow::{bail, Result}; +use hurrycurry_protocol::{registry::Submission, VERSION}; +use log::{info, warn}; +use rand::random; +use reqwest::{header::USER_AGENT, Client, Url}; +use std::{str::FromStr, sync::Arc, time::Duration}; +use tokio::{sync::RwLock, time::interval}; + +const REGISTRY_URI: &'static str = "https://hurrycurry-registry.metamuffin.org"; + +pub struct Register { + name: String, + port: u16, + register_uri: Option<String>, + state: Arc<RwLock<Server>>, + client: Client, + secret: u128, + players: usize, +} + +impl Register { + pub fn new( + name: String, + port: u16, + register_uri: Option<String>, + state: Arc<RwLock<Server>>, + ) -> Self { + Self { + name, + register_uri, + players: 0, + port, + secret: random(), + state, + client: Client::new(), + } + } + pub async fn register_loop(mut self) { + let mut interval = interval(Duration::from_secs(60)); + loop { + interval.tick().await; + self.players = self.state.read().await.count_chefs(); + if let Err(e) = self.register().await { + warn!("register error: {e}") + } + } + } + // TODO ip v6 + pub async fn register(&mut self) -> Result<()> { + let res = self + .client + .post(Url::from_str(&format!("{REGISTRY_URI}/v1/register")).unwrap()) + .header( + USER_AGENT, + format!("hurrycurry-server {}", env!("CARGO_PKG_VERSION")), + ) + .json(&Submission { + last_game: 0, + name: self.name.clone(), + uri: self + .register_uri + .clone() + .unwrap_or_else(|| format!("ws://0.0.0.0:{}", self.port)), + players: self.players, + secret: self.secret, + version: VERSION, + }) + .send() + .await?; + + let r = res.text().await?; + if r == "ok" { + info!("register ok"); + Ok(()) + } else { + bail!("{r}"); + } + } +} |