2019-11-21 13:36:28 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use failure::*;
|
|
|
|
use futures::*;
|
|
|
|
use hyper;
|
|
|
|
use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype};
|
|
|
|
|
|
|
|
use proxmox::tools::try_block;
|
|
|
|
use proxmox::api::RpcEnvironmentType;
|
|
|
|
|
2019-03-11 08:38:35 +00:00
|
|
|
use proxmox_backup::configdir;
|
2019-09-09 08:51:08 +00:00
|
|
|
use proxmox_backup::buildcfg;
|
2019-04-08 12:00:23 +00:00
|
|
|
use proxmox_backup::server;
|
2019-03-18 13:13:44 +00:00
|
|
|
use proxmox_backup::tools::daemon;
|
2019-11-22 08:23:03 +00:00
|
|
|
use proxmox_backup::server::{ApiConfig, rest::*};
|
2019-01-29 16:21:58 +00:00
|
|
|
use proxmox_backup::auth_helpers::*;
|
2019-01-28 12:17:03 +00:00
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
if let Err(err) = run().await {
|
2019-02-11 13:43:26 +00:00
|
|
|
eprintln!("Error: {}", err);
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
async fn run() -> Result<(), Error> {
|
2019-01-28 12:17:03 +00:00
|
|
|
if let Err(err) = syslog::init(
|
|
|
|
syslog::Facility::LOG_DAEMON,
|
|
|
|
log::LevelFilter::Info,
|
|
|
|
Some("proxmox-backup-proxy")) {
|
2019-02-11 13:43:26 +00:00
|
|
|
bail!("unable to inititialize syslog - {}", err);
|
2019-01-28 12:17:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:21:58 +00:00
|
|
|
let _ = public_auth_key(); // load with lazy_static
|
|
|
|
let _ = csrf_secret(); // load with lazy_static
|
|
|
|
|
2019-01-28 12:17:03 +00:00
|
|
|
let mut config = ApiConfig::new(
|
2019-11-21 08:36:41 +00:00
|
|
|
buildcfg::JS_DIR, &proxmox_backup::api2::ROUTER, RpcEnvironmentType::PUBLIC);
|
2019-01-28 12:17:03 +00:00
|
|
|
|
|
|
|
// add default dirs which includes jquery and bootstrap
|
|
|
|
// my $base = '/usr/share/libpve-http-server-perl';
|
|
|
|
// add_dirs($self->{dirs}, '/css/' => "$base/css/");
|
|
|
|
// add_dirs($self->{dirs}, '/js/' => "$base/js/");
|
|
|
|
// add_dirs($self->{dirs}, '/fonts/' => "$base/fonts/");
|
|
|
|
config.add_alias("novnc", "/usr/share/novnc-pve");
|
|
|
|
config.add_alias("extjs", "/usr/share/javascript/extjs");
|
|
|
|
config.add_alias("fontawesome", "/usr/share/fonts-font-awesome");
|
|
|
|
config.add_alias("xtermjs", "/usr/share/pve-xtermjs");
|
|
|
|
config.add_alias("widgettoolkit", "/usr/share/javascript/proxmox-widget-toolkit");
|
2019-12-16 17:16:05 +00:00
|
|
|
config.add_alias("css", "/usr/share/javascript/proxmox-backup/css");
|
2019-01-28 12:17:03 +00:00
|
|
|
|
|
|
|
let rest_server = RestServer::new(config);
|
|
|
|
|
2019-07-02 11:33:58 +00:00
|
|
|
//openssl req -x509 -newkey rsa:4096 -keyout /etc/proxmox-backup/proxy.key -out /etc/proxmox-backup/proxy.pem -nodes
|
|
|
|
let key_path = configdir!("/proxy.key");
|
|
|
|
let cert_path = configdir!("/proxy.pem");
|
|
|
|
|
|
|
|
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
|
|
|
acceptor.set_private_key_file(key_path, SslFiletype::PEM)
|
|
|
|
.map_err(|err| format_err!("unable to read proxy key {} - {}", key_path, err))?;
|
|
|
|
acceptor.set_certificate_chain_file(cert_path)
|
|
|
|
.map_err(|err| format_err!("unable to read proxy cert {} - {}", cert_path, err))?;
|
|
|
|
acceptor.check_private_key().unwrap();
|
|
|
|
|
|
|
|
let acceptor = Arc::new(acceptor.build());
|
2019-02-04 13:56:07 +00:00
|
|
|
|
2019-03-18 13:13:44 +00:00
|
|
|
let server = daemon::create_daemon(
|
|
|
|
([0,0,0,0,0,0,0,0], 8007).into(),
|
2019-09-02 13:16:21 +00:00
|
|
|
|listener, ready| {
|
2019-12-12 14:27:07 +00:00
|
|
|
let connections = proxmox_backup::tools::async_io::StaticIncoming::from(listener)
|
2019-03-18 13:13:44 +00:00
|
|
|
.map_err(Error::from)
|
2019-12-12 14:27:07 +00:00
|
|
|
.try_filter_map(move |(sock, _addr)| {
|
2019-08-29 07:45:34 +00:00
|
|
|
let acceptor = Arc::clone(&acceptor);
|
|
|
|
async move {
|
|
|
|
sock.set_nodelay(true).unwrap();
|
|
|
|
sock.set_send_buffer_size(1024*1024).unwrap();
|
|
|
|
sock.set_recv_buffer_size(1024*1024).unwrap();
|
|
|
|
Ok(tokio_openssl::accept(&acceptor, sock)
|
|
|
|
.await
|
|
|
|
.ok() // handshake errors aren't be fatal, so return None to filter
|
|
|
|
)
|
2019-03-18 13:13:44 +00:00
|
|
|
}
|
|
|
|
});
|
2019-12-12 14:27:07 +00:00
|
|
|
let connections = proxmox_backup::tools::async_io::HyperAccept(connections);
|
2019-09-02 13:16:21 +00:00
|
|
|
|
|
|
|
Ok(ready
|
|
|
|
.and_then(|_| hyper::Server::builder(connections)
|
|
|
|
.serve(rest_server)
|
|
|
|
.with_graceful_shutdown(server::shutdown_future())
|
|
|
|
.map_err(Error::from)
|
|
|
|
)
|
|
|
|
.map_err(|err| eprintln!("server error: {}", err))
|
|
|
|
.map(|_| ())
|
2019-03-18 13:13:44 +00:00
|
|
|
)
|
2019-03-11 08:38:35 +00:00
|
|
|
},
|
2019-09-02 13:16:21 +00:00
|
|
|
);
|
2019-03-11 08:38:35 +00:00
|
|
|
|
2019-04-25 08:38:26 +00:00
|
|
|
daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
|
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
let init_result: Result<(), Error> = try_block!({
|
|
|
|
server::create_task_control_socket()?;
|
|
|
|
server::server_state_init()?;
|
|
|
|
Ok(())
|
|
|
|
});
|
2019-04-09 10:15:06 +00:00
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
if let Err(err) = init_result {
|
|
|
|
bail!("unable to start daemon - {}", err);
|
|
|
|
}
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2019-09-02 13:16:21 +00:00
|
|
|
server.await?;
|
2019-08-29 07:45:34 +00:00
|
|
|
log::info!("done - exit server");
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2019-02-11 13:43:26 +00:00
|
|
|
Ok(())
|
2019-01-28 12:17:03 +00:00
|
|
|
}
|