-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
348 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,92 @@ | ||
package smtp | ||
|
||
import ( | ||
"github.com/awakari/int-email/service/writer" | ||
"github.com/emersion/go-smtp" | ||
"io" | ||
"context" | ||
"github.com/awakari/int-email/service/converter" | ||
"github.com/awakari/int-email/service/writer" | ||
"github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb" | ||
"github.com/emersion/go-smtp" | ||
"github.com/segmentio/ksuid" | ||
"io" | ||
) | ||
|
||
type session struct { | ||
svcWriter writer.Service | ||
rcptsAllowed map[string]bool | ||
dataLimit int64 | ||
// | ||
allowed bool | ||
from string | ||
data []byte | ||
svcWriter writer.Service | ||
rcptsAllowed map[string]bool | ||
dataLimit int64 | ||
evtType string | ||
conv converter.Service | ||
// | ||
allowed bool | ||
from string | ||
data []byte | ||
} | ||
|
||
func newSession(svcWriter writer.Service, rcptsAllowed map[string]bool, dataLimit int64) smtp.Session { | ||
return &session{ | ||
svcWriter: svcWriter, | ||
rcptsAllowed: rcptsAllowed, | ||
dataLimit: dataLimit, | ||
} | ||
func newSession(svcWriter writer.Service, rcptsAllowed map[string]bool, dataLimit int64, evtType string, conv converter.Service) smtp.Session { | ||
return &session{ | ||
svcWriter: svcWriter, | ||
rcptsAllowed: rcptsAllowed, | ||
dataLimit: dataLimit, | ||
evtType: evtType, | ||
conv: conv, | ||
} | ||
} | ||
|
||
func (s *session) Reset() { | ||
s.allowed = false | ||
s.from = "" | ||
s.data = nil | ||
return | ||
s.allowed = false | ||
s.from = "" | ||
s.data = nil | ||
return | ||
} | ||
|
||
func (s *session) Logout() (err error) { | ||
return | ||
return | ||
} | ||
|
||
func (s *session) Mail(from string, opts *smtp.MailOptions) (err error) { | ||
s.from = from | ||
return | ||
s.from = from | ||
return | ||
} | ||
|
||
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) (err error) { | ||
if s.rcptsAllowed[to] { | ||
s.allowed = true | ||
} | ||
return | ||
if s.rcptsAllowed[to] { | ||
s.allowed = true | ||
} | ||
return | ||
} | ||
|
||
func (s *session) Data(r io.Reader) (err error) { | ||
switch s.allowed { | ||
case true: | ||
r = io.LimitReader(r, s.dataLimit) | ||
s.data, err = io.ReadAll(r) | ||
if err != nil { | ||
err = &smtp.SMTPError{ | ||
Code: 554, | ||
EnhancedCode: smtp.EnhancedCode{ | ||
5, 3, 0, | ||
}, | ||
Message: err.Error(), | ||
} | ||
} | ||
default: | ||
err = &smtp.SMTPError{ | ||
Code: 550, | ||
EnhancedCode: smtp.EnhancedCode{ | ||
5, 1, 1, | ||
}, | ||
Message: "recipient rejected", | ||
} | ||
} | ||
return | ||
switch s.allowed { | ||
case true: | ||
r = io.LimitReader(r, s.dataLimit) | ||
evt := &pb.CloudEvent{ | ||
Id: ksuid.New().String(), | ||
Source: s.from, | ||
SpecVersion: "1.0", | ||
Type: s.evtType, | ||
Attributes: make(map[string]*pb.CloudEventAttributeValue), | ||
} | ||
err = s.conv.Convert(r, evt) | ||
switch err { | ||
case nil: | ||
err = s.svcWriter.Write(context.TODO(), evt, "default", s.from) | ||
default: | ||
err = &smtp.SMTPError{ | ||
Code: 554, | ||
EnhancedCode: smtp.EnhancedCode{ | ||
5, 3, 0, | ||
}, | ||
Message: err.Error(), | ||
} | ||
} | ||
default: | ||
err = &smtp.SMTPError{ | ||
Code: 550, | ||
EnhancedCode: smtp.EnhancedCode{ | ||
5, 1, 1, | ||
}, | ||
Message: "recipient rejected", | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
"time" | ||
"crypto/tls" | ||
"github.com/stretchr/testify/assert" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
os.Setenv("API_SMTP_HOST", "email.awakari.com") | ||
os.Setenv("API_WRITER_BACKOFF", "23h") | ||
os.Setenv("API_WRITER_URI", "writer:56789") | ||
os.Setenv("LOG_LEVEL", "4") | ||
cfg, err := NewConfigFromEnv() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 23*time.Hour, cfg.Api.Writer.Backoff) | ||
assert.Equal(t, "writer:56789", cfg.Api.Writer.Uri) | ||
assert.Equal(t, slog.LevelWarn, slog.Level(cfg.Log.Level)) | ||
os.Setenv("API_SMTP_HOST", "email.awakari.com") | ||
os.Setenv("API_WRITER_BACKOFF", "23h") | ||
os.Setenv("API_WRITER_URI", "writer:56789") | ||
os.Setenv("LOG_LEVEL", "4") | ||
cfg, err := NewConfigFromEnv() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 23*time.Hour, cfg.Api.Writer.Backoff) | ||
assert.Equal(t, "writer:56789", cfg.Api.Writer.Uri) | ||
assert.Equal(t, slog.LevelWarn, slog.Level(cfg.Log.Level)) | ||
assert.Equal(t, tls.VerifyClientCertIfGiven, cfg.Api.Smtp.Tls.ClientAuthType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ securityContext: {} | |
|
||
service: | ||
type: LoadBalancer | ||
port: 465 | ||
port: 25 | ||
|
||
ingress: | ||
enabled: false | ||
|
Oops, something went wrong.