Exemplo n.º 1
0
func NewToken() string {
	uuid := uuid.NewRandom()
	mac := hmac.New(sha256.New, nil)
	mac.Write([]byte(uuid.String()))
	tokenBytes := mac.Sum(nil)
	return hex.EncodeToString(tokenBytes)
}
Exemplo n.º 2
0
func (agent *agent_t) handle_beacon() (err error) {

	msg, err := agent.udp.RecvMessage(0)
	if len(msg[0]) != 16 {
		return errors.New("Not a uuid")
	}

	//  If we got a UUID and it's not our own beacon, we have a peer
	uuid := uuid.UUID(msg[0])
	if bytes.Compare(uuid, agent.uuid_bytes) != 0 {
		//  Find or create peer via its UUID string
		uuid_string := uuid.String()
		peer, ok := agent.peers[uuid_string]
		if !ok {
			peer = new_peer(uuid)
			agent.peers[uuid_string] = peer

			//  Report peer joined the network
			agent.pipe.SendMessage("JOINED", uuid_string)
		}
		//  Any activity from the peer means it's alive
		peer.is_alive()
	}
	return
}
Exemplo n.º 3
0
// uUIDFromPath extracts the upload UUID from a given path
// If the UUID is the last path component, this is the containing
// directory for all upload files
func uUIDFromPath(path string) (string, bool) {
	components := strings.Split(path, "/")
	for i := len(components) - 1; i >= 0; i-- {
		if uuid := uuid.Parse(components[i]); uuid != nil {
			return uuid.String(), i == len(components)-1
		}
	}
	return "", false
}
Exemplo n.º 4
0
// Handle requests for saving secrets
func saveHandler(response http.ResponseWriter, request *http.Request,
	db Database) {
	response.Header().Set("Content-type", "application/json")

	if request.Method != "POST" {
		http.Error(response,
			`{"message": "Bad Request, see https://github.com/jhaals/yopass for more info"}`,
			http.StatusBadRequest)
		return
	}

	decoder := json.NewDecoder(request.Body)
	var secret struct {
		Message    string `json:"secret"`
		Expiration int32  `json:"expiration"`
	}
	err := decoder.Decode(&secret)
	if err != nil {
		http.Error(response, `{"message": "Unable to parse json"}`, http.StatusBadRequest)
		return
	}

	if validExpiration(secret.Expiration) == false {
		http.Error(response, `{"message": "Invalid expiration specified"}`, http.StatusBadRequest)
		return
	}

	if len(secret.Message) > 10000 {
		http.Error(response, `{"message": "Message is too long"}`, http.StatusBadRequest)
		return
	}

	// Generate new UUID and store secret in memcache with specified expiration
	uuid := uuid.NewUUID()
	err = db.Set(uuid.String(), secret.Message, secret.Expiration)
	if err != nil {
		http.Error(response, `{"message": "Failed to store secret in database"}`, http.StatusInternalServerError)
		return
	}

	resp := map[string]string{"key": uuid.String(), "message": "secret stored"}
	jsonData, _ := json.Marshal(resp)
	response.Write(jsonData)
}
Exemplo n.º 5
0
func (driver *MesosExecutorDriver) statusUpdateAcknowledgement(from *upid.UPID, pbMsg proto.Message) {
	log.Infoln("Executor statusUpdateAcknowledgement")

	msg := pbMsg.(*mesosproto.StatusUpdateAcknowledgementMessage)
	log.Infof("Receiving status update acknowledgement %v", msg)

	frameworkID := msg.GetFrameworkId()
	taskID := msg.GetTaskId()
	uuid := uuid.UUID(msg.GetUuid())

	if driver.stopped {
		log.Infof("Ignoring status update acknowledgement %v for task %v of framework %v because the driver is stopped!\n",
			uuid, taskID, frameworkID)
	}

	// Remove the corresponding update.
	delete(driver.updates, uuid.String())
	// Remove the corresponding task.
	delete(driver.tasks, taskID.String())
}
Exemplo n.º 6
0
func new_agent() (agent *agent_t) {

	// push output from udp into zmq socket
	bcast := &net.UDPAddr{Port: PING_PORT_NUMBER, IP: net.IPv4bcast}
	conn, e := net.ListenUDP("udp", bcast)
	if e != nil {
		panic(e)
	}
	go func() {
		buffer := make([]byte, 1024)
		udp, _ := zmq.NewSocket(zmq.PAIR)
		udp.Bind("inproc://udp")
		for {
			if n, _, err := conn.ReadFrom(buffer); err == nil {
				udp.SendBytes(buffer[:n], 0)
			}
		}
	}()
	time.Sleep(100 * time.Millisecond)

	pipe, _ := zmq.NewSocket(zmq.PAIR)
	pipe.Connect("inproc://iface")
	udp, _ := zmq.NewSocket(zmq.PAIR)
	udp.Connect("inproc://udp")

	uuid := uuid.NewRandom()
	agent = &agent_t{
		pipe:        pipe,
		udp:         udp,
		conn:        conn,
		uuid_bytes:  []byte(uuid),
		uuid_string: uuid.String(),
		peers:       make(map[string]*peer_t),
	}

	return
}
Exemplo n.º 7
0
func main() {
	counter := 0

	strCheck := "<request type='budget'><merchant>1</merchant></request>"
	strTransaction := "<request type='transaction'><merchant>1</merchant><operation_ident>aw%v</operation_ident><description>hello</description><amount>1</amount><operation_created_at>2014-10-01 20:13:56</operation_created_at></request>"

	for r := 0; r < 100; r++ {
		go func() {
			servAddr := "localhost:2000"
			tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
			if err != nil {
				println("ResolveTCPAddr failed:", err.Error())
				os.Exit(1)
			}

			conn, err := net.DialTCP("tcp", nil, tcpAddr)
			defer conn.Close()

			if err != nil {
				println("Dial failed:", err.Error())
				os.Exit(1)
			}

			uuid := uuid.NewUUID()

			req := fmt.Sprintf(strTransaction, uuid.String()[:10])
			counter++

			_, err = conn.Write([]byte(req))
			if err != nil {
				println("Write to server failed:", err.Error())
				os.Exit(1)
			}

			fmt.Println("write to server = ", req)

			runtime.Gosched()
		}()
	}

	time.Sleep(time.Second)

	servAddr := "localhost:2000"
	tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
	if err != nil {
		println("ResolveTCPAddr failed:", err.Error())
		os.Exit(1)
	}

	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	defer conn.Close()

	if err != nil {
		println("Dial failed:", err.Error())
		os.Exit(1)
	}

	req := fmt.Sprintf(strCheck, counter)
	counter++

	_, err = conn.Write([]byte(req))
	if err != nil {
		println("Write to server failed:", err.Error())
		os.Exit(1)
	}

	fmt.Println("write to server = ", req)

	reply := make([]byte, 1024)

	_, err = conn.Read(reply)
	if err != nil {
		println("Write to server failed:", err.Error())
		os.Exit(1)
	}

	println("reply from server=", string(reply))

	os.Exit(0)
}
Exemplo n.º 8
0
func vcardify(emp Employee, org, prefix, suffix string) string {
	var buf bytes.Buffer

	buf.WriteString("BEGIN:VCARD\n")
	buf.WriteString("VERSION:3.0\n")
	buf.WriteString(fmt.Sprintf(
		"REV:%s\n", time.Now().UTC().Format("2006-01-02T15:04:05Z")))

	buf.WriteString("PRODID:-//Topface//yml2vcard//EN\n")

	names := strings.Split(emp.Name, " ")
	var firstname, lastname string
	switch len(names) {
	case 1:
		firstname, lastname = names[0], ""
	case 2:
		firstname, lastname = names[0], names[1]
	case 3:
		firstname, lastname = names[0], names[2]
	default:
		firstname, lastname = emp.Name, ""
	}

	buf.WriteString(fmt.Sprintf(
		"N:%s;%s;;%s;%s\n", lastname, firstname, prefix, suffix))

	buf.WriteString("FN:")
	if prefix != "" {
		buf.WriteString(prefix + " ")
	}
	buf.WriteString(emp.Name)
	if suffix != "" {
		buf.WriteString(" " + suffix)
	}
	buf.WriteString("\n")

	if org != "" {
		buf.WriteString("ORG:")
		buf.WriteString(org)
		buf.WriteString("\n")
	}

	if emp.Phone != "" {
		buf.WriteString(fmt.Sprintf(
			"TEL:%s\n", emp.Phone))
	}

	if len(emp.ExtraPhones) > 0 {
		for _, p := range emp.ExtraPhones {
			buf.WriteString(fmt.Sprintf(
				"TEL:%s\n", p))
		}
	}

	if emp.WorkMail != "" {
		buf.WriteString(fmt.Sprintf(
			"EMAIL;TYPE=work:%s\n", emp.WorkMail))
	}

	if emp.PersonalMail != "" {
		buf.WriteString(fmt.Sprintf(
			"EMAIL;TYPE=home:%s\n", emp.PersonalMail))
	}

	if len(emp.ExtraMails) > 0 {
		for _, m := range emp.ExtraMails {
			buf.WriteString(fmt.Sprintf(
				"EMAIL:%s\n", m))
		}
	}

	uuid := uuid.NewMD5(uuid.NIL, []byte(emp.Name))
	buf.WriteString(fmt.Sprintf(
		"X-RADICALE-NAME:%s.vcf\n", uuid.String()))
	buf.WriteString(fmt.Sprintf(
		"UID:%s\n", uuid.String()))

	buf.WriteString("END:VCARD\n")

	return buf.String()
}