@@ -51,13 +51,12 @@ func main() {
5151
5252 switch choice {
5353 case "1" :
54- level := "medium" // Example encryption level
5554 filename , err := generalAskUser ("Enter the filename to encrypt: " )
5655 if err != nil {
5756 fmt .Println ("Error:" , err )
5857 continue
5958 }
60- if err := encryptFile (filename , level ); err != nil {
59+ if err := encryptFile (filename ); err != nil {
6160 fmt .Println ("Error:" , err )
6261 }
6362
@@ -297,21 +296,6 @@ func saveCID(cid string, level string) error {
297296}
298297
299298
300- func ipfsUpload (filePath string , level string ) error {
301- cid , err := ipfs_link .AddFileToIPFS (filePath )
302- if err != nil {
303- return fmt .Errorf ("\033 [1;31merror uploading file to IPFS: %w\033 [0m" , err )
304- }
305-
306- if err := saveCID (cid , level ); err != nil {
307- return fmt .Errorf ("\033 [1;31merror saving CID and level: %w\033 [0m" , err )
308- }
309-
310- fmt .Printf ("\033 [1;32mFile uploaded to IPFS successfully, CID:\n %s\033 [0m\n " , cid )
311- fmt .Println ("\033 [1;33mDeleted standard encrypted format.\033 [0m" )
312- exec .Command ("rm" , filePath )
313- return nil
314- }
315299
316300func decryptFileOption () error {
317301 useLog := askUserYN ("\033 [1;36mDo you want to use the CID log?\033 [0m" )
@@ -364,14 +348,43 @@ func askUserYN(question string) bool {
364348
365349
366350
367- func getEncryptionLevelForCID (){
368- //used only for the CID log
369- //read the encryption level associated with that cid
370- //if none "Sorry, the level could not be found in the log, which one is it for this file? select manually what it should be
371- // > 1 Easy
372- // > 2 Medium
373- // > 3 Hard
351+ func getEncryptionLevelForCID (cid string ) (string , error ) {
352+ logFile := os .Getenv ("HOME" ) + "/.config/DangerousNet/log_CID.log"
353+ data , err := os .ReadFile (logFile )
354+ if err != nil {
355+ return "" , fmt .Errorf ("error reading CID log: %w" , err )
356+ }
357+
358+ lines := strings .Split (string (data ), "\n " )
359+ for _ , line := range lines {
360+ parts := strings .Split (line , "," )
361+ if len (parts ) == 2 && parts [0 ] == cid {
362+ return parts [1 ], nil
363+ }
364+ }
365+
366+ // CID not found in log, ask the user to select the level manually
367+ fmt .Println ("Sorry, the level could not be found in the log. Which one is it for this file?" )
368+ fmt .Println (" > 1 Easy" )
369+ fmt .Println (" > 2 Medium" )
370+ fmt .Println (" > 3 Hard" )
371+ reader := bufio .NewReader (os .Stdin )
372+ choice , err := reader .ReadString ('\n' )
373+ if err != nil {
374+ return "" , fmt .Errorf ("error reading user input: %w" , err )
375+ }
374376
377+ choice = strings .TrimSpace (choice )
378+ switch choice {
379+ case "1" :
380+ return "easy" , nil
381+ case "2" :
382+ return "medium" , nil
383+ case "3" :
384+ return "hard" , nil
385+ default :
386+ return "" , fmt .Errorf ("invalid selection" )
387+ }
375388}
376389
377390func decryptFileFromLog () error {
@@ -385,11 +398,14 @@ func decryptFileFromLog() error {
385398 continue
386399 }
387400
388-
389- level := getEncryptionLevelForCID (cid ) // Implement this based on your logging mechanism
401+ level , err := getEncryptionLevelForCID (cid ) // Correctly handle both returned values
402+ if err != nil {
403+ fmt .Printf ("\033 [1;31mError retrieving encryption level for CID %s: %v\033 [0m\n " , cid , err )
404+ continue
405+ }
390406
391407 fmt .Printf ("\033 [1;34mDecrypting file for CID: %s\033 [0m\n " , cid )
392- err : = decryptSingleFile (cid , level ) // Use the obtained level here
408+ err = decryptSingleFile (cid , level )
393409 if err != nil {
394410 fmt .Printf ("\033 [1;31mError decrypting file for CID %s: %v\033 [0m\n " , cid , err )
395411 continue
@@ -480,6 +496,30 @@ func generateThreeWordPhrase() (string, error) {
480496 return fmt .Sprintf ("%s %s %s" , phrase [0 ], phrase [1 ], phrase [2 ]), nil
481497}
482498
499+
500+ func ipfsUpload (filePath string , level string ) error {
501+ cid , err := ipfs_link .AddFileToIPFS (filePath )
502+ if err != nil {
503+ return fmt .Errorf ("\033 [1;31merror uploading file to IPFS: %w\033 [0m" , err )
504+ }
505+
506+ if err := saveCID (cid , level ); err != nil {
507+ return fmt .Errorf ("\033 [1;31merror saving CID and level: %w\033 [0m" , err )
508+ }
509+
510+ fmt .Printf ("\033 [1;32mFile uploaded to IPFS successfully, CID:\n %s\033 [0m\n " , cid )
511+ fmt .Println ("\033 [1;33mDeleted standard encrypted format.\033 [0m" )
512+ exec .Command ("rm" , filePath )
513+ return nil
514+ }
515+
516+
517+ func ipfsDownload (){
518+ //Split, include logic to download a file from IPFS
519+ //Enter CID, etc...
520+
521+ }
522+
483523func decryptFile (filename string ) error {
484524 cid , err := generalAskUser ("Enter the CID for the file to decrypt: " )
485525 if err != nil {
@@ -551,9 +591,52 @@ func decryptFile(filename string) error {
551591 return nil
552592}
553593
594+ func setupConfig () error {
595+ configDir := os .Getenv ("HOME" ) + "/.config/DangerousNet"
596+ configFile := configDir + "/config"
597+
598+ // Create the DangerousNet directory if it doesn't exist
599+ err := os .MkdirAll (configDir , 0755 )
600+ if err != nil {
601+ return fmt .Errorf ("error creating config directory: %w" , err )
602+ }
603+
604+ // Check if the config file exists
605+ if _ , err := os .Stat (configFile ); os .IsNotExist (err ) {
606+ // Create a default config file
607+ defaultConfig := []byte ("encryptionLevel=medium\n " )
608+ err = os .WriteFile (configFile , defaultConfig , 0644 )
609+ if err != nil {
610+ return fmt .Errorf ("error creating default config file: %w" , err )
611+ }
612+ }
613+
614+ return nil
615+ }
616+
617+ func readConfig () (string , error ) {
618+ configFile := os .Getenv ("HOME" ) + "/.config/DangerousNet/config"
619+ data , err := os .ReadFile (configFile )
620+ if err != nil {
621+ return "" , fmt .Errorf ("error reading config file: %w" , err )
622+ }
554623
624+ lines := strings .Split (string (data ), "\n " )
625+ for _ , line := range lines {
626+ if strings .HasPrefix (line , "encryptionLevel=" ) {
627+ return strings .TrimPrefix (line , "encryptionLevel=" ), nil
628+ }
629+ }
555630
556- func encryptFile (filename string , level string ) error {
631+ return "medium" , nil // Default level if not specified in the config
632+ }
633+
634+
635+ func encryptFile (filename string ) error {
636+ level , erro := readConfig ()
637+ if erro != nil {
638+ return fmt .Errorf ("error reading encryption level from config: %w" , erro )
639+ }
557640 var passphrase string
558641 var err error
559642
0 commit comments