2014-04-20 18:49:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"encoding/json"
|
2014-04-27 22:38:43 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2014-04-20 18:49:05 +00:00
|
|
|
"net/http"
|
2014-04-27 16:31:00 +00:00
|
|
|
"net/smtp"
|
2014-04-27 18:59:14 +00:00
|
|
|
"strconv"
|
2014-04-27 22:38:43 +00:00
|
|
|
"bufio"
|
|
|
|
"strings"
|
2014-04-27 16:31:00 +00:00
|
|
|
"github.com/ian-kent/MailHog/mailhog/data"
|
2014-04-23 23:22:50 +00:00
|
|
|
"github.com/ian-kent/MailHog/mailhog/config"
|
2014-04-20 18:49:05 +00:00
|
|
|
"github.com/ian-kent/MailHog/mailhog/storage"
|
2014-04-27 20:13:35 +00:00
|
|
|
"github.com/ian-kent/MailHog/mailhog/http/router"
|
2014-04-20 18:49:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type APIv1 struct {
|
2014-04-23 23:22:50 +00:00
|
|
|
config *config.Config
|
2014-04-20 18:49:05 +00:00
|
|
|
exitChannel chan int
|
|
|
|
server *http.Server
|
2014-04-27 22:38:43 +00:00
|
|
|
eventlisteners []*EventListener
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventListener struct {
|
|
|
|
conn net.Conn
|
|
|
|
bufrw *bufio.ReadWriter
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 16:31:00 +00:00
|
|
|
type ReleaseConfig struct {
|
|
|
|
Email string
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
2014-04-23 23:22:50 +00:00
|
|
|
func CreateAPIv1(exitCh chan int, conf *config.Config, server *http.Server) *APIv1 {
|
2014-04-20 18:49:05 +00:00
|
|
|
log.Println("Creating API v1")
|
|
|
|
apiv1 := &APIv1{
|
|
|
|
config: conf,
|
|
|
|
exitChannel: exitCh,
|
|
|
|
server: server,
|
2014-04-27 22:38:43 +00:00
|
|
|
eventlisteners: make([]*EventListener, 0),
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
r := server.Handler.(*router.Router)
|
|
|
|
|
|
|
|
r.Get("^/api/v1/messages/?$", apiv1.messages)
|
|
|
|
r.Delete("^/api/v1/messages/?$", apiv1.delete_all)
|
|
|
|
r.Get("^/api/v1/messages/([0-9a-f]+)/?$", apiv1.message)
|
|
|
|
r.Delete("^/api/v1/messages/([0-9a-f]+)/?$", apiv1.delete_one)
|
|
|
|
r.Get("^/api/v1/messages/([0-9a-f]+)/download/?$", apiv1.download)
|
|
|
|
r.Get("^/api/v1/messages/([0-9a-f]+)/mime/part/(\\d+)/download/?$", apiv1.download_part)
|
|
|
|
r.Post("^/api/v1/messages/([0-9a-f]+)/release/?$", apiv1.release_one)
|
2014-04-27 22:38:43 +00:00
|
|
|
r.Get("^/api/v1/events/?$", apiv1.eventstream)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <- apiv1.config.MessageChan:
|
|
|
|
log.Println("Got message in APIv1 event stream")
|
|
|
|
bytes, _ := json.MarshalIndent(msg, "", " ")
|
|
|
|
json := string(bytes)
|
|
|
|
log.Printf("Sending content: %s\n", json)
|
|
|
|
apiv1.broadcast(json)
|
|
|
|
case <- exitCh:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-04-20 18:49:05 +00:00
|
|
|
|
|
|
|
return apiv1
|
|
|
|
}
|
|
|
|
|
2014-04-27 22:38:43 +00:00
|
|
|
func (apiv1 *APIv1) broadcast(json string) {
|
|
|
|
log.Println("[APIv1] BROADCAST /api/v1/events")
|
|
|
|
for _, l := range apiv1.eventlisteners {
|
|
|
|
log.Printf("Sending to connection: %s\n", l.conn.RemoteAddr())
|
|
|
|
|
|
|
|
lines := strings.Split(json, "\n")
|
|
|
|
data := ""
|
|
|
|
for _, l := range lines {
|
|
|
|
data += "data: " + l + "\n"
|
|
|
|
}
|
|
|
|
data += "\n"
|
|
|
|
|
|
|
|
size := fmt.Sprintf("%X", len(data) + 1)
|
|
|
|
l.bufrw.Write([]byte(size + "\r\n"))
|
|
|
|
|
|
|
|
lines = strings.Split(data, "\n")
|
|
|
|
for _, ln := range lines {
|
|
|
|
l.bufrw.Write([]byte(ln + "\n"))
|
|
|
|
}
|
|
|
|
_, err := l.bufrw.Write([]byte("\r\n"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error writing to connection: %s\n", err)
|
|
|
|
l.conn.Close()
|
|
|
|
// TODO remove from array
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.bufrw.Flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error flushing buffer: %s\n", err)
|
|
|
|
l.conn.Close()
|
|
|
|
// TODO remove from array
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (apiv1 *APIv1) eventstream(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
|
|
|
log.Println("[APIv1] GET /api/v1/events")
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
w.Write([]byte("\n\n"))
|
|
|
|
hj, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
log.Println("[APIv1] Connection hijack failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn, bufrw, err := hj.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[APIv1] Connection hijack failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiv1.eventlisteners = append(apiv1.eventlisteners, &EventListener{conn, bufrw})
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) messages(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-20 18:49:05 +00:00
|
|
|
log.Println("[APIv1] GET /api/v1/messages")
|
|
|
|
|
|
|
|
// TODO start, limit
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
messages, _ := apiv1.config.Storage.(*storage.MongoDB).List(0, 1000)
|
|
|
|
bytes, _ := json.Marshal(messages)
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
|
|
|
w.Write(bytes)
|
|
|
|
case *storage.Memory:
|
|
|
|
messages, _ := apiv1.config.Storage.(*storage.Memory).List(0, 1000)
|
|
|
|
bytes, _ := json.Marshal(messages)
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
|
|
|
w.Write(bytes)
|
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) message(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 19:52:46 +00:00
|
|
|
|
2014-04-20 19:01:53 +00:00
|
|
|
match := route.Pattern.FindStringSubmatch(r.URL.Path)
|
|
|
|
id := match[1]
|
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
|
|
|
|
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
bytes, _ := json.Marshal(message)
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
|
|
|
w.Write(bytes)
|
|
|
|
case *storage.Memory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.Memory).Load(id)
|
|
|
|
bytes, _ := json.Marshal(message)
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
|
|
|
w.Write(bytes)
|
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-26 10:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) download(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-26 10:50:34 +00:00
|
|
|
match := route.Pattern.FindStringSubmatch(r.URL.Path)
|
|
|
|
id := match[1]
|
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s/download\n", id)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "message/rfc822")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\"" + id + ".eml\"")
|
|
|
|
|
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
for h, l := range message.Content.Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
w.Write([]byte(h + ": " + v + "\r\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("\r\n" + message.Content.Body))
|
|
|
|
case *storage.Memory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.Memory).Load(id)
|
|
|
|
for h, l := range message.Content.Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
w.Write([]byte(h + ": " + v + "\r\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("\r\n" + message.Content.Body))
|
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) download_part(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 18:59:14 +00:00
|
|
|
match := route.Pattern.FindStringSubmatch(r.URL.Path)
|
|
|
|
id := match[1]
|
|
|
|
part, _ := strconv.Atoi(match[2])
|
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s/mime/part/%d/download\n", id, part)
|
|
|
|
|
|
|
|
// TODO extension from content-type?
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=\"" + id + "-part-" + match[2] + "\"")
|
|
|
|
|
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
for h, l := range message.MIME.Parts[part].Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("\r\n" + message.MIME.Parts[part].Body))
|
|
|
|
case *storage.Memory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.Memory).Load(id)
|
|
|
|
for h, l := range message.MIME.Parts[part].Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte("\r\n" + message.MIME.Parts[part].Body))
|
|
|
|
default:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) delete_all(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 19:52:46 +00:00
|
|
|
log.Println("[APIv1] POST /api/v1/messages")
|
2014-04-20 18:49:05 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
apiv1.config.Storage.(*storage.MongoDB).DeleteAll()
|
|
|
|
case *storage.Memory:
|
|
|
|
apiv1.config.Storage.(*storage.Memory).DeleteAll()
|
2014-04-26 10:50:34 +00:00
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) release_one(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-27 16:31:00 +00:00
|
|
|
match := route.Pattern.FindStringSubmatch(r.URL.Path)
|
|
|
|
id := match[1]
|
|
|
|
log.Printf("[APIv1] POST /api/v1/messages/%s/release\n", id)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
|
|
|
var msg = &data.Message{}
|
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
msg, _ = apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
case *storage.Memory:
|
|
|
|
msg, _ = apiv1.config.Storage.(*storage.Memory).Load(id)
|
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-27 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
var cfg ReleaseConfig
|
|
|
|
err := decoder.Decode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error decoding request body: %s", err)
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte("Error decoding request body"))
|
2014-04-27 16:31:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Releasing to %s (via %s:%s)", cfg.Email, cfg.Host, cfg.Port)
|
|
|
|
log.Printf("Got message: %s", msg.Id)
|
|
|
|
|
|
|
|
bytes := make([]byte, 0)
|
|
|
|
for h, l := range msg.Content.Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
bytes = append(bytes, []byte(h + ": " + v + "\r\n")...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bytes = append(bytes, []byte("\r\n" + msg.Content.Body)...)
|
|
|
|
|
|
|
|
err = smtp.SendMail(cfg.Host + ":" + cfg.Port, nil, "nobody@" + apiv1.config.Hostname, []string{cfg.Email}, bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to release message: %s", err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Message released successfully")
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:13:35 +00:00
|
|
|
func (apiv1 *APIv1) delete_one(w http.ResponseWriter, r *http.Request, route *router.Route) {
|
2014-04-20 18:49:05 +00:00
|
|
|
match := route.Pattern.FindStringSubmatch(r.URL.Path)
|
|
|
|
id := match[1]
|
|
|
|
log.Printf("[APIv1] POST /api/v1/messages/%s/delete\n", id)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/json")
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
|
|
|
case *storage.MongoDB:
|
|
|
|
apiv1.config.Storage.(*storage.MongoDB).DeleteOne(id)
|
|
|
|
case *storage.Memory:
|
|
|
|
apiv1.config.Storage.(*storage.Memory).DeleteOne(id)
|
2014-04-26 10:50:34 +00:00
|
|
|
default:
|
2014-04-27 16:34:48 +00:00
|
|
|
w.WriteHeader(500)
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|