Ejemplo n.º 1
0
//GetKey uses the HKID to lookup the PrivateKey.
func GetKey(h objects.HKID) (*objects.PrivateKey, error) {
	datach := make(chan objects.Blob, len(keygeters))
	errorch := make(chan error, len(keygeters))
	for _, rangekeygeterInstance := range keygeters {
		go func(
			keygeterInstance keygeter,
			datach chan objects.Blob,
			errorch chan error,
			h objects.HKID,
		) {
			k, err := keygeterInstance.GetKey(h)
			if err == nil {
				datach <- k
				return
			}
			errorch <- fmt.Errorf("[%s]: %s", keygeterInstance.ID(), err)
			return
		}(rangekeygeterInstance, datach, errorch, h)
	}
	for {
		select {
		case b := <-datach:
			privkey, err := objects.PrivateKeyFromBytes(b)
			if bytes.Equal(privkey.Hkid(), h) && privkey.Verify() {
				return privkey, err
			}
			log.Println("Key Verify Failed")
		case err := <-errorch:
			if err.Error() == "[timeout]: GetKey Timeout" {
				return nil, err
			}
			log.Println(err)
		}
	}
}
Ejemplo n.º 2
0
func Testbadfrombytes(t *testing.T) {
	var err error
	_, err = objects.ListFromBytes([]byte{})
	if err.Error() != "Could not parse list bytes" {
		t.Errorf("[] should not parse")
	}
	_, err = objects.CommitFromBytes([]byte{})
	if err.Error() != "Could not parse commit bytes" {
		t.Errorf("[] should not parse")
	}
	_, err = objects.TagFromBytes([]byte{})
	if err.Error() != "Could not parse tag bytes" {
		t.Errorf("[] should not parse")
	}
	_, err = objects.PrivateKeyFromBytes([]byte{})
	if err.Error() != "Could not parse commit bytes" {
		t.Errorf("[] should not parse")
	}
}
Ejemplo n.º 3
0
func (m multicastservice) receivemessage(
	message string,
	addr net.Addr,
) (err error) {
	//log.Printf("Received message, %s,\n", message)
	hkid, hcid, typestring, namesegment, url := parseMessage(message)
	if url == "" {
		checkAndRespond(hkid, hcid, typestring, namesegment)
		return nil
	}
	host, _, err := net.SplitHostPort(addr.String())
	if err != nil {
		return err
	}
	url = fmt.Sprintf("http://%s:%d%s", host, 8080, url)

	if typestring == "blob" {
		blobchannel, present := m.waitingforblob[hcid.String()]
		//log.Printf("url is %s", url)
		data, err := m.geturl(url)

		if err == nil {
			if present {
				blobchannel <- data
				//log.Printf("Data: %s\nHCID: %s", data, hcid)
			} else {
				log.Printf(
					"%s \nis not present in waiting map, \n%v",
					hcid.String(),
					m.waitingforblob,
				)
			}
			if objects.Blob(data).Hash().Hex() == hcid.Hex() {

				localfile.Instance.PostBlob(data)
			}
		} else {
			log.Printf("error: %s", err)
		}
	}

	if typestring == "tag" {
		tagchannel, present := m.waitingfortag[hkid.Hex()+namesegment]
		data, err := m.geturl(url)
		if err != nil {
			log.Printf("Error from geturl in tag, %s", err)
		}
		t, err := objects.TagFromBytes(data)
		if err == nil {
			if present {
				//log.Printf("Tag is present")
				tagchannel <- t
			} else {
				//log.Printf(
				//	"%s \n is not present in map \n %s",
				//	hkid.Hex()+namesegment,
				//	m.waitingfortag,
				//)
			}
			if t.Verify() {
				localfile.Instance.PostTag(t)
			}
		} else {
			log.Printf("error, %s", err)
			log.Printf("Data, %s", data)
		}
	}

	if typestring == "commit" {
		commitchannel, present := m.waitingforcommit[hkid.String()]
		data, err := m.geturl(url)
		if err != nil {
			log.Printf("Error for geturl in commitchannel is, %s", err)
		} else {
			c, err := objects.CommitFromBytes(data)

			if err == nil {
				if present {
					//log.Printf("commit is present")
					commitchannel <- c
				} else {
					//log.Printf(
					//	"commit %s\n is not present, \n%v",
					//	hkid.String(),
					//	m.waitingforcommit,
					//)
				}
				if c.Verify() {
					localfile.Instance.PostCommit(c)
				}
			}
		}
	}
	if typestring == "key" {
		keychannel, present := m.waitingforkey[hkid.String()]
		data, err := m.geturl(url)
		if err == nil {
			if present {
				log.Printf("key is present")
				keychannel <- data
			} else {
				log.Printf("key is not present, %v", m.waitingforkey)
			}
			p, err := objects.PrivateKeyFromBytes(data)

			if err != nil && p.Verify() && p.Hkid().Hex() == hkid.Hex() {
				localfile.Instance.PostKey(p)
			}

		}
	}

	return err
}