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
|
use crate::common::*;
use crate::database;
use hbb_common::{
log,
rendezvous_proto::*,
tokio::sync::{Mutex, RwLock},
ResultType,
};
use serde_derive::{Deserialize, Serialize};
use std::{collections::HashMap, collections::HashSet, net::SocketAddr, sync::Arc, time::Instant};
lazy_static::lazy_static! {
pub(crate) static ref IP_BLOCKER: Mutex<HashMap<String, ((u32, Instant), (HashSet<String>, Instant))>> = Default::default();
pub(crate) static ref USER_STATUS: RwLock<HashMap<Vec<u8>, Arc<(Option<Vec<u8>>, bool)>>> = Default::default();
pub(crate) static ref IP_CHANGES: Mutex<HashMap<String, (Instant, HashMap<String, i32>)>> = Default::default();
}
pub static IP_CHANGE_DUR: u64 = 180;
pub static IP_CHANGE_DUR_X2: u64 = IP_CHANGE_DUR * 2;
pub static DAY_SECONDS: u64 = 3600 * 24;
pub static IP_BLOCK_DUR: u64 = 60;
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub(crate) struct PeerInfo {
#[serde(default)]
pub(crate) ip: String,
}
#[derive(Clone, Debug)]
pub(crate) struct Peer {
pub(crate) socket_addr: SocketAddr,
pub(crate) last_reg_time: Instant,
pub(crate) guid: Vec<u8>,
pub(crate) uuid: Vec<u8>,
pub(crate) pk: Vec<u8>,
pub(crate) user: Option<Vec<u8>>,
pub(crate) info: PeerInfo,
pub(crate) disabled: bool,
pub(crate) reg_pk: (u32, Instant), // how often register_pk
}
impl Default for Peer {
fn default() -> Self {
Self {
socket_addr: "0.0.0.0:0".parse().unwrap(),
last_reg_time: get_expired_time(),
guid: Vec::new(),
uuid: Vec::new(),
pk: Vec::new(),
info: Default::default(),
user: None,
disabled: false,
reg_pk: (0, get_expired_time()),
}
}
}
pub(crate) type LockPeer = Arc<RwLock<Peer>>;
#[derive(Clone)]
pub(crate) struct PeerMap {
map: Arc<RwLock<HashMap<String, LockPeer>>>,
pub(crate) db: database::Database,
}
impl PeerMap {
pub(crate) async fn new() -> ResultType<Self> {
let db = std::env::var("DB_URL").unwrap_or({
#[allow(unused_mut)]
let mut db = "db_v2.sqlite3".to_owned();
#[cfg(all(windows, not(debug_assertions)))]
{
if let Some(path) = hbb_common::config::Config::icon_path().parent() {
db = format!("{}\\{}", path.to_str().unwrap_or("."), db);
}
}
#[cfg(not(windows))]
{
db = format!("./{}", db);
}
db
});
log::info!("DB_URL={}", db);
let pm = Self {
map: Default::default(),
db: database::Database::new(&db).await?,
};
Ok(pm)
}
#[inline]
pub(crate) async fn update_pk(
&mut self,
id: String,
peer: LockPeer,
addr: SocketAddr,
uuid: Vec<u8>,
pk: Vec<u8>,
ip: String,
) -> register_pk_response::Result {
log::info!("update_pk {} {:?} {:?} {:?}", id, addr, uuid, pk);
let (info_str, guid) = {
let mut w = peer.write().await;
w.socket_addr = addr;
w.uuid = uuid.clone();
w.pk = pk.clone();
w.last_reg_time = Instant::now();
w.info.ip = ip;
(
serde_json::to_string(&w.info).unwrap_or_default(),
w.guid.clone(),
)
};
if guid.is_empty() {
match self.db.insert_peer(&id, &uuid, &pk, &info_str).await {
Err(err) => {
log::error!("db.insert_peer failed: {}", err);
return register_pk_response::Result::SERVER_ERROR;
}
Ok(guid) => {
peer.write().await.guid = guid;
}
}
} else {
if let Err(err) = self.db.update_pk(&guid, &id, &pk, &info_str).await {
log::error!("db.update_pk failed: {}", err);
return register_pk_response::Result::SERVER_ERROR;
}
log::info!("pk updated instead of insert");
}
register_pk_response::Result::OK
}
#[inline]
pub(crate) async fn get(&self, id: &str) -> Option<LockPeer> {
let p = self.map.read().await.get(id).map(|x| x.clone());
if p.is_some() {
return p;
} else {
if let Ok(Some(v)) = self.db.get_peer(id).await {
let peer = Peer {
guid: v.guid,
uuid: v.uuid,
pk: v.pk,
user: v.user,
info: serde_json::from_str::<PeerInfo>(&v.info).unwrap_or_default(),
disabled: v.status == Some(0),
..Default::default()
};
let peer = Arc::new(RwLock::new(peer));
self.map.write().await.insert(id.to_owned(), peer.clone());
return Some(peer);
}
}
None
}
#[inline]
pub(crate) async fn get_or(&self, id: &str) -> LockPeer {
if let Some(p) = self.get(id).await {
return p;
}
let mut w = self.map.write().await;
if let Some(p) = w.get(id) {
return p.clone();
}
let tmp = LockPeer::default();
w.insert(id.to_owned(), tmp.clone());
tmp
}
#[inline]
pub(crate) async fn get_in_memory(&self, id: &str) -> Option<LockPeer> {
self.map.read().await.get(id).map(|x| x.clone())
}
#[inline]
pub(crate) async fn is_in_memory(&self, id: &str) -> bool {
self.map.read().await.contains_key(id)
}
#[inline]
pub(crate) async fn remove(&self, id: &str) {
self.map.write().await.remove(id);
}
}
|