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