@@ -42,15 +42,15 @@ pub async fn get_public_storage_dir() -> Result<PathBuf> {
4242}
4343
4444#[ inline( always) ]
45- pub async fn get_allowed_client_dir ( ) -> Result < PathBuf > {
45+ pub async fn get_authorized_client_dir ( ) -> Result < PathBuf > {
4646 let home_dir =
4747 dirs:: home_dir ( ) . ok_or_else ( || anyhow:: anyhow!( "Failed to get home directory" ) ) ?;
4848 let allowed_clients_path = home_dir. join ( ".rdrive" ) . join ( "authorized_keys" ) ;
4949 Ok ( allowed_clients_path)
5050}
5151
5252#[ inline]
53- pub async fn get_server_key_dir ( ) -> Result < PathBuf > {
53+ pub fn get_server_key_dir ( ) -> Result < PathBuf > {
5454 let home_dir =
5555 dirs:: home_dir ( ) . ok_or_else ( || anyhow:: anyhow!( "Failed to get home directory" ) ) ?;
5656 let server_keys_path = home_dir. join ( ".rdrive" ) . join ( "server" ) ;
@@ -72,6 +72,11 @@ pub fn get_catalog_path() -> Result<PathBuf> {
7272 Ok ( path)
7373}
7474
75+ pub fn get_authorized_server_map_path ( ) -> Result < PathBuf > {
76+ let path = get_user_key_dir ( ) ?. join ( "server.map" ) ;
77+ Ok ( path)
78+ }
79+
7580/// Hash a whole file and return the hex string of the hash
7681pub fn file_hasher ( path : & Path ) -> Result < String > {
7782 let file = std:: fs:: File :: open ( path) ?;
@@ -145,28 +150,43 @@ impl MetadataFile {
145150}
146151
147152#[ derive( Deserialize , Serialize , Default ) ]
148- pub struct FileInfo {
153+ pub struct FileHistory {
149154 pub name : String ,
150155 pub last_push : String ,
151156 pub last_pull : String ,
152157}
153158
154159#[ derive( Deserialize , Serialize , Default ) ]
155160pub struct Catalog {
156- pub file_map : HashMap < String , FileInfo > ,
161+ pub file_map : HashMap < String , FileHistory > ,
157162 pub file_index : HashMap < String , Vec < String > > ,
158163}
159164
160165impl Catalog {
161- pub async fn read ( path : & PathBuf ) -> Result < Self > {
166+ async fn read ( path : & PathBuf ) -> Result < Self > {
162167 use postcard:: from_bytes;
163168
164169 let file = tokio:: fs:: read ( path) . await ?;
165170 let catalog = from_bytes ( & file) ?;
166171 Ok ( catalog)
167172 }
168173
169- pub async fn write ( & mut self , path : & PathBuf ) -> Result < ( ) > {
174+ pub async fn read_or_create ( path : & PathBuf ) -> Result < Self > {
175+ let catalog_dir = path
176+ . parent ( )
177+ . ok_or_else ( || anyhow:: anyhow!( "Invalid catalog path" ) ) ?;
178+ tokio:: fs:: create_dir_all ( catalog_dir) . await ?;
179+
180+ // Read existing or new
181+ let catalog = match path. exists ( ) {
182+ true => Self :: read ( path) . await ?,
183+ false => Self :: default ( ) ,
184+ } ;
185+
186+ Ok ( catalog)
187+ }
188+
189+ async fn write ( & mut self , path : & PathBuf ) -> Result < ( ) > {
170190 use postcard:: to_allocvec;
171191
172192 let bytes = to_allocvec ( self ) ?;
@@ -188,7 +208,7 @@ impl Catalog {
188208 . and_modify ( |meta| {
189209 meta. last_push = timestamp. clone ( ) ;
190210 } )
191- . or_insert_with ( || FileInfo {
211+ . or_insert_with ( || FileHistory {
192212 name : file_name. to_string ( ) ,
193213 last_push : timestamp. clone ( ) ,
194214 last_pull : "never" . to_string ( ) ,
@@ -217,6 +237,39 @@ impl Catalog {
217237 }
218238}
219239
240+ #[ derive( Deserialize , Serialize , Default ) ]
241+ pub struct AuthServerMap {
242+ /// Map -> (Host/IP, pubkey_hex)
243+ pub server_map : HashMap < String , String > ,
244+ }
245+
246+ impl AuthServerMap {
247+ pub async fn read_or_create ( path : & PathBuf ) -> Result < Self > {
248+ let server_map = path
249+ . parent ( )
250+ . ok_or_else ( || anyhow:: anyhow!( "Invalid server map path" ) ) ?;
251+ tokio:: fs:: create_dir_all ( server_map) . await ?;
252+
253+ let map = match path. exists ( ) {
254+ true => {
255+ let str = tokio:: fs:: read_to_string ( & path) . await ?;
256+ serde_json:: from_str ( str. as_str ( ) )
257+ . map_err ( |e| anyhow:: anyhow!( "Failed to parse server map JSON: {}" , e) ) ?
258+ }
259+ false => Self :: default ( ) ,
260+ } ;
261+
262+ Ok ( map)
263+ }
264+
265+ pub async fn write ( & mut self , path : & PathBuf ) -> Result < ( ) > {
266+ let json = serde_json:: to_string_pretty ( self )
267+ . map_err ( |e| anyhow:: anyhow!( "Failed to serialize server map to JSON: {}" , e) ) ?;
268+ tokio:: fs:: write ( path, json) . await ?;
269+ Ok ( ( ) )
270+ }
271+ }
272+
220273pub static START_TIME : OnceLock < chrono:: DateTime < Local > > = OnceLock :: new ( ) ;
221274pub static ACTIVE_CONNECTIONS : LazyLock < Arc < AtomicUsize > > =
222275 LazyLock :: new ( || Arc :: new ( AtomicUsize :: new ( 0 ) ) ) ;
@@ -226,6 +279,53 @@ pub static ENABLE_CLIENT_WHITELIST: LazyLock<bool> = LazyLock::new(|| {
226279 . and_then ( |s| s. parse ( ) . ok ( ) )
227280 . unwrap_or ( true ) // default to true
228281} ) ;
282+
283+ pub static SERVER_PUB_KEY_PEM : LazyLock < String > = LazyLock :: new ( || {
284+ let pubkey = get_server_key_dir ( )
285+ . unwrap_or_else ( |e| {
286+ eprintln ! (
287+ "{}" ,
288+ format!( "Failed to get server key directory: {}" , e)
289+ . red( )
290+ . bold( )
291+ ) ;
292+ std:: process:: exit ( 1 ) ;
293+ } )
294+ . join ( "public.pem" ) ;
295+ std:: fs:: read_to_string ( pubkey) . unwrap_or_else ( |e| {
296+ eprintln ! (
297+ "{}" ,
298+ format!( "Failed to read server public key: {}" , e)
299+ . red( )
300+ . bold( )
301+ ) ;
302+ std:: process:: exit ( 1 ) ;
303+ } )
304+ } ) ;
305+
306+ pub static SERVER_PRI_KEY_PEM : LazyLock < String > = LazyLock :: new ( || {
307+ let prikey = get_server_key_dir ( )
308+ . unwrap_or_else ( |e| {
309+ eprintln ! (
310+ "{}" ,
311+ format!( "Failed to get server key directory: {}" , e)
312+ . red( )
313+ . bold( )
314+ ) ;
315+ std:: process:: exit ( 1 ) ;
316+ } )
317+ . join ( "private.pem" ) ;
318+ std:: fs:: read_to_string ( prikey) . unwrap_or_else ( |e| {
319+ eprintln ! (
320+ "{}" ,
321+ format!( "Failed to read server private key: {}" , e)
322+ . red( )
323+ . bold( )
324+ ) ;
325+ std:: process:: exit ( 1 ) ;
326+ } )
327+ } ) ;
328+
229329pub static MAX_CONNECTIONS : LazyLock < usize > = LazyLock :: new ( || {
230330 std:: env:: var ( "MAX_CONNECTIONS" )
231331 . ok ( )
@@ -243,7 +343,7 @@ pub static SHARED_FILE_LOCK: LazyLock<Arc<DashMap<String, Arc<RwLock<()>>>>> =
243343 LazyLock :: new ( || Arc :: new ( DashMap :: new ( ) ) ) ;
244344
245345#[ inline( always) ]
246- pub fn get_file_lock ( file_id : & str ) -> Arc < RwLock < ( ) > > {
346+ pub fn hold_file_lock ( file_id : & str ) -> Arc < RwLock < ( ) > > {
247347 let map = & * SHARED_FILE_LOCK ;
248348 map. entry ( file_id. to_string ( ) )
249349 . or_insert_with ( || Arc :: new ( RwLock :: new ( ( ) ) ) )
0 commit comments