]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/proxmox-backup-proxy.rs
use service Type=notify
[proxmox-backup.git] / src / bin / proxmox-backup-proxy.rs
CommitLineData
d607b886 1use proxmox_backup::try_block;
a2ca7137 2use proxmox_backup::configdir;
4223d9f8 3use proxmox_backup::tools;
e3f41f21 4use proxmox_backup::server;
a690ecac 5use proxmox_backup::tools::daemon;
dc9a007b
DM
6use proxmox_backup::api_schema::router::*;
7use proxmox_backup::api_schema::config::*;
02c7a755 8use proxmox_backup::server::rest::*;
d01e2420 9use proxmox_backup::auth_helpers::*;
02c7a755 10
0d176f36 11use failure::*;
02c7a755
DM
12use lazy_static::lazy_static;
13
e3f41f21 14use futures::*;
0d176f36 15use futures::stream::Stream;
02c7a755
DM
16
17use hyper;
18
19fn main() {
20
4223d9f8
DM
21 if let Err(err) = run() {
22 eprintln!("Error: {}", err);
23 std::process::exit(-1);
24 }
25}
26
27fn run() -> Result<(), Error> {
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 58 let cert_path = configdir!("/proxy.pfx");
a8f268af 59 let raw_cert = tools::file_get_contents(cert_path)?;
4223d9f8
DM
60
61 let identity = match native_tls::Identity::from_pkcs12(&raw_cert, "") {
62 Ok(data) => data,
63 Err(err) => bail!("unabled to decode pkcs12 identity {} - {}", cert_path, err),
64 };
0d176f36 65
a690ecac
WB
66 let server = daemon::create_daemon(
67 ([0,0,0,0,0,0,0,0], 8007).into(),
68 |listener| {
69 let acceptor = native_tls::TlsAcceptor::new(identity)?;
70 let acceptor = std::sync::Arc::new(tokio_tls::TlsAcceptor::from(acceptor));
71 let connections = listener
72 .incoming()
73 .map_err(Error::from)
74 .and_then(move |sock| acceptor.accept(sock).map_err(|e| e.into()))
75 .then(|r| match r {
76 // accept()s can fail here with an Err() when eg. the client rejects
77 // the cert and closes the connection, so we follow up with mapping
78 // it to an option and then filtering None with filter_map
79 Ok(c) => Ok::<_, Error>(Some(c)),
80 Err(e) => {
81 if let Some(_io) = e.downcast_ref::<std::io::Error>() {
82 // "real" IO errors should not simply be ignored
83 bail!("shutting down...");
84 } else {
85 // handshake errors just get filtered by filter_map() below:
86 Ok(None)
87 }
88 }
89 })
90 .filter_map(|r| {
91 // Filter out the Nones
92 r
93 });
5f550fd9 94
a690ecac 95 Ok(hyper::Server::builder(connections)
5f550fd9
DM
96 .serve(rest_server)
97 .with_graceful_shutdown(server::shutdown_future())
98 .map_err(|err| eprintln!("server error: {}", err))
a690ecac 99 )
a2ca7137
WB
100 },
101 )?;
a2ca7137 102
d98c9a7a
WB
103 daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
104
e3f41f21
DM
105 tokio::run(lazy(|| {
106
d607b886
DM
107 let init_result: Result<(), Error> = try_block!({
108 server::create_task_control_socket()?;
109 server::server_state_init()?;
110 Ok(())
111 });
112
113 if let Err(err) = init_result {
e3f41f21
DM
114 eprintln!("unable to start daemon - {}", err);
115 } else {
5f550fd9
DM
116 tokio::spawn(server.then(|_| {
117 log::info!("done - exit server");
118 Ok(())
119 }));
e3f41f21
DM
120 }
121
122 Ok(())
123 }));
124
4223d9f8 125 Ok(())
02c7a755 126}