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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use crate::socket::{SocketListener, SocketStream};
use crate::{
    error::NotFoundError,
    link::Link,
    signal::{Closer, Stop},
    state::{ProxyState, SharedProxyInfo, ToxicStateHolder},
    stream::{Read, Write},
    toxic::{update_toxic_list_in_place, StreamDirection, Toxic, ToxicEvent, ToxicEventResult},
};
use async_trait::async_trait;
use bmrng::{Payload, RequestReceiver};
use futures::{stream, StreamExt};
#[cfg(test)]
use mockall::automock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::{io, mem};
use thiserror::Error;
use tokio_util::codec::{BytesCodec, FramedRead, FramedWrite};
use tracing::{debug, error, info, instrument};

/// The default Go io.Copy buffer size is 32K, so also use 32K buffers here to imitate Toxiproxy.
const READ_BUFFER_SIZE: usize = 32768;

/// The immutable configuration for a proxy
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProxyConfig {
    /// An arbitrary name
    pub name: String,
    /// The host name and the port the proxy listens on, like 127.0.0.1:5431
    pub listen: String,
    /// The host name and the port the proxy connects to, like 127.0.0:5432
    pub upstream: String,
    /// The client can set the enabled field to false to stop this proxy.
    /// Proxies are enabled by default
    #[serde(default = "default_enabled")]
    pub enabled: bool,
    /// A random seed. Not exposed in the API
    #[serde(skip)]
    pub rand_seed: Option<u64>,
}

fn default_enabled() -> bool {
    true
}

/// A holder for upstream and downstream links, as well as the per-connection state
#[derive(Debug)]
pub struct Links {
    upstream: Link,
    client: Link,
    /// Optional, connection-wide state for toxics that need such state (like LimitData)
    /// Toxic Name -> State
    state_holder: Option<Arc<ToxicStateHolder>>,
}

/// Toxics applied on a proxy connection
#[derive(Debug, Clone)]
pub struct Toxics {
    /// The toxics applied on the upstream link
    pub upstream: Vec<Toxic>,
    /// The toxics applied on the downstream link
    pub downstream: Vec<Toxic>,
}

/// The serializable API response
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProxyWithToxics {
    /// The proxy details
    #[serde(flatten)]
    pub proxy: ProxyConfig,
    /// Toxics installed on the proxy
    pub toxics: Vec<Toxic>,
}

impl ProxyConfig {
    /// Validate the proxy config, return `ProxyValidateError` if invalid
    pub fn validate(&self) -> Result<(), ProxyValidateError> {
        if self.name.is_empty() {
            Err(ProxyValidateError::MissingName)
        } else if self.upstream.is_empty() {
            Err(ProxyValidateError::MissingUpstream)
        } else if self.listen.is_empty() {
            Err(ProxyValidateError::MissingListen)
        } else {
            Ok(())
        }
    }
}

impl Toxics {
    /// Initialize an empty set up toxics
    pub fn empty() -> Self {
        Toxics {
            upstream: Vec::new(),
            downstream: Vec::new(),
        }
    }

    /// Consume this Toxics struct to combine upstream and downstream toxics in a flat unordered vec
    pub fn into_vec(mut self) -> Vec<Toxic> {
        self.upstream.append(&mut self.downstream);
        self.upstream
    }

    /// Find a toxic by name in upstream and downstream lists
    pub fn find_by_name(&self, toxic_name: &str) -> Option<Toxic> {
        self.upstream
            .iter()
            .find(|toxic| toxic.name == toxic_name)
            .or_else(|| {
                self.downstream
                    .iter()
                    .find(|toxic| toxic.name == toxic_name)
            })
            .map(|toxic| toxic.to_owned())
    }
}

impl ProxyWithToxics {
    /// Create the full ProxyWithToxics from SharedProxyInfo
    pub fn from_shared_proxy_info(info: SharedProxyInfo) -> Self {
        let proxy_state = info.state.lock();
        ProxyWithToxics {
            proxy: info.clone_config(),
            toxics: proxy_state.toxics.clone().into_vec(),
        }
    }

    /// Create a new ProxyWithToxics with empty toxics
    pub fn from_proxy_config(proxy_config: ProxyConfig) -> Self {
        ProxyWithToxics {
            proxy: proxy_config,
            toxics: Vec::new(),
        }
    }
}

struct Streams {
    client_read: Read,
    client_write: Write,
    upstream_read: Read,
    upstream_write: Write,
}

/// The proxy runner interface (defined for mocking, mainly)
#[cfg_attr(test, automock)]
#[async_trait]
pub trait Runner {
    /// Initialize a proxy, bind to a TCP port but don't start accepting clients
    async fn initialize_proxy<Listener>(
        config: ProxyConfig,
        initial_toxics: Toxics,
    ) -> io::Result<(Listener, SharedProxyInfo)>
    where
        Listener: SocketListener + 'static;

    /// Run the initialized proxy, accept clients, establish links
    async fn run_proxy<Listener>(
        listener: Listener,
        proxy_info: SharedProxyInfo,
        receiver: RequestReceiver<ToxicEvent, ToxicEventResult>,
        mut stop: Stop,
        closer: Closer,
    ) -> io::Result<()>
    where
        Listener: SocketListener + 'static;
}

/// The proxy runner
#[derive(Debug, Copy, Clone)]
pub struct ProxyRunner;

#[async_trait]
impl Runner for ProxyRunner {
    /// Initialize a proxy, bind to a TCP port but don't start accepting clients
    #[instrument(level = "debug")]
    async fn initialize_proxy<Listener>(
        config: ProxyConfig,
        initial_toxics: Toxics,
    ) -> io::Result<(Listener, SharedProxyInfo)>
    where
        Listener: SocketListener + 'static,
    {
        let listener = Listener::bind(&config.listen).await?;

        info!(name = ?config.name, proxy = ?config.listen, upstream = ?config.upstream, "Initialized proxy");

        let state = Arc::new(ProxyState::new(initial_toxics));

        let proxy_info = SharedProxyInfo {
            state,
            config: Arc::new(config),
        };

        Ok((listener, proxy_info))
    }

    /// Run the initialized proxy, accept clients, establish links
    #[instrument(level = "debug", skip(listener, receiver, stop, closer))]
    async fn run_proxy<Listener>(
        listener: Listener,
        proxy_info: SharedProxyInfo,
        receiver: RequestReceiver<ToxicEvent, ToxicEventResult>,
        mut stop: Stop,
        closer: Closer,
    ) -> io::Result<()>
    where
        Listener: SocketListener + 'static,
    {
        let state = proxy_info.state;
        let config = proxy_info.config;

        tokio::spawn(listen_toxic_events(
            state.clone(),
            receiver,
            stop.clone(),
            config.clone(),
        ));

        while !stop.stop_received() {
            let maybe_connection = tokio::select! {
                res = listener.accept() => {
                    Ok::<Option<(Listener::Stream, SocketAddr)>, io::Error>(Some(res?))
                },
                _ = stop.recv() => {
                    Ok(None)
                },
            }?;

            if let Some((client_stream, addr)) = maybe_connection {
                debug!(proxy = ?&config, addr = ?&addr, "Accepted client {}", addr);
                let upstream = match Listener::Stream::connect(&config.upstream).await {
                    Ok(upstream) => upstream,
                    Err(err) => {
                        error!(err = ?err, proxy = ?&config.name, upstream = ?&config.upstream, listen = ?&config.listen, "Unable to open connection to upstream");
                        // This is not a fatal error, can retry next time another client connects
                        continue;
                    }
                };

                let (client_read, client_write) = client_stream.into_split();
                let (upstream_read, upstream_write) = upstream.into_split();

                let client_read =
                    FramedRead::with_capacity(client_read, BytesCodec::new(), READ_BUFFER_SIZE);
                let client_write = FramedWrite::new(client_write, BytesCodec::new());
                let upstream_read =
                    FramedRead::with_capacity(upstream_read, BytesCodec::new(), READ_BUFFER_SIZE);
                let upstream_write = FramedWrite::new(upstream_write, BytesCodec::new());

                let toxics = state.lock().toxics.clone();

                let streams = Streams {
                    client_read,
                    client_write,
                    upstream_read,
                    upstream_write,
                };

                let res = create_links(
                    state.clone(),
                    addr,
                    &config,
                    &mut stop,
                    toxics,
                    streams,
                    None,
                );
                if let Err(err) = res {
                    error!(err = ?err, proxy = ?&config.name, listen = ?&config.listen, "Unable to establish link for proxy");
                    continue;
                }
            } else {
                break;
            }
        }
        drop(listener);
        let _ = closer.close();
        debug!(proxy = ?&config.name, listen = ?&config.listen, "Shutting down proxy");
        Ok(())
    }
}

#[instrument(level = "debug", skip(state, streams, stop))]
fn create_links(
    state: Arc<ProxyState>,
    addr: SocketAddr,
    config: &ProxyConfig,
    stop: &mut Stop,
    toxics: Toxics,
    streams: Streams,
    previous_toxic_state_holder: Option<Arc<ToxicStateHolder>>,
) -> io::Result<()> {
    let mut current_state = state.lock();

    if current_state.clients.contains_key(&addr) {
        return Err(io::Error::new(
            io::ErrorKind::AlreadyExists,
            format!(
                "State error: there is already a client connected with this address: {}",
                addr.to_string()
            ),
        ));
    }

    let (links_stop, links_stopper) = stop.fork();

    let toxics_state_holder =
        previous_toxic_state_holder.or_else(|| ToxicStateHolder::for_toxics(&toxics));

    let mut upstream_link = Link::new(
        addr,
        StreamDirection::Upstream,
        config.clone(),
        links_stop.clone(),
    );
    let mut client_link = Link::new(
        addr,
        StreamDirection::Downstream,
        config.clone(),
        links_stop,
    );

    let upstream_handle = upstream_link.establish(
        streams.client_read,
        streams.upstream_write,
        toxics.upstream,
        toxics_state_holder.clone(),
    );
    let downstream_handle = client_link.establish(
        streams.upstream_read,
        streams.client_write,
        toxics.downstream,
        toxics_state_holder.clone(),
    );

    let state = state.clone();
    tokio::spawn(async move {
        // No need to listen for the stop signal here, we're ending as soon as one of the tasks have stopped.
        let _ = tokio::select! {
            up = upstream_handle => {
                debug!("Upstream joined first");
                up
            },
            down = downstream_handle => {
                debug!("Downstream joined first");
                down
            }
        };
        links_stopper.stop();
        let mut state = state.lock();
        state.clients.remove(&addr);
        debug!("Removed client {}", addr);
    });

    current_state.clients.insert(
        addr,
        Links {
            upstream: upstream_link,
            client: client_link,
            state_holder: toxics_state_holder,
        },
    );
    Ok(())
}

#[doc(hidden)]
pub async fn listen_toxic_events(
    state: Arc<ProxyState>,
    mut receiver: RequestReceiver<ToxicEvent, ToxicEventResult>,
    mut stop: Stop,
    config: Arc<ProxyConfig>,
) {
    while !stop.stop_received() {
        let maybe_payload: Option<Payload<ToxicEvent, ToxicEventResult>> = tokio::select! {
            res = receiver.recv() => {
                if let Ok(payload) = res {
                    Some(payload)
                } else {
                    None
                }
            },
            _ = stop.recv() => None,
        };
        if let Some(payload) = maybe_payload {
            process_toxic_event(state.clone(), config.clone(), stop.clone(), payload).await;
        } else {
            break;
        }
    }
}

async fn process_toxic_event(
    state: Arc<ProxyState>,
    config: Arc<ProxyConfig>,
    stop: Stop,
    (request, mut responder): Payload<ToxicEvent, ToxicEventResult>,
) {
    let new_toxics = {
        let mut current_state = state.lock();
        if let Err(err) = update_toxics(request, &mut current_state.toxics) {
            let _ = responder.respond(Err(err.into()));
            return;
        }
        current_state.toxics.clone()
    };

    let old_map = {
        let mut current_state = state.lock();
        mem::replace(&mut current_state.clients, HashMap::new())
    };

    let mut clients = stream::iter(old_map);
    while let Some((addr, links)) = clients.next().await {
        if let Err(err) = recreate_links(
            state.clone(),
            &config,
            stop.clone(),
            addr,
            links,
            new_toxics.clone(),
        )
        .await
        {
            error!(err = ?err, addr = ?addr, proxy = ?&config.name, "Failed to recreate links for client");
        }
    }
    let _ = responder.respond(Ok(()));
}

async fn recreate_links(
    state: Arc<ProxyState>,
    config: &ProxyConfig,
    stop: Stop,
    addr: SocketAddr,
    links: Links,
    new_toxics: Toxics,
) -> io::Result<()> {
    let (client_read, upstream_write) = links.client.disband().await?;
    let (upstream_read, client_write) = links.upstream.disband().await?;
    let streams = Streams {
        client_read,
        client_write,
        upstream_read,
        upstream_write,
    };
    create_links(
        state.clone(),
        addr,
        config,
        &mut stop.clone(),
        new_toxics,
        streams,
        links.state_holder,
    )
}

/// Update the toxics collection in place
fn update_toxics(event: ToxicEvent, toxics: &mut Toxics) -> Result<(), NotFoundError> {
    update_toxic_list_in_place(&mut toxics.upstream, event.kind, StreamDirection::Upstream)
        .or_else(|kind| {
            update_toxic_list_in_place(&mut toxics.downstream, kind, StreamDirection::Downstream)
        })
        .or(Err(NotFoundError))
}

/// Errors return when ProxyConfig validation fails
#[derive(Debug, Clone, Copy, Error, PartialEq)]
pub enum ProxyValidateError {
    /// The name field is empty
    #[error("name missing")]
    MissingName,
    /// The upstream field is empty
    #[error("upstream missing")]
    MissingUpstream,
    /// The listen field is empty
    #[error("listen address missing")]
    MissingListen,
}

#[cfg(test)]
mod serde_tests {
    use super::*;
    use serde_json::{from_str, to_string};

    #[test]
    fn test_ser_and_de() {
        let config = ProxyConfig {
            name: "foo".to_owned(),
            listen: "127.0.0.1:5431".to_owned(),
            upstream: "127.0.0.1:5432".to_owned(),
            enabled: false,
            rand_seed: Some(3),
        };
        let serialized = to_string(&config).unwrap();
        let expected = "{\"name\":\"foo\",\"listen\":\"127.0.0.1:5431\",\"upstream\":\"127.0.0.1:5432\",\"enabled\":false}";
        assert_eq!(expected, serialized);

        let expected = ProxyConfig {
            name: "foo".to_owned(),
            listen: "127.0.0.1:5431".to_owned(),
            upstream: "127.0.0.1:5432".to_owned(),
            enabled: false,
            rand_seed: None,
        };

        let deserialized = from_str(&serialized).unwrap();
        assert_eq!(expected, deserialized);
    }

    #[test]
    fn test_optional_enabled() {
        let expected = ProxyConfig {
            name: "foo".to_owned(),
            listen: "127.0.0.1:5431".to_owned(),
            upstream: "127.0.0.1:5432".to_owned(),
            enabled: true,
            rand_seed: None,
        };
        let input =
            "{\"name\":\"foo\",\"listen\":\"127.0.0.1:5431\",\"upstream\":\"127.0.0.1:5432\"}";
        let deserialized = from_str(&input).unwrap();
        assert_eq!(expected, deserialized);
    }
}

#[cfg(test)]
mod config_tests {
    use super::*;

    #[test]
    fn validates_name() {
        let config = ProxyConfig {
            name: "".to_owned(),
            listen: "".to_owned(),
            upstream: "".to_owned(),
            enabled: true,
            rand_seed: None,
        };
        assert_eq!(config.validate(), Err(ProxyValidateError::MissingName))
    }

    #[test]
    fn validates_listen() {
        let config = ProxyConfig {
            name: "name".to_owned(),
            listen: "".to_owned(),
            upstream: "bogus_addr".to_owned(),
            enabled: true,
            rand_seed: None,
        };
        assert_eq!(config.validate(), Err(ProxyValidateError::MissingListen))
    }

    #[test]
    fn validates_upstream() {
        let config = ProxyConfig {
            name: "name".to_owned(),
            listen: "bogus_addr".to_owned(),
            upstream: "".to_owned(),
            enabled: true,
            rand_seed: None,
        };
        assert_eq!(config.validate(), Err(ProxyValidateError::MissingUpstream))
    }

    #[test]
    fn allows_invalid_addresses() {
        let config = ProxyConfig {
            name: "name".to_owned(),
            listen: "bogus_addr".to_owned(),
            upstream: "bogus_upstream".to_owned(),
            enabled: true,
            rand_seed: None,
        };
        assert_eq!(config.validate(), Ok(()))
    }
}