]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-proxy.rs
bin/proxmox-backup-proxy.rs: improve error handling
[proxmox-backup.git] / src / bin / proxmox-backup-proxy.rs
CommitLineData
9d02e5c3 1#[macro_use]
02c7a755
DM
2extern crate proxmox_backup;
3
4223d9f8 4use proxmox_backup::tools;
02c7a755
DM
5use proxmox_backup::api::router::*;
6use proxmox_backup::api::config::*;
7use proxmox_backup::server::rest::*;
d01e2420 8use proxmox_backup::auth_helpers::*;
02c7a755 9
0d176f36 10use failure::*;
02c7a755
DM
11use lazy_static::lazy_static;
12
13use futures::future::Future;
0d176f36 14use futures::stream::Stream;
02c7a755
DM
15
16use hyper;
17
18fn main() {
19
4223d9f8
DM
20 if let Err(err) = run() {
21 eprintln!("Error: {}", err);
22 std::process::exit(-1);
23 }
24}
25
26fn run() -> Result<(), Error> {
27
02c7a755
DM
28 if let Err(err) = syslog::init(
29 syslog::Facility::LOG_DAEMON,
30 log::LevelFilter::Info,
31 Some("proxmox-backup-proxy")) {
4223d9f8 32 bail!("unable to inititialize syslog - {}", err);
02c7a755
DM
33 }
34
d01e2420
DM
35 let _ = public_auth_key(); // load with lazy_static
36 let _ = csrf_secret(); // load with lazy_static
37
02c7a755
DM
38 lazy_static!{
39 static ref ROUTER: Router = proxmox_backup::api2::router();
40 }
41
42 let mut config = ApiConfig::new(
6285b251 43 env!("PROXMOX_JSDIR"), &ROUTER, RpcEnvironmentType::PUBLIC);
02c7a755
DM
44
45 // add default dirs which includes jquery and bootstrap
46 // my $base = '/usr/share/libpve-http-server-perl';
47 // add_dirs($self->{dirs}, '/css/' => "$base/css/");
48 // add_dirs($self->{dirs}, '/js/' => "$base/js/");
49 // add_dirs($self->{dirs}, '/fonts/' => "$base/fonts/");
50 config.add_alias("novnc", "/usr/share/novnc-pve");
51 config.add_alias("extjs", "/usr/share/javascript/extjs");
52 config.add_alias("fontawesome", "/usr/share/fonts-font-awesome");
53 config.add_alias("xtermjs", "/usr/share/pve-xtermjs");
54 config.add_alias("widgettoolkit", "/usr/share/javascript/proxmox-widget-toolkit");
55
56 let rest_server = RestServer::new(config);
57
4223d9f8
DM
58 let cert_path = configdir!("/proxy.pfx");
59 let raw_cert = match tools::file_get_contents(cert_path) {
60 Ok(data) => data,
61 Err(err) => bail!("unable to read certificate {} - {}", cert_path, err),
62 };
63
64 let identity = match native_tls::Identity::from_pkcs12(&raw_cert, "") {
65 Ok(data) => data,
66 Err(err) => bail!("unabled to decode pkcs12 identity {} - {}", cert_path, err),
67 };
0d176f36
WB
68
69 let addr = ([0,0,0,0,0,0,0,0], 8007).into();
4223d9f8
DM
70 let listener = tokio::net::TcpListener::bind(&addr)?;
71 let acceptor = native_tls::TlsAcceptor::new(identity)?;
0d176f36
WB
72 let acceptor = std::sync::Arc::new(tokio_tls::TlsAcceptor::from(acceptor));
73 let connections = listener
74 .incoming()
75 .map_err(|e| Error::from(e))
76 .and_then(move |sock| acceptor.accept(sock).map_err(|e| e.into()))
77 .then(|r| match r {
78 // accept()s can fail here with an Err() when eg. the client rejects
79 // the cert and closes the connection, so we follow up with mapping
80 // it to an option and then filtering None with filter_map
81 Ok(c) => Ok::<_, Error>(Some(c)),
82 Err(_) => Ok(None),
83 })
84 .filter_map(|r| {
85 // Filter out the Nones
86 r
87 });
88
89 let server = hyper::Server::builder(connections)
02c7a755
DM
90 .serve(rest_server)
91 .map_err(|e| eprintln!("server error: {}", e));
92
02c7a755
DM
93 // Run this server for... forever!
94 hyper::rt::run(server);
4223d9f8
DM
95
96 Ok(())
02c7a755 97}