Example #1
0
File: server.go Project: 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()
}
Example #2
0
File: server.go Project: CPSSD/MDFS
func send(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDService) (err error) {

	// get current dir
	currentDir, _ := r.ReadString('\n')
	currentDir = strings.TrimSpace(currentDir)

	// receive filename from client
	filename, _ := r.ReadString('\n')
	filename = strings.TrimSpace(filename)

	// clean the path
	if !path.IsAbs(filename) {
		filename = path.Join(currentDir, filename)
	}

	fmt.Printf("\tUser %d called send to \"%s\"\n", uuid, currentDir+filename)

	// check if the filename exists already
	_, err = os.Stat(md.getPath() + "files" + filename)

	hidden := utils.IsHidden(filename)
	if hidden {
		fmt.Printf("\tCall to hidden filepath \"%s\" is not permitted\n", filename)
	}

	if !checkBase(uuid, filename, "w", md) {

		fmt.Println("\tUser does not have permission to send to this directory.")
		w.WriteByte(3)
		w.Flush()

		return nil

	} else if err != nil && !hidden { // not a path ie. not a dir OR a file

		fmt.Println("\tFile \"" + filename + "\" does not already exist here")

		// notify the client that it is not already on system
		w.WriteByte(2)
		w.Flush()

	} else { // notify the client that the file exists with error code "1"

		fmt.Println("\tFile already exists in this directory")
		w.WriteByte(1)
		w.Flush()

		return nil
	}

	// is the user encrypting the file?
	protected := false
	enc, _ := r.ReadByte()
	if enc == 1 { // we are encrypting

		protected = true
		fmt.Println("\tReceiving encrypted file")
		// send the pubkeys of users here

		// get lenArgs
		lenArgs, _ := r.ReadByte()

		for i := 0; i < int(lenArgs); i++ {

			md.userDB.View(func(tx *bolt.Tx) error {

				b := tx.Bucket([]byte("users"))

				// get the proposed uuid for a user
				tmpUuid, _ := r.ReadString('\n')
				tmpUuid = strings.TrimSpace(tmpUuid)
				uuidUint64, _ := strconv.ParseUint(tmpUuid, 10, 64)

				v := b.Get(itob(uuidUint64))

				if v == nil {

					fmt.Println("\tNo user profile matching UUID: " + tmpUuid)
					w.WriteString("INV" + "\n")
					w.Flush()
					return nil
				}

				var tmpUser utils.User
				json.Unmarshal(v, &tmpUser)

				fmt.Println("\tFound user: "******"\n")
				w.Write([]byte(tmpUser.Pubkey.N.String() + "\n"))
				w.Write([]byte(strconv.Itoa(tmpUser.Pubkey.E) + "\n"))
				w.Flush()

				return nil
			})

		}

	} else if enc == 2 {

		//invalid cmd on client side
		return nil
		// else we are not
	}

	// get the hash of the file
	hash := utils.ReadHashAsString(r)

	var success byte
	var unid string

	md.stnodeDB.View(func(tx *bolt.Tx) error {
		// Assume bucket exists and has keys
		b := tx.Bucket([]byte("stnodes"))

		c := b.Cursor()

		for k, v := c.First(); k != nil; k, v = c.Next() {

			w.WriteByte(1) // got a stnode
			w.Flush()

			var tmpStnode utils.Stnode
			json.Unmarshal(v, &tmpStnode)

			fmt.Println("\tGot details for stnode: protocol: " + tmpStnode.Protocol + ", address: " + tmpStnode.NAddress)

			w.WriteString(tmpStnode.Protocol + "\n")
			w.WriteString(tmpStnode.NAddress + "\n")
			unid = tmpStnode.Unid
			w.Flush()

			success, _ = r.ReadByte()
			if success != 1 {
				fmt.Println("\tSuccessful send to stnode from client")
				return nil
			}
		}

		w.WriteByte(2) // no more stnodes
		w.Flush()

		return nil
	})

	if success != 2 {
		fmt.Println("\tNo stnodes were available to the client")
		return nil
	}

	success, _ = r.ReadByte()
	if success != 1 {
		fmt.Println("\tError on client side sending file to stnode")
		return nil
	}

	permissions := []bool{false, false}
	var groups []uint64
	err = createFile(md.getPath()+"files"+filename, hash, unid, protected, uuid, groups, permissions)
	if err != nil {
		panic(err)
	}

	return nil
}