-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserver.rs
More file actions
241 lines (210 loc) · 8.92 KB
/
server.rs
File metadata and controls
241 lines (210 loc) · 8.92 KB
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2022 Oxide Computer Company
use anyhow::{bail, Result};
use async_trait::async_trait;
use log::{debug, error, info, trace};
use std::marker::{Send, Sync};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Mutex;
use crate::rfb::{
ClientInit, ClientMessage, FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent,
ProtoVersion, ReadMessage, SecurityResult, SecurityType, SecurityTypes, ServerInit,
WriteMessage,
};
/// Immutable state
pub struct VncServerConfig {
pub addr: SocketAddr,
pub version: ProtoVersion,
pub sec_types: SecurityTypes,
pub name: String,
}
/// Mutable state
pub struct VncServerData {
pub width: u16,
pub height: u16,
/// The pixel format of the framebuffer data passed in to the server via
/// get_framebuffer_update.
pub input_pixel_format: PixelFormat,
}
#[derive(Clone)]
pub struct VncServer<S: Server> {
config: Arc<VncServerConfig>,
data: Arc<Mutex<VncServerData>>,
pub server: Arc<S>,
}
#[async_trait]
pub trait Server: Sync + Send + Clone + 'static {
async fn get_framebuffer_update(&self) -> FramebufferUpdate;
async fn key_event(&self, _ke: KeyEvent) {}
async fn pointer_event(&self, _pe: PointerEvent) {}
async fn cut_text(&self, _t: String) {}
}
impl<S: Server> VncServer<S> {
pub fn new(server: S, config: VncServerConfig, data: VncServerData) -> Self {
assert!(
config.sec_types.0.len() > 0,
"at least one security type must be defined"
);
Self {
config: Arc::new(config),
data: Arc::new(Mutex::new(data)),
server: Arc::new(server),
}
}
pub async fn set_pixel_format(&self, pixel_format: PixelFormat) {
let mut locked = self.data.lock().await;
locked.input_pixel_format = pixel_format;
}
pub async fn set_resolution(&self, width: u16, height: u16) {
let mut locked = self.data.lock().await;
locked.width = width;
locked.height = height;
}
async fn rfb_handshake(&self, s: &mut TcpStream, addr: SocketAddr) -> Result<()> {
// ProtocolVersion handshake
info!("Tx [{:?}]: ProtoVersion={:?}", addr, self.config.version);
self.config.version.write_to(s).await?;
let client_version = ProtoVersion::read_from(s).await?;
info!("Rx [{:?}]: ClientVersion={:?}", addr, client_version);
if client_version < self.config.version {
let err_str = format!(
"[{:?}] unsupported client version={:?} (server version: {:?})",
addr, client_version, self.config.version
);
error!("{}", err_str);
bail!(err_str);
}
// Security Handshake
let supported_types = self.config.sec_types.clone();
info!("Tx [{:?}]: SecurityTypes={:?}", addr, supported_types);
supported_types.write_to(s).await?;
let client_choice = SecurityType::read_from(s).await?;
info!("Rx [{:?}]: SecurityType Choice={:?}", addr, client_choice);
if !self.config.sec_types.0.contains(&client_choice) {
info!("Tx [{:?}]: SecurityResult=Failure", addr);
let failure = SecurityResult::Failure("unsupported security type".to_string());
failure.write_to(s).await?;
let err_str = format!("invalid security choice={:?}", client_choice);
error!("{}", err_str);
bail!(err_str);
}
let res = SecurityResult::Success;
info!("Tx: SecurityResult=Success");
res.write_to(s).await?;
Ok(())
}
async fn rfb_initialization(&self, s: &mut TcpStream, addr: SocketAddr) -> Result<()> {
let client_init = ClientInit::read_from(s).await?;
info!("Rx [{:?}]: ClientInit={:?}", addr, client_init);
// TODO: decide what to do in exclusive case
match client_init.shared {
true => {}
false => {}
}
let data = self.data.lock().await;
let server_init = ServerInit::new(
data.width,
data.height,
self.config.name.clone(),
data.input_pixel_format.clone(),
);
info!("Tx [{:?}]: ServerInit={:#?}", addr, server_init);
server_init.write_to(s).await?;
Ok(())
}
async fn handle_conn(&self, s: &mut TcpStream, addr: SocketAddr) {
info!("[{:?}] new connection", addr);
if let Err(e) = self.rfb_handshake(s, addr).await {
error!("[{:?}] could not complete handshake: {:?}", addr, e);
return;
}
if let Err(e) = self.rfb_initialization(s, addr).await {
error!("[{:?}] could not complete handshake: {:?}", addr, e);
return;
}
let data = self.data.lock().await;
let mut output_pixel_format = data.input_pixel_format.clone();
drop(data);
loop {
let req = ClientMessage::read_from(s).await;
match req {
Ok(client_msg) => match client_msg {
ClientMessage::SetPixelFormat(pf) => {
debug!("Rx [{:?}]: SetPixelFormat={:#?}", addr, pf);
// TODO: invalid pixel formats?
output_pixel_format = pf;
}
ClientMessage::SetEncodings(e) => {
debug!("Rx [{:?}]: SetEncodings={:?}", addr, e);
}
ClientMessage::FramebufferUpdateRequest(f) => {
debug!("Rx [{:?}]: FramebufferUpdateRequest={:?}", addr, f);
let mut fbu = self.server.get_framebuffer_update().await;
let data = self.data.lock().await;
// We only need to change pixel formats if the client requested a different
// one than what's specified in the input.
//
// For now, we only support transformations between 4-byte RGB formats, so
// if the requested format isn't one of those, we'll just leave the pixels
// as is.
if data.input_pixel_format != output_pixel_format
&& data.input_pixel_format.is_rgb_888()
&& output_pixel_format.is_rgb_888()
{
debug!(
"transforming: input={:#?}, output={:#?}",
data.input_pixel_format, output_pixel_format
);
fbu = fbu.transform(&data.input_pixel_format, &output_pixel_format);
} else if !(data.input_pixel_format.is_rgb_888()
&& output_pixel_format.is_rgb_888())
{
debug!("cannot transform between pixel formats (not rgb888): input.is_rgb_888()={}, output.is_rgb_888()={}", data.input_pixel_format.is_rgb_888(), output_pixel_format.is_rgb_888());
} else {
debug!("no input transformation needed");
}
if let Err(e) = fbu.write_to(s).await {
error!(
"[{:?}] could not write FramebufferUpdateRequest: {:?}",
addr, e
);
return;
}
debug!("Tx [{:?}]: FramebufferUpdate", addr);
}
ClientMessage::KeyEvent(ke) => {
trace!("Rx [{:?}]: KeyEvent={:?}", addr, ke);
self.server.key_event(ke).await;
}
ClientMessage::PointerEvent(pe) => {
trace!("Rx [{:?}: PointerEvent={:?}", addr, pe);
self.server.pointer_event(pe).await;
}
ClientMessage::ClientCutText(t) => {
trace!("Rx [{:?}: ClientCutText={:?}", addr, t);
self.server.cut_text(t).await;
}
},
Err(e) => {
error!("[{:?}] error reading client message: {}", addr, e);
return;
}
}
}
}
pub async fn start(&self) {
let listener = TcpListener::bind(self.config.addr).await.unwrap();
loop {
let (mut s, a) = listener.accept().await.unwrap();
let server = self.clone();
tokio::spawn(async move {
VncServer::handle_conn(&server, &mut s, a).await;
});
}
}
}