Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d76fa52
move the event handling loop inside the proxy implementations
Geal Feb 2, 2018
69a1bbb
move some methods out of ProxyClient
Geal Feb 3, 2018
577e7a8
use vacant entries to move socket registering in the accept methods
Geal Feb 5, 2018
10849e4
use vacant entries for connect_to_backend
Geal Feb 5, 2018
cd7d91d
move readiness settings to Configuration
Geal Feb 5, 2018
c339c98
move close() behaviour in the clients
Geal Feb 5, 2018
0a2708f
remove some methods from ProxyClient
Geal Feb 5, 2018
fa1d76d
handle backend connection status in clients
Geal Feb 5, 2018
94fc12b
remove some methods from ProxyClient
Geal Feb 5, 2018
d27da9f
remove the distinction between front and back tokens in Session
Geal Feb 6, 2018
111598d
return all the tokens linked to the client in close()
Geal Feb 6, 2018
c549476
move some methods out of ProxyClient
Geal Feb 6, 2018
99668ff
move Readiness and SessionMetrics to network/mod.rs
Geal Feb 6, 2018
b99e2d6
Remove redundant CloseBoth result
Geal Feb 6, 2018
d0be338
CloseBackend should indicate the token of the backend
Geal Feb 6, 2018
0b2024d
move ProxyClient, ProxyConfiguration and related structs to mod.rs
Geal Feb 6, 2018
5241983
Remove the dependency of ProxyCLient on ProxyConfiguration
Geal Feb 7, 2018
eed016b
Simplify the ProxyConfiguration::accept() method
Geal Feb 8, 2018
1d5b7f1
Simplify the ProxyConfiguration::connect_to_backend method
Geal Feb 8, 2018
45012b8
move back Session features in proxy::Server
Geal Feb 9, 2018
e19ea5c
Fix TCP listener registration
Geal Feb 15, 2018
581b13f
do not override the result of connect_to_backend
Geal Feb 15, 2018
44dacf5
fix a few FIXME entries
Geal Feb 15, 2018
4348c58
fix unit tests
Geal Feb 15, 2018
f5915f5
fix the openssl proxy refactoring
Geal Feb 16, 2018
644ff43
do not unwrap the backend
Geal Feb 16, 2018
ecea671
remove debug logs
Geal Feb 16, 2018
5d0f882
remove some debug logs
Geal Feb 16, 2018
ddb8b40
do not unwrap the backend token
Geal Feb 16, 2018
c05c63c
flush the accept queue if we reached the maximum number of connections
Geal Feb 16, 2018
05489ef
fix an unwrap
Geal Feb 16, 2018
e51bfcf
fix a panic
Geal Feb 19, 2018
9265bda
do not process clients that do not exist anymore
Geal Feb 19, 2018
892a36a
simplify a bit the accept() handling
Geal Feb 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
445 changes: 339 additions & 106 deletions lib/src/network/http.rs

Large diffs are not rendered by default.

454 changes: 342 additions & 112 deletions lib/src/network/https_openssl.rs

Large diffs are not rendered by default.

352 changes: 266 additions & 86 deletions lib/src/network/https_rustls/client.rs

Large diffs are not rendered by default.

106 changes: 77 additions & 29 deletions lib/src/network/https_rustls/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::os::unix::io::{AsRawFd};
use std::io::{self,Read,Write,ErrorKind,BufReader};
use std::collections::{HashMap,HashSet};
use std::error::Error;
use slab::Slab;
use slab::{Slab,Entry,VacantEntry};
use pool::{Pool,Checkout};
use std::net::{IpAddr,SocketAddr};
use std::str::{FromStr, from_utf8, from_utf8_unchecked};
Expand All @@ -32,11 +32,10 @@ use sozu_command::certificate::split_certificate_chain;

use parser::http11::{HttpState,RequestState,ResponseState,RRequestLine,parse_request_until_stop,hostname_and_port};
use network::buffer_queue::BufferQueue;
use network::{AppId,Backend,ClientResult,ConnectionError,Protocol};
use network::{AppId,Backend,ClientResult,ConnectionError,Protocol,Readiness,SessionMetrics,
ProxyClient,ProxyConfiguration,AcceptError,BackendConnectAction,BackendConnectionStatus};
use network::backends::BackendMap;
use network::proxy::{Server,ProxyChannel};
use network::session::{BackendConnectAction,BackendConnectionStatus,ProxyClient,ProxyConfiguration,
Readiness,ListenToken,FrontToken,BackToken,AcceptError,Session,SessionMetrics};
use network::proxy::{Server,ProxyChannel,ListenToken,ClientToken,ListenClient};
use network::http::{self,DefaultAnswers};
use network::socket::{SocketHandler,SocketResult,server_bind,FrontRustls};
use network::trie::*;
Expand All @@ -48,10 +47,6 @@ use util::UnwrapLog;
use super::resolver::{CertificateResolver,CertificateResolverWrapper};
use super::client::TlsClient;

type BackendToken = Token;

type ClientToken = Token;

#[derive(Debug,Clone,PartialEq,Eq)]
pub struct TlsApp {
pub app_id: String,
Expand All @@ -72,14 +67,13 @@ pub struct ServerConfiguration {
pool: Rc<RefCell<Pool<BufferQueue>>>,
answers: DefaultAnswers,
config: HttpsProxyConfiguration,
base_token: usize,
ssl_config: Arc<ServerConfig>,
resolver: Arc<CertificateResolverWrapper>,
}

impl ServerConfiguration {
pub fn new(config: HttpsProxyConfiguration, base_token: usize, event_loop: &mut Poll, start_at: usize,
pool: Rc<RefCell<Pool<BufferQueue>>>, tcp_listener: Option<TcpListener>) -> io::Result<(ServerConfiguration, HashSet<ListenToken>)> {
pub fn new(config: HttpsProxyConfiguration, event_loop: &mut Poll,
pool: Rc<RefCell<Pool<BufferQueue>>>, tcp_listener: Option<TcpListener>, token: Token) -> io::Result<(ServerConfiguration, HashSet<Token>)> {

let mut fronts = HashMap::new();
let default_name = config.default_name.as_ref().map(|name| name.clone()).unwrap_or(String::new());
Expand All @@ -90,8 +84,8 @@ impl ServerConfiguration {

let mut listeners = HashSet::new();
if let Some(ref sock) = listener {
event_loop.register(sock, Token(base_token), Ready::readable(), PollOpt::edge());
listeners.insert(ListenToken(0));
event_loop.register(sock, token, Ready::readable(), PollOpt::edge());
listeners.insert(token);
}

let default = DefaultAnswers {
Expand Down Expand Up @@ -119,7 +113,6 @@ impl ServerConfiguration {
fronts: fronts,
pool: pool,
answers: default,
base_token: base_token,
config: config,
ssl_config: Arc::new(server_config),
resolver: resolver,
Expand Down Expand Up @@ -306,7 +299,9 @@ impl ServerConfiguration {
}

impl ProxyConfiguration<TlsClient> for ServerConfiguration {
fn accept(&mut self, token: ListenToken) -> Result<(TlsClient,bool), AcceptError> {
fn accept(&mut self, token: ListenToken, poll: &mut Poll, client_token: Token)
-> Result<(Rc<RefCell<TlsClient>>,bool), AcceptError> {

if let Some(ref listener) = self.listener.as_ref() {
listener.accept().map_err(|e| {
match e.kind() {
Expand All @@ -319,15 +314,33 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
}).map(|(frontend_sock, _)| {
frontend_sock.set_nodelay(true);
let session = ServerSession::new(&self.ssl_config);
let c = TlsClient::new(session, frontend_sock, Rc::downgrade(&self.pool), self.config.public_address);
(c, false)
let mut c = TlsClient::new(session, frontend_sock, Rc::downgrade(&self.pool), self.config.public_address);

c.readiness().front_interest = UnixReady::from(Ready::readable()) | UnixReady::hup() | UnixReady::error();
c.set_front_token(client_token);
poll.register(
c.front_socket(),
client_token,
Ready::readable() | Ready::writable() | Ready::from(UnixReady::hup() | UnixReady::error()),
PollOpt::edge()
);

(Rc::new(RefCell::new(c)), false)
})
} else {
Err(AcceptError::IoError)
}
}

fn connect_to_backend(&mut self, event_loop: &mut Poll, client: &mut TlsClient) -> Result<BackendConnectAction,ConnectionError> {
fn accept_flush(&mut self) {
if let Some(ref sock) = self.listener {
while sock.accept().is_ok() {
error!("accepting and closing connection");
}
}
}

fn connect_to_backend(&mut self, poll: &mut Poll, client: &mut TlsClient, back_token: Token) -> Result<BackendConnectAction,ConnectionError> {
let h = try!(unwrap_msg!(client.http()).state().get_host().ok_or(ConnectionError::NoHostGiven));

let host: &str = if let IResult::Done(i, (hostname, port)) = hostname_and_port(h.as_bytes()) {
Expand All @@ -347,6 +360,7 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
error!("TLS SNI hostname '{:?}' and Host header '{}' don't match", servername, hostname_str);
unwrap_msg!(client.http()).set_answer(DefaultAnswerStatus::Answer404, &self.answers.NotFound);
client.readiness().front_interest = UnixReady::from(Ready::writable()) | UnixReady::hup() | UnixReady::error();
client.readiness().back_interest = UnixReady::hup() | UnixReady::error();
return Err(ConnectionError::HostNotFound);
}

Expand Down Expand Up @@ -382,7 +396,7 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
//client.readiness().back_interest = UnixReady::from(Ready::empty());
client.readiness().back_readiness = UnixReady::from(Ready::empty());
client.back_socket().as_ref().map(|sock| {
event_loop.deregister(*sock);
poll.deregister(*sock);
sock.shutdown(Shutdown::Both);
});
}
Expand All @@ -402,7 +416,7 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
client.readiness().back_interest = UnixReady::from(Ready::empty());
client.readiness().back_readiness = UnixReady::from(Ready::empty());
client.back_socket().as_ref().map(|sock| {
event_loop.deregister(*sock);
poll.deregister(*sock);
sock.shutdown(Shutdown::Both);
});
}
Expand All @@ -426,7 +440,7 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
client.back_connected = BackendConnectionStatus::NotConnected;
client.readiness().back_readiness = UnixReady::from(Ready::empty());
client.back_socket().as_ref().map(|sock| {
event_loop.deregister(*sock);
poll.deregister(*sock);
sock.shutdown(Shutdown::Both);
});
// we still want to use the new socket
Expand All @@ -449,29 +463,49 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
});

socket.set_nodelay(true);
client.set_back_socket(socket);

if old_app_id == new_app_id {
poll.register(
&socket,
client.back_token().expect("FIXME"),
Ready::readable() | Ready::writable() | Ready::from(UnixReady::hup() | UnixReady::error()),
PollOpt::edge()
);

client.set_back_socket(socket);
Ok(BackendConnectAction::Replace)
} else {
poll.register(
&socket,
back_token,
Ready::readable() | Ready::writable() | Ready::from(UnixReady::hup() | UnixReady::error()),
PollOpt::edge()
);

client.set_back_socket(socket);
client.set_back_token(back_token);
incr!("backend.connections");
Ok(BackendConnectAction::New)
}
},
Err(ConnectionError::NoBackendAvailable) => {
unwrap_msg!(client.http()).set_answer(DefaultAnswerStatus::Answer503, &self.answers.ServiceUnavailable);
client.readiness().front_interest = UnixReady::from(Ready::writable()) | UnixReady::hup() | UnixReady::error();
client.readiness().back_interest = UnixReady::hup() | UnixReady::error();
Err(ConnectionError::NoBackendAvailable)
},
Err(ConnectionError::HostNotFound) => {
unwrap_msg!(client.http()).set_answer(DefaultAnswerStatus::Answer404, &self.answers.NotFound);
client.readiness().front_interest = UnixReady::from(Ready::writable()) | UnixReady::hup() | UnixReady::error();
client.readiness().back_interest = UnixReady::hup() | UnixReady::error();
Err(ConnectionError::HostNotFound)
},
e => panic!(e)
}
} else {
unwrap_msg!(client.http()).set_answer(DefaultAnswerStatus::Answer404, &self.answers.NotFound);
client.readiness().front_interest = UnixReady::from(Ready::writable()) | UnixReady::hup() | UnixReady::error();
client.readiness().back_interest = UnixReady::hup() | UnixReady::error();
Err(ConnectionError::HostNotFound)
}
}
Expand Down Expand Up @@ -585,8 +619,6 @@ impl ProxyConfiguration<TlsClient> for ServerConfiguration {
}
}

pub type RustlsServer = Session<ServerConfiguration,TlsClient>;

pub fn start(config: HttpsProxyConfiguration, channel: ProxyChannel, max_buffers: usize, buffer_size: usize) {
let mut event_loop = Poll::new().expect("could not create event loop");
let max_listeners = 1;
Expand All @@ -595,12 +627,28 @@ pub fn start(config: HttpsProxyConfiguration, channel: ProxyChannel, max_buffers
Pool::with_capacity(2*max_buffers, 0, || BufferQueue::with_capacity(buffer_size))
));

// start at max_listeners + 1 because token(0) is the channel, and token(1) is the timer
if let Ok((configuration, listeners)) = ServerConfiguration::new(config, 6148914691236517205, &mut event_loop, 1 + max_listeners, pool, None) {
let session = Session::new(max_listeners, max_buffers, 6148914691236517205, configuration, listeners, &mut event_loop);
let mut clients: Slab<Rc<RefCell<ProxyClient>>,ClientToken> = Slab::with_capacity(max_buffers);
{
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
info!("taking token {:?} for channel", entry.index());
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::HTTPListen })));
}
{
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
info!("taking token {:?} for metrics", entry.index());
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::HTTPListen })));
}

let token = {
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
let e = entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::HTTPListen })));
Token(e.index().0)
};

if let Ok((configuration, listeners)) = ServerConfiguration::new(config, &mut event_loop, pool, None, token) {
let (scm_server, scm_client) = UnixStream::pair().unwrap();
let mut server = Server::new(event_loop, channel, ScmSocket::new(scm_server.as_raw_fd()),
None, Some(session), None, None);
clients, None, Some(configuration), None, None, max_buffers);

info!("starting event loop");
server.run();
Expand Down
Loading