forked from kai4711x/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_server.rs
219 lines (214 loc) · 8.27 KB
/
custom_server.rs
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
use hbb_common::{
bail,
base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _},
sodiumoxide::crypto::sign,
ResultType,
};
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Default, Serialize, Deserialize, Clone)]
pub struct CustomServer {
#[serde(default)]
pub key: String,
#[serde(default)]
pub host: String,
#[serde(default)]
pub api: String,
#[serde(default)]
pub relay: String,
}
fn get_custom_server_from_config_string(s: &str) -> ResultType<CustomServer> {
let tmp: String = s.chars().rev().collect();
const PK: &[u8; 32] = &[
88, 168, 68, 104, 60, 5, 163, 198, 165, 38, 12, 85, 114, 203, 96, 163, 70, 48, 0, 131, 57,
12, 46, 129, 83, 17, 84, 193, 119, 197, 130, 103,
];
let pk = sign::PublicKey(*PK);
let data = URL_SAFE_NO_PAD.decode(tmp)?;
if let Ok(lic) = serde_json::from_slice::<CustomServer>(&data) {
return Ok(lic);
}
if let Ok(data) = sign::verify(&data, &pk) {
Ok(serde_json::from_slice::<CustomServer>(&data)?)
} else {
bail!("sign:verify failed");
}
}
pub fn get_custom_server_from_string(s: &str) -> ResultType<CustomServer> {
let s = if s.to_lowercase().ends_with(".exe.exe") {
&s[0..s.len() - 8]
} else if s.to_lowercase().ends_with(".exe") {
&s[0..s.len() - 4]
} else {
s
};
/*
* The following code tokenizes the file name based on commas and
* extracts relevant parts sequentially.
*
* host= is expected to be the first part.
*
* Since Windows renames files adding (1), (2) etc. before the .exe
* in case of duplicates, which causes the host or key values to be
* garbled.
*
* This allows using a ',' (comma) symbol as a final delimiter.
*/
if s.to_lowercase().contains("host=") {
let stripped = &s[s.to_lowercase().find("host=").unwrap_or(0)..s.len()];
let strs: Vec<&str> = stripped.split(",").collect();
let mut host = String::default();
let mut key = String::default();
let mut api = String::default();
let mut relay = String::default();
let strs_iter = strs.iter();
for el in strs_iter {
let el_lower = el.to_lowercase();
if el_lower.starts_with("host=") {
host = el.chars().skip(5).collect();
}
if el_lower.starts_with("key=") {
key = el.chars().skip(4).collect();
}
if el_lower.starts_with("api=") {
api = el.chars().skip(4).collect();
}
if el_lower.starts_with("relay=") {
relay = el.chars().skip(6).collect();
}
}
return Ok(CustomServer {
host,
key,
api,
relay,
});
} else {
let s = s
.replace("-licensed---", "--")
.replace("-licensed--", "--")
.replace("-licensed-", "--");
let strs = s.split("--");
for s in strs {
if let Ok(lic) = get_custom_server_from_config_string(s.trim()) {
return Ok(lic);
} else if s.contains("(") {
// https://github.com/rustdesk/rustdesk/issues/4162
for s in s.split("(") {
if let Ok(lic) = get_custom_server_from_config_string(s.trim()) {
return Ok(lic);
}
}
}
}
}
bail!("Failed to parse");
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_filename_license_string() {
assert!(get_custom_server_from_string("rustdesk.exe").is_err());
assert!(get_custom_server_from_string("rustdesk").is_err());
assert_eq!(
get_custom_server_from_string("rustdesk-host=server.example.net.exe").unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "".to_owned(),
api: "".to_owned(),
relay: "".to_owned(),
}
);
assert_eq!(
get_custom_server_from_string("rustdesk-host=server.example.net,.exe").unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "".to_owned(),
api: "".to_owned(),
relay: "".to_owned(),
}
);
// key in these tests is "foobar.,2" base64 encoded
assert_eq!(
get_custom_server_from_string(
"rustdesk-host=server.example.net,api=abc,key=Zm9vYmFyLiwyCg==.exe"
)
.unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "Zm9vYmFyLiwyCg==".to_owned(),
api: "abc".to_owned(),
relay: "".to_owned(),
}
);
assert_eq!(
get_custom_server_from_string(
"rustdesk-host=server.example.net,key=Zm9vYmFyLiwyCg==,.exe"
)
.unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "Zm9vYmFyLiwyCg==".to_owned(),
api: "".to_owned(),
relay: "".to_owned(),
}
);
assert_eq!(
get_custom_server_from_string(
"rustdesk-host=server.example.net,key=Zm9vYmFyLiwyCg==,relay=server.example.net.exe"
)
.unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "Zm9vYmFyLiwyCg==".to_owned(),
api: "".to_owned(),
relay: "server.example.net".to_owned(),
}
);
assert_eq!(
get_custom_server_from_string(
"rustdesk-Host=server.example.net,Key=Zm9vYmFyLiwyCg==,RELAY=server.example.net.exe"
)
.unwrap(),
CustomServer {
host: "server.example.net".to_owned(),
key: "Zm9vYmFyLiwyCg==".to_owned(),
api: "".to_owned(),
relay: "server.example.net".to_owned(),
}
);
let lic = CustomServer {
host: "1.1.1.1".to_owned(),
key: "5Qbwsde3unUcJBtrx9ZkvUmwFNoExHzpryHuPUdqlWM=".to_owned(),
api: "".to_owned(),
relay: "".to_owned(),
};
assert_eq!(
get_custom_server_from_string("rustdesk-licensed-0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye.exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed-0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye(1).exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk--0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye(1).exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed-0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye (1).exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed-0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye (1) (2).exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed-0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye--abc.exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed--0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye--.exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed---0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye--.exe")
.unwrap(), lic);
assert_eq!(
get_custom_server_from_string("rustdesk-licensed--0nI900VsFHZVBVdIlncwpHS4V0bOZ0dtVldrpVO4JHdCp0YV5WdzUGZzdnYRVjI6ISeltmIsISMuEjLx4SMiojI0N3boJye--.exe")
.unwrap(), lic);
}
}