hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! Public discovery for the AI bridge. |
| 2 | //! |
| 3 | //! Two reads: the bridge's certificate, which a service needs before it can |
| 4 | //! dial in, and the capabilities currently on offer, which a client needs |
| 5 | //! before it offers an AI feature it cannot get an answer from. |
| 6 | //! |
| 7 | //! An AI service needs the bridge's certificate before it can dial in, and the |
| 8 | //! certificate is regenerated on every boot when no PEM pair is configured. So |
| 9 | //! it is published here: the service fetches this over HTTP, pins what it gets, |
| 10 | //! and then connects over QUIC. |
| 11 | //! |
| 12 | //! Unauthenticated on purpose. A server certificate is public by construction — |
| 13 | //! it is handed to every peer during the TLS handshake — so serving it reveals |
| 14 | //! nothing a port scan would not. The private key never leaves the process. |
| 15 | //! |
| 16 | //! What this is *not*: a substitute for a CA. Fetch-then-pin over plain HTTP is |
| 17 | //! trust-on-first-use, only as trustworthy as that HTTP hop. On a hostile |
| 18 | //! network, put a real certificate in `AI_TLS_CERT`/`AI_TLS_KEY` and distribute |
| 19 | //! it out of band. |
| 20 | |
| 21 | use BTreeMap; |
| 22 | |
| 23 | use Json; |
| 24 | use State; |
| 25 | use Engine; |
| 26 | use Serialize; |
| 27 | use ToSchema; |
| 28 | use OpenApiRouter; |
| 29 | use routes; |
| 30 | |
| 31 | use cratetls; |
| 32 | use crateAI_PROTOCOL; |
| 33 | use crate; |
| 34 | use crateAppState; |
| 35 | use crateCurrentUser; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | /// Everything a service needs to reach the bridge, except the shared token |
| 46 | /// (which is a secret and is configured out of band). |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | /// The AI bridge's certificate (PEM + SHA-256 fingerprint) for a service to |
| 62 | /// pin; `404` when the bridge is off. |
| 63 | /// |
| 64 | /// Public: a server certificate is presented to every peer in the TLS |
| 65 | /// handshake, so publishing it discloses nothing. |
| 66 | /// |
| 67 | /// **Re-fetch on every reconnect.** With no `AI_TLS_CERT`/`AI_TLS_KEY` |
| 68 | /// configured the bridge generates a fresh self-signed certificate at each |
| 69 | /// boot, so a certificate pinned once at service startup goes stale the moment |
| 70 | /// the *backend* restarts. A service whose reconnect loop only redials will |
| 71 | /// then fail forever; one that re-fetches this first recovers on its own. |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | async |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | /// What the bridge can currently do, grouped by capability. |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | /// One capability's live fleet: how many workers serve it, and the work in |
| 110 | /// flight across them. |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | /// The capabilities connected AI services are serving right now, per |
| 124 | /// capability, with the work in flight — a discovery read for an operator or a |
| 125 | /// service deciding what it may ask this bridge to do. |
| 126 | /// |
| 127 | /// **`200`, even with the bridge off.** This lists what is available rather |
| 128 | /// than probing a dependency, so a disabled bridge answers `enabled: false` |
| 129 | /// and an empty list, not a `503` — a caller learns *that* there is no AI |
| 130 | /// fleet rather than that the request itself failed. |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | async |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | /// Wrap DER bytes as a PEM certificate: base64 in 64-character lines between |
| 174 | /// the standard armour, which is what every TLS library's loader expects. |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 |