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
//! # Blaze SSL Async
//! **BlazeSSL Async** is an implementation of the SSLv3 protocol and the TLS_RSA_WITH_RC4_128_SHA,
//! and TLS_RSA_WITH_RC4_128_MD5 ciphers.
//!
//! This library does not implement the entirety of the protocol it only implements client auth through x509
//! certificates and server auth through the self signed key.pem and cert.pem stored in the src directory.
//!
//! This is used by the [Pocket Relay](https://github.com/PocketRelay) project to allow the server to accept connections
//! that would normally go to gosredirector.ea.com and also connect to the official servers for both MITM logic
//! and Origin authentication.
//!
//! ## Usage
//!
//! Add dependency to your cargo dependencies
//!
//! ```toml
//! blaze-ssl-async = "^0.3"
//! ```
//!
//! ### Connecting to a server
//!
//! The example below if for connecting to a server as a client
//!
//! ```rust
//! // BlazeStream is a wrapper over tokios TcpStream
//! use blaze_ssl_async::stream::BlazeStream;
//!
//! // Tokio read write extensions used for read_exact and write_all
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//!
//! // BlazeStream::connect takes in any value that implements ToSocketAddrs
//! // some common implementations are "HOST:PORT" and ("HOST", PORT)
//! let mut stream = BlazeStream::connect(("159.153.64.175", 42127))
//!     .await
//!     .expect("Failed to create blaze stream");
//!
//! // TODO... Read from the stream as you would a normal TcpStream
//! let mut buf = [0u8; 12];
//! stream.read_exact(&mut buf)
//!     .await
//!     .expect("Failed to read 12 bytes");
//! // Write the bytes back
//! stream.write_all(&buf)
//!     .await
//!     .expect("Failed to write 12 by tes");
//! // You **MUST** flush BlazeSSL streams or else the data will never
//! // be sent to the client (If you don't flush the data won't be written till the next read)
//! stream.flush()
//!     .await
//!     .expect("Failed to flush");
//! ```
//!
//! ### Binding a server
//!
//! The example below is an example for creating a server that accepts clients
//!
//! ```rust
//! // BlazeListener is wrapper over tokios TcpListener
//! use crate::stream::BlazeListener;
//!
//! // Tokio read write extensions used for read_exact and write_all
//! use tokio::io::{AsyncReadExt, AsyncWriteExt};
//!
//! // Bind a listener accepts the same address values as the tokio TcpListener
//! let listener = BlazeListener::bind(("0.0.0.0", 42127))
//!         .await
//!         .expect("Failed to bind blaze listener");
//!
//! // Accept new connections
//! loop {
//!     // Accept the initial TcpStream without SSL
//!     let (stream, _) = listener
//!         .accept()
//!         .await
//!         .expect("Failed to accept stream");
//!     tokio::spawn(async move {
//!         // Complete the SSL handshake process in a spawned task
//!         let stream = stream.finish_accept()
//!             .await
//!             .expect("Failed to finish accepting stream");
//!
//!         // Read and write to the stream the same as in the client example
//!     });
//! }
//! ```
//!
//! > **Note**: This `accept` and `finish_accept` system is in place as to not prevent accepting new connections while a handshake is being completed. If you want
//! > to block new connections and do the handshaking portion in the accept you can
//! > use `blocking_accept` instead of `accept` and the `finish_accept` call is no longer necessary

mod crypto;
pub mod data;
mod handshake;
mod msg;

/// Module containing stream related logic
pub mod stream;

/// Re-export all stream types
pub use stream::*;

#[cfg(test)]
mod test {
    use crate::stream::{BlazeListener, BlazeStream};
    use std::time::Duration;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::time::sleep;

    #[tokio::test]
    async fn test_server() {
        // Begin listening for connections
        let listener = BlazeListener::bind(("0.0.0.0", 42127))
            .await
            .expect("Failed to bind blaze listener");

        loop {
            let (stream, _) = listener
                .blocking_accept()
                .await
                .expect("Failed to accept stream");
            tokio::spawn(handle(stream));
        }
    }

    async fn handle(mut stream: BlazeStream) {
        let mut buf = [0u8; 20];
        loop {
            buf.fill(0);
            let read_count = stream.read(&mut buf).await.unwrap();
            if read_count > 0 {
                println!("{:?}", &buf[..read_count]);
            }
            sleep(Duration::from_secs(5)).await
        }
    }

    #[tokio::test]
    async fn test_client() {
        let addr = ("159.153.64.175", 42127);
        // old = 159.153.64.175;

        let mut stream = BlazeStream::connect(addr)
            .await
            .expect("Failed to create blaze stream");

        let test = [0u8; 12];
        stream.write_all(&test).await.expect("Failed to write");
        stream.flush().await.expect("Failed to flush");

        let mut buf = [0u8; 12];
        stream.read_exact(&mut buf).await.expect("Read bytes");

        println!("{:?} Bytes", buf)
    }
}