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
#![feature(try_trait)] extern crate reqwest; use std::ffi::{CStr, CString}; use std::os::raw::{c_char, c_int}; use std::path::Path; use serde_json::json; use sled::Db; use crate::service::auth_service::AuthServiceImpl; use crate::client::ClientImpl; use crate::service::clock_service::ClockImpl; use crate::model::state::Config; use crate::repo::account_repo::{AccountRepo, AccountRepoImpl}; use crate::repo::db_provider::{DbProvider, DiskBackedDB}; use crate::repo::file_metadata_repo::{FileMetadataRepo, FileMetadataRepoImpl}; use crate::repo::file_repo::{FileRepo, FileRepoImpl}; use crate::service::account_service::{AccountService, AccountServiceImpl}; use crate::service::crypto_service::{AesImpl, RsaImpl}; use crate::service::file_encryption_service::FileEncryptionServiceImpl; use crate::service::file_metadata_service::{FileMetadataService, FileMetadataServiceImpl}; use crate::service::file_service::{FileService, FileServiceImpl}; pub mod client; pub mod error_enum; pub mod model; pub mod repo; pub mod service; static API_LOC: &str = "http://lockbook.app:8000"; static BUCKET_LOC: &str = "https://locked.nyc3.digitaloceanspaces.com"; static DB_NAME: &str = "lockbook.sled"; type DefaultCrypto = RsaImpl; type DefaultSymmetric = AesImpl; type DefaultDbProvider = DiskBackedDB; type DefaultClient = ClientImpl; type DefaultAccountRepo = AccountRepoImpl; type DefaultClock = ClockImpl; type DefaultAuthService = AuthServiceImpl<DefaultClock, DefaultCrypto>; type DefaultAccountService = AccountServiceImpl<DefaultCrypto, DefaultAccountRepo, DefaultClient, DefaultAuthService>; type DefaultFileMetadataRepo = FileMetadataRepoImpl; type DefaultFileRepo = FileRepoImpl; type DefaultFileEncryptionService = FileEncryptionServiceImpl<DefaultCrypto, DefaultSymmetric>; type DefaultFileMetadataService = FileMetadataServiceImpl< DefaultFileMetadataRepo, DefaultFileRepo, DefaultAccountRepo, DefaultClient, DefaultFileEncryptionService, >; type DefaultFileService = FileServiceImpl< DefaultFileMetadataRepo, DefaultFileRepo, DefaultAccountRepo, DefaultFileEncryptionService, >; static FAILURE_DB: &str = "FAILURE<DB_ERROR>"; static FAILURE_ACCOUNT: &str = "FAILURE<ACCOUNT_MISSING>"; static FAILURE_META_CREATE: &str = "FAILURE<META_CREATE>"; static FAILURE_FILE_GET: &str = "FAILURE<FILE_GET>"; #[allow(dead_code)] fn info(msg: String) { println!("ℹ️ {}", msg) } #[allow(dead_code)] fn debug(msg: String) { println!("🚧 {}", msg) } #[allow(dead_code)] fn warn(msg: String) { println!("⚠️ {}", msg) } #[allow(dead_code)] fn error(msg: String) { eprintln!("🛑 {}", msg) } #[allow(dead_code)] fn fatal(msg: String) { eprintln!("🆘 {}", msg) } unsafe fn string_from_ptr(c_path: *const c_char) -> String { CStr::from_ptr(c_path) .to_str() .expect("Could not C String -> Rust String") .to_string() } unsafe fn connect_db(c_path: *const c_char) -> Option<Db> { let path = string_from_ptr(c_path); let config = Config { writeable_path: path, }; match DefaultDbProvider::connect_to_db(&config) { Ok(db) => Some(db), Err(err) => { error(format!("DB connection failed! Error: {:?}", err)); None } } } #[no_mangle] pub unsafe extern "C" fn is_db_present(c_path: *const c_char) -> c_int { let path = string_from_ptr(c_path); let db_path = path + "/" + DB_NAME; debug(format!("Checking if {:?} exists", db_path)); if Path::new(db_path.as_str()).exists() { debug(format!("DB Exists!")); 1 } else { error(format!("DB Does not exist!")); 0 } } #[no_mangle] pub unsafe extern "C" fn release_pointer(s: *mut c_char) { if s.is_null() { return; } CString::from_raw(s); } #[no_mangle] pub unsafe extern "C" fn get_account(c_path: *const c_char) -> *mut c_char { let db = match connect_db(c_path) { None => return CString::new(FAILURE_DB).unwrap().into_raw(), Some(db) => db, }; match DefaultAccountRepo::get_account(&db) { Ok(account) => CString::new(account.username).unwrap().into_raw(), Err(err) => { error(format!("Account retrieval failed with error: {:?}", err)); CString::new(FAILURE_ACCOUNT).unwrap().into_raw() } } } #[no_mangle] pub unsafe extern "C" fn create_account(c_path: *const c_char, c_username: *const c_char) -> c_int { let db = match connect_db(c_path) { None => return 0, Some(db) => db, }; let username = string_from_ptr(c_username); match DefaultAccountService::create_account(&db, username.to_string()) { Ok(_) => 1, Err(err) => { error(format!("Account creation failed with error: {:?}", err)); 0 } } } #[no_mangle] pub unsafe extern "C" fn sync_files(c_path: *const c_char) -> *mut c_char { let db = match connect_db(c_path) { None => return CString::new(FAILURE_DB).unwrap().into_raw(), Some(db) => db, }; match DefaultFileMetadataService::sync(&db) { Ok(metas) => CString::new(json!(&metas).to_string()).unwrap().into_raw(), Err(err) => { error(format!("Update metadata failed with error: {:?}", err)); CString::new(json!([]).to_string()).unwrap().into_raw() } } } #[no_mangle] pub unsafe extern "C" fn create_file( c_path: *const c_char, c_file_name: *const c_char, c_file_path: *const c_char, ) -> *mut c_char { let db = match connect_db(c_path) { None => return CString::new(FAILURE_DB).unwrap().into_raw(), Some(db) => db, }; let file_name = string_from_ptr(c_file_name); let file_path = string_from_ptr(c_file_path); match DefaultFileMetadataService::create(&db, file_name, file_path) { Ok(meta) => CString::new(json!(&meta).to_string()).unwrap().into_raw(), Err(err) => { error(format!("Failed to create file metadata! Error: {:?}", err)); CString::new(FAILURE_META_CREATE).unwrap().into_raw() } } } #[no_mangle] pub unsafe extern "C" fn get_file(c_path: *const c_char, c_file_id: *const c_char) -> *mut c_char { let db = match connect_db(c_path) { None => return CString::new(FAILURE_DB).unwrap().into_raw(), Some(db) => db, }; let file_id = string_from_ptr(c_file_id); match DefaultFileService::get(&db, file_id) { Ok(file) => CString::new(json!(&file).to_string()).unwrap().into_raw(), Err(err) => { error(format!("Failed to get file! Error: {:?}", err)); CString::new(FAILURE_FILE_GET).unwrap().into_raw() } } } #[no_mangle] pub unsafe extern "C" fn update_file( c_path: *const c_char, c_file_id: *const c_char, c_file_content: *const c_char, ) -> c_int { let db = match connect_db(c_path) { None => return 0, Some(db) => db, }; let file_id = string_from_ptr(c_file_id); let file_content = string_from_ptr(c_file_content); match DefaultFileService::update(&db, file_id, file_content) { Ok(_) => 1, Err(err) => { error(format!("Failed to update file! Error: {:?}", err)); 0 } } } #[no_mangle] pub unsafe extern "C" fn purge_files(c_path: *const c_char) -> c_int { let db = match connect_db(c_path) { None => return 0, Some(db) => db, }; match DefaultFileMetadataRepo::get_all(&db) { Ok(metas) => metas.into_iter().for_each(|meta| { DefaultFileMetadataRepo::delete(&db, &meta.id).unwrap(); DefaultFileRepo::delete(&db, &meta.id).unwrap(); () }), Err(err) => error(format!("Failed to delete file! Error: {:?}", err)), } 1 } #[no_mangle] pub unsafe extern "C" fn import_account( c_path: *const c_char, c_username: *const c_char, c_key: *const c_char, ) -> c_int { let db = match connect_db(c_path) { None => return 0, Some(db) => db, }; let username = string_from_ptr(c_username); let key_string = string_from_ptr(c_key); match DefaultAccountService::import_account(&db, username, key_string) { Ok(acc) => { debug(format!("Loaded account: {:?}", acc)); 1 } Err(err) => { error(format!("Failed to delete file! Error: {:?}", err)); 0 } } }