Add message forwarding
parent: tbd commit: e4fa992
1 | use |
2 | , | HashMap
3 | , | SocketAddr
4 | |
5 | , |
6 | Arc, |
7 | , |
8 | ; |
9 | |
10 | use Error; |
11 | use ; |
12 | use |
13 | , | ConnectionError
14 | , |
15 | ; |
16 | use RsaPublicKey; |
17 | use Serialize; |
18 | use Value; |
19 | use |
20 | , | TcpStream
21 | , |
22 | ; |
23 | use ; |
24 | |
25 | use crate:: |
26 | , | AuthenticationTokenGranter
27 | , |
28 | , | wrap_forwarded
29 | , | InstanceConnections
30 | , | NetworkMessage
31 | ; |
32 | |
33 | use |
34 | , handshake_handle, | authentication_handle
35 | , user_handle, Connections, | repository_handle
36 | ; |
37 | |
38 | pub async |
39 | socket: , |
40 | connections: , |
41 | repository_backend: , |
42 | user_backend: , |
43 | auth_granter: , |
44 | addr: SocketAddr, |
45 | instance: impl , |
46 | instance_connections: , |
47 | |
48 | let connection_state = ConnectionState |
49 | socket: new, |
50 | connections, |
51 | repository_backend, |
52 | user_backend, |
53 | auth_granter, |
54 | addr, |
55 | instance: instance.to_owned, |
56 | handshaked: new, |
57 | cached_keys: default, |
58 | ; |
59 | |
60 | let mut handshaked = false; |
61 | |
62 | loop |
63 | let mut socket = connection_state.socket.lock .await; |
64 | let message = socket.next .await; |
65 | drop; |
66 | |
67 | match message |
68 | Some => |
69 | let payload = match message |
70 | => payload, | Binary
71 | => | Ping
72 | let mut socket = connection_state.socket.lock .await; |
73 | let _ = socket.send .await; |
74 | drop; |
75 | continue; |
76 | |
77 | => return, | Close
78 | _ => continue, |
79 | ; |
80 | |
81 | let message = NetworkMessage; |
82 | |
83 | if !handshaked |
84 | if handshake_handle .await.is_ok |
85 | if connection_state.handshaked.load |
86 | handshaked = true; |
87 | |
88 | |
89 | else |
90 | let raw = .unwrap; |
91 | |
92 | if let Some = &raw.target_instance |
93 | // Forward request |
94 | let mut instance_connections = instance_connections.lock .await; |
95 | let pool = instance_connections.get_or_open .unwrap; |
96 | let pool_clone = pool.clone; |
97 | drop; |
98 | |
99 | let result = wrap_forwarded .await; |
100 | |
101 | let mut socket = connection_state.socket.lock .await; |
102 | let _ = socket.send .await; |
103 | |
104 | continue; |
105 | |
106 | |
107 | let message_type = &raw.message_type; |
108 | |
109 | match authentication_handle .await |
110 | Err => |
111 | let _ = connection_state.send .await; |
112 | |
113 | Ok => continue, |
114 | Ok => |
115 | |
116 | |
117 | match repository_handle .await |
118 | Err => |
119 | let _ = connection_state.send .await; |
120 | |
121 | Ok => continue, |
122 | Ok => |
123 | |
124 | |
125 | match user_handle .await |
126 | Err => |
127 | let _ = connection_state.send .await; |
128 | |
129 | Ok => continue, |
130 | Ok => |
131 | |
132 | |
133 | match authentication_handle .await |
134 | Err => |
135 | let _ = connection_state.send .await; |
136 | |
137 | Ok => continue, |
138 | Ok => |
139 | |
140 | |
141 | error! |
142 | "Message completely unhandled: {}", |
143 | unwrap | from_utf8 .
144 | ; |
145 | |
146 | |
147 | Some => |
148 | error!; |
149 | return; |
150 | |
151 | _ => |
152 | info!; |
153 | continue; |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | socket: , |
162 | pub connections: , |
163 | pub repository_backend: , |
164 | pub user_backend: , |
165 | pub auth_granter: , |
166 | pub addr: SocketAddr, |
167 | pub instance: Instance, |
168 | pub handshaked: , |
169 | pub cached_keys: , |
170 | |
171 | |
172 | |
173 | pub async |
174 | let payload = to_string?; |
175 | info!; |
176 | self.socket |
177 | .lock |
178 | .await |
179 | .send |
180 | .await?; |
181 | |
182 | Ok |
183 | |
184 | |
185 |