WIP on impl redirect use remote addr

using remote address on daemon registration as pubsub endpoint
validating pubsub node using client req remote address
remove fetching public ip
This commit is contained in:
Noor 2025-06-10 20:48:16 +05:30
parent af7d4157ee
commit 786489fcbd
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
7 changed files with 65 additions and 354 deletions

339
Cargo.lock generated

@ -263,7 +263,7 @@ dependencies = [
"fnv", "fnv",
"futures-timer", "futures-timer",
"futures-util", "futures-util",
"http 1.3.1", "http",
"indexmap 2.9.0", "indexmap 2.9.0",
"mime", "mime",
"multer", "multer",
@ -285,7 +285,7 @@ checksum = "29db05b624fb6352fc11bfe30c54ab1b16a1fe937d7c05a783f4e88ef1292b3b"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"async-graphql-parser", "async-graphql-parser",
"darling 0.20.11", "darling",
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -390,8 +390,8 @@ dependencies = [
"axum-core", "axum-core",
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"itoa", "itoa",
"matchit", "matchit",
@ -416,8 +416,8 @@ dependencies = [
"async-trait", "async-trait",
"bytes", "bytes",
"futures-util", "futures-util",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"mime", "mime",
"pin-project-lite", "pin-project-lite",
@ -908,38 +908,14 @@ dependencies = [
"syn 2.0.100", "syn 2.0.100",
] ]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core 0.10.2",
"darling_macro 0.10.2",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.20.11" version = "0.20.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee"
dependencies = [ dependencies = [
"darling_core 0.20.11", "darling_core",
"darling_macro 0.20.11", "darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.9.3",
"syn 1.0.109",
] ]
[[package]] [[package]]
@ -952,28 +928,17 @@ dependencies = [
"ident_case", "ident_case",
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim 0.11.1", "strsim",
"syn 2.0.100", "syn 2.0.100",
] ]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core 0.10.2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "darling_macro" name = "darling_macro"
version = "0.20.11" version = "0.20.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead"
dependencies = [ dependencies = [
"darling_core 0.20.11", "darling_core",
"quote", "quote",
"syn 2.0.100", "syn 2.0.100",
] ]
@ -1032,31 +997,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "derive_builder"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2658621297f2cf68762a6f7dc0bb7e1ff2cfd6583daef8ee0fed6f7ec468ec0"
dependencies = [
"darling 0.10.2",
"derive_builder_core",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_builder_core"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2791ea3e372c8495c0bc2033991d76b512cd799d07491fbd6890124db9458bef"
dependencies = [
"darling 0.10.2",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "detee-shared" name = "detee-shared"
version = "0.1.0" version = "0.1.0"
@ -1132,18 +1072,6 @@ dependencies = [
"urlencoding", "urlencoding",
] ]
[[package]]
name = "dns-lookup"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53ecafc952c4528d9b51a458d1a8904b81783feff9fde08ab6ed2545ff396872"
dependencies = [
"cfg-if",
"libc",
"socket2 0.4.10",
"winapi",
]
[[package]] [[package]]
name = "doc-comment" name = "doc-comment"
version = "0.3.3" version = "0.3.3"
@ -1221,18 +1149,6 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d"
[[package]]
name = "enum-as-inner"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "570d109b813e904becc80d8d5da38376818a143348413f7149f1340fe04754d4"
dependencies = [
"heck 0.4.1",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "env_filter" name = "env_filter"
version = "0.1.3" version = "0.1.3"
@ -1583,7 +1499,7 @@ dependencies = [
"fnv", "fnv",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"http 1.3.1", "http",
"indexmap 2.9.0", "indexmap 2.9.0",
"slab", "slab",
"tokio", "tokio",
@ -1693,17 +1609,6 @@ dependencies = [
"syn 2.0.100", "syn 2.0.100",
] ]
[[package]]
name = "http"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]] [[package]]
name = "http" name = "http"
version = "1.3.1" version = "1.3.1"
@ -1715,17 +1620,6 @@ dependencies = [
"itoa", "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]] [[package]]
name = "http-body" name = "http-body"
version = "1.0.1" version = "1.0.1"
@ -1733,7 +1627,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
dependencies = [ dependencies = [
"bytes", "bytes",
"http 1.3.1", "http",
] ]
[[package]] [[package]]
@ -1744,8 +1638,8 @@ checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"pin-project-lite", "pin-project-lite",
] ]
@ -1767,29 +1661,6 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f"
[[package]]
name = "hyper"
version = "0.14.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
dependencies = [
"bytes",
"futures-channel",
"futures-core",
"futures-util",
"http 0.2.12",
"http-body 0.4.6",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"socket2 0.5.9",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "1.6.0" version = "1.6.0"
@ -1800,8 +1671,8 @@ dependencies = [
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"h2", "h2",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"httparse", "httparse",
"httpdate", "httpdate",
"itoa", "itoa",
@ -1818,8 +1689,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d191583f3da1305256f22463b9bb0471acad48a4e534a5218b9963e9c1f59b2" checksum = "2d191583f3da1305256f22463b9bb0471acad48a4e534a5218b9963e9c1f59b2"
dependencies = [ dependencies = [
"futures-util", "futures-util",
"http 1.3.1", "http",
"hyper 1.6.0", "hyper",
"hyper-util", "hyper-util",
"rustls", "rustls",
"rustls-pki-types", "rustls-pki-types",
@ -1829,27 +1700,13 @@ dependencies = [
"webpki-roots", "webpki-roots",
] ]
[[package]]
name = "hyper-system-resolver"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6eea26c5d0b6ab9d72219f65000af310f042a740926f7b2fa3553e774036e2e7"
dependencies = [
"derive_builder",
"dns-lookup",
"hyper 0.14.32",
"tokio",
"tower-service",
"tracing",
]
[[package]] [[package]]
name = "hyper-timeout" name = "hyper-timeout"
version = "0.5.2" version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0"
dependencies = [ dependencies = [
"hyper 1.6.0", "hyper",
"hyper-util", "hyper-util",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
@ -1865,12 +1722,12 @@ dependencies = [
"bytes", "bytes",
"futures-channel", "futures-channel",
"futures-util", "futures-util",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"hyper 1.6.0", "hyper",
"libc", "libc",
"pin-project-lite", "pin-project-lite",
"socket2 0.5.9", "socket2",
"tokio", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
@ -2024,17 +1881,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[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]] [[package]]
name = "idna" name = "idna"
version = "1.0.3" version = "1.0.3"
@ -2335,12 +2181,6 @@ dependencies = [
"tendril", "tendril",
] ]
[[package]]
name = "matches"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]] [[package]]
name = "matchit" name = "matchit"
version = "0.7.3" version = "0.7.3"
@ -2441,7 +2281,7 @@ dependencies = [
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-util", "futures-util",
"http 1.3.1", "http",
"httparse", "httparse",
"memchr", "memchr",
"mime", "mime",
@ -2986,27 +2826,6 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "public-ip"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4c40db5262d93298c363a299f8bc1b3a956a78eecddba3bc0e58b76e2f419a"
dependencies = [
"dns-lookup",
"futures-core",
"futures-util",
"http 0.2.12",
"hyper 0.14.32",
"hyper-system-resolver",
"pin-project-lite",
"thiserror 1.0.69",
"tokio",
"tracing",
"tracing-futures",
"trust-dns-client",
"trust-dns-proto",
]
[[package]] [[package]]
name = "quick_cache" name = "quick_cache"
version = "0.5.2" version = "0.5.2"
@ -3032,7 +2851,7 @@ dependencies = [
"quinn-udp", "quinn-udp",
"rustc-hash", "rustc-hash",
"rustls", "rustls",
"socket2 0.5.9", "socket2",
"thiserror 2.0.12", "thiserror 2.0.12",
"tokio", "tokio",
"tracing", "tracing",
@ -3068,7 +2887,7 @@ dependencies = [
"cfg_aliases", "cfg_aliases",
"libc", "libc",
"once_cell", "once_cell",
"socket2 0.5.9", "socket2",
"tracing", "tracing",
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
@ -3284,10 +3103,10 @@ dependencies = [
"bytes", "bytes",
"futures-core", "futures-core",
"futures-util", "futures-util",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"hyper 1.6.0", "hyper",
"hyper-rustls", "hyper-rustls",
"hyper-util", "hyper-util",
"ipnet", "ipnet",
@ -3698,7 +3517,7 @@ version = "3.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d00caa5193a3c8362ac2b73be6b9e768aa5a4b2f721d8f4b339600c3cb51f8e" checksum = "8d00caa5193a3c8362ac2b73be6b9e768aa5a4b2f721d8f4b339600c3cb51f8e"
dependencies = [ dependencies = [
"darling 0.20.11", "darling",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.100", "syn 2.0.100",
@ -3830,16 +3649,6 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b" checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b"
[[package]]
name = "socket2"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.9" version = "0.5.9"
@ -3940,12 +3749,6 @@ dependencies = [
"quote", "quote",
] ]
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"
@ -3998,7 +3801,6 @@ dependencies = [
"itertools 0.14.0", "itertools 0.14.0",
"log", "log",
"nanoid", "nanoid",
"public-ip",
"rand 0.8.5", "rand 0.8.5",
"serde", "serde",
"serde_json", "serde_json",
@ -4084,7 +3886,7 @@ dependencies = [
"geo", "geo",
"geo-types", "geo-types",
"hex", "hex",
"http 1.3.1", "http",
"ipnet", "ipnet",
"jsonwebtoken", "jsonwebtoken",
"lexicmp", "lexicmp",
@ -4122,7 +3924,7 @@ dependencies = [
"sha2", "sha2",
"snap", "snap",
"storekey", "storekey",
"strsim 0.11.1", "strsim",
"subtle", "subtle",
"sysinfo", "sysinfo",
"thiserror 1.0.69", "thiserror 1.0.69",
@ -4373,7 +4175,7 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"pin-project-lite", "pin-project-lite",
"socket2 0.5.9", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@ -4469,17 +4271,17 @@ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"bytes", "bytes",
"h2", "h2",
"http 1.3.1", "http",
"http-body 1.0.1", "http-body",
"http-body-util", "http-body-util",
"hyper 1.6.0", "hyper",
"hyper-timeout", "hyper-timeout",
"hyper-util", "hyper-util",
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"prost", "prost",
"rustls-pemfile", "rustls-pemfile",
"socket2 0.5.9", "socket2",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tokio-stream", "tokio-stream",
@ -4581,18 +4383,6 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "tracing-futures"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2"
dependencies = [
"futures",
"futures-task",
"pin-project",
"tracing",
]
[[package]] [[package]]
name = "trice" name = "trice"
version = "0.4.0" version = "0.4.0"
@ -4604,51 +4394,6 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "trust-dns-client"
version = "0.20.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b4ef9b9bde0559b78a4abb00339143750085f05e5a453efb7b8bef1061f09dc"
dependencies = [
"cfg-if",
"data-encoding",
"futures-channel",
"futures-util",
"lazy_static",
"log",
"radix_trie",
"rand 0.8.5",
"thiserror 1.0.69",
"time",
"tokio",
"trust-dns-proto",
]
[[package]]
name = "trust-dns-proto"
version = "0.20.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca94d4e9feb6a181c690c4040d7a24ef34018d8313ac5044a61d21222ae24e31"
dependencies = [
"async-trait",
"cfg-if",
"data-encoding",
"enum-as-inner",
"futures-channel",
"futures-io",
"futures-util",
"idna 0.2.3",
"ipnet",
"lazy_static",
"log",
"rand 0.8.5",
"smallvec",
"thiserror 1.0.69",
"tinyvec",
"tokio",
"url",
]
[[package]] [[package]]
name = "try-lock" name = "try-lock"
version = "0.2.5" version = "0.2.5"
@ -4664,7 +4409,7 @@ dependencies = [
"byteorder", "byteorder",
"bytes", "bytes",
"data-encoding", "data-encoding",
"http 1.3.1", "http",
"httparse", "httparse",
"log", "log",
"rand 0.8.5", "rand 0.8.5",
@ -4705,12 +4450,6 @@ version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.18" version = "1.0.18"
@ -4779,7 +4518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60"
dependencies = [ dependencies = [
"form_urlencoded", "form_urlencoded",
"idna 1.0.3", "idna",
"percent-encoding", "percent-encoding",
] ]

@ -23,7 +23,6 @@ thiserror = "2.0.12"
nanoid = "0.4.0" nanoid = "0.4.0"
dotenv = "0.15.0" dotenv = "0.15.0"
hex = "0.4.3" hex = "0.4.3"
public-ip = { version = "0.2.2", features = ["dns-resolver"] }
[profile.release] [profile.release]
lto = true lto = true

@ -32,6 +32,8 @@ async fn main() {
let db = db::db_connection(&db_url, &db_user, &db_pass, &db_ns, &db_name).await.unwrap(); let db = db::db_connection(&db_url, &db_user, &db_pass, &db_ns, &db_name).await.unwrap();
let db_arc = Arc::new(db); let db_arc = Arc::new(db);
let addr = BRAIN_GRPC_ADDR.parse().unwrap();
let snp_daemon_server = BrainVmDaemonServer::new(VmDaemonServer::new(db_arc.clone())); let snp_daemon_server = BrainVmDaemonServer::new(VmDaemonServer::new(db_arc.clone()));
let snp_cli_server = BrainVmCliServer::new(VmCliServer::new(db_arc.clone())); let snp_cli_server = BrainVmCliServer::new(VmCliServer::new(db_arc.clone()));
let general_service_server = BrainGeneralCliServer::new(GeneralCliServer::new(db_arc.clone())); let general_service_server = BrainGeneralCliServer::new(GeneralCliServer::new(db_arc.clone()));
@ -53,7 +55,7 @@ async fn main() {
.add_service(general_service_server) .add_service(general_service_server)
.add_service(sgx_daemon_server) .add_service(sgx_daemon_server)
.add_service(sgx_cli_server) .add_service(sgx_cli_server)
.serve(*BRAIN_GRPC_ADDR) .serve(addr)
.await .await
.unwrap(); .unwrap();
} }

@ -1,14 +1,6 @@
use std::{ use std::sync::LazyLock;
net::{IpAddr, SocketAddr},
sync::LazyLock,
};
use tokio::sync::OnceCell;
pub const BRAIN_GRPC_PORT: u16 = 31337; pub const BRAIN_GRPC_ADDR: &str = "0.0.0.0:31337";
pub static BRAIN_GRPC_ADDR: LazyLock<SocketAddr> = LazyLock::new(|| {
let ip = IpAddr::from([0, 0, 0, 0]);
SocketAddr::new(ip, BRAIN_GRPC_PORT)
});
pub const CERT_PATH: &str = "/etc/detee/brain/brain-crt.pem"; pub const CERT_PATH: &str = "/etc/detee/brain/brain-crt.pem";
pub const CERT_KEY_PATH: &str = "/etc/detee/brain/brain-key.pem"; pub const CERT_KEY_PATH: &str = "/etc/detee/brain/brain-key.pem";
@ -29,21 +21,6 @@ pub static ADMIN_ACCOUNTS: LazyLock<Vec<String>> = LazyLock::new(|| {
.unwrap_or(default_admin_keys) .unwrap_or(default_admin_keys)
}); });
pub static CURRENT_BRAIN_ENDPOINT: OnceCell<SocketAddr> = OnceCell::const_new();
pub async fn current_brain_endpoint() -> Result<SocketAddr, &'static str> {
if !CURRENT_BRAIN_ENDPOINT.initialized() {
let pub_ip = public_ip::addr().await.ok_or("Could not determine public IP")?;
let brain_endpoint = *CURRENT_BRAIN_ENDPOINT
.get_or_init(|| async { SocketAddr::new(pub_ip, BRAIN_GRPC_PORT) })
.await;
Ok(brain_endpoint)
} else {
let brain_endpoint = *CURRENT_BRAIN_ENDPOINT.get().unwrap_or(&BRAIN_GRPC_ADDR);
Ok(brain_endpoint)
}
}
pub const OLD_BRAIN_DATA_PATH: &str = "./saved_data.yaml"; pub const OLD_BRAIN_DATA_PATH: &str = "./saved_data.yaml";
pub const ACCOUNT: &str = "account"; pub const ACCOUNT: &str = "account";

@ -3,7 +3,7 @@ pub mod general;
pub mod types; pub mod types;
pub mod vm; pub mod vm;
use crate::constants::{current_brain_endpoint, ADMIN_ACCOUNTS}; use crate::constants::ADMIN_ACCOUNTS;
use crate::db::prelude as db; use crate::db::prelude as db;
use detee_shared::app_proto::*; use detee_shared::app_proto::*;
use detee_shared::common_proto::{Empty, Pubkey}; use detee_shared::common_proto::{Empty, Pubkey};
@ -16,26 +16,21 @@ use surrealdb::engine::remote::ws::Client;
use surrealdb::{RecordId, Surreal}; use surrealdb::{RecordId, Surreal};
use tonic::{Request, Status}; use tonic::{Request, Status};
pub async fn set_pubsub_node(db: &Surreal<Client>, id: RecordId) { pub async fn set_pubsub_node(db: &Surreal<Client>, local_addr: &str, id: RecordId) {
match current_brain_endpoint().await { dbg!(&local_addr);
Ok(public_endpoint) => { match db.query(format!("UPDATE $id SET pub_sub_node = '{local_addr}'",)).bind(("id", id)).await
let addr = public_endpoint.to_string(); {
match db Ok(res) => log::info!("Updated pub_sub_node {:?}", res),
.query(format!("UPDATE $id SET pub_sub_node = '{addr}'",)) Err(e) => log::error!("Could not update pub_sub_node {:?}", e),
.bind(("id", id))
.await
{
Ok(res) => log::info!("Updated pub_sub_node {:?}", res),
Err(e) => log::error!("Could not update pub_sub_node {:?}", e),
}
}
_ => {
log::error!("Could not determine public brain endpoint. Using local one.");
}
} }
} }
pub async fn check_pubsub_node(db: &Surreal<Client>, id: RecordId) -> Result<String, db::Error> { pub async fn check_pubsub_node(
db: &Surreal<Client>,
local_addr: &str,
id: RecordId,
) -> Result<String, db::Error> {
dbg!(&local_addr);
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct PubSubNode { struct PubSubNode {
pub pub_sub_node: String, pub pub_sub_node: String,
@ -46,13 +41,9 @@ pub async fn check_pubsub_node(db: &Surreal<Client>, id: RecordId) -> Result<Str
.ok_or(Status::internal("Could not get current brain endpoint"))? .ok_or(Status::internal("Could not get current brain endpoint"))?
.pub_sub_node; .pub_sub_node;
let current_endpoint = crate::constants::current_brain_endpoint() dbg!(&local_addr, &node_endpoint);
.await
.map_err(|_| Status::internal("Could not get current brain endpoint"))?;
dbg!(&current_endpoint, &node_endpoint); if local_addr == node_endpoint {
if current_endpoint.to_string() == node_endpoint {
return Ok(node_endpoint); return Ok(node_endpoint);
} }

@ -36,6 +36,7 @@ impl BrainVmDaemon for VmDaemonServer {
&self, &self,
req: Request<RegisterVmNodeReq>, req: Request<RegisterVmNodeReq>,
) -> Result<Response<Self::RegisterVmNodeStream>, Status> { ) -> Result<Response<Self::RegisterVmNodeStream>, Status> {
let local_addr = req.local_addr().map(|addr| addr.to_string()).unwrap_or_default();
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("Starting registration process for {:?}", req); info!("Starting registration process for {:?}", req);
let id = surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone())); let id = surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone()));
@ -59,7 +60,7 @@ impl BrainVmDaemon for VmDaemonServer {
} }
.register(&self.db) .register(&self.db)
.await?; .await?;
set_pubsub_node(&self.db, id).await; set_pubsub_node(&self.db, &local_addr, id).await;
info!("Sending deleted contracts to {}", req.node_pubkey); info!("Sending deleted contracts to {}", req.node_pubkey);
let deleted_vms = db::DeletedVm::list_by_node(&self.db, &req.node_pubkey).await?; let deleted_vms = db::DeletedVm::list_by_node(&self.db, &req.node_pubkey).await?;
@ -223,9 +224,11 @@ impl BrainVmCli for VmCliServer {
type ListVmNodesStream = Pin<Box<dyn Stream<Item = Result<VmNodeListResp, Status>> + Send>>; type ListVmNodesStream = Pin<Box<dyn Stream<Item = Result<VmNodeListResp, Status>> + Send>>;
async fn new_vm(&self, req: Request<NewVmReq>) -> Result<Response<NewVmResp>, Status> { async fn new_vm(&self, req: Request<NewVmReq>) -> Result<Response<NewVmResp>, Status> {
let local_addr = req.local_addr().map(|addr| addr.to_string()).unwrap_or_default();
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
let id = surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone())); let id = surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone()));
check_pubsub_node(&self.db, id).await?; check_pubsub_node(&self.db, &local_addr, id).await?;
// TODO: make it atleast 1 hour // TODO: make it atleast 1 hour
if req.locked_nano < 100 { if req.locked_nano < 100 {

@ -344,7 +344,7 @@ async fn test_pub_sub_redirect() {
let id = surrealdb::RecordId::from((VM_NODE, node_key.pubkey.clone())); let id = surrealdb::RecordId::from((VM_NODE, node_key.pubkey.clone()));
let endpoint = check_pubsub_node(&db, id).await; let endpoint = check_pubsub_node(&db, "", id).await;
dbg!(&endpoint); dbg!(&endpoint);
// let mut client_vm_cli = BrainVmCliClient::new(brain_channel.clone()); // let mut client_vm_cli = BrainVmCliClient::new(brain_channel.clone());