-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
345 lines (323 loc) · 11.7 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Provides a service that listens for and processes incoming Webmentions.
// After some preliminary (synchronous) validation, Webmention requests are
// queued and then processed asynchronously.
//
// This application can be run (for example) as a daemon.
// A systemd service configuration is provided together with the source code.
//
// Configuration is read from a .env file (or just the OS env vars directly).
// An .env file must be present in either the process working directory
// `$PWD/.env`, or in `/etc/webmention/mentionee.env`.
//
// Configurable values are:
// - SHUTDOWN_TIMEOUT=Seconds: How long to wait for a clean shutdown after SIGINT or SIGTERM (default 120)
// - ENDPOINT=URL Path: On which path to listen for Webmentions (default /api/webmention)
// - LISTEN_ADDR=Domain with Port: Bind listener to this domain:port (default :8080)
// - ACCEPT_DOMAIN=Domain: Accept mentions if they point to this domain (e.g., the domain of your blog, required, no default)
// - NOTIFY_BY_MAIL=external, internal or no: Whether or not to enable notifications by mail (default no)
//
// Options for external SMTP server:
// - MAIL_HOST=Domain: Domain of the outgoing mail server (no default, required)
// - MAIL_PORT=Port: Port of the outgoing mail server (no default, required)
// - MAIL_USER=Username: User to authenticate to the outgoing mail server (no default, required)
// - MAIL_PASS=Password: Password to authenticate to the outgoing mail server (no default, required)
// - MAIL_FROM=E-Mail address: Address used in the FROM header (default same as MAIL_USER)
// - MAIL_TO=E-Mail address: Address used in the TO header (default same as MAIL_FROM, or MAIL_USER if MAIL_FROM not set)
//
// Options for internal SMPT server:
// - MAIL_FROM=E-Mail address: Send emails from this address (required)
// - MAIL_TO=E-Mail address: Send emails to this email address (required)
// - MAIL_FROM_ADDR=Domain: Domain from which to send mails (required)
// - MAIL_TO_ADDR=Domain: Domain of the receiving mail server (required)
// - MAIL_DKIM_PRIV=Path to private key: Path to private key used for dkim signing (default empty, don't sign)
// - MAIL_DKIM_SELECTOR=Selector: DKIM selector (default is "default")
// - MAIL_DKIM_HOST=Domain: Domain on which DKIM is configured
//
// For more information on how to setup the internal mail server, check the
// documentation on ConfigMailInternal.
//
// Configuration is reloaded on SIGHUP.
package main
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/cvanloo/parsenv"
"github.com/emersion/go-msgauth/dkim"
"github.com/joho/godotenv"
"gopkg.in/gomail.v2"
webmention "github.com/cvanloo/gowebmention"
"github.com/cvanloo/gowebmention/listener"
)
func init() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
}
var Config struct {
ShutdownTimeout int `cfg:"default=120"`
EndpointUrl string `cfg:"default=/api/webmention"`
ListenAddr string `cfg:"default=:8080"`
AcceptDomain string `cfg:"required"`
NotifyByMail string `cfg:"default=no"`
}
var ConfigMailExternal struct {
MailHost string `cfg:"required"`
MailPort int `cfg:"required"`
MailUser string `cfg:"required"`
MailPass string `cfg:"required"`
MailFrom string
MailTo string
}
// Mentionee can deliver emails directly to your inbox.
// For this, it needs to be authorized to send emails from the MAIL_FROM_ADDR
// domain. The following DNS entries are required:
//
// - A | mail.example.com | 11.22.33.44
// - MX | example.com | mail.example.com
// - TXT | example.com | v=spf1 mx -all
// - TXT | _dmarc.example.com | v=DMARC1; p=quarantine;
//
// Replace example.com with your own domain, and 11.22.33.44 with the IP of the
// server on which mentionee runs.
//
// For DKIM refer to the documentation on ConfigMailDkim.
var ConfigMailInternal struct {
MailFrom string `cfg:"required"`
MailTo string `cfg:"required"`
MailFromAddr string `cfg:"required"`
MailToAddr string `cfg:"required"`
MailDkimPriv string
}
// In addition to the DNS entries explained in ConfigMailInternal, you'll have
// to setup another DNS entry for DKIM verification to work.
//
// - TXT | default._domainkey.example.com | v=DKIM1; k=rsa; p=YOUR_PUBLIC_KEY_HERE
//
// "default" must be the same as in MAIL_DKIM_SELECTOR.
// You can create a pub/priv key pair using the commands:
//
// openssl genrsa -out private 1024
// openssl rsa -in private -pubout -out public
// sed '1d;$d' public | tr -d '\n' > spublic; echo "" >> spublic
//
// MAIL_DKIM_PRIV must point to the 'private' file.
// As YOUR_PUBLIC_KEY_HERE in the above DNS entry, you must use the contents
// from the 'spublic' file.
//
// MAIL_DKIM_HOST is your domain, e.g., example.com.
//
// Since mentionee can only send, not receive emails, you might want to setup
// 'rua' and 'ruf' to point to a different email address:
//
// - TXT | _dmarc.example.com | v=DMARC1; p=quarantine; rua=mailto:[email protected]; ruf=mailto:[email protected];
//
// For this to work, you also need an entry on the whatever.else domain:
//
// - TXT | example.com._report._dmarc | v=DMARC1
var ConfigMailDkim struct {
MailDkimSelector string `cfg:"default=default"`
MailDkimHost string `cfg:"required"`
}
const (
ExitSuccess = 0
ExitFailure = 1
ExitConfigError = -1
)
func loadConfig() (opts []webmention.ReceiverOption, listenAddr, endpoint string, shutdownTimeout time.Duration, agg *listener.ReportAggregator, err error) {
if err := godotenv.Load(); err != nil {
godotenv.Load("/etc/webmention/mentionee.env")
}
if err := parsenv.Load(&Config); err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
listenAddr = Config.ListenAddr
endpoint = Config.EndpointUrl
shutdownTimeout = time.Duration(Config.ShutdownTimeout) * time.Second
acceptDomain, err := url.Parse(Config.AcceptDomain)
if err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
opts = append(opts, webmention.WithAcceptsFunc(func(source, target *url.URL) bool {
return target.Scheme == acceptDomain.Scheme && target.Host == acceptDomain.Host
}))
if Config.NotifyByMail == "external" {
if err := parsenv.Load(&ConfigMailExternal); err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
dialer := gomail.NewDialer(ConfigMailExternal.MailHost, ConfigMailExternal.MailPort, ConfigMailExternal.MailUser, ConfigMailExternal.MailPass)
from := ConfigMailExternal.MailUser
if ConfigMailExternal.MailFrom != "" {
from = ConfigMailExternal.MailFrom
}
to := from
if ConfigMailExternal.MailTo != "" {
to = ConfigMailExternal.MailTo
}
mailer := listener.ExternalMailer{
SubjectLine: listener.DefaultSubjectLine,
Body: listener.DefaultBody,
From: from,
To: to,
Dialer: dialer,
}
aggregator := &listener.ReportAggregator{
SendAfterTime: 12 * time.Hour,
SendAfterCount: -1,
Sender: mailer,
}
opts = append(opts, webmention.WithNotifier(listener.Mailer{aggregator}))
agg = aggregator
} else if Config.NotifyByMail == "internal" {
if err := parsenv.Load(&ConfigMailInternal); err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
if ConfigMailInternal.MailDkimPriv != "" {
if err := parsenv.Load(&ConfigMailDkim); err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
pkbs, err := os.ReadFile(ConfigMailInternal.MailDkimPriv)
if err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
block, _ := pem.Decode(pkbs)
if block == nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, errors.New("failed to decode PEM block containing private key")
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return opts, listenAddr, endpoint, shutdownTimeout, agg, err
}
pk, ok := key.(*rsa.PrivateKey)
if !ok {
return opts, listenAddr, endpoint, shutdownTimeout, agg, fmt.Errorf("not an RSA private key: %T", key)
}
mailer := listener.InternalDKIMMailer{
InternalMailer: listener.InternalMailer{
SubjectLine: listener.DefaultSubjectLine,
Body: listener.DefaultBody,
FromAddr: ConfigMailInternal.MailFromAddr,
ToAddr: ConfigMailInternal.MailToAddr,
From: ConfigMailInternal.MailFrom,
To: ConfigMailInternal.MailTo,
},
DkimSignOpts: &dkim.SignOptions{
Domain: ConfigMailDkim.MailDkimHost,
Selector: ConfigMailDkim.MailDkimSelector,
Signer: pk,
},
}
aggregator := &listener.ReportAggregator{
SendAfterTime: 12 * time.Hour,
SendAfterCount: -1,
Sender: mailer,
}
opts = append(opts, webmention.WithNotifier(listener.Mailer{aggregator}))
agg = aggregator
} else {
mailer := listener.InternalMailer{
SubjectLine: listener.DefaultSubjectLine,
Body: listener.DefaultBody,
FromAddr: ConfigMailInternal.MailFromAddr,
ToAddr: ConfigMailInternal.MailToAddr,
From: ConfigMailInternal.MailFrom,
To: ConfigMailInternal.MailTo,
}
aggregator := &listener.ReportAggregator{
SendAfterTime: 12 * time.Hour,
SendAfterCount: -1,
Sender: mailer,
}
opts = append(opts, webmention.WithNotifier(listener.Mailer{aggregator}))
agg = aggregator
}
}
return opts, listenAddr, endpoint, shutdownTimeout, agg, nil
}
type OptionsCollection []webmention.ReceiverOption
func (c OptionsCollection) Configuration(r *webmention.Receiver) {
for _, f := range c {
f(r)
}
}
func main() {
reload := make(chan os.Signal, 1)
signal.Notify(reload, syscall.SIGHUP) // kill -HUP $(pidof mentionee)
exit := make(chan os.Signal, 1)
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM) // kill -TERM $(pidof mentionee)
appLoop:
for {
options, listenAddr, endpoint, shutdownTimeout, aggregator, err := loadConfig()
if err != nil {
slog.Error("erroneous configuration, *** all services stopped ***: ", "configError", err)
slog.Error("...waiting for SIGHUP (reload config) or SIGTERM/INT (terminate)")
select {
case <-reload:
slog.Info("sighup received, reloading configuration")
continue appLoop
case <-exit:
slog.Info("interrupt received, shutting down")
os.Exit(ExitConfigError)
return
}
}
receiver := webmention.NewReceiver(
webmention.WithNotifier(webmention.NotifierFunc(func(mention webmention.Mention) {
slog.Info("received webmention",
"source", mention.Source.String(),
"target", mention.Target.String(),
"status", mention.Status,
)
})),
OptionsCollection(options).Configuration,
)
if aggregator != nil {
go aggregator.Start()
}
go receiver.ProcessMentions()
mux := &http.ServeMux{}
mux.Handle(endpoint, receiver)
server := http.Server{
Addr: listenAddr,
Handler: mux,
}
go func() {
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error(fmt.Sprintf("http server error: %s", err))
os.Exit(ExitFailure)
}
}()
doShutdown := func() {
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), shutdownTimeout)
server.SetKeepAlivesEnabled(false)
defer shutdownRelease()
if err := server.Shutdown(shutdownCtx); err != nil {
slog.Error(fmt.Sprintf("http shutdown error: %s", err))
}
receiver.Shutdown(shutdownCtx)
if aggregator != nil {
aggregator.SendNow()
}
}
select {
case <-reload:
slog.Info("sighup received, reloading configuration")
doShutdown()
continue appLoop
case <-exit:
slog.Info("interrupt received, shutting down")
doShutdown()
os.Exit(ExitSuccess)
return
}
}
}