Git repository hosting, collaboration, and discovery for the Fediverse.
Add connection pool
parent: tbd commit: 98d3b04
1 | use Infallible; |
2 | use SocketAddr; |
3 | use FromStr; |
4 | use Arc; |
5 | |
6 | use Error; |
7 | use ; |
8 | use ; |
9 | use ; |
10 | use UnvalidatedUserAuthenticated; |
11 | use RepositoryVisibility; |
12 | use User; |
13 | use |
14 | , |
15 | |
16 | |
17 | AuthenticationMessage, AuthenticationRequest, AuthenticationResponse, |
18 | AuthenticationTokenRequest, TokenExtensionRequest, |
19 | , |
20 | |
21 | CreateRepositoryRequest, RepositoryInfoRequest, RepositoryMessage, |
22 | RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
23 | , |
24 | InstanceAuthenticated, MessageKind, |
25 | , |
26 | |
27 | , | Instance
28 | , |
29 | , |
30 | ; |
31 | use ; |
32 | use Serialize; |
33 | use TcpStream; |
34 | use ; |
35 | use ; |
36 | |
37 | type Socket = ; |
38 | |
39 | |
40 | extern crate tracing; |
41 | |
42 | |
43 | our_instance: Instance, |
44 | our_private_key: , |
45 | our_public_key: , |
46 | target_instance: , |
47 | |
48 | |
49 | |
50 | type Error: Error + Send + Sync + 'static; |
51 | |
52 | ; |
53 | |
54 | |
55 | |
56 | type Error = Err; |
57 | |
58 | |
59 | from_str |
60 | |
61 | |
62 | |
63 | |
64 | type Error = Infallible; |
65 | |
66 | |
67 | Ok |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | Ok |
74 | our_instance: instance.into_instance?, |
75 | our_private_key: None, |
76 | our_public_key: None, |
77 | target_instance: None, |
78 | |
79 | |
80 | |
81 | |
82 | instance: impl AsInstance, |
83 | other: impl AsInstance, |
84 | |
85 | Ok |
86 | our_instance: instance.into_instance?, |
87 | our_private_key: None, |
88 | our_public_key: None, |
89 | target_instance: Some, |
90 | |
91 | |
92 | |
93 | |
94 | self.our_private_key = Some; |
95 | |
96 | self |
97 | |
98 | |
99 | |
100 | self.our_public_key = Some; |
101 | |
102 | self |
103 | |
104 | |
105 | pub async |
106 | Ok |
107 | configuration: new |
108 | our_private_key: self.our_private_key.take .unwrap, |
109 | our_public_key: self.our_public_key.take .unwrap, |
110 | target_instance: self.target_instance.take, |
111 | // todo |
112 | target_public_key: None, |
113 | , |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | target_instance: Instance, |
120 | |
121 | |
122 | |
123 | |
124 | type Type = Socket; |
125 | type Error = Error; |
126 | |
127 | async |
128 | info!; |
129 | let mut connection = connect_to .await?; |
130 | |
131 | // Handshake first! |
132 | ?; | handle_handshake .await
133 | |
134 | Ok |
135 | |
136 | |
137 | async |
138 | Ok |
139 | |
140 | |
141 | |
142 | |
143 | pub our_private_key: String, |
144 | pub our_public_key: String, |
145 | pub target_instance: , |
146 | pub target_public_key: , |
147 | |
148 | |
149 | |
150 | ; |
151 | |
152 | |
153 | |
154 | instance: impl , |
155 | |
156 | Ok |
157 | builder |
158 | target_instance: instance.to_owned, |
159 | |
160 | .build?, |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | configuration: , |
168 | |
169 | |
170 | |
171 | pub async |
172 | if let Some = &self.configuration.target_public_key |
173 | public_key.clone |
174 | else |
175 | assert!; |
176 | |
177 | self.configuration.our_public_key.clone |
178 | |
179 | |
180 | |
181 | /// Register on an [`Instance`]. |
182 | /// |
183 | /// # Authorization |
184 | /// - Must be made by the same instance its being sent to |
185 | pub async |
186 | &self, |
187 | username: String, |
188 | email: , |
189 | password: String, |
190 | pool: &DaemonConnectionPool, |
191 | |
192 | let mut connection = pool.0.get .await.unwrap; |
193 | |
194 | let message = new |
195 | RegisterAccountRequest |
196 | username, |
197 | email, |
198 | password, |
199 | , |
200 | pool.0.manager .target_instance.clone, |
201 | self.configuration.our_private_key.clone, |
202 | |
203 | .unwrap; |
204 | |
205 | Self send_message |
206 | & Authentication |
207 | , | RegisterAccount
208 | , |
209 | &mut connection, |
210 | |
211 | .await?; |
212 | |
213 | while let Ok = self.next_payload .await |
214 | if let Ok |
215 | , | RegisterAccount
216 | = from_slice |
217 | |
218 | return Ok; |
219 | |
220 | |
221 | |
222 | unreachable! |
223 | |
224 | |
225 | /// Create repository on the target instance. |
226 | pub async |
227 | &self, |
228 | user_token: String, |
229 | name: String, |
230 | description: , |
231 | visibility: RepositoryVisibility, |
232 | default_branch: String, |
233 | owner: User, |
234 | pool: &DaemonConnectionPool, |
235 | |
236 | let mut connection = pool.0.get .await.unwrap; |
237 | |
238 | let target_respository = Repository |
239 | owner: owner.clone, |
240 | name: name.clone, |
241 | instance: self |
242 | .configuration |
243 | .target_instance |
244 | .as_ref |
245 | .unwrap_or |
246 | .clone, |
247 | ; |
248 | |
249 | let request = CreateRepositoryRequest |
250 | name, |
251 | description, |
252 | visibility, |
253 | default_branch, |
254 | owner, |
255 | ; |
256 | |
257 | let message = new |
258 | request, |
259 | user_token, |
260 | self.configuration.our_private_key.clone, |
261 | |
262 | .unwrap; |
263 | |
264 | Self send_message |
265 | & Repository |
266 | target: target_respository, |
267 | command: Request |
268 | message, |
269 | , |
270 | , |
271 | &mut connection, |
272 | |
273 | .await?; |
274 | |
275 | while let Ok = self.next_payload .await |
276 | if let Ok |
277 | command: |
278 | , | Response
279 | .. |
280 | = from_slice |
281 | |
282 | return Ok; |
283 | |
284 | |
285 | |
286 | unreachable! |
287 | |
288 | |
289 | pub async |
290 | &mut self, |
291 | token: &str, |
292 | repository: Repository, |
293 | pool: &DaemonConnectionPool, |
294 | |
295 | let mut connection = pool.0.get .await.unwrap; |
296 | |
297 | let message = new |
298 | RepositoryInfoRequest |
299 | repository: repository.clone, |
300 | extra_metadata: true, |
301 | rev: None, |
302 | path: None, |
303 | , |
304 | token.to_string, |
305 | self.configuration.our_private_key.clone, |
306 | |
307 | .unwrap; |
308 | |
309 | Self send_message |
310 | & Repository |
311 | target: repository.clone, |
312 | command: Request, |
313 | , |
314 | &mut connection, |
315 | |
316 | .await?; |
317 | |
318 | loop |
319 | // while let Ok(payload) = Self::next_payload(&mut socket).await { |
320 | let payload = match self.next_payload .await |
321 | Ok => payload, |
322 | Err => |
323 | error!; |
324 | continue; |
325 | |
326 | ; |
327 | |
328 | if let Ok |
329 | command: |
330 | , | Response
331 | .. |
332 | = from_slice |
333 | |
334 | return Ok; |
335 | |
336 | |
337 | |
338 | |
339 | /// Requests an authentication token for the given login. |
340 | /// |
341 | /// # Authorization |
342 | /// This request can only be sent to the same instance from which |
343 | /// it is issued. |
344 | pub async |
345 | &mut self, |
346 | secret_key: String, |
347 | username: String, |
348 | password: String, |
349 | pool: &DaemonConnectionPool, |
350 | |
351 | let mut connection = pool.0.get .await.unwrap; |
352 | |
353 | let request = new |
354 | AuthenticationTokenRequest |
355 | secret_key, |
356 | username, |
357 | password, |
358 | , |
359 | pool.0.manager .target_instance.clone, |
360 | include_str! .to_string, |
361 | |
362 | .unwrap; |
363 | |
364 | Self send_message |
365 | & Authentication |
366 | , | AuthenticationToken
367 | , |
368 | &mut connection, |
369 | |
370 | .await?; |
371 | |
372 | loop |
373 | // while let Ok(payload) = Self::next_payload(&mut socket).await { |
374 | let payload = match self.next_payload .await |
375 | Ok => payload, |
376 | Err => |
377 | error!; |
378 | continue; |
379 | |
380 | ; |
381 | |
382 | if let Ok |
383 | , | AuthenticationToken
384 | = from_slice |
385 | |
386 | return Ok; |
387 | |
388 | |
389 | |
390 | |
391 | /// Requests a new token for the given login. |
392 | /// |
393 | /// # Authorization |
394 | /// This request can only be sent to the same instance from which |
395 | /// it is issued. |
396 | pub async |
397 | &mut self, |
398 | secret_key: String, |
399 | token: String, |
400 | pool: &DaemonConnectionPool, |
401 | |
402 | let mut connection = pool.0.get .await.unwrap; |
403 | |
404 | let request = new |
405 | TokenExtensionRequest , |
406 | pool.0.manager .target_instance.clone, |
407 | self.configuration.our_private_key.clone, |
408 | |
409 | .unwrap; |
410 | |
411 | Self send_message |
412 | & Authentication |
413 | , | TokenExtension
414 | , |
415 | &mut connection, |
416 | |
417 | .await?; |
418 | |
419 | while let Ok = self.next_payload .await |
420 | if let Ok |
421 | , | TokenExtension
422 | = from_slice |
423 | |
424 | return Ok; |
425 | |
426 | |
427 | |
428 | todo! |
429 | |
430 | |
431 | async |
432 | let url = &instance.url; |
433 | info! |
434 | "Connecting to {}", |
435 | format! |
436 | ; |
437 | let = |
438 | connect_async .await?; |
439 | info!; |
440 | |
441 | Ok |
442 | |
443 | |
444 | async |
445 | socket: &mut Socket, |
446 | instance: &Instance, |
447 | |
448 | // Send handshake initiation |
449 | |
450 | Self send_message |
451 | & Handshake |
452 | identity: instance.clone, |
453 | version: version, |
454 | , |
455 | socket, |
456 | |
457 | .await?; |
458 | |
459 | while let Some = socket.next .await |
460 | let message = match message |
461 | Ok => message, |
462 | Err => |
463 | error!; |
464 | continue; |
465 | |
466 | ; |
467 | |
468 | let payload = match message |
469 | => text.into_bytes, | Text
470 | => bytes, | Binary
471 | => continue, | Ping
472 | => continue, | Pong
473 | => | Close
474 | panic! |
475 | |
476 | _ => unreachable!, |
477 | ; |
478 | |
479 | info!; |
480 | |
481 | let message = match |
482 | Ok => message, |
483 | Err => |
484 | error!; |
485 | continue; |
486 | |
487 | ; |
488 | |
489 | if let Handshake = message |
490 | match handshake |
491 | => unimplemented!, | Initiate
492 | => | Response
493 | let message = if !validate_version |
494 | error! |
495 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
496 | version, |
497 | response.version |
498 | ; |
499 | |
500 | HandshakeFinalize |
501 | else |
502 | info!; |
503 | |
504 | HandshakeFinalize |
505 | ; |
506 | // Send HandshakeMessage::Finalize |
507 | Self send_message |
508 | & Handshake, |
509 | socket, |
510 | |
511 | .await?; |
512 | |
513 | => | Finalize
514 | if finalize.success |
515 | return Ok; |
516 | else |
517 | panic! |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | Ok |
525 | |
526 | |
527 | async |
528 | message: &T, |
529 | socket: &mut Socket, |
530 | |
531 | socket |
532 | .send |
533 | .await?; |
534 | |
535 | Ok |
536 | |
537 | |
538 | async |
539 | while let Some = socket.next .await |
540 | let message = message?; |
541 | |
542 | match message |
543 | => return Ok, | Text
544 | => return Ok, | Binary
545 | => continue, | Ping
546 | => continue, | Pong
547 | => | Close
548 | panic! |
549 | |
550 | _ => unreachable!, |
551 | |
552 | |
553 | |
554 | unreachable! |
555 | |
556 | |
557 |