コード例 #1
0
ファイル: server.go プロジェクト: CPSSD/MDFS
// checks request code and calls corresponding function
func (st StorageNode) handleCode(uuid uint64, code uint8, conn net.Conn, r *bufio.Reader, w *bufio.Writer) {

	switch code {
	case 1: // client is requesting a file
		hash := utils.ReadHashAsString(r)
		fmt.Println("Hash received: " + hash)

		// check if file exists
		var sendcode uint8
		fp := st.getPath() + "files/" + hash
		if _, err := os.Stat(fp); err == nil {
			fmt.Println("File with hash " + hash + " exists")
			sendcode = 3                 // file available code
			err := w.WriteByte(sendcode) // let client know
			if err != nil {
				panic(err)
			}
			w.Flush()

			// send the file
			fmt.Println("Sending file " + hash)
			utils.SendFile(conn, w, fp)
		} else {
			fmt.Println("File with hash " + hash + " does not exist")
			sendcode = 4
			err := w.WriteByte(sendcode) // let client know
			if err != nil {
				panic(err)
			}
			w.Flush()
		}

	case 2: // receive file from client
		fmt.Println("Receiving file")

		hash := utils.ReadHashAsString(r)
		output := st.getPath() + "files/" + hash
		utils.ReceiveFile(conn, r, output)

		fmt.Println("File received; md5 checksum of file is: " + hash)
	}
	fmt.Println()
	conn.Close()
}
コード例 #2
0
ファイル: mdfs-client.go プロジェクト: CPSSD/MDFS
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
}