Update version checks
parent: tbd commit: 1ed7c39
1 | use ; |
2 | |
3 | use Error; |
4 | use ; |
5 | use Version; |
6 | use Serialize; |
7 | use |
8 | , | TcpStream
9 | |
10 | , |
11 | Mutex, |
12 | , |
13 | , | JoinHandle
14 | ; |
15 | use ; |
16 | |
17 | use crate:: |
18 | , | AuthenticationTokenGranter
19 | , |
20 | , |
21 | , | Listeners
22 | |
23 | |
24 | AuthenticationMessage, AuthenticationRequest, AuthenticationResponse, |
25 | TokenExtensionResponse, |
26 | , |
27 | |
28 | RepositoryMessage, RepositoryMessageKind, RepositoryRequest, RepositoryResponse, |
29 | , |
30 | MessageKind, |
31 | , |
32 | |
33 | , |
34 | , | Repository
35 | , | User
36 | , |
37 | validate_version, version, |
38 | ; |
39 | |
40 | |
41 | pub task: , |
42 | |
43 | |
44 | |
45 | pub instance: InstanceMeta, |
46 | pub sender: , |
47 | pub task: , |
48 | |
49 | |
50 | /// Represents a connection which hasn't finished the handshake. |
51 | |
52 | pub socket: , |
53 | |
54 | |
55 | |
56 | |
57 | pub connections: , |
58 | pub instance_connections: , |
59 | |
60 | |
61 | pub async |
62 | mut socket: , |
63 | listeners: , |
64 | connections: , |
65 | backend: , |
66 | user_backend: , |
67 | auth_granter: , |
68 | discovery_backend: , |
69 | addr: SocketAddr, |
70 | |
71 | let mut handshaked = false; |
72 | let this_instance = Instance |
73 | url: String from, |
74 | ; |
75 | |
76 | while let Some = socket.next .await |
77 | let message = match message |
78 | Ok => message, |
79 | Err => |
80 | error!; |
81 | continue; |
82 | |
83 | ; |
84 | |
85 | let payload = match message |
86 | => text.into_bytes, | Text
87 | => bytes, | Binary
88 | => continue, | Ping
89 | => continue, | Pong
90 | => | Close
91 | info!; |
92 | |
93 | return; |
94 | |
95 | _ => unreachable!, |
96 | ; |
97 | |
98 | let message = match |
99 | Ok => message, |
100 | Err => |
101 | error!; |
102 | continue; |
103 | |
104 | ; |
105 | |
106 | // info!("Read payload: {}", std::str::from_utf8(&payload).unwrap()); |
107 | |
108 | if let Handshake = message |
109 | match handshake |
110 | => | Initiate
111 | // Send HandshakeMessage::Response |
112 | let message = HandshakeResponse |
113 | identity: this_instance.clone, |
114 | version: version, |
115 | ; |
116 | |
117 | let version_check = validate_version; |
118 | |
119 | let _result = if !version_check |
120 | error! |
121 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
122 | from_str |
123 | .unwrap, |
124 | request.version |
125 | ; |
126 | |
127 | send |
128 | &mut socket, |
129 | Handshake |
130 | success: false, |
131 | , |
132 | |
133 | .await |
134 | else |
135 | send |
136 | &mut socket, |
137 | , | Handshake
138 | |
139 | .await |
140 | ; |
141 | |
142 | continue; |
143 | |
144 | => | Response
145 | // Check version |
146 | let message = if validate_version |
147 | error! |
148 | "Version compatibility failure! Our Version: {}, Their Version: {}", |
149 | version, |
150 | response.version |
151 | ; |
152 | |
153 | HandshakeFinalize |
154 | else |
155 | info!; |
156 | |
157 | HandshakeFinalize |
158 | ; |
159 | |
160 | let _result = send |
161 | &mut socket, |
162 | , | Handshake
163 | |
164 | .await; |
165 | |
166 | continue; |
167 | |
168 | => | Finalize
169 | if !response.success |
170 | error!; |
171 | return; |
172 | |
173 | |
174 | handshaked = true; |
175 | |
176 | // Send HandshakeMessage::Finalize |
177 | let message = HandshakeFinalize ; |
178 | |
179 | let _result = send |
180 | &mut socket, |
181 | , | Handshake
182 | |
183 | .await; |
184 | |
185 | continue; |
186 | |
187 | |
188 | |
189 | |
190 | if !handshaked |
191 | continue; |
192 | |
193 | |
194 | if let Repository = &message |
195 | if repository.target.instance != this_instance |
196 | info!; |
197 | // We need to send this command to a different instance |
198 | |
199 | let mut listener = send_and_get_listener .await; |
200 | |
201 | // Wait for response |
202 | while let Ok = listener.recv .await |
203 | if let Repository |
204 | command: Response, |
205 | .. |
206 | = message |
207 | |
208 | let _result = send .await; |
209 | |
210 | |
211 | continue; |
212 | else |
213 | // This message is targeting this instance |
214 | match &repository.command |
215 | => match request.clone | Request
216 | => | CreateRepository
217 | let mut backend = backend.lock .await; |
218 | let request = request.validate .await.unwrap; |
219 | let response = backend.create_repository .await; |
220 | |
221 | let response = match response |
222 | Ok => response, |
223 | Err => |
224 | error!; |
225 | continue; |
226 | |
227 | ; |
228 | drop; |
229 | |
230 | let _result = send |
231 | &mut socket, |
232 | Repository |
233 | target: repository.target.clone, |
234 | command: Response |
235 | , | CreateRepository
236 | , |
237 | , |
238 | |
239 | .await; |
240 | |
241 | continue; |
242 | |
243 | => | RepositoryFileInspect
244 | let mut backend = backend.lock .await; |
245 | let request = request.validate .await.unwrap; |
246 | let response = backend.repository_file_inspect; |
247 | |
248 | let response = match response |
249 | Ok => response, |
250 | Err => |
251 | error!; |
252 | continue; |
253 | |
254 | ; |
255 | drop; |
256 | |
257 | let _result = send |
258 | &mut socket, |
259 | Repository |
260 | target: repository.target.clone, |
261 | command: Response |
262 | , | RepositoryFileInspection
263 | , |
264 | , |
265 | |
266 | .await; |
267 | |
268 | continue; |
269 | |
270 | => | RepositoryInfo
271 | let mut backend = backend.lock .await; |
272 | let request = request.validate .await.unwrap; |
273 | let response = backend.repository_info .await; |
274 | |
275 | let response = match response |
276 | Ok => response, |
277 | Err => |
278 | error!; |
279 | continue; |
280 | |
281 | ; |
282 | drop; |
283 | |
284 | let _result = send |
285 | &mut socket, |
286 | Repository |
287 | target: repository.target.clone, |
288 | command: Response |
289 | , | RepositoryInfo
290 | , |
291 | , |
292 | |
293 | .await; |
294 | |
295 | continue; |
296 | |
297 | => | IssuesCount
298 | let request = &request.validate .await.unwrap; |
299 | |
300 | let mut backend = backend.lock .await; |
301 | let response = backend.issues_count; |
302 | |
303 | let response = match response |
304 | Ok => response, |
305 | Err => |
306 | error!; |
307 | continue; |
308 | |
309 | ; |
310 | drop; |
311 | |
312 | let _result = send |
313 | &mut socket, |
314 | Repository |
315 | target: repository.target.clone, |
316 | command: Response |
317 | , | IssuesCount
318 | , |
319 | , |
320 | |
321 | .await; |
322 | |
323 | continue; |
324 | |
325 | => | IssueLabels
326 | let request = &request.validate .await.unwrap; |
327 | |
328 | let mut backend = backend.lock .await; |
329 | let response = backend.issue_labels; |
330 | |
331 | let response = match response |
332 | Ok => response, |
333 | Err => |
334 | error!; |
335 | continue; |
336 | |
337 | ; |
338 | drop; |
339 | |
340 | let _result = send |
341 | &mut socket, |
342 | Repository |
343 | target: repository.target.clone, |
344 | command: Response |
345 | , | IssueLabels
346 | , |
347 | , |
348 | |
349 | .await; |
350 | |
351 | continue; |
352 | |
353 | => | Issues
354 | let request = request.validate .await.unwrap; |
355 | |
356 | let mut backend = backend.lock .await; |
357 | let response = backend.issues; |
358 | |
359 | let response = match response |
360 | Ok => response, |
361 | Err => |
362 | error!; |
363 | continue; |
364 | |
365 | ; |
366 | drop; |
367 | |
368 | let _result = send |
369 | &mut socket, |
370 | Repository |
371 | target: repository.target.clone, |
372 | command: Response |
373 | , | Issues
374 | , |
375 | , |
376 | |
377 | .await; |
378 | |
379 | continue; |
380 | |
381 | , |
382 | => | Response
383 | unreachable! |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | if let Authentication = &message |
390 | match authentication |
391 | => match request | Request
392 | => | AuthenticationToken
393 | let mut granter = auth_granter.lock .await; |
394 | |
395 | let response = granter.token_request .await.unwrap; |
396 | drop; |
397 | |
398 | let _result = send |
399 | &mut socket, |
400 | Authentication |
401 | , | AuthenticationToken
402 | , |
403 | |
404 | .await; |
405 | |
406 | continue; |
407 | |
408 | => | TokenExtension
409 | let mut granter = auth_granter.lock .await; |
410 | |
411 | let response = granter |
412 | .extension_request |
413 | .await |
414 | .unwrap_or; |
415 | drop; |
416 | |
417 | let _result = send |
418 | &mut socket, |
419 | Authentication |
420 | , | TokenExtension
421 | , |
422 | |
423 | .await; |
424 | |
425 | continue; |
426 | |
427 | => | RegisterAccount
428 | let request = request.inner .await.clone; |
429 | |
430 | let mut user_backend = user_backend.lock .await; |
431 | |
432 | let response = user_backend.register .await.unwrap; |
433 | drop; |
434 | |
435 | let _result = send |
436 | &mut socket, |
437 | Authentication |
438 | , | RegisterAccount
439 | , |
440 | |
441 | .await; |
442 | |
443 | continue; |
444 | |
445 | , |
446 | => unreachable!, | Response
447 | |
448 | |
449 | |
450 | if let Discovery = &message |
451 | let mut backend = discovery_backend.lock .await; |
452 | backend.try_handle .await.unwrap; |
453 | |
454 | continue; |
455 | |
456 | |
457 | |
458 | info!; |
459 | |
460 | |
461 | async |
462 | message: MessageKind, |
463 | listeners: & , |
464 | connections: & , |
465 | |
466 | let : = |
467 | match &message |
468 | => | Handshake
469 | todo! |
470 | |
471 | => , | Repository
472 | => todo!, | Authentication
473 | => todo!, | Discovery
474 | ; |
475 | |
476 | let target = match |
477 | => instance.clone, |
478 | => user.instance.clone, |
479 | => repository.instance.clone, |
480 | _ => unreachable!, |
481 | ; |
482 | |
483 | let mut listeners = listeners.lock .await; |
484 | let listener = listeners.add; |
485 | drop; |
486 | |
487 | let connections = connections.lock .await; |
488 | |
489 | if let Some = connections.instance_connections.get |
490 | if let Err = connection.sender.send |
491 | error!; |
492 | |
493 | else |
494 | error!; |
495 | |
496 | panic!; |
497 | |
498 | |
499 | drop; |
500 | |
501 | listener |
502 | |
503 | |
504 | async |
505 | socket: &mut , |
506 | message: T, |
507 | |
508 | socket |
509 | .send |
510 | .await?; |
511 | |
512 | Ok |
513 | |
514 |