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
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::path::PathBuf;

use protocol::perform_handshake;
use stream::WsStream;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;

/// websocket error definitions
pub mod errors;
/// websocket transport unit
pub mod frame;
/// build connection & read/write frame utils
pub mod protocol;
/// connection proxy support
pub mod proxy;
/// stream definition
pub mod stream;

/// frame codec impl
pub mod codec;

use errors::WsError;
use tokio_util::codec::{Decoder, Encoder, Framed};

use crate::protocol::Mode;
use crate::protocol::{handle_handshake, wrap_tls};

/// websocket connection state
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionState {
    /// init state
    Created,
    /// tcp & tls connection creating state
    HandShaking,
    /// websocket connection has been successfully established
    Running,
    /// client or peer has send "close frame"
    Closing,
    /// client or peer have send "close" response frame
    Closed,
}

pub struct ClientBuilder {
    uri: String,
    proxy_uri: Option<String>,
    protocols: HashSet<String>,
    extensions: HashSet<String>,
    certs: HashSet<PathBuf>,
    version: u8,
    headers: HashMap<String, String>,
}

impl ClientBuilder {
    pub fn new<S: ToString>(uri: S) -> Self {
        Self {
            uri: uri.to_string(),
            proxy_uri: None,
            protocols: HashSet::new(),
            extensions: HashSet::new(),
            headers: HashMap::new(),
            certs: HashSet::new(),
            version: 13,
        }
    }

    pub fn proxy<S: ToString>(self, uri: S) -> Self {
        Self {
            proxy_uri: Some(uri.to_string()),
            ..self
        }
    }

    /// add protocols
    pub fn protocol(mut self, protocol: String) -> Self {
        self.protocols.insert(protocol);
        self
    }

    /// set extension in handshake http header
    ///
    /// **NOTE** it will clear protocols set by `protocol` method
    pub fn protocols(self, protocols: HashSet<String>) -> Self {
        Self { protocols, ..self }
    }

    /// add protocols
    pub fn extension(mut self, extension: String) -> Self {
        self.extensions.insert(extension);
        self
    }

    /// set extension in handshake http header
    ///
    /// **NOTE** it will clear protocols set by `protocol` method
    pub fn extensions(self, extensions: HashSet<String>) -> Self {
        Self { extensions, ..self }
    }

    pub fn cert(mut self, cert: PathBuf) -> Self {
        self.certs.insert(cert);
        self
    }

    // set ssl certs in wss connection
    ///
    /// **NOTE** it will clear certs set by `cert` method
    pub fn certs(self, certs: HashSet<PathBuf>) -> Self {
        Self { certs, ..self }
    }

    /// set websocket version
    pub fn version(self, version: u8) -> Self {
        Self { version, ..self }
    }

    pub fn header<K: ToString, V: ToString>(mut self, name: K, value: V) -> Self {
        self.headers.insert(name.to_string(), value.to_string());
        self
    }

    pub fn headers(self, headers: HashMap<String, String>) -> Self {
        Self { headers, ..self }
    }

    async fn _connect(&self) -> Result<(String, http::Response<()>, WsStream), WsError> {
        let Self {
            uri,
            proxy_uri,
            protocols,
            extensions,
            certs,
            version,
            headers,
        } = self;
        let uri = uri
            .parse::<http::Uri>()
            .map_err(|e| WsError::InvalidUri(format!("{} {}", uri, e.to_string())))?;
        let mode = if let Some(schema) = uri.scheme_str() {
            match schema.to_ascii_lowercase().as_str() {
                "ws" => Ok(Mode::WS),
                "wss" => Ok(Mode::WSS),
                _ => Err(WsError::InvalidUri(format!("invalid schema {}", schema))),
            }
        } else {
            Err(WsError::InvalidUri("missing ws or wss schema".to_string()))
        }?;
        if mode == Mode::WS && !certs.is_empty() {
            tracing::warn!("setting tls cert has no effect on insecure ws")
        }
        let ws_proxy: Option<proxy::Proxy> = match proxy_uri {
            Some(uri) => Some(uri.parse()?),
            None => None,
        };

        let host = uri
            .host()
            .ok_or_else(|| WsError::InvalidUri(format!("can not find host {}", self.uri)))?;
        let port = match uri.port_u16() {
            Some(port) => port,
            None => mode.default_port(),
        };

        let stream = match &ws_proxy {
            Some(proxy_conf) => proxy_conf.connect((host, port)).await?,
            None => TcpStream::connect((host, port)).await.map_err(|e| {
                WsError::ConnectionFailed(format!(
                    "failed to create tcp connection {}",
                    e.to_string()
                ))
            })?,
        };
        tracing::debug!("tcp connection established");
        let mut stream = match mode {
            Mode::WS => WsStream::Plain(stream),
            Mode::WSS => {
                let tls_stream = wrap_tls(stream, host, &self.certs).await?;
                WsStream::Tls(tls_stream)
            }
        };
        let (key, resp) = perform_handshake(
            &mut stream,
            &mode,
            &uri,
            protocols.iter().cloned().collect::<Vec<String>>().join(" "),
            extensions
                .iter()
                .cloned()
                .collect::<Vec<String>>()
                .join(" "),
            *version,
            headers.clone(),
        )
        .await?;
        Ok((key, resp, stream))
    }

    pub async fn connect_with_check<C, EI, DI, F>(
        &self,
        check_fn: F,
    ) -> Result<Framed<WsStream, C>, WsError>
    where
        C: Encoder<EI, Error = WsError> + Decoder<Item = DI, Error = WsError>,
        F: Fn(String, http::Response<()>, WsStream) -> Result<Framed<WsStream, C>, WsError>,
    {
        let (key, resp, stream) = self._connect().await?;
        check_fn(key, resp, stream)
    }
}

pub struct ServerBuilder {}

impl ServerBuilder {
    pub async fn accept<C, EI, DI, F1, F2, T>(
        stream: TcpStream,
        handshake_handler: F1,
        codec_factory: F2,
    ) -> Result<Framed<WsStream, C>, WsError>
    where
        C: Encoder<EI, Error = WsError> + Decoder<Item = DI, Error = WsError>,
        F1: Fn(http::Request<()>) -> Result<(http::Request<()>, http::Response<T>), WsError>,
        F2: Fn(http::Request<()>, WsStream) -> Result<Framed<WsStream, C>, WsError>,
        T: ToString + Debug,
    {
        let mut stream = WsStream::Plain(stream);
        let req = handle_handshake(&mut stream).await?;
        let (req, resp) = handshake_handler(req)?;
        let mut resp_lines = vec![format!("{:?} {}", resp.version(), resp.status())];
        resp.headers().iter().for_each(|(k, v)| {
            resp_lines.push(format!("{}: {}", k, v.to_str().unwrap_or_default()))
        });
        resp_lines.push("\r\n".to_string());
        stream.write_all(resp_lines.join("\r\n").as_bytes()).await?;
        tracing::debug!("{:?}", &resp);
        if resp.status() != http::StatusCode::SWITCHING_PROTOCOLS {
            return Err(WsError::HandShakeFailed(resp.body().to_string()));
        }
        codec_factory(req, stream)
    }
}