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
use std::net::IpAddr;
use base64;
use sha1::Sha1;
use derive_more::{Display, From, Into};
#[derive(
Clone, Debug, Display, Eq, From, Hash, Into, Ord, PartialOrd, PartialEq,
)]
pub struct BackendKey(String);
pub type BackendPort = u16;
pub type BackendName = String;
pub type BackendAddress = IpAddr;
#[derive(Clone, Debug)]
pub struct Backend {
pub name: BackendName,
pub address: BackendAddress,
pub port: BackendPort,
}
impl Backend {
pub fn new(address: &BackendAddress, port: BackendPort) -> Self {
Backend {
name: backend_name(address, port),
address: *address,
port,
}
}
}
fn backend_name(address: &BackendAddress, port: BackendPort) -> BackendName {
let address_str = format!("{}", address);
[address_str, String::from(":"), port.to_string()].concat()
}
pub fn srv_key(backend: &Backend) -> BackendKey {
let mut sha1 = Sha1::new();
sha1.update(backend.name.as_bytes());
sha1.update(b"||");
sha1.update(backend.port.to_string().as_bytes());
sha1.update(b"||");
sha1.update(backend.address.to_string().as_bytes());
base64::encode(&sha1.digest().bytes()).into()
}