-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
234 lines (210 loc) · 6.03 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/smtp"
"os"
"strconv"
"strings"
)
const authTokenKey = "HG_AUTH_TOKEN"
const smtpHostKey = "HG_SMTP_HOST"
const smtpPortKey = "HG_SMTP_PORT"
const smtpUsernameKey = "HG_SMTP_USERNAME"
const smtpPasswordKey = "HG_SMTP_PASSWORD"
const smtpIdentityKey = "HG_SMTP_IDENTITY"
const listenAddrKey = "HG_LISTEN_ADDR"
const smtpFrom = "[email protected]"
type AppSmtpClient struct {
authToken string
smtpHost string
smtpPort uint16
smtpUsername string
smtpPassword string
smtpIdentity string
}
func (c *AppSmtpClient) InitSmtpClient() error {
exist := false
const errTemplate = "unable to read environment variable %s"
c.authToken, exist = os.LookupEnv(authTokenKey)
if !exist {
return fmt.Errorf(errTemplate, authTokenKey)
}
c.smtpHost, exist = os.LookupEnv(smtpHostKey)
if !exist {
return fmt.Errorf(errTemplate, smtpHostKey)
}
smtpPortString, exist := os.LookupEnv(smtpPortKey)
if !exist {
return fmt.Errorf(errTemplate, smtpPortKey)
}
smtpPort, err := strconv.ParseUint(smtpPortString, 10, 16)
if err != nil {
return fmt.Errorf("failed to convert %s=\"%s\" to uint16: %w", smtpPortKey, smtpPortString, err)
}
c.smtpPort = uint16(smtpPort)
c.smtpUsername, exist = os.LookupEnv(smtpUsernameKey)
if !exist {
return fmt.Errorf(errTemplate, smtpUsernameKey)
}
c.smtpPassword, exist = os.LookupEnv(smtpPasswordKey)
if !exist {
return fmt.Errorf(errTemplate, smtpPasswordKey)
}
c.smtpIdentity, exist = os.LookupEnv(smtpIdentityKey)
if !exist {
return fmt.Errorf(errTemplate, smtpIdentityKey)
}
return nil
}
func (c *AppSmtpClient) SendMail(receiver string, subject string, content string) error {
headers := map[string]string{
"From": smtpFrom,
"To": receiver,
"Subject": subject,
"Content-Type": "text/plain; charset=utf-8",
}
var messageBuilder strings.Builder
for k, v := range headers {
messageBuilder.WriteString(k)
messageBuilder.WriteString(": ")
messageBuilder.WriteString(v)
messageBuilder.WriteString("\r\n")
}
messageBuilder.WriteString("\r\n")
messageBuilder.WriteString(content)
messageBuilder.WriteString("\r\n")
message := messageBuilder.String()
tlsConfig := tls.Config{
ServerName: c.smtpHost,
}
hostAddr := fmt.Sprintf("%s:%d", c.smtpHost, c.smtpPort)
conn, err := tls.Dial("tcp", hostAddr, &tlsConfig)
defer conn.Close()
if err != nil {
return fmt.Errorf("failed to establish secure connection to %s: %w", hostAddr, err)
}
smtpClient, err := smtp.NewClient(conn, c.smtpHost)
defer smtpClient.Close()
auth := smtp.PlainAuth(c.smtpIdentity, c.smtpUsername, c.smtpPassword, c.smtpHost)
if err = smtpClient.Auth(auth); err != nil {
return fmt.Errorf("failed to authenticate with %s as %s: %w", c.smtpHost, c.smtpUsername, err)
}
if err = smtpClient.Mail(c.smtpIdentity); err != nil {
return fmt.Errorf("failed to indicate mail from %s: %w", c.smtpIdentity, err)
}
if err = smtpClient.Rcpt(receiver); err != nil {
return fmt.Errorf("failed to indicate mail to %s: %w", receiver, err)
}
writer, err := smtpClient.Data()
if err != nil {
return fmt.Errorf("failed to get data writer: %w", err)
}
defer writer.Close()
_, err = writer.Write([]byte(message))
if err != nil {
return fmt.Errorf("failed to write message data")
}
smtpClient.Quit()
return nil
}
type HttpError struct {
status int
err error
}
func NewHttpError(status int, err error) *HttpError {
return &HttpError{
status: status,
err: err,
}
}
type MailApiResponse struct {
Success bool `json:"success"`
Message string `json:"msg"`
}
type MailApiRequest struct {
To string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
IP string `json:"ip"`
}
func (h *HttpError) Error() string {
return h.err.Error()
}
func (c *AppSmtpClient) MailApiHandler(w http.ResponseWriter, r *http.Request) error {
token := r.Header.Get("Authorization")
if len(token) < 7 || !strings.HasPrefix(token, "Bearer") {
return NewHttpError(http.StatusUnauthorized, fmt.Errorf("Malformed Token"))
}
token = token[7:]
if token != c.authToken {
return NewHttpError(http.StatusUnauthorized, fmt.Errorf("Malformed Token"))
}
request := MailApiRequest{}
reqBuffer := make([]byte, 4096)
reqReader := r.Body
reqLen, err := reqReader.Read(reqBuffer)
if err != nil && !errors.Is(err, io.EOF) {
return NewHttpError(http.StatusInternalServerError, fmt.Errorf("failed to read request: %w", err))
}
reqBuffer = reqBuffer[:reqLen]
err = json.Unmarshal(reqBuffer, &request)
if err != nil {
return NewHttpError(http.StatusBadRequest, fmt.Errorf("failed to parse request: %w", err))
}
err = c.SendMail(request.To, request.Subject, request.Body)
if err != nil {
log.Printf("Failed to send mail to %s: %w", request.To, err)
return NewHttpError(http.StatusInternalServerError, fmt.Errorf("failed to send mail to %s: %w", request.To, err))
}
log.Printf("Successfully send mail to %s, from %s", request.To, request.IP)
return nil
}
func (c *AppSmtpClient) MailApiHandlerWrapper(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusNotFound)
return
}
err := c.MailApiHandler(w, r)
var resp []byte
if err != nil {
resp, err = json.Marshal(MailApiResponse{
Success: false,
Message: err.Error(),
})
} else {
resp, err = json.Marshal(MailApiResponse{
Success: true,
Message: "",
})
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write([]byte(fmt.Errorf("failed to serialize repsonse: %w", err).Error()))
} else {
w.WriteHeader(http.StatusOK)
_, err = w.Write(resp)
}
if err != nil {
log.Printf("Failed to write response: %w", err)
}
}
func main() {
app := AppSmtpClient{}
err := app.InitSmtpClient()
if err != nil {
log.Fatalf("Failed to initialize: %s", err)
}
listenAddr, exist := os.LookupEnv(listenAddrKey)
if !exist {
listenAddr = ":8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/mail", app.MailApiHandlerWrapper)
log.Fatal(http.ListenAndServe(listenAddr, mux))
}