Beispiel #1
0
func initSender(username, password string) {
	// Open the key file
	/*keyFile, err := os.Open(keyPath)
	if err != nil {
		log.WithField("error", err.Error()).Fatal("Unable to open the private key file")
		return
	}

	// Parse the key
	keyring, err := openpgp.ReadArmoredKeyRing(r)*/
	// jk, what am i smoking

	api, err := client.New(*apiURL, 0)
	if err != nil {
		log.WithField("error", err.Error()).Fatal("Unable to connect to the Lavaboom API")
		return
	}

	token, err := api.CreateToken(&routes.TokensCreateRequest{
		Type:     "auth",
		Username: username,
		Password: password,
	})
	if err != nil {
		log.WithField("error", err.Error()).Fatal("Unable to sign into Lavaboom's API")
		return
	}

	api.Headers["Authorization"] = "Bearer " + token.ID

	cons, err := nsq.NewConsumer("sender_"+username, "sender", nsq.NewConfig())
	if err != nil {
		log.WithField("error", err.Error()).Fatal("Unable to consume the hub topic")
	}

	cons.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
		log.Print("Handling sender event")

		var ev *SenderEvent
		if err := json.Unmarshal(m.Body, &ev); err != nil {
			return err
		}

		// Parse the version
		version, err := semver.Parse(ev.Version)
		if err != nil {
			return err
		}

		templateLock.RLock()
		// Check if we have such template
		if x, ok := templates[ev.Name]; !ok || len(x) == 0 {
			templateLock.RUnlock()
			return errors.New("No such template")
		}

		// Match the version
		found := -1
		for i := len(templateVersions[ev.Name]) - 1; i >= 0; i-- {
			v2 := templateVersions[ev.Name][i]
			if version.Major == v2.Major {
				found = i
				break
			}
		}

		// Get the template
		template := templates[ev.Name][templateVersions[ev.Name][found].String()]

		// Execute the subject
		subject := &bytes.Buffer{}
		if err := template.SubjectTpl.Execute(subject, ev.Input); err != nil {
			return err
		}

		// Execute the body
		body := &bytes.Buffer{}
		if err := template.BodyTpl.Execute(body, ev.Input); err != nil {
			return err
		}

		// Send unencrypted
		if !strings.HasSuffix(ev.To[0], "@lavaboom.com") {
			// Send the email
			resp, err := api.CreateEmail(&routes.EmailsCreateRequest{
				Kind:        "raw",
				To:          ev.To,
				Body:        body.String(),
				Subject:     subject.String(),
				ContentType: "text/html",
			})
			if err != nil {
				return err
			}

			// Log some debug info
			log.Printf("Sent a \"%s\" email to %v - %v", subject.String(), ev.To, resp)

			// We're done!
			return nil
		} else {
			// Get user's key
			key, err := api.GetKey(ev.To[0])
			if err != nil {
				log.Print(err)
				m.Finish()
				m.DisableAutoResponse()
				return nil
			}

			// Parse the key
			keyReader := strings.NewReader(key.Key)
			keyring, err := openpgp.ReadArmoredKeyRing(keyReader)
			if err != nil {
				return err
			}

			// Hash the subject
			subjectHash := sha256.Sum256(subject.Bytes())

			// Hash the body
			bodyHash := sha256.Sum256(body.Bytes())

			// Parse the from address
			from, err := mail.ParseAddress(ev.From)
			if err != nil {
				return err
			}

			// Manifest definition
			manifest := &man.Manifest{
				Version: semver.Version{1, 0, 0, nil, nil},
				From:    from,
				To: []*mail.Address{
					{
						Address: ev.To[0],
					},
				},
				Subject: subject.String(),
				Parts: []*man.Part{
					{
						Hash:        hex.EncodeToString(bodyHash[:]),
						ID:          "body",
						ContentType: "text/html",
						Size:        body.Len(),
					},
				},
			}

			// Encrypt the body
			ebody, err := shared.EncryptAndArmor(body.Bytes(), keyring)
			if err != nil {
				return err
			}

			// Generate the manifest
			sman, err := man.Write(manifest)
			if err != nil {
				return err
			}
			eman, err := shared.EncryptAndArmor(sman, keyring)
			if err != nil {
				return err
			}

			// Send the email
			resp, err := api.CreateEmail(&routes.EmailsCreateRequest{
				Kind:        "manifest",
				From:        ev.From,
				To:          ev.To,
				Body:        string(ebody),
				Manifest:    string(eman),
				SubjectHash: hex.EncodeToString(subjectHash[:]),
			})
			if err != nil {
				return err
			}

			// Log some debug info
			log.Printf("Sent an encrypted \"%s\" email to %v - %v", subject.String(), ev.To, resp)

			// We're done!
			return nil
		}
	}))

	cons.ConnectToNSQLookupd(*lookupdAddress)
}
Beispiel #2
0
func main() {
	flag.Parse()

	var (
		welcomeTpl  = template.Must(template.New("welcome").Parse(welcomeTemplate))
		startedTpl  = template.Must(template.New("started").Parse(startedTemplate))
		securityTpl = template.Must(template.New("security").Parse(securityTemplate))
		whatsupTpl  = template.Must(template.New("whatsup").Parse(whatsupTemplate))
	)

	client, err := client.New(*apiURL, 0)
	if err != nil {
		log.Fatal(err)
	}

	token, err := client.CreateToken(&routes.TokensCreateRequest{
		Type:     "auth",
		Username: *username,
		Password: *password,
	})
	if err != nil {
		log.Fatal(err)
	}

	client.Headers["Authorization"] = "Bearer " + token.ID

	const target = "*****@*****.**"

	input := tplInput{
		FirstName: "Piotr",
	}

	go func() {
		time.Sleep(welcomeDelay)
		log.Print("Sending welcome")

		output := &bytes.Buffer{}
		log.Print(welcomeTpl.Execute(output, input))

		resp, err := client.CreateEmail(&routes.EmailsCreateRequest{
			Kind:        "raw",
			To:          []string{target},
			Body:        output.String(),
			Subject:     welcomeSubject,
			ContentType: "text/html",
		})
		if err != nil {
			log.Print(err)
		} else {
			log.Printf("Sent! %v", resp)
		}
	}()

	go func() {
		time.Sleep(startedDelay)
		log.Print("Sending started")

		output := &bytes.Buffer{}
		log.Print(startedTpl.Execute(output, input))

		resp, err := client.CreateEmail(&routes.EmailsCreateRequest{
			Kind:        "raw",
			To:          []string{target},
			Body:        output.String(),
			Subject:     startedSubject,
			ContentType: "text/html",
		})
		if err != nil {
			log.Print(err)
		} else {
			log.Printf("Sent! %v", resp)
		}
	}()

	go func() {
		time.Sleep(securityDelay)
		log.Print("Sending security")

		output := &bytes.Buffer{}
		log.Print(securityTpl.Execute(output, input))

		resp, err := client.CreateEmail(&routes.EmailsCreateRequest{
			Kind:        "raw",
			To:          []string{target},
			Body:        output.String(),
			Subject:     securitySubject,
			ContentType: "text/html",
		})
		if err != nil {
			log.Print(err)
		} else {
			log.Printf("Sent! %v", resp)
		}
	}()

	go func() {
		time.Sleep(whatsupDelay)
		log.Print("Sending whatsup")

		output := &bytes.Buffer{}
		log.Print(whatsupTpl.Execute(output, input))

		resp, err := client.CreateEmail(&routes.EmailsCreateRequest{
			Kind:        "raw",
			To:          []string{target},
			Body:        output.String(),
			Subject:     whatsupSubject,
			ContentType: "text/html",
		})
		if err != nil {
			log.Print(err)
		} else {
			log.Printf("Sent! %v", resp)
		}
	}()

	select {}
}