2023-09-06 16:54:38 +00:00
|
|
|
use actix::Actor;
|
2023-09-04 12:11:56 +00:00
|
|
|
use actix_cors::Cors;
|
2023-09-02 07:12:36 +00:00
|
|
|
use actix_identity::config::LogoutBehaviour;
|
|
|
|
use actix_identity::IdentityMiddleware;
|
2023-09-05 11:19:25 +00:00
|
|
|
use actix_multipart::form::tempfile::TempFileConfig;
|
|
|
|
use actix_multipart::form::MultipartFormConfig;
|
2023-09-02 06:07:06 +00:00
|
|
|
use actix_remote_ip::RemoteIPConfig;
|
2023-09-02 07:12:36 +00:00
|
|
|
use actix_session::storage::CookieSessionStore;
|
|
|
|
use actix_session::SessionMiddleware;
|
|
|
|
use actix_web::cookie::{Key, SameSite};
|
2023-09-04 12:11:56 +00:00
|
|
|
use actix_web::http::header;
|
2023-09-02 06:07:06 +00:00
|
|
|
use actix_web::middleware::Logger;
|
2023-09-04 09:25:03 +00:00
|
|
|
use actix_web::web::Data;
|
2023-09-02 07:12:36 +00:00
|
|
|
use actix_web::{web, App, HttpServer};
|
2023-09-04 09:25:03 +00:00
|
|
|
use light_openid::basic_state_manager::BasicStateManager;
|
2023-09-02 07:12:36 +00:00
|
|
|
use std::time::Duration;
|
2023-09-06 16:54:38 +00:00
|
|
|
use virtweb_backend::actors::libvirt_actor::LibVirtActor;
|
2023-10-18 10:27:50 +00:00
|
|
|
use virtweb_backend::actors::vnc_tokens_actor::VNCTokensManager;
|
2023-09-02 07:12:36 +00:00
|
|
|
use virtweb_backend::app_config::AppConfig;
|
2023-09-05 11:19:25 +00:00
|
|
|
use virtweb_backend::constants;
|
2023-09-02 07:12:36 +00:00
|
|
|
use virtweb_backend::constants::{
|
|
|
|
MAX_INACTIVITY_DURATION, MAX_SESSION_DURATION, SESSION_COOKIE_NAME,
|
|
|
|
};
|
2023-10-04 09:18:50 +00:00
|
|
|
use virtweb_backend::controllers::{
|
2024-04-23 17:04:43 +00:00
|
|
|
api_tokens_controller, auth_controller, iso_controller, network_controller,
|
|
|
|
nwfilter_controller, server_controller, static_controller, vm_controller,
|
2023-10-04 09:18:50 +00:00
|
|
|
};
|
2023-09-06 16:54:38 +00:00
|
|
|
use virtweb_backend::libvirt_client::LibVirtClient;
|
2023-09-02 17:15:11 +00:00
|
|
|
use virtweb_backend::middlewares::auth_middleware::AuthChecker;
|
2024-01-10 18:29:24 +00:00
|
|
|
use virtweb_backend::nat::nat_conf_mode;
|
2023-09-05 11:19:25 +00:00
|
|
|
use virtweb_backend::utils::files_utils;
|
2023-09-02 06:07:06 +00:00
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
2024-01-10 18:29:24 +00:00
|
|
|
// Run in NAT configuration mode, if requested
|
|
|
|
if std::env::var(constants::NAT_MODE_ENV_VAR_NAME).is_ok() {
|
|
|
|
nat_conf_mode::sub_main().await.unwrap();
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-12-19 22:23:19 +00:00
|
|
|
// Load additional config from file, if requested
|
|
|
|
AppConfig::parse_env_file().unwrap();
|
|
|
|
|
2023-09-05 11:19:25 +00:00
|
|
|
log::debug!("Create required directory, if missing");
|
2023-10-26 09:43:05 +00:00
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().iso_storage_path()).unwrap();
|
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().vnc_sockets_path()).unwrap();
|
2023-12-06 19:32:03 +00:00
|
|
|
files_utils::set_file_permission(AppConfig::get().vnc_sockets_path(), 0o777).unwrap();
|
2023-10-26 09:43:05 +00:00
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().disks_storage_path()).unwrap();
|
2024-01-08 20:03:39 +00:00
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().nat_path()).unwrap();
|
2023-12-23 17:12:46 +00:00
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().definitions_path()).unwrap();
|
2024-04-23 17:04:43 +00:00
|
|
|
files_utils::create_directory_if_missing(AppConfig::get().api_tokens_path()).unwrap();
|
2023-09-05 11:19:25 +00:00
|
|
|
|
2023-09-06 16:54:38 +00:00
|
|
|
let conn = Data::new(LibVirtClient(
|
|
|
|
LibVirtActor::connect()
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to hypervisor!")
|
|
|
|
.start(),
|
|
|
|
));
|
|
|
|
|
2023-10-18 10:27:50 +00:00
|
|
|
let vnc_tokens = Data::new(VNCTokensManager::start());
|
|
|
|
|
2023-09-02 06:07:06 +00:00
|
|
|
log::info!("Start to listen on {}", AppConfig::get().listen_address);
|
|
|
|
|
2023-09-04 09:25:03 +00:00
|
|
|
let state_manager = Data::new(BasicStateManager::new());
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
2023-09-02 07:12:36 +00:00
|
|
|
let session_mw = SessionMiddleware::builder(
|
|
|
|
CookieSessionStore::default(),
|
|
|
|
Key::from(AppConfig::get().secret().as_bytes()),
|
|
|
|
)
|
|
|
|
.cookie_name(SESSION_COOKIE_NAME.to_string())
|
|
|
|
.cookie_secure(AppConfig::get().cookie_secure)
|
|
|
|
.cookie_same_site(SameSite::Strict)
|
2023-09-04 13:12:00 +00:00
|
|
|
.cookie_domain(AppConfig::get().cookie_domain())
|
|
|
|
.cookie_http_only(true)
|
2023-09-02 07:12:36 +00:00
|
|
|
.build();
|
|
|
|
|
|
|
|
let identity_middleware = IdentityMiddleware::builder()
|
|
|
|
.logout_behaviour(LogoutBehaviour::PurgeSession)
|
|
|
|
.visit_deadline(Some(Duration::from_secs(MAX_INACTIVITY_DURATION)))
|
|
|
|
.login_deadline(Some(Duration::from_secs(MAX_SESSION_DURATION)))
|
|
|
|
.build();
|
|
|
|
|
2023-12-12 14:45:56 +00:00
|
|
|
let mut cors = Cors::default()
|
2023-09-04 12:11:56 +00:00
|
|
|
.allowed_origin(&AppConfig::get().website_origin)
|
2024-04-23 17:04:43 +00:00
|
|
|
.allowed_methods(vec!["GET", "POST", "DELETE", "PUT", "PATCH"])
|
2023-09-04 12:11:56 +00:00
|
|
|
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
|
|
|
|
.allowed_header(header::CONTENT_TYPE)
|
|
|
|
.supports_credentials()
|
|
|
|
.max_age(3600);
|
|
|
|
|
2023-12-12 14:45:56 +00:00
|
|
|
for additional_origin in &AppConfig::get().additional_origins {
|
|
|
|
cors = cors.allowed_origin(additional_origin);
|
|
|
|
}
|
|
|
|
|
2023-09-02 06:07:06 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Logger::default())
|
2023-09-02 17:15:11 +00:00
|
|
|
.wrap(AuthChecker)
|
2023-09-02 07:12:36 +00:00
|
|
|
.wrap(identity_middleware)
|
|
|
|
.wrap(session_mw)
|
2023-09-04 13:12:00 +00:00
|
|
|
.wrap(cors)
|
2023-09-04 09:25:03 +00:00
|
|
|
.app_data(state_manager.clone())
|
2023-10-18 10:27:50 +00:00
|
|
|
.app_data(vnc_tokens.clone())
|
2023-09-04 09:25:03 +00:00
|
|
|
.app_data(Data::new(RemoteIPConfig {
|
2023-09-02 07:12:36 +00:00
|
|
|
proxy: AppConfig::get().proxy_ip.clone(),
|
2023-09-02 06:07:06 +00:00
|
|
|
}))
|
2023-09-06 16:54:38 +00:00
|
|
|
.app_data(conn.clone())
|
2023-09-05 11:19:25 +00:00
|
|
|
// Uploaded files
|
|
|
|
.app_data(MultipartFormConfig::default().total_limit(constants::ISO_MAX_SIZE))
|
|
|
|
.app_data(TempFileConfig::default().directory(&AppConfig::get().temp_dir))
|
2023-09-02 16:44:16 +00:00
|
|
|
// Server controller
|
2023-09-02 08:28:08 +00:00
|
|
|
.route(
|
|
|
|
"/api/server/static_config",
|
|
|
|
web::get().to(server_controller::static_config),
|
|
|
|
)
|
2023-09-06 16:54:38 +00:00
|
|
|
.route(
|
|
|
|
"/api/server/info",
|
|
|
|
web::get().to(server_controller::server_info),
|
|
|
|
)
|
2024-01-11 18:02:47 +00:00
|
|
|
.route(
|
|
|
|
"/api/server/network_hook_status",
|
|
|
|
web::get().to(server_controller::network_hook_status),
|
|
|
|
)
|
2023-12-07 16:09:33 +00:00
|
|
|
.route(
|
|
|
|
"/api/server/number_vcpus",
|
|
|
|
web::get().to(server_controller::number_vcpus),
|
|
|
|
)
|
2023-12-06 14:30:30 +00:00
|
|
|
.route(
|
|
|
|
"/api/server/networks",
|
|
|
|
web::get().to(server_controller::networks_list),
|
|
|
|
)
|
2023-09-02 16:44:16 +00:00
|
|
|
// Auth controller
|
|
|
|
.route(
|
|
|
|
"/api/auth/local",
|
|
|
|
web::post().to(auth_controller::local_auth),
|
|
|
|
)
|
2023-09-04 09:25:03 +00:00
|
|
|
.route(
|
|
|
|
"/api/auth/start_oidc",
|
|
|
|
web::get().to(auth_controller::start_oidc),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/auth/finish_oidc",
|
|
|
|
web::post().to(auth_controller::finish_oidc),
|
|
|
|
)
|
2023-09-02 17:15:11 +00:00
|
|
|
.route(
|
|
|
|
"/api/auth/user",
|
|
|
|
web::get().to(auth_controller::current_user),
|
|
|
|
)
|
2023-09-04 09:25:03 +00:00
|
|
|
.route(
|
|
|
|
"/api/auth/sign_out",
|
|
|
|
web::get().to(auth_controller::sign_out),
|
|
|
|
)
|
2023-09-05 11:19:25 +00:00
|
|
|
// ISO controller
|
|
|
|
.route(
|
|
|
|
"/api/iso/upload",
|
|
|
|
web::post().to(iso_controller::upload_file),
|
|
|
|
)
|
2023-09-05 14:12:20 +00:00
|
|
|
.route(
|
|
|
|
"/api/iso/upload_from_url",
|
|
|
|
web::post().to(iso_controller::upload_from_url),
|
|
|
|
)
|
2023-09-06 12:21:26 +00:00
|
|
|
.route("/api/iso/list", web::get().to(iso_controller::get_list))
|
2023-09-06 14:57:38 +00:00
|
|
|
.route(
|
|
|
|
"/api/iso/{filename}",
|
|
|
|
web::get().to(iso_controller::download_file),
|
|
|
|
)
|
2023-09-06 12:55:41 +00:00
|
|
|
.route(
|
|
|
|
"/api/iso/{filename}",
|
|
|
|
web::delete().to(iso_controller::delete_file),
|
|
|
|
)
|
2023-10-04 09:18:50 +00:00
|
|
|
// Virtual machines controller
|
|
|
|
.route("/api/vm/create", web::post().to(vm_controller::create))
|
2023-10-09 17:16:33 +00:00
|
|
|
.route("/api/vm/list", web::get().to(vm_controller::list_all))
|
2023-10-04 17:03:20 +00:00
|
|
|
.route("/api/vm/{uid}", web::get().to(vm_controller::get_single))
|
2023-12-08 17:14:01 +00:00
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/src",
|
|
|
|
web::get().to(vm_controller::get_single_src_def),
|
|
|
|
)
|
2023-10-28 15:30:27 +00:00
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/autostart",
|
|
|
|
web::get().to(vm_controller::get_autostart),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/autostart",
|
|
|
|
web::put().to(vm_controller::set_autostart),
|
|
|
|
)
|
2023-10-13 14:44:56 +00:00
|
|
|
.route("/api/vm/{uid}", web::put().to(vm_controller::update))
|
|
|
|
.route("/api/vm/{uid}", web::delete().to(vm_controller::delete))
|
2023-10-10 10:35:43 +00:00
|
|
|
.route("/api/vm/{uid}/start", web::get().to(vm_controller::start))
|
2023-10-11 16:23:45 +00:00
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/shutdown",
|
|
|
|
web::get().to(vm_controller::shutdown),
|
|
|
|
)
|
|
|
|
.route("/api/vm/{uid}/kill", web::get().to(vm_controller::kill))
|
|
|
|
.route("/api/vm/{uid}/reset", web::get().to(vm_controller::reset))
|
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/suspend",
|
|
|
|
web::get().to(vm_controller::suspend),
|
|
|
|
)
|
|
|
|
.route("/api/vm/{uid}/resume", web::get().to(vm_controller::resume))
|
2023-10-13 13:30:32 +00:00
|
|
|
.route("/api/vm/{uid}/state", web::get().to(vm_controller::state))
|
2023-10-13 13:27:01 +00:00
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/screenshot",
|
|
|
|
web::get().to(vm_controller::screenshot),
|
|
|
|
)
|
2023-10-18 10:27:50 +00:00
|
|
|
.route(
|
|
|
|
"/api/vm/{uid}/vnc_token",
|
|
|
|
web::get().to(vm_controller::vnc_token),
|
|
|
|
)
|
2023-10-18 16:00:25 +00:00
|
|
|
.route("/api/vnc", web::get().to(vm_controller::vnc))
|
2023-10-31 08:26:42 +00:00
|
|
|
// Network controller
|
|
|
|
.route(
|
|
|
|
"/api/network/create",
|
|
|
|
web::post().to(network_controller::create),
|
|
|
|
)
|
2023-10-31 09:51:13 +00:00
|
|
|
.route("/api/network/list", web::get().to(network_controller::list))
|
2023-10-31 10:08:05 +00:00
|
|
|
.route(
|
|
|
|
"/api/network/{uid}",
|
|
|
|
web::get().to(network_controller::get_single),
|
|
|
|
)
|
2023-12-11 17:41:59 +00:00
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/src",
|
|
|
|
web::get().to(network_controller::single_src),
|
|
|
|
)
|
2023-10-31 11:03:37 +00:00
|
|
|
.route(
|
|
|
|
"/api/network/{uid}",
|
|
|
|
web::put().to(network_controller::update),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/network/{uid}",
|
|
|
|
web::delete().to(network_controller::delete),
|
|
|
|
)
|
2023-12-04 19:16:32 +00:00
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/autostart",
|
|
|
|
web::get().to(network_controller::get_autostart),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/autostart",
|
|
|
|
web::put().to(network_controller::set_autostart),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/status",
|
|
|
|
web::get().to(network_controller::status),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/start",
|
|
|
|
web::get().to(network_controller::start),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/network/{uid}/stop",
|
|
|
|
web::get().to(network_controller::stop),
|
|
|
|
)
|
2023-12-28 14:12:38 +00:00
|
|
|
// Network filters controller
|
2023-12-29 19:11:21 +00:00
|
|
|
.route(
|
|
|
|
"/api/nwfilter/create",
|
|
|
|
web::post().to(nwfilter_controller::create),
|
|
|
|
)
|
2023-12-28 14:12:38 +00:00
|
|
|
.route(
|
|
|
|
"/api/nwfilter/list",
|
|
|
|
web::get().to(nwfilter_controller::list),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/nwfilter/{uid}",
|
|
|
|
web::get().to(nwfilter_controller::get_single),
|
|
|
|
)
|
2024-01-02 14:52:04 +00:00
|
|
|
.route(
|
|
|
|
"/api/nwfilter/{uid}/src",
|
|
|
|
web::get().to(nwfilter_controller::single_src),
|
|
|
|
)
|
2023-12-29 19:11:21 +00:00
|
|
|
.route(
|
|
|
|
"/api/nwfilter/{uid}",
|
|
|
|
web::put().to(nwfilter_controller::update),
|
|
|
|
)
|
2024-01-02 14:45:31 +00:00
|
|
|
.route(
|
|
|
|
"/api/nwfilter/{uid}",
|
|
|
|
web::delete().to(nwfilter_controller::delete),
|
|
|
|
)
|
2024-04-23 17:04:43 +00:00
|
|
|
// API tokens controller
|
|
|
|
.route(
|
|
|
|
"/api/token/create",
|
|
|
|
web::post().to(api_tokens_controller::create),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/token/list",
|
|
|
|
web::get().to(api_tokens_controller::list),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/token/{uid}",
|
|
|
|
web::get().to(api_tokens_controller::get_single),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/token/{uid}",
|
|
|
|
web::patch().to(api_tokens_controller::update),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/api/token/{uid}",
|
|
|
|
web::delete().to(api_tokens_controller::delete),
|
|
|
|
)
|
2023-12-12 14:45:56 +00:00
|
|
|
// Static assets
|
|
|
|
.route("/", web::get().to(static_controller::root_index))
|
|
|
|
.route(
|
|
|
|
"/{tail:.*}",
|
|
|
|
web::get().to(static_controller::serve_static_content),
|
|
|
|
)
|
2023-09-02 06:07:06 +00:00
|
|
|
})
|
2023-09-02 07:12:36 +00:00
|
|
|
.bind(&AppConfig::get().listen_address)?
|
|
|
|
.run()
|
|
|
|
.await
|
2023-09-01 08:59:53 +00:00
|
|
|
}
|