why2-chat 2.0.1

Lightweight, fast and secure chat application powered by WHY2 encryption.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
174
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
This is part of WHY2
Copyright (C) 2022-2026 Václav Šmejkal

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use std::
{
    path::PathBuf,
    net::SocketAddr,
    time::{ Instant, Duration },
    collections::HashMap,
    sync::{ Arc, Mutex as MutexSync },
};

use tokio::
{
    sync::Mutex,
    net::tcp::OwnedWriteHalf,
    task::AbortHandle,
};

use zeroize::Zeroizing;

use crate::
{
    config,
    role::Role,
    consts::SharedKeys,
};

//STRUCTS
#[derive(Clone)]
pub struct AvailableFile //UPLOADED FILE
{
    pub hash: [u8; 32],   //FILE HASH
    pub path: PathBuf,    //PATH
    pub filename: String, //FILENAME
    pub size: u64,        //FILE SIZE
    pub key: Zeroizing<Vec<i64>>,
    pub nonce: Vec<i64>,
}

#[derive(Clone)]
pub struct Attach //SCREEN ATTACHMENT
{
    pub stream: Arc<Mutex<OwnedWriteHalf>>, //RECEIVE STREAM
    pub target_id: usize,                   //ID OF SCREENSHARER
    pub token: [u8; 32],                    //TOKEN FOR REXSTREAM
}

//ENUMS
pub enum ConnectionType //TYPES OF TCP CHANNEL
{
    FileUpload
    {
        uid: u64,
    },
    FileDownload
    {
        uid: u64,
        file: AvailableFile,
    },
    Screen,
    Attach
    {
        id: usize,
    },
}

#[derive(Clone)]
pub enum Connection //CLIENT CONNECTION (WHAT IS PUSHED TO connections LIST)
{
    Authenticated
    {
        write_stream: Arc<Mutex<OwnedWriteHalf>>,                //STREAM
        task: AbortHandle,                                       //HANDLER TASK (USED TO FORCE-CLOSE THE CONNECTION)
        file_streams: Arc<MutexSync<HashMap<u64, AbortHandle>>>, //ACTIVE FILE TRANSFER TASKS
        screen_stream: Option<AbortHandle>,                      //SCREEN UPLOAD TASK
        peer_addr: SocketAddr,                                   //ADDRESS & PORT
        username: String,                                        //USERNAME
        role: Role,                                              //ROLE
        id: usize,                                               //ID OF USER
        keys: SharedKeys,                                        //SHARED KEYS BETWEEN SERVER AND CLIENT (one to one)
        attached_screen: Option<Attach>,                         //SCREEN DOWNLOAD STREAM & TARGET ID
        last_activity: Instant,                                  //TIME OF LAST MESSAGE (USED FOR TIMEOUT)
        last_key_exchange: Instant,                              //TIME OF LAST REKEY
        spam_violations: usize,                                  //SPAM VIOLATIONS (unexpected, huh?)
        channel: Option<String>,                                 //CHANNEL
        seq: usize,                                              //SEQUENCE NUMBER (CLIENT -> SERVER)
        server_seq: usize,                                       //SEQUENCE NUMBER (SERVER -> CLIENT)
        alive: bool,                                             //RESPONDED TO KEEPALIVE
        muted: bool,                                             //USER HAS SAID TOO MUCH, GIVING HIM A REST
    },

    NonAuthenticated
    {
        write_stream: Arc<Mutex<OwnedWriteHalf>>, //STREAM
        task: AbortHandle,                        //HANDLER TASK
        peer_addr: SocketAddr,                    //ADDRESS & PORT
        username: Option<String>,                 //CHOSEN USERNAME
        keys: Option<SharedKeys>,                 //SHARED KEYS
        obfuscation_key: [u8; 32],                //OBFUSCATION KEY FROM PLAIN PACKETS
        last_activity: Instant,                   //TIME OF LAST MESSAGE
        seq: usize,                               //SEQUENCE NUMBER
        connect: Instant,                         //TIME OF CONNECTION
    },
}

//IMPLEMENTATIONS
impl Connection
{
    //GET STREAM FROM Connection
    pub fn write_stream(&self) -> &Arc<Mutex<OwnedWriteHalf>>
    {
        match self
        {
            Self::Authenticated { write_stream, .. } => write_stream,
            Self::NonAuthenticated { write_stream, .. } => write_stream,
        }
    }

    //GET HANDLER TASK FROM Connection
    pub fn task(&self) -> &AbortHandle
    {
        match self
        {
            Self::Authenticated { task, .. } => task,
            Self::NonAuthenticated { task, .. } => task,
        }
    }

    //GET ALL ACTIVE FILE STREAMS
    pub fn file_streams(&self) -> Option<&Arc<MutexSync<HashMap<u64, AbortHandle>>>>
    {
        match self
        {
            Self::Authenticated { file_streams, .. } => Some(file_streams),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET PEER ADDR FROM Connection#stream
    pub fn peer_addr(&self) -> &SocketAddr
    {
        match self
        {
            Self::Authenticated { peer_addr, .. } => peer_addr,
            Self::NonAuthenticated { peer_addr, .. } => peer_addr,
        }
    }

    //GET USERNAME FROM Connection
    pub fn username(&self) -> Option<&String>
    {
        match self
        {
            Self::Authenticated { username, .. } => Some(username),
            Self::NonAuthenticated { username, .. } => username.as_ref(),
        }
    }

    //GET USERNAME FROM Connection AS MUTABLE
    pub(super) fn username_mut(&mut self) -> &mut Option<String>
    {
        match self
        {
            Self::Authenticated { .. } => panic!("Do not use username_mut() on Authenticated client"),
            Self::NonAuthenticated { username, .. } => username,
        }
    }

    //GET ROLE
    pub fn role(&self) -> Option<&Role>
    {
        match self
        {
            Self::Authenticated { role, .. } => Some(role),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET ID FROM Connection
    pub fn id(&self) -> Option<&usize>
    {
        match self
        {
            Self::Authenticated { id, .. } => Some(id),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET SHARED KEYS FROM Connection
    pub fn keys(&self) -> Option<&SharedKeys>
    {
        match self
        {
            Self::Authenticated { keys, .. } => Some(keys),
            Self::NonAuthenticated { keys, .. } => keys.as_ref(),
        }
    }

    //GET OBFUSCATION KEY
    pub fn obfuscation_key(&self) -> Option<&[u8; 32]>
    {
        match self
        {
            Self::Authenticated { .. } => None,
            Self::NonAuthenticated { obfuscation_key, .. } => Some(obfuscation_key),
        }
    }

    //GET LAST ACTIVITY FROM Connection
    pub fn last_activity(&self) -> &Instant
    {
        match self
        {
            Self::Authenticated { last_activity, .. } => last_activity,
            Self::NonAuthenticated { last_activity, .. } => last_activity,
        }
    }

    //GET LAST ACTIVITY FROM Connection AS MUTABLE
    pub fn last_activity_mut(&mut self) -> &mut Instant
    {
        match self
        {
            Self::Authenticated { last_activity, .. } => last_activity,
            Self::NonAuthenticated { last_activity, .. } => last_activity,
        }
    }

    //GET LAST KEY EXCHANGE FROM Connection
    pub fn last_key_exchange(&self) -> Option<&Instant>
    {
        match self
        {
            Self::Authenticated { last_key_exchange, .. } => Some(last_key_exchange),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET SPAM VIOLATIONS FROM Connection
    pub fn spam_violations(&self) -> Option<&usize>
    {
        match self
        {
            Self::Authenticated { spam_violations, .. } => Some(spam_violations),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET SPAM VIOLATIONS FROM Connection AS MUTABLE
    pub fn spam_violations_mut(&mut self) -> Option<&mut usize>
    {
        match self
        {
            Self::Authenticated { spam_violations, .. } => Some(spam_violations),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET CHANNEL
    pub fn channel(&self) -> &Option<String>
    {
        match self
        {
            Self::Authenticated { channel, .. } => channel,
            Self::NonAuthenticated { .. }  => &None,
        }
    }

    //GET LAST SEQUENCE NUMBER
    pub fn seq(&self) -> &usize
    {
        match self
        {
            Self::Authenticated { seq, .. } => seq,
            Self::NonAuthenticated { seq, .. } => seq,
        }
    }

    //GET LAST SEQUENCE NUMBER AS MUTABLE
    pub fn seq_mut(&mut self) -> &mut usize
    {
        match self
        {
            Self::Authenticated { seq, .. } => seq,
            Self::NonAuthenticated { seq, .. } => seq,
        }
    }

    //GET LAST SERVER SEQUENCE NUMBER
    pub fn server_seq(&self) -> Option<&usize>
    {
        match self
        {
            Self::Authenticated { server_seq, .. } => Some(server_seq),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //GET LAST SERVER SEQUENCE NUMBER AS MUTABLE
    pub fn server_seq_mut(&mut self) -> Option<&mut usize>
    {
        match self
        {
            Self::Authenticated { server_seq, .. } => Some(server_seq),
            Self::NonAuthenticated { .. } => None,
        }
    }

    //CHECK IF CONNECTION IS INACTIVE
    pub(super) fn is_inactive(&self, now: Option<Instant>) -> bool
    {
        match self
        {
            Self::Authenticated { last_activity, .. } =>
            {
                now.unwrap_or_else(Instant::now).duration_since(*last_activity) >
                    Duration::from_secs(config::read_config::<u64>("communication_time"))
            },

            Self::NonAuthenticated { connect, .. } =>
            {
                now.unwrap_or_else(Instant::now).duration_since(*connect) >
                    Duration::from_secs(config::read_config::<u64>("max_auth_time"))
            },
        }
    }

    //IS AUTHENTICATED
    pub fn is_authenticated(&self) -> bool
    {
        match self
        {
            Self::Authenticated { .. } => true,
            Self::NonAuthenticated { .. } => false,
        }
    }

    //SET CONNECTION TO ALIVE
    pub fn set_alive(&mut self, val: bool)
    {
        match self
        {
            Self::Authenticated { alive, .. } => *alive = val,
            _ => {}
        }
    }

    //CHECK IF CONNECTION IS ALIVE
    pub fn is_alive(&self) -> &bool
    {
        match self
        {
            Self::Authenticated { alive, .. } => alive,
            Self::NonAuthenticated { .. } => &false,
        }
    }

    //ADD FILE STREAM
    pub fn add_file_stream(&self, uid: u64, task: AbortHandle)
    {
        if let Self::Authenticated { file_streams, .. } = self
        {
            file_streams.lock().unwrap().insert(uid, task);
        }
    }

    //REMOVE FILE STREAM
    pub fn remove_file_stream(&self, uid: u64)
    {
        if let Self::Authenticated { file_streams, .. } = self
        {
            if let Some(task) = file_streams.lock().unwrap().remove(&uid)
            {
                task.abort(); //DROPS THE STREAM HALVES, CLOSING THE SOCKET
            }
        }
    }

    //GET SCREEN UPLOAD STREAM
    pub fn screen_stream(&self) -> &Option<AbortHandle>
    {
        match self
        {
            Self::Authenticated { screen_stream, .. } => screen_stream,
            Self::NonAuthenticated { .. } => &None,
        }
    }

    //GET ATTACHED SCREENSHARE
    pub fn attached_screen(&self) -> &Option<Attach>
    {
        match self
        {
            Self::Authenticated { attached_screen, .. } => attached_screen,
            Self::NonAuthenticated { .. } => &None,
        }
    }

    //SET ATTACHED SCREENSHARE
    pub fn attach_screen(&mut self, target_id: usize, stream: Arc<Mutex<OwnedWriteHalf>>, token: [u8; 32])
    {
        match self
        {
            Self::Authenticated { attached_screen, .. } => *attached_screen = Some(Attach
            {
                target_id,
                stream,
                token,
            }),
            _ => {},
        }
    }

    //UNSET ATTACHED SCREENSHARE
    pub fn deattach_screen(&mut self)
    {
        match self
        {
            Self::Authenticated { attached_screen, .. } =>
            {
                *attached_screen = None;
                log::info!("Stop screen attach: {}", self.peer_addr());
            },
            _ => {},
        }
    }

    //ADD SCREEN UPLOAD STREAM
    pub fn set_screen_stream(&mut self, task: AbortHandle)
    {
        //CLEAN OLD STREAM
        self.remove_screen_stream();

        if let Self::Authenticated { screen_stream, .. } = self
        {
            *screen_stream = Some(task);
        }
    }

    //TAKE SCREEN UPLOAD STREAM WITHOUT ABORTING IT (FOR THE SHARE TASK TEARING ITSELF DOWN)
    pub fn take_screen_stream(&mut self) -> Option<(usize, AbortHandle)>
    {
        if let Self::Authenticated { screen_stream, peer_addr, id, .. } = self
        {
            if let Some(old_task) = screen_stream.take()
            {
                log::info!("Stop screenshare: {}", peer_addr);

                return Some((*id, old_task));
            }
        }

        None
    }

    //REMOVE SCREEN UPLOAD STREAM
    pub fn remove_screen_stream(&mut self) -> Option<usize>
    {
        self.take_screen_stream().map(|(id, old_task)|
        {
            old_task.abort();
            id
        })
    }

    //MUTE
    pub fn toggle_mute(&mut self)
    {
        if let Self::Authenticated { muted, .. } = self
        {
            *muted = !*muted;
        }
    }

    //SET ROLE
    pub fn set_role(&mut self, new_role: Role)
    {
        if let Self::Authenticated { role, .. } = self
        {
            *role = new_role;
        }
    }

    //MUTED
    pub fn muted(&self) -> &bool
    {
        match self
        {
            Self::Authenticated { muted, .. } => muted,
            Self::NonAuthenticated { .. } => &false,
        }
    }
}