-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.go
201 lines (164 loc) · 5.84 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const (
StatusOK Status = 200
StatusError Status = 400
StatusNoResult Status = 500
ResponseNoResult string = "NO RESULT"
ResponsePayloadError string = "PAYLOAD ERROR"
ErrPayloadError string = "Error getting payload"
ErrAPIError string = "Error fetching data"
)
// Status is the status code for the response.
type Status int
// Response is the response to a postfix command.
type Response struct {
Status Status
Response string
}
// String returns the response as a string.
func (r *Response) String() string {
return fmt.Sprintf("%d %s\n", r.Status, strings.ReplaceAll(r.Response, " ", "%20"))
}
// PostfixAdapter is an adapter for postfix postmap commands.
// See https://www.postfix.org/postmap.1.html
type PostfixAdapter struct {
client UserliService
}
// NewPostfixAdapter creates a new Handler with the given UserliService.
func NewPostfixAdapter(client UserliService) *PostfixAdapter {
return &PostfixAdapter{client: client}
}
// AliasHandler handles the get command for aliases.
// It fetches the destinations for the given alias.
// The response is a comma separated list of destinations.
func (p *PostfixAdapter) AliasHandler(conn net.Conn) {
now := time.Now()
payload, err := p.payload(conn)
if err != nil {
log.WithError(err).Error(ErrPayloadError)
p.write(conn, Response{Status: StatusError, Response: ResponsePayloadError}, now, "alias")
return
}
aliases, err := p.client.GetAliases(payload)
if err != nil {
log.WithError(err).WithField("email", payload).Error(ErrAPIError)
p.write(conn, Response{Status: StatusError, Response: "Error fetching aliases"}, now, "alias")
return
}
if len(aliases) == 0 {
p.write(conn, Response{Status: StatusNoResult, Response: ResponseNoResult}, now, "alias")
return
}
p.write(conn, Response{Status: StatusOK, Response: strings.Join(aliases, ",")}, now, "alias")
}
// DomainHandler handles the get command for domains.
// It checks if the domain exists.
// The response is a single line with the status code.
func (p *PostfixAdapter) DomainHandler(conn net.Conn) {
now := time.Now()
payload, err := p.payload(conn)
if err != nil {
log.WithError(err).Error("Error getting payload")
p.write(conn, Response{Status: StatusError, Response: ResponsePayloadError}, now, "domain")
return
}
exists, err := p.client.GetDomain(payload)
if err != nil {
log.WithError(err).WithField("domain", payload).Error(ErrAPIError)
p.write(conn, Response{Status: StatusError, Response: "Error fetching domain"}, now, "domain")
return
}
if !exists {
p.write(conn, Response{Status: StatusNoResult, Response: ResponseNoResult}, now, "domain")
return
}
p.write(conn, Response{Status: StatusOK, Response: "1"}, now, "domain")
}
// MailboxHandler handles the get command for mailboxes.
// It checks if the mailbox exists.
// The response is a single line with the status code.
func (p *PostfixAdapter) MailboxHandler(conn net.Conn) {
now := time.Now()
payload, err := p.payload(conn)
if err != nil {
log.WithError(err).Error(ErrPayloadError)
p.write(conn, Response{Status: StatusError, Response: ResponsePayloadError}, now, "mailbox")
return
}
exists, err := p.client.GetMailbox(payload)
if err != nil {
log.WithError(err).WithField("email", payload).Error(ErrAPIError)
p.write(conn, Response{Status: StatusError, Response: "Error fetching mailbox"}, now, "mailbox")
return
}
if !exists {
p.write(conn, Response{Status: StatusNoResult, Response: ResponseNoResult}, now, "mailbox")
return
}
p.write(conn, Response{Status: StatusOK, Response: "1"}, now, "mailbox")
}
// SendersHandler handles the get command for senders.
// It fetches the senders for the given email.
// The response is a comma separated list of senders.
func (p *PostfixAdapter) SendersHandler(conn net.Conn) {
now := time.Now()
payload, err := p.payload(conn)
if err != nil {
log.WithError(err).Error(ErrPayloadError)
p.write(conn, Response{Status: StatusError, Response: ResponsePayloadError}, now, "senders")
return
}
senders, err := p.client.GetSenders(payload)
if err != nil {
log.WithError(err).WithField("email", payload).Error(ErrAPIError)
p.write(conn, Response{Status: StatusError, Response: "Error fetching senders"}, now, "senders")
return
}
if len(senders) == 0 {
p.write(conn, Response{Status: StatusNoResult, Response: ResponseNoResult}, now, "senders")
return
}
p.write(conn, Response{Status: StatusOK, Response: strings.Join(senders, ",")}, now, "senders")
}
// payload reads the data from the connection. It checks for valid
// commands sent by postfix and returns the payload.
func (h *PostfixAdapter) payload(conn net.Conn) (string, error) {
data := make([]byte, 4096)
_, err := conn.Read(data)
if err != nil {
return "", err
}
data = bytes.Trim(data, "\x00")
parts := strings.Split(string(data), " ")
if len(parts) < 2 || parts[0] != "get" {
return "", errors.New("invalid or unsupported command")
}
payload := strings.TrimSuffix(parts[1], "\n")
log.WithFields(log.Fields{"command": parts[0], "payload": payload}).Debug("Received payload")
return payload, nil
}
func (h *PostfixAdapter) write(conn net.Conn, response Response, now time.Time, handler string) {
var status string
switch response.Status {
case StatusOK:
status = "success"
default:
status = "error"
}
log.WithFields(log.Fields{"response": response.String(), "handler": handler, "status": status}).Debug("Writing response")
_, err := conn.Write([]byte(response.String()))
if err != nil {
log.WithError(err).WithFields(log.Fields{"response": response.String(), "handler": handler, "status": status}).Error("Error writing response")
}
requestDurations.With(prometheus.Labels{"handler": handler, "status": status}).Observe(time.Since(now).Seconds())
}