master
James 7 months ago
parent 0af7e4fa8b
commit d78e4fb3b2
  1. 8
      README.md
  2. 8
      context.go
  3. 5
      example_smtpd_test.go
  4. 2
      go.mod
  5. 2
      go.sum
  6. 29
      smtd.go
  7. 4
      smtp.go

@ -1,17 +1,19 @@
# smtpd
smtp server
smtp server
使用者需实现`SmtpServerConfigure`接口
```
type SmtpServerConfigure interface {
GetConfig() *smtpd.ServerConfig
GetConfig() *lib.ServerConfig
func (this *SmtpConf) Auth(username string, password string) string
func TakeOff(email *smtpd.Mail)
}
```
准备妥当后只需构建一个smtpd实例并监听即可
准备妥当后只需构建一个 smtpd 实例并监听即可
```
smtpServer := smtpd.New(&SmtpConf {})

@ -5,6 +5,8 @@ import (
"net"
"regexp"
"time"
"github.com/watsonserve/maild"
)
const (
@ -17,11 +19,11 @@ type smtp_context_t struct {
sock net.Conn
Address string
handlers SmtpServerConfigure
conf *ServerConfig
conf *maild.ServerConfig
Module int
Login bool
re *regexp.Regexp
Email *Mail
Email *maild.Mail
// 其他
Msg string
User string
@ -36,7 +38,7 @@ func initSmtpContext(sock net.Conn, config SmtpServerConfigure) *smtp_context_t
Module: mod_COMMAND,
Login: false,
re: regexp.MustCompile("<(.+)>"),
Email: &Mail{},
Email: &maild.Mail{},
}
return scxt

@ -6,12 +6,13 @@ import (
"github.com/watsonserve/goutils"
"github.com/watsonserve/smtpd"
"github.com/watsonserve/maild"
)
type SmtpConf struct{}
func (sc *SmtpConf) GetConfig() *smtpd.ServerConfig {
return &smtpd.ServerConfig{
func (sc *SmtpConf) GetConfig() *maild.ServerConfig {
return &maild.ServerConfig{
Domain: "watsonserve.com",
Ip: "127.0.0.1",
Type: "SMTP",

@ -2,6 +2,8 @@ module github.com/watsonserve/smtpd
go 1.21.3
require github.com/watsonserve/maild v0.0.1
require github.com/watsonserve/goutils v0.1.13
require (

@ -32,6 +32,8 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/watsonserve/goutils v0.1.13 h1:l6kSncz81JJnnIJ7Pkq3n2TEEuhrqej5JVS734woOPc=
github.com/watsonserve/goutils v0.1.13/go.mod h1:UCuHnlSRGLqWAUu7nC4w6yAJMT8bRNPq35Irm6OEzBQ=
github.com/watsonserve/maild v0.0.1 h1:V0l84ALbshS7Btjrqx7u7+tk/I4FSwuzFmEGAquroc4=
github.com/watsonserve/maild v0.0.1/go.mod h1:7XewEYIUD6DQ+dwrQrQAk/KEXZX5x6vXk0J7CI4cq0s=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.4.0 h1:UVQgzMY87xqpKNgb+kDsll2Igd33HszWHFLmpaRMq/8=

@ -3,40 +3,21 @@ package smtpd
import (
"bufio"
"container/list"
"errors"
"fmt"
"log"
"net"
"strings"
)
type KV struct {
Name string
Value string
}
type Mail struct {
Sender string
Recver list.List
Head []KV
MailContent string
}
"github.com/watsonserve/maild"
)
// 配置
type ServerConfig struct {
Domain string
Ip string // 服务器的IP
Name string
Type string
Version string
}
// 接口集合
type SmtpServerConfigure interface {
GetConfig() *ServerConfig
GetConfig() *maild.ServerConfig
Auth(username string, password string) string
TakeOff(email *Mail)
TakeOff(email *maild.Mail)
}
type smtpd_t struct {
@ -98,7 +79,7 @@ func dataHead(ctx *smtp_context_t) {
ctx.Email.Head[len(ctx.Email.Head)-1].Value += "\r\n" + ctx.Msg
} else {
attr := strings.Split(ctx.Msg, ": ")
ele := &KV{
ele := &maild.KV{
Name: attr[0],
Value: attr[1],
}

@ -6,6 +6,8 @@ import (
"log"
"strings"
"time"
"github.com/watsonserve/maild"
)
// helo命令
@ -112,7 +114,7 @@ func data(ctx *smtp_context_t) {
format := "from %s ([%s]) by %s over TLS secured channel with %s(%s)\r\n\t%d"
ctx.Module = mod_HEAD
config := ctx.conf
ele := &KV{
ele := &maild.KV{
Name: "Received",
Value: fmt.Sprintf(format, config.Domain, config.Ip, config.Domain, config.Name, config.Version, time.Now().Unix()),
}

Loading…
Cancel
Save