func (ths *LoopBackProvider) handleOutgoingMessages() {
	log.Println("Entering handleOutgoingMessages.")
	for {
		select {

		case m := <-ths.outgoing:
			log.Println("An outgoing event arrived.")
			if ths.failureStrategy.Failure(mtacontainer.EK_OK) == true {
				//Oh no we fail
				ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("Loop Back MTA is failing and going down"), m)
				ths.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, errors.New("Loop Back MTA is down"), ths)
				return // <- This actually stops this provider :-)
			}

			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_OK, errors.New("Loop Back MTA Got message"), ths)

			var headers = m.GetHeaders()
			if headers != nil {
				var temp = headers[model.EML_HDR_FROM]
				headers[model.EML_HDR_FROM] = headers[model.EML_HDR_TO]
				headers[model.EML_HDR_TO] = temp
			}
			ths.incoming <- m

		case c := <-ths.command:
			log.Println("Got a command")
			if c == commandprotocol.CMD_MTA_PROVIDER_SHUTDOWN {
				ths.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, errors.New("Going down, SHUT DOWN Command"),
					ths)
				return
			}
		}
	}
}
示例#2
0
//
//
// Forward a login check to the backend
//
func (ths *ClientAPI) handleLogin(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	ths.log.Println("Handle Login")

	if len(r.Header["Authorization"]) < 1 {
		ths.log.Println("Authorization header is missing")
		http.Error(w, "Missing authorization", http.StatusBadRequest)
		return
	}
	authStr := r.Header["Authorization"][0]

	var username, password, ok = decodeBasicAuth(authStr, ths.log)
	if ok == false {
		ths.log.Println("Authorization failed it is not basic ")
		http.Error(w, "Bad authorization", http.StatusBadRequest)
		return
	}

	q := "/login?username="******"&password="******"http://localhost" + utilities.RECEIVE_BACKEND_LISTEN_FOR_CLIENT_API + q

	resp, err := http.Get(backendLoginQuery)
	if err == nil {
		ths.log.Println("Ok connection to receiver backend")
		sessionId, errAll := ioutil.ReadAll(resp.Body)

		if errAll == nil {
			if len(sessionId) < 64 {
				ths.log.Println("Reporting back to UI that an error occured... " + goh.IntToStr(len(sessionId)))
				http.Error(w, resp.Status, resp.StatusCode)
				return
			}
			ths.log.Println("Adding session id " + string(sessionId))
			ths.validSessions[string(sessionId)] = username
			w.Write(sessionId)
			return
		} else {
			ths.log.Println("Auch failed to read response body from Receiver backend.")
			http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		}

	} else {
		http.Error(w, "Backend refuses login from user", http.StatusInternalServerError)
		ths.events <- mtacontainer.NewEvent(mtacontainer.EK_CRITICAL,
			errors.New("Could not connect to receiver back end"), ths)
	}
}
示例#3
0
func (a *ClientAPI) serve() {

	var mux = http.NewServeMux()
	mux.HandleFunc("/go.api/alive", a.alivePingHandler)
	mux.HandleFunc("/go.api/login", a.handleLogin)
	mux.HandleFunc("/go.api/logout", a.logoutHandler)
	mux.HandleFunc("/go.api/sendmail", a.sendMailHandler)
	mux.HandleFunc("/", a.viewHandler)

	var addr = ":" + strconv.Itoa(a.port)
	a.events <- mtacontainer.NewEvent(mtacontainer.EK_OK, errors.New("Serving on port: "+addr))

	err := http.ListenAndServeTLS(addr, "cert.pem", "key.pem", mux)

	if err != nil {
		log.Fatalln("[ClientApi, Error] " + err.Error())
	}
}
示例#4
0
func (mgp *MailGunProvider) mgSend(m model.Email) {

	from := m.GetHeader(model.EML_HDR_FROM)
	to := m.GetHeader(model.EML_HDR_TO)
	subject := m.GetHeader(model.EML_HDR_SUBJECT)
	content := m.GetContent()

	message := mgp.mg.NewMessage(from, subject, content, to)

	mgp.log.Println("Invoking MailGun API to send e-mail")
	var mm, mailId, err = mgp.mg.Send(message)

	// report MailGunProvider as down
	if err != nil {
		mgp.log.Println("MG Failed to send e-mail")
		mgp.log.Println(err.Error())
		if mgp.failureStrategy.Failure(mtacontainer.EK_CRITICAL) == false {
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_WARNING, err, mgp)
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, err, m)
		} else {
			mgp.Stop() // we are officially going down
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, err)
			mgp.log.Println("The MailGun Provider is considered Down.")
			mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("MailGun is down for sending"), m)
			for e := range mgp.out {
				var ee model.Email = e
				mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("MailGun is down for sending"), ee)
			}

		}
	} else {
		mgp.log.Println("MG Has sent email with success.")
		mgp.failureStrategy.Success()
		mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_BEAT,
			errors.New("MailGun says: "+mm+" for sending message giving it id "+mailId))
		mgp.log.Println("After sending event on event channel")
		mgp.events <- mtacontainer.NewEvent(mtacontainer.EK_INFORM_USER,
			errors.New("Mail Delivered With MailGun Successfully"), m.GetSessionId())

	}
}
func (ths *SendGridProvider) sgSend(m model.Email) {

	message := sendgrid.NewMail()
	message.From = m.GetHeader(model.EML_HDR_FROM)
	message.To = m.GetHeaders()[model.EML_HDR_TO]
	message.Subject = m.GetHeader(model.EML_HDR_SUBJECT)
	for k, _ := range m.GetHeaders() {
		if strings.Compare(k, model.EML_HDR_FROM) != 0 &&
			strings.Compare(k, model.EML_HDR_TO) != 0 &&
			strings.Compare(k, model.EML_HDR_SUBJECT) != 0 {
			message.AddHeader(k, m.GetHeader(k))
		}
	}
	message.SetText(m.GetContent())

	err := ths.sg.Send(message)

	// report SendGrid Provider as down
	if err != nil {
		ths.log.Println(err.Error())
		if ths.failureStrategy.Failure(mtacontainer.EK_CRITICAL) == false {
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("Sendgrid resubmits mail"), m)
		} else {
			ths.Stop() // we are officially going down
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_FATAL, err)
			ths.log.Println("The SendGrid Provider is considered Down.")
			ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("SendGrid is down for sending"), m)
			for e := range ths.outgoing {
				var ee model.Email = e
				ths.events <- mtacontainer.NewEvent(mtacontainer.EK_RESUBMIT, errors.New("SendGrid is down for sending"), ee)
			}

		}
	} else {
		ths.failureStrategy.Success()
		ths.events <- mtacontainer.NewEvent(mtacontainer.EK_BEAT,
			errors.New("SendGrid send a message"))
		ths.events <- mtacontainer.NewEvent(mtacontainer.EK_INFORM_USER, errors.New("Mail Delivered With SendGrid Successfully"), m.GetSessionId())

	}
}