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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use super::service::{BtpOutgoingService, WsError};
use super::{packet::*, BtpAccount, BtpStore};
use futures::{future::result, Async, AsyncSink, Future, Poll, Sink, Stream};
use interledger_packet::Address;
use interledger_service::*;
use log::{debug, error, warn};
use std::{str::FromStr, time::Duration};
use tokio_timer::Timeout;
use tungstenite;
use warp::{
    self,
    ws::{Message, WebSocket, Ws2},
    Filter,
};

// Close the incoming websocket connection if the auth details
// have not been received within this timeout
const WEBSOCKET_TIMEOUT: Duration = Duration::from_secs(10);

// const MAX_MESSAGE_SIZE: usize = 40000;

/// Returns a BtpOutgoingService and a warp Filter.
///
/// The BtpOutgoingService wraps all BTP/WebSocket connections that come
/// in on the given address. Calling `handle_incoming` with an `IncomingService` will
/// turn the returned BtpOutgoingService into a bidirectional handler.
/// The separation is designed to enable the returned BtpOutgoingService to be passed
/// to another service like the Router, and _then_ for the Router to be passed as the
/// IncomingService to the BTP server.
///
/// The warp filter handles the websocket upgrades and adds incoming connections
/// to the BTP service so that it will handle each of the messages.
pub fn create_btp_service_and_filter<O, S, A>(
    ilp_address: Address,
    store: S,
    next_outgoing: O,
) -> (
    BtpOutgoingService<O, A>,
    warp::filters::BoxedFilter<(impl warp::Reply,)>,
)
where
    O: OutgoingService<A> + Clone + Send + Sync + 'static,
    S: BtpStore<Account = A> + Clone + Send + Sync + 'static,
    A: BtpAccount + 'static,
{
    let service = BtpOutgoingService::new(ilp_address, next_outgoing);
    let service_clone = service.clone();
    let filter = warp::ws2()
        .map(move |ws: Ws2| {
            let store = store.clone();
            let service_clone = service_clone.clone();
            ws.on_upgrade(move |ws: WebSocket| {
                // TODO set max_message_size once https://github.com/seanmonstar/warp/pull/272 is merged
                let service_clone = service_clone.clone();
                Timeout::new(validate_auth(store, ws), WEBSOCKET_TIMEOUT)
                    .and_then(move |(account, connection)| {
                        debug!(
                            "Added connection for account {}: (id: {})",
                            account.username(),
                            account.id()
                        );
                        service_clone.add_connection(account, WsWrap { connection });
                        Ok(())
                    })
                    .or_else(|_| {
                        warn!("Closing Websocket connection because of an error");
                        Ok(())
                    })
            })
        })
        .boxed();
    (service, filter)
}

/// This wraps a warp Websocket connection to make it act like a
/// tungstenite Websocket connection. It is needed for
/// compatibility with the BTP service that interacts with the
/// websocket implementation from warp and tokio-tungstenite
struct WsWrap<W> {
    connection: W,
}

impl<W> Stream for WsWrap<W>
where
    W: Stream<Item = Message, Error = warp::Error>
        + Sink<SinkItem = Message, SinkError = warp::Error>,
{
    type Item = tungstenite::Message;
    type Error = WsError;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match self.connection.poll() {
            Ok(Async::NotReady) => Ok(Async::NotReady),
            Ok(Async::Ready(None)) => Ok(Async::Ready(None)),
            Ok(Async::Ready(Some(message))) => {
                let message = if message.is_ping() {
                    tungstenite::Message::Ping(message.into_bytes())
                } else if message.is_binary() {
                    tungstenite::Message::Binary(message.into_bytes())
                } else if message.is_text() {
                    tungstenite::Message::Text(message.to_str().unwrap_or_default().to_string())
                } else if message.is_close() {
                    tungstenite::Message::Close(None)
                } else {
                    warn!(
                        "Got unexpected websocket message, closing connection: {:?}",
                        message
                    );
                    tungstenite::Message::Close(None)
                };
                Ok(Async::Ready(Some(message)))
            }
            Err(err) => Err(WsError::from(err)),
        }
    }
}

impl<W> Sink for WsWrap<W>
where
    W: Stream<Item = Message, Error = warp::Error>
        + Sink<SinkItem = Message, SinkError = warp::Error>,
{
    type SinkItem = tungstenite::Message;
    type SinkError = WsError;

    fn start_send(
        &mut self,
        item: Self::SinkItem,
    ) -> Result<AsyncSink<Self::SinkItem>, Self::SinkError> {
        match item {
            tungstenite::Message::Binary(data) => self
                .connection
                .start_send(Message::binary(data))
                .map(|result| {
                    if let AsyncSink::NotReady(message) = result {
                        AsyncSink::NotReady(tungstenite::Message::Binary(message.into_bytes()))
                    } else {
                        AsyncSink::Ready
                    }
                })
                .map_err(WsError::from),
            tungstenite::Message::Text(data) => {
                match self.connection.start_send(Message::text(data)) {
                    Ok(AsyncSink::NotReady(message)) => {
                        if let Ok(string) = String::from_utf8(message.into_bytes()) {
                            Ok(AsyncSink::NotReady(tungstenite::Message::text(string)))
                        } else {
                            Err(WsError::Tungstenite(tungstenite::Error::Utf8))
                        }
                    }
                    Ok(AsyncSink::Ready) => Ok(AsyncSink::Ready),
                    Err(err) => Err(WsError::from(err)),
                }
            }
            // Ignore other message types because warp's WebSocket type doesn't
            // allow us to send any other types of messages
            // TODO make sure warp's websocket responds to pings and/or sends them to keep the
            // connection alive
            _ => Ok(AsyncSink::Ready),
        }
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.connection.poll_complete().map_err(WsError::from)
    }
}

struct Auth {
    request_id: u32,
    token: AuthToken,
}

fn validate_auth<S, A>(
    store: S,
    connection: impl Stream<Item = Message, Error = warp::Error>
        + Sink<SinkItem = Message, SinkError = warp::Error>,
) -> impl Future<
    Item = (
        A,
        impl Stream<Item = Message, Error = warp::Error>
            + Sink<SinkItem = Message, SinkError = warp::Error>,
    ),
    Error = (),
>
where
    S: BtpStore<Account = A> + 'static,
    A: BtpAccount + 'static,
{
    get_auth(connection).and_then(move |(auth, connection)| {
        debug!("Got BTP connection for username: {}", auth.token.username());
        store
            .get_account_from_btp_auth(&auth.token.username(), &auth.token.password())
            .map_err(move |_| warn!("BTP connection does not correspond to an account"))
            .and_then(move |account| {
                let auth_response = Message::binary(
                    BtpResponse {
                        request_id: auth.request_id,
                        protocol_data: Vec::new(),
                    }
                    .to_bytes(),
                );
                connection
                    .send(auth_response)
                    .map_err(|_err| error!("warp::Error sending auth response"))
                    .and_then(|connection| Ok((account, connection)))
            })
    })
}

fn get_auth(
    connection: impl Stream<Item = Message, Error = warp::Error>
        + Sink<SinkItem = Message, SinkError = warp::Error>,
) -> impl Future<
    Item = (
        Auth,
        impl Stream<Item = Message, Error = warp::Error>
            + Sink<SinkItem = Message, SinkError = warp::Error>,
    ),
    Error = (),
> {
    connection
        .skip_while(|message| {
            // Skip non-binary messages like Pings and Pongs
            // Note that the BTP protocol spec technically specifies that
            // the auth message MUST be the first packet sent over the 
            // WebSocket connection. However, the JavaScript implementation
            // of BTP sends a Ping packet first, so we should ignore it.
            // (Be liberal in what you accept but strict in what you send)
            Ok(!message.is_binary())
            // TODO: should we error if the client sends something other than a binary or ping packet first?
        })
        .into_future()
        .map_err(|_err| ())
        .and_then(move |(message, connection)| {
            // The first packet sent on the connection MUST be the auth packet
            result(parse_auth(message).map(|auth| (auth, connection)).ok_or_else(|| {
                warn!("Got a BTP connection where the first packet sent was not a valid BTP Auth message. Closing the connection")
            }))
        })
}

fn parse_auth(ws_packet: Option<Message>) -> Option<Auth> {
    if let Some(message) = ws_packet {
        if message.is_binary() {
            match BtpMessage::from_bytes(message.as_bytes()) {
                Ok(message) => {
                    let request_id = message.request_id;
                    let mut username: Option<String> = None;
                    let mut token: Option<String> = None;
                    for protocol_data in message.protocol_data.iter() {
                        let protocol_name: &str = protocol_data.protocol_name.as_ref();
                        if protocol_name == "auth_token" {
                            token = String::from_utf8(protocol_data.data.clone()).ok();
                        } else if protocol_name == "auth_username" {
                            username = String::from_utf8(protocol_data.data.clone()).ok();
                        }
                    }

                    match (username, token) {
                        (Some(ref username), Some(ref token)) => {
                            return AuthToken::new(username, token)
                                .ok()
                                .map(|token| Auth { request_id, token });
                        }
                        (None, Some(ref token)) => {
                            return AuthToken::from_str(token)
                                .ok()
                                .map(|token| Auth { request_id, token });
                        }
                        _ => warn!("BTP packet is missing auth token"),
                    }
                }
                Err(err) => {
                    warn!(
                        "warp::Error parsing BTP packet from Websocket message: {:?}",
                        err
                    );
                }
            }
        } else {
            warn!("Websocket packet is not binary");
        }
    }
    None
}