Ejemplo n.º 1
0
func request(r *bufio.Reader, w *bufio.Writer, currentDir string, args []string, thisUser *utils.User) (err error) {

	// should have args format:
	// request [remote filename] [local filename]
	// currently:
	// request [remote filename]
	// Local filename can be a relative or absolute path

	if len(args) < 2 {
		return err
	}

	// START SENDCODE BLOCK
	err = w.WriteByte(5)
	w.Flush()
	if err != nil {
		panic(err)
	}
	// END SENDCODE BLOCK

	// Send current dir
	w.WriteString(currentDir + "\n")
	w.Flush()

	// Format the file to send
	w.WriteString(args[1] + "\n")
	w.Flush()

	fmt.Println("Requesting: " + path.Join(currentDir, args[1]))

	success, _ := r.ReadByte()
	if success == 3 {

		fmt.Println("You do not have permission to request this file")
		return nil

	} else if success != 4 {

		fmt.Println("Invalid file request")
		return nil
	}

	protected := false
	enc, _ := r.ReadByte()
	if enc == 1 {
		protected = true
	}

	success, _ = r.ReadByte()
	if success != 2 {

		fmt.Println("No stnodes for your file")
		return nil
	}

	hash, _ := r.ReadString('\n')
	protocol, _ := r.ReadString('\n')
	nAddress, _ := r.ReadString('\n')

	hash = strings.TrimSpace(hash)
	protocol = strings.TrimSpace(protocol)
	nAddress = strings.TrimSpace(nAddress)

	conn, err := net.Dial(protocol, nAddress)
	if err != nil {
		fmt.Println("Error connecting to stnode")
	}

	ws := bufio.NewWriter(conn)
	rs := bufio.NewReader(conn)

	ws.WriteByte(1)

	bytehash, err := hex.DecodeString(hash)
	if err != nil {
		return err
	}

	err = utils.WriteHash(ws, bytehash)
	if err != nil {
		return err
	}

	success, _ = rs.ReadByte()
	if success != 3 {

		fmt.Println("File cannot be found on stnode")
		return err
	}

	output := utils.GetUserHome() + "/.mdfs/client/" + thisUser.Uname + "/files/" + path.Base(args[1])

	if protected {

		encrypFile := os.TempDir() + "/" + path.Base(args[1])
		utils.ReceiveFile(conn, rs, encrypFile)

		err = utils.DecryptFile(encrypFile, output, *thisUser)
		if err != nil {

			fmt.Println("Your key does not match the lock for this file")
			return nil
		}
		fmt.Println("Protected file successfully unlocked")

	} else {

		utils.ReceiveFile(conn, rs, output)

	}

	fmt.Println("Successfully received file")
	fmt.Println("File stored at: " + output)

	return
}
Ejemplo n.º 2
0
func CheckCrypto() (success bool, err error) {

	usrHome := utils.GetUserHome()

	path := "/.testing_files/"

	keys := usrHome + path + ".private_key_mdfs"
	source1 := usrHome + path + "test.txt"
	encryp1 := usrHome + path + "test.enc"
	result1 := usrHome + path + "result.txt"

	source2 := usrHome + path + "test.jpg"
	encryp2 := usrHome + path + "test.jpg.enc"
	result2 := usrHome + path + "result.jpg"

	utils.GenUserKeys(keys)

	var prk *rsa.PrivateKey
	var puk *rsa.PublicKey

	err = utils.FileToStruct(keys, &prk)
	if err != nil {
		return false, err
	}
	puk = &prk.PublicKey

	user1 := utils.User{Uuid: 1, Pubkey: puk, Privkey: prk}
	user2 := utils.User{Uuid: 2, Pubkey: puk, Privkey: prk}

	//test two files for encryption and then decryption

	// Test 1st file
	users := []utils.User{user1, user2}

	err = utils.EncryptFile(source1, encryp1, users...)
	if err != nil {
		return false, err
	}
	err = utils.DecryptFile(encryp1, result1, user1)
	if err != nil {
		return false, err
	}
	err = utils.DecryptFile(encryp1, result1, user2)
	if err != nil {
		return false, err
	}

	test1 := compareFiles(source1, result1)

	// Test 2nd file
	err = utils.EncryptFile(source2, encryp2, user1)
	if err != nil {
		return false, err
	}
	err = utils.DecryptFile(encryp2, result2, user1)
	if err != nil {
		return false, err
	}

	test2 := compareFiles(source2, result2)

	return test1 && test2, err
}