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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
#![allow(dead_code, unused_must_use, unused_variables, unused_imports)]
use std::thread::{self,Thread,Builder};
use std::sync::mpsc::TryRecvError;
use std::net::{SocketAddr,Shutdown};
use std::rc::Rc;
use std::cell::RefCell;
use mio::net::*;
use mio::*;
use mio::unix::UnixReady;
use std::collections::{HashSet,HashMap,VecDeque};
use std::io::{self,Read,ErrorKind};
use std::os::unix::io::{AsRawFd,FromRawFd};
use nom::HexDisplay;
use std::error::Error;
use slab::Slab;
use pool::Pool;
use std::io::Write;
use std::str::FromStr;
use std::marker::PhantomData;
use std::fmt::Debug;
use time::precise_time_ns;
use std::time::Duration;
use rand::random;
use sozu_command::config::Config;
use sozu_command::channel::Channel;
use sozu_command::scm_socket::{Listeners,ScmSocket};
use sozu_command::state::{ConfigState,get_application_ids_by_domain};
use sozu_command::messages::{self,TcpFront,Order,Backend,MessageId,OrderMessageAnswer,
OrderMessageAnswerData,OrderMessageStatus,OrderMessage,Topic,Query,QueryAnswer,
QueryApplicationType,TlsProvider};
use sozu_command::messages::HttpsProxyConfiguration;
use network::buffer_queue::BufferQueue;
use network::{ClientResult,ConnectionError,Protocol,RequiredEvents,ProxyClient,ProxyConfiguration,
CloseResult,AcceptError,BackendConnectAction};
use network::{http,tcp,AppId};
use network::metrics::METRICS;
const SERVER: Token = Token(0);
const DEFAULT_FRONT_TIMEOUT: u64 = 50000;
const DEFAULT_BACK_TIMEOUT: u64 = 50000;
pub type ProxyChannel = Channel<OrderMessageAnswer,OrderMessage>;
#[derive(Debug,Clone,PartialEq)]
enum ProxyType {
HTTP,
HTTPS,
TCP,
}
#[derive(PartialEq)]
pub enum ListenPortState {
Available,
InUse
}
#[derive(Copy,Clone,Debug,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct ListenToken(pub usize);
#[derive(Copy,Clone,Debug,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct ClientToken(pub usize);
impl From<usize> for ListenToken {
fn from(val: usize) -> ListenToken {
ListenToken(val)
}
}
impl From<ListenToken> for usize {
fn from(val: ListenToken) -> usize {
val.0
}
}
impl From<usize> for ClientToken {
fn from(val: usize) -> ClientToken {
ClientToken(val)
}
}
impl From<ClientToken> for usize {
fn from(val: ClientToken) -> usize {
val.0
}
}
pub struct Server {
pub poll: Poll,
shutting_down: Option<MessageId>,
accept_ready: HashSet<ListenToken>,
can_accept: bool,
channel: ProxyChannel,
queue: VecDeque<OrderMessageAnswer>,
http: Option<http::ServerConfiguration>,
https: Option<HttpsProvider>,
tcp: Option<tcp::ServerConfiguration>,
config_state: ConfigState,
scm: ScmSocket,
clients: Slab<Rc<RefCell<ProxyClientCast>>,ClientToken>,
max_connections: usize,
nb_connections: usize,
}
impl Server {
pub fn new_from_config(channel: ProxyChannel, scm: ScmSocket, config: Config, config_state: ConfigState) -> Self {
let mut event_loop = Poll::new().expect("could not create event loop");
let pool = Rc::new(RefCell::new(
Pool::with_capacity(2*config.max_buffers, 0, || BufferQueue::with_capacity(config.buffer_size))
));
info!("will try to receive listeners");
scm.set_blocking(false);
let listeners = scm.receive_listeners();
scm.set_blocking(true);
info!("received listeners: {:#?}", listeners);
let mut listeners = listeners.unwrap_or(Listeners {
http: None,
tls: None,
tcp: Vec::new(),
});
let mut clients: Slab<Rc<RefCell<ProxyClientCast>>,ClientToken> = Slab::with_capacity(10+2*config.max_connections);
{
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
trace!("taking token {:?} for channel", entry.index());
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::Channel })));
}
{
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
trace!("taking token {:?} for metrics", entry.index());
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::Metrics })));
}
let http_session = config.http.and_then(|conf| conf.to_http()).map(|http_conf| {
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
let token = Token(entry.index().0);
let (configuration, listener_tokens) = http::ServerConfiguration::new(http_conf, &mut event_loop,
pool.clone(), listeners.http.map(|fd| unsafe { TcpListener::from_raw_fd(fd) }), token);
if listener_tokens.len() == 1 {
let e = entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::HTTPListen })));
trace!("inserting http listener at token: {:?}", e.index());
}
configuration
});
let https_session = config.https.and_then(|conf| conf.to_tls()).and_then(|https_conf| {
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
let token = Token(entry.index().0);
HttpsProvider::new(https_conf, &mut event_loop, pool.clone(),
listeners.tls.map(|fd| unsafe { TcpListener::from_raw_fd(fd) }), token
).map(|(configuration, listener_tokens)| {
if listener_tokens.len() == 1 {
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::HTTPSListen })));
}
configuration
}).ok()
});
let tcp_listeners: Vec<(String, TcpListener)> = listeners.tcp.drain(..).map(|(app_id, fd)| {
(app_id, unsafe { TcpListener::from_raw_fd(fd) })
}).collect();
let mut tokens = Vec::new();
for _ in 0..tcp_listeners.len() {
let entry = clients.vacant_entry().expect("client list should have enough room at startup");
let token = Token(entry.index().0);
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::TCPListen })));
tokens.push(token);
}
let tcp_tokens: HashSet<Token> = tokens.iter().cloned().collect();
let tcp_session = config.tcp.map(|conf| {
let (configuration, listener_tokens) = tcp::ServerConfiguration::new(&mut event_loop,
pool.clone(), tcp_listeners, tokens);
let to_remove:Vec<Token> = tcp_tokens.difference(&listener_tokens).cloned().collect();
for token in to_remove.into_iter() {
clients.remove(ClientToken(token.0));
}
configuration
});
Server::new(event_loop, channel, scm, clients, http_session, https_session, tcp_session, Some(config_state),
config.max_connections)
}
pub fn new(poll: Poll, channel: ProxyChannel, scm: ScmSocket,
clients: Slab<Rc<RefCell<ProxyClientCast>>,ClientToken>,
http: Option<http::ServerConfiguration>,
https: Option<HttpsProvider>,
tcp: Option<tcp::ServerConfiguration>,
config_state: Option<ConfigState>,
max_connections: usize) -> Self {
poll.register(
&channel,
Token(0),
Ready::readable() | Ready::writable() | Ready::from(UnixReady::hup() | UnixReady::error()),
PollOpt::edge()
).expect("should register the channel");
METRICS.with(|metrics| {
if let Some(sock) = (*metrics.borrow()).socket() {
poll.register(sock, Token(1), Ready::writable(), PollOpt::edge()).expect("should register the metrics socket");
}
});
let mut server = Server {
poll: poll,
shutting_down: None,
accept_ready: HashSet::new(),
can_accept: true,
channel: channel,
queue: VecDeque::new(),
http: http,
https: https,
tcp: tcp,
config_state: ConfigState::new(),
scm: scm,
clients: clients,
max_connections: max_connections,
nb_connections: 0,
};
if let Some(state) = config_state {
let mut counter = 0usize;
for order in state.generate_orders() {
let id = format!("INIT-{}", counter);
let message = OrderMessage {
id: id,
order: order,
};
trace!("generating initial config order: {:#?}", message);
server.notify_sessions(message);
counter += 1;
}
server.queue.clear();
}
server
}
}
impl Server {
pub fn run(&mut self) {
let mut events = Events::with_capacity(1024);
let poll_timeout = Some(Duration::from_millis(1000));
let max_poll_errors = 10000;
let mut current_poll_errors = 0;
loop {
if current_poll_errors == max_poll_errors {
error!("Something is going very wrong. Last {} poll() calls failed, crashing..", current_poll_errors);
panic!(format!("poll() calls failed {} times in a row", current_poll_errors));
}
if let Err(error) = self.poll.poll(&mut events, poll_timeout) {
error!("Error while polling events: {:?}", error);
current_poll_errors += 1;
continue;
} else {
current_poll_errors = 0;
}
for event in events.iter() {
if event.token() == Token(0) {
let kind = event.readiness();
if UnixReady::from(kind).is_error() {
error!("error reading from command channel");
continue;
}
if UnixReady::from(kind).is_hup() {
error!("command channel was closed");
continue;
}
self.channel.handle_events(kind);
loop {
if !self.queue.is_empty() {
self.channel.interest.insert(Ready::writable());
}
if self.channel.readiness() == Ready::empty() {
break;
}
if self.channel.readiness().is_readable() {
self.channel.readable();
loop {
let msg = self.channel.read_message();
if msg.is_none() {
if (self.channel.interest & self.channel.readiness).is_readable() {
self.channel.readable();
continue;
} else {
break;
}
}
let msg = msg.expect("the message should be valid");
if let Order::HardStop = msg.order {
let id_msg = msg.id.clone();
self.notify(msg);
self.channel.write_message(&OrderMessageAnswer{ id: id_msg, status: OrderMessageStatus::Ok, data: None});
self.channel.run();
return;
} else if let Order::SoftStop = msg.order {
self.shutting_down = Some(msg.id.clone());
self.notify(msg);
} else if let Order::ReturnListenSockets = msg.order {
info!("received ReturnListenSockets order");
self.return_listen_sockets();
} else {
self.notify(msg);
}
}
}
if !self.queue.is_empty() {
self.channel.interest.insert(Ready::writable());
}
if self.channel.readiness.is_writable() {
loop {
if let Some(msg) = self.queue.pop_front() {
if !self.channel.write_message(&msg) {
self.queue.push_front(msg);
}
}
if self.channel.back_buf.available_data() > 0 {
self.channel.writable();
}
if !self.channel.readiness.is_writable() {
break;
}
if self.channel.back_buf.available_data() == 0 && self.queue.len() == 0 {
break;
}
}
}
}
} else if event.token() == Token(1) {
METRICS.with(|metrics| {
(*metrics.borrow_mut()).writable();
});
} else {
self.ready(event.token(), event.readiness());
}
}
self.handle_remaining_readiness();
METRICS.with(|metrics| {
(*metrics.borrow_mut()).send_data();
});
if self.shutting_down.is_some() {
info!("last client stopped, shutting down!");
self.channel.run();
self.channel.set_blocking(true);
self.channel.write_message(&OrderMessageAnswer{ id: self.shutting_down.take().expect("should have shut down correctly"), status: OrderMessageStatus::Ok, data: None});
return;
}
}
}
fn notify(&mut self, message: OrderMessage) {
if let Order::Metrics = message.order {
let q = &mut self.queue;
let msg = METRICS.with(|metrics| {
q.push_back(OrderMessageAnswer {
id: message.id.clone(),
status: OrderMessageStatus::Ok,
data: Some(OrderMessageAnswerData::Metrics(
(*metrics.borrow_mut()).dump_metrics_data()
))
});
});
return;
}
if let Order::Query(ref query) = message.order {
match query {
&Query::ApplicationsHashes => {
self.queue.push_back(OrderMessageAnswer {
id: message.id.clone(),
status: OrderMessageStatus::Ok,
data: Some(OrderMessageAnswerData::Query(
QueryAnswer::ApplicationsHashes(self.config_state.hash_state())
))
});
},
&Query::Applications(ref query_type) => {
let answer = match query_type {
&QueryApplicationType::AppId(ref app_id) => {
QueryAnswer::Applications(vec!(self.config_state.application_state(app_id)))
},
&QueryApplicationType::Domain(ref domain) => {
let app_ids = get_application_ids_by_domain(&self.config_state, domain.hostname.clone(), domain.path_begin.clone());
let answer = app_ids.iter().map(|ref app_id| self.config_state.application_state(app_id)).collect();
QueryAnswer::Applications(answer)
}
};
self.queue.push_back(OrderMessageAnswer {
id: message.id.clone(),
status: OrderMessageStatus::Ok,
data: Some(OrderMessageAnswerData::Query(answer))
});
}
}
return
}
self.notify_sessions(message);
}
pub fn notify_sessions(&mut self, message: OrderMessage) {
self.config_state.handle_order(&message.order);
let topics = message.order.get_topics();
if topics.contains(&Topic::HttpProxyConfig) {
if let Some(mut http) = self.http.take() {
self.queue.push_back(http.notify(&mut self.poll, message.clone()));
self.http = Some(http);
} else {
match message.clone() {
OrderMessage{ id, order: Order::AddHttpFront(http_front) } => {
warn!("No HTTP proxy configured to handle {:?}", message);
let status = OrderMessageStatus::Error(String::from("No HTTP proxy configured"));
self.queue.push_back(OrderMessageAnswer{ id: id.clone(), status, data: None });
},
_ => {}
}
}
}
if topics.contains(&Topic::HttpsProxyConfig) {
if let Some(mut https) = self.https.take() {
self.queue.push_back(https.notify(&mut self.poll, message.clone()));
self.https = Some(https);
} else {
match message.clone() {
OrderMessage{ id, order: Order::AddHttpsFront(https_front) } => {
warn!("No HTTPS proxy configured to handle {:?}", message);
let status = OrderMessageStatus::Error(String::from("No HTTPS proxy configured"));
self.queue.push_back(OrderMessageAnswer{ id: id.clone(), status, data: None });
},
_ => {}
}
}
}
if topics.contains(&Topic::TcpProxyConfig) {
if let Some(mut tcp) = self.tcp.take() {
match message {
OrderMessage { id, order: Order::AddTcpFront(tcp_front) } => {
if self.listen_port_state(&tcp_front.port, &tcp) == ListenPortState::InUse {
error!("Couldn't add TCP front {:?}: port already in use", tcp_front);
self.queue.push_back(OrderMessageAnswer {
id,
status: OrderMessageStatus::Error(String::from("Couldn't add TCP front: port already in use")),
data: None
});
self.tcp = Some(tcp);
return;
}
let entry = self.clients.vacant_entry();
if entry.is_none() {
self.queue.push_back(OrderMessageAnswer {
id,
status: OrderMessageStatus::Error(String::from("client list is full, cannot add a listener")),
data: None
});
self.tcp = Some(tcp);
return;
}
let entry = entry.unwrap();
let token = Token(entry.index().0);
entry.insert(Rc::new(RefCell::new(ListenClient { protocol: Protocol::TCPListen })));
let addr_string = tcp_front.ip_address + ":" + &tcp_front.port.to_string();
let status = if let Ok(front) = addr_string.parse() {
if let Some(token) = tcp.add_tcp_front(&tcp_front.app_id, &front, &mut self.poll, token) {
OrderMessageStatus::Ok
} else {
error!("Couldn't add tcp front");
OrderMessageStatus::Error(String::from("cannot add tcp front"))
}
} else {
error!("Couldn't parse tcp front address");
OrderMessageStatus::Error(String::from("cannot parse the address"))
};
let answer = OrderMessageAnswer { id, status, data: None };
self.queue.push_back(answer);
},
m => self.queue.push_back(tcp.notify(&mut self.poll, m)),
}
self.tcp = Some(tcp);
} else {
match message.clone() {
OrderMessage { id, order: Order::AddTcpFront(tcp_front) } => {
warn!("No TCP proxy configured to handle {:?}", message);
let status = OrderMessageStatus::Error(String::from("No TCP proxy configured"));
self.queue.push_back(OrderMessageAnswer{ id, status, data: None });
},
_ => {}
}
}
}
}
pub fn return_listen_sockets(&mut self) {
self.scm.set_blocking(false);
let http_listener = self.http.as_mut()
.and_then(|http| http.give_back_listener());
if let Some(ref sock) = http_listener {
self.poll.deregister(sock);
}
let https_listener = self.https.as_mut()
.and_then(|https| https.give_back_listener());
if let Some(ref sock) = https_listener {
self.poll.deregister(sock);
}
let tcp_listeners = self.tcp.as_mut()
.map(|tcp| tcp.give_back_listeners()).unwrap_or(Vec::new());
for &(_, ref sock) in tcp_listeners.iter() {
self.poll.deregister(sock);
}
let res = self.scm.send_listeners(Listeners {
http: http_listener.map(|listener| listener.as_raw_fd()),
tls: https_listener.map(|listener| listener.as_raw_fd()),
tcp: tcp_listeners.into_iter().map(|(app_id, listener)| (app_id, listener.as_raw_fd())).collect(),
});
self.scm.set_blocking(true);
info!("sent default listeners: {:?}", res);
}
pub fn to_client(&self, token: Token) -> ClientToken {
ClientToken(token.0)
}
pub fn from_client(&self, token: ClientToken) -> Token {
Token(token.0)
}
pub fn from_client_add(&self) -> usize {
0
}
pub fn close_client(&mut self, token: ClientToken) {
if self.clients.contains(token) {
let client = self.clients.remove(token).expect("client shoud be there");
let CloseResult { tokens, backends } = client.borrow_mut().close(&mut self.poll);
for tk in tokens.into_iter() {
let cl = self.to_client(tk);
self.clients.remove(cl);
}
let protocol = { client.borrow().protocol() };
match protocol {
Protocol::TCP => {
self.tcp.as_mut().map(|tcp| {
for (app_id, address) in backends.into_iter() {
tcp.close_backend(app_id, &address);
}
});
},
Protocol::HTTP => {
self.http.as_mut().map(|http| {
for (app_id, address) in backends.into_iter() {
http.close_backend(app_id, &address);
}
});
},
Protocol::HTTPS => {
self.https.as_mut().map(|https| {
for (app_id, address) in backends.into_iter() {
https.close_backend(app_id, &address);
}
});
},
_ => {}
}
assert!(self.nb_connections != 0);
self.nb_connections -= 1;
gauge!("client.connections", self.nb_connections);
}
if !self.can_accept && self.nb_connections < self.max_connections * 90 / 100 {
debug!("nb_connections = {}, max_connections = {}, starting to accept again", self.nb_connections, self.max_connections);
self.can_accept = true;
}
}
pub fn accept(&mut self, token: ListenToken, protocol: Protocol) -> bool {
let add = self.from_client_add();
let res = {
if self.nb_connections == self.max_connections {
error!("max number of client connection reached, flushing the accept queue");
Err(AcceptError::TooManyClients)
} else {
match self.clients.vacant_entry() {
None => {
error!("not enough memory to accept another client, flushing the accept queue");
error!("nb_connections: {}, max_connections: {}", self.nb_connections, self.max_connections);
Err(AcceptError::TooManyClients)
},
Some(entry) => {
let client_token = Token(entry.index().0 + add);
let index = entry.index();
match protocol {
Protocol::TCPListen => {
let mut tcp = self.tcp.take().expect("if we have a TCPListen, we should have a TCP configuration");
let res1 = tcp.accept(token, &mut self.poll, client_token).map(|(client, should_connect)| {
entry.insert(client);
(index, should_connect)
});
self.tcp = Some(tcp);
res1
},
Protocol::HTTPListen => {
let mut http = self.http.take().expect("if we have a HTTPListen, we should have a HTTP configuration");
let res1 = http.accept(token, &mut self.poll, client_token).map(|(client, should_connect)| {
entry.insert(client);
(index, should_connect)
});
self.http = Some(http);
res1
},
Protocol::HTTPSListen => {
let mut https = self.https.take().expect("if we have a HTTPSListen, we should have a HTTPS configuration");
let res1 = https.accept(token, &mut self.poll, client_token).map(|(client, should_connect)| {
entry.insert(client);
(index, should_connect)
});
self.https = Some(https);
res1
},
_ => panic!("should not call accept() on a HTTP, HTTPS or TCP client"),
}
}
}
}
};
match res {
Err(AcceptError::IoError) => {
false
},
Err(AcceptError::TooManyClients) => {
self.accept_flush();
self.can_accept = false;
false
},
Err(AcceptError::WouldBlock) => {
self.accept_ready.remove(&token);
false
},
Ok((client_token, should_connect)) => {
self.nb_connections += 1;
assert!(self.nb_connections <= self.max_connections);
gauge!("client.connections", self.nb_connections);
if should_connect {
self.connect_to_backend(client_token);
}
true
}
}
}
pub fn accept_flush(&mut self) {
let mut counter = self.tcp.as_mut().map(|tcp| tcp.accept_flush()).unwrap_or(0);
counter += self.http.as_mut().map(|http| http.accept_flush()).unwrap_or(0);
counter += self.https.as_mut().map(|https| https.accept_flush()).unwrap_or(0);
error!("flushed {} new connections", counter);
}
pub fn connect_to_backend(&mut self, token: ClientToken) {
let add = self.from_client_add();
let (protocol, res) = {
let cl = self.clients[token].clone();
let cl2: Rc<RefCell<ProxyClientCast>> = self.clients[token].clone();
let protocol = { cl.borrow().protocol() };
let entry = self.clients.vacant_entry();
if entry.is_none() {
error!("not enough memory, cannot connect to backend");
return;
}
let entry = entry.unwrap();
let entry = entry.insert(cl);
let back_token = Token(entry.index().0 + add);
let (protocol, res) = match protocol {
Protocol::TCP => {
let r = if let Some(mut tcp) = self.tcp.take() {
let mut b = cl2.borrow_mut();
let client: &mut tcp::Client = b.as_tcp() ;
let r = tcp.connect_to_backend(&mut self.poll, client, back_token);
self.tcp = Some(tcp);
r
} else {
Err(ConnectionError::HostNotFound)
};
(Protocol::TCP, r)
},
Protocol::HTTP => {
(Protocol::HTTP, if let Some(mut http) = self.http.take() {
let mut b = cl2.borrow_mut();
let client: &mut http::Client = b.as_http() ;
let r = http.connect_to_backend(&mut self.poll, client, back_token);
self.http = Some(http);
r
} else {
Err(ConnectionError::HostNotFound)
})
},
Protocol::HTTPS => {
(Protocol::HTTPS, if let Some(mut https) = self.https.take() {
let r = https.connect_to_backend(&mut self.poll, cl2, back_token);
self.https = Some(https);
r
} else {
Err(ConnectionError::HostNotFound)
})
},
_ => {
panic!("should not call connect_to_backend on listeners");
},
};
if res != Ok(BackendConnectAction::New) {
entry.remove();
}
(protocol, res)
};
match res {
Ok(BackendConnectAction::Reuse) => {
debug!("keepalive, reusing backend connection");
}
Ok(BackendConnectAction::Replace) => {
},
Ok(BackendConnectAction::New) => {
},
Err(ConnectionError::HostNotFound) | Err(ConnectionError::NoBackendAvailable) | Err(ConnectionError::HttpsRedirect) => {
if protocol == Protocol::TCP {
self.close_client(token);
}
},
_ => self.close_client(token),
}
}
pub fn interpret_client_order(&mut self, token: ClientToken, order: ClientResult) {
match order {
ClientResult::CloseClient => self.close_client(token),
ClientResult::CloseBackend(opt) => {
if let Some(token) = opt {
let cl = self.to_client(token);
if let Some(client) = self.clients.remove(cl) {
let protocol = client.borrow().protocol();
let res = client.borrow_mut().close_backend(token, &mut self.poll);
if let Some((app_id, address)) = res {
match protocol {
Protocol::TCP => self.tcp.as_mut().map(|c| {
c.close_backend(app_id, &address);
}),
Protocol::HTTP => self.http.as_mut().map(|c| {
c.close_backend(app_id, &address);
}),
Protocol::HTTPS => self.https.as_mut().map(|c| {
c.close_backend(app_id, &address);
}),
_ => {
panic!("should not call interpret_client_order on a listen socket");
}
};
}
}
}
},
ClientResult::ReconnectBackend(main_token, backend_token) => {
if let Some(t) = backend_token {
let cl = self.to_client(t);
let _ = self.clients.remove(cl);
}
if let Some(t) = main_token {
let tok = self.to_client(t);
self.connect_to_backend(tok)
}
},
ClientResult::ConnectBackend => self.connect_to_backend(token),
ClientResult::Continue => {}
}
}
pub fn ready(&mut self, token: Token, events: Ready) {
trace!("PROXY\t{:?} got events: {:?}", token, events);
let mut client_token = ClientToken(token.0);
if self.clients.contains(client_token) {
let protocol = self.clients[client_token].borrow().protocol();
match protocol {
Protocol::HTTPListen | Protocol::HTTPSListen | Protocol::TCPListen => {
if events.is_readable() {
self.accept_ready.insert(ListenToken(token.0));
if self.can_accept {
loop {
if !self.accept(ListenToken(token.0), protocol) {
break;
}
}
}
return;
}
if events.is_writable() {
error!("received writable for listener {:?}, this should not happen", token);
return;
}
if UnixReady::from(events).is_hup() {
error!("should not happen: server {:?} closed", token);
return;
}
unreachable!();
},
_ => {}
}
self.clients[client_token].borrow_mut().process_events(token, events);
loop {
if !self.clients.contains(client_token) {
break;
}
let order = self.clients[client_token].borrow_mut().ready();
trace!("client[{:?} -> {:?}] got events {:?} and returned order {:?}", client_token, self.from_client(client_token), events, order);
let is_connect = match order {
ClientResult::ConnectBackend | ClientResult::ReconnectBackend(_,_) => true,
_ => false,
};
if let ClientResult::ReconnectBackend(Some(t), _) = order {
client_token = self.to_client(t);
}
self.interpret_client_order(client_token, order);
if !is_connect {
break;
}
}
}
}
pub fn handle_remaining_readiness(&mut self) {
if self.can_accept && !self.accept_ready.is_empty() {
loop {
if let Some(token) = self.accept_ready.iter().next().map(|token| ListenToken(token.0)) {
let protocol = self.clients[ClientToken(token.0)].borrow().protocol();
if !self.accept(token, protocol) {
if !self.can_accept || self.accept_ready.is_empty() {
break;
}
}
} else {
break;
}
}
}
}
fn listen_port_state(&self, port: &u16, tcp_proxy: &tcp::ServerConfiguration) -> ListenPortState {
let http_binded = self.http.as_ref().map(|http| http.listen_port_state(&port)).unwrap_or(ListenPortState::Available);
if http_binded == ListenPortState::Available {
let https_binded = match self.https.as_ref() {
#[cfg(feature = "use-openssl")]
Some(&HttpsProvider::Openssl(ref openssl)) => openssl.listen_port_state(&port),
Some(&HttpsProvider::Rustls(ref rustls)) => rustls.listen_port_state(&port),
None => ListenPortState::Available
};
if https_binded == ListenPortState::Available {
return tcp_proxy.listen_port_state(&port);
}
}
ListenPortState::InUse
}
}
pub struct ListenClient {
pub protocol: Protocol,
}
impl ProxyClient for ListenClient {
fn protocol(&self) -> Protocol {
self.protocol
}
fn ready(&mut self) -> ClientResult {
ClientResult::Continue
}
fn process_events(&mut self, token: Token, events: Ready) {}
fn close(&mut self, poll: &mut Poll) -> CloseResult {
CloseResult::default()
}
fn close_backend(&mut self, token: Token, poll: &mut Poll) -> Option<(String, SocketAddr)> {
None
}
}
#[cfg(feature = "use-openssl")]
use network::https_openssl;
use network::https_rustls;
#[cfg(feature = "use-openssl")]
pub enum HttpsProvider {
Openssl(https_openssl::ServerConfiguration),
Rustls(https_rustls::configuration::ServerConfiguration),
}
#[cfg(not(feature = "use-openssl"))]
pub enum HttpsProvider {
Rustls(https_rustls::configuration::ServerConfiguration),
}
#[cfg(feature = "use-openssl")]
impl HttpsProvider {
pub fn new(config: HttpsProxyConfiguration, event_loop: &mut Poll,
pool: Rc<RefCell<Pool<BufferQueue>>>, tcp_listener: Option<TcpListener>, token: Token) -> io::Result<(HttpsProvider, HashSet<Token>)> {
match config.tls_provider {
TlsProvider::Openssl => {
https_openssl::ServerConfiguration::new(config, event_loop, pool,
tcp_listener, token).map(|(conf, set)| {
(HttpsProvider::Openssl(conf), set)
})
},
TlsProvider::Rustls => {
https_rustls::configuration::ServerConfiguration::new(config, event_loop, pool,
tcp_listener, token).map(|(conf, set)| {
(HttpsProvider::Rustls(conf), set)
})
}
}
}
pub fn notify(&mut self, event_loop: &mut Poll, message: OrderMessage) -> OrderMessageAnswer {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => rustls.notify(event_loop, message),
&mut HttpsProvider::Openssl(ref mut openssl) => openssl.notify(event_loop, message),
}
}
pub fn give_back_listener(&mut self) -> Option<TcpListener> {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => rustls.give_back_listener(),
&mut HttpsProvider::Openssl(ref mut openssl) => openssl.give_back_listener(),
}
}
pub fn accept(&mut self, token: ListenToken, poll: &mut Poll, client_token: Token) -> Result<(Rc<RefCell<ProxyClientCast>>,bool), AcceptError> {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => rustls.accept(token, poll, client_token).map(|(r,b)| {
(r as Rc<RefCell<ProxyClientCast>>, b)
}),
&mut HttpsProvider::Openssl(ref mut openssl) => openssl.accept(token, poll, client_token).map(|(r,b)| {
(r as Rc<RefCell<ProxyClientCast>>, b)
}),
}
}
pub fn close_backend(&mut self, app_id: AppId, addr: &SocketAddr) {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => rustls.close_backend(app_id, addr),
&mut HttpsProvider::Openssl(ref mut openssl) => openssl.close_backend(app_id, addr),
}
}
pub fn accept_flush(&mut self) -> usize {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => rustls.accept_flush(),
&mut HttpsProvider::Openssl(ref mut openssl) => openssl.accept_flush(),
}
}
pub fn connect_to_backend(&mut self, poll: &mut Poll, proxy_client: Rc<RefCell<ProxyClientCast>>, back_token: Token)
-> Result<BackendConnectAction, ConnectionError> {
match self {
&mut HttpsProvider::Rustls(ref mut rustls) => {
let mut b = proxy_client.borrow_mut();
let client: &mut https_rustls::client::TlsClient = b.as_https_rustls();
rustls.connect_to_backend(poll, client, back_token)
},
&mut HttpsProvider::Openssl(ref mut openssl) => {
let mut b = proxy_client.borrow_mut();
let client: &mut https_openssl::TlsClient = b.as_https_openssl();
openssl.connect_to_backend(poll, client, back_token)
}
}
}
}
use network::https_rustls::client::TlsClient;
#[cfg(not(feature = "use-openssl"))]
impl HttpsProvider {
pub fn new(config: HttpsProxyConfiguration, event_loop: &mut Poll,
pool: Rc<RefCell<Pool<BufferQueue>>>, tcp_listener: Option<TcpListener>, token: Token) -> io::Result<(HttpsProvider, HashSet<Token>)> {
if config.tls_provider == TlsProvider::Openssl {
error!("the openssl provider is not compiled, continuing with the rustls provider");
}
https_rustls::configuration::ServerConfiguration::new(config, event_loop, pool,
tcp_listener, token).map(|(conf, set)| {
(HttpsProvider::Rustls(conf), set)
})
}
pub fn notify(&mut self, event_loop: &mut Poll, message: OrderMessage) -> OrderMessageAnswer {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
rustls.notify(event_loop, message)
}
pub fn give_back_listener(&mut self) -> Option<TcpListener> {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
rustls.give_back_listener()
}
pub fn accept(&mut self, token: ListenToken, poll: &mut Poll, client_token: Token) -> Result<(Rc<RefCell<TlsClient>>,bool), AcceptError> {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
rustls.accept(token, poll, client_token)
}
pub fn close_backend(&mut self, app_id: AppId, addr: &SocketAddr) {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
rustls.close_backend(app_id, addr);
}
pub fn accept_flush(&mut self) -> usize {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
rustls.accept_flush()
}
pub fn connect_to_backend(&mut self, poll: &mut Poll, proxy_client: Rc<RefCell<ProxyClientCast>>, back_token: Token)
-> Result<BackendConnectAction, ConnectionError> {
let &mut HttpsProvider::Rustls(ref mut rustls) = self;
let mut b = proxy_client.borrow_mut();
let client: &mut https_rustls::client::TlsClient = b.as_https_rustls() ;
rustls.connect_to_backend(poll, client, back_token)
}
}
#[cfg(not(feature = "use-openssl"))]
pub trait ProxyClientCast: ProxyClient {
fn as_tcp(&mut self) -> &mut tcp::Client;
fn as_http(&mut self) -> &mut http::Client;
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient;
}
#[cfg(feature = "use-openssl")]
pub trait ProxyClientCast: ProxyClient {
fn as_tcp(&mut self) -> &mut tcp::Client;
fn as_http(&mut self) -> &mut http::Client;
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient;
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient;
}
#[cfg(not(feature = "use-openssl"))]
impl ProxyClientCast for ListenClient {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
}
#[cfg(feature = "use-openssl")]
impl ProxyClientCast for ListenClient {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() }
}
#[cfg(not(feature = "use-openssl"))]
impl ProxyClientCast for http::Client {
fn as_http(&mut self) -> &mut http::Client { self }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
}
#[cfg(feature = "use-openssl")]
impl ProxyClientCast for http::Client {
fn as_http(&mut self) -> &mut http::Client { self }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() }
}
#[cfg(not(feature = "use-openssl"))]
impl ProxyClientCast for tcp::Client {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { self }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
}
#[cfg(feature = "use-openssl")]
impl ProxyClientCast for tcp::Client {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { self }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() }
}
#[cfg(not(feature = "use-openssl"))]
impl ProxyClientCast for https_rustls::client::TlsClient {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { self }
}
#[cfg(feature = "use-openssl")]
impl ProxyClientCast for https_rustls::client::TlsClient {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { self }
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { panic!() }
}
#[cfg(feature = "use-openssl")]
impl ProxyClientCast for https_openssl::TlsClient {
fn as_http(&mut self) -> &mut http::Client { panic!() }
fn as_tcp(&mut self) -> &mut tcp::Client { panic!() }
fn as_https_rustls(&mut self) -> &mut https_rustls::client::TlsClient { panic!() }
fn as_https_openssl(&mut self) -> &mut https_openssl::TlsClient { self }
}