This commit is contained in:
lu.bai
2022-11-24 22:47:24 +08:00
parent f6a161d52f
commit 71d0135d6f
3213 changed files with 583576 additions and 216 deletions

196
pkg/alg/bfs_pathfinding.go Normal file
View File

@@ -0,0 +1,196 @@
package alg
const (
NODE_NONE = iota
NODE_START
NODE_END
NODE_BLOCK
)
type PathNode struct {
x int16
y int16
z int16
visit bool
state int
parent *PathNode
}
type BFS struct {
gMap map[int16]map[int16]map[int16]*PathNode
startPathNode *PathNode
endPathNode *PathNode
}
func NewBFS() (r *BFS) {
r = new(BFS)
return r
}
func (b *BFS) InitMap(terrain map[MeshMapPos]bool, start MeshMapPos, end MeshMapPos, extR int16) {
xLen := end.X - start.X
yLen := end.Y - start.Y
zLen := end.Z - start.Z
dx := int16(1)
dy := int16(1)
dz := int16(1)
if xLen < 0 {
dx = -1
xLen *= -1
}
if yLen < 0 {
dy = -1
yLen *= -1
}
if zLen < 0 {
dz = -1
zLen *= -1
}
b.gMap = make(map[int16]map[int16]map[int16]*PathNode)
for x := start.X - extR*dx; x != end.X+extR*dx; x += dx {
b.gMap[x] = make(map[int16]map[int16]*PathNode)
for y := start.Y - extR*dy; y != end.Y+extR*dy; y += dy {
b.gMap[x][y] = make(map[int16]*PathNode)
for z := start.Z - extR*dz; z != end.Z+extR*dz; z += dz {
state := -1
if x == start.X && y == start.Y && z == start.Z {
state = NODE_START
} else if x == end.X && y == end.Y && z == end.Z {
state = NODE_END
} else {
_, exist := terrain[MeshMapPos{
X: x,
Y: y,
Z: z,
}]
if exist {
state = NODE_NONE
} else {
state = NODE_BLOCK
}
}
node := &PathNode{
x: x,
y: y,
z: z,
visit: false,
state: state,
parent: nil,
}
b.gMap[x][y][z] = node
if node.state == NODE_START {
b.startPathNode = node
} else if node.state == NODE_END {
b.endPathNode = node
}
}
}
}
}
func (b *BFS) GetNeighbor(node *PathNode) []*PathNode {
neighborList := make([]*PathNode, 0)
dir := [][3]int16{
//
{1, 0, 0},
{-1, 0, 0},
{0, 1, 0},
{0, -1, 0},
{0, 0, 1},
{0, 0, -1},
//
{1, 1, 0},
{-1, 1, 0},
{-1, -1, 0},
{1, -1, 0},
//
{1, 0, 1},
{-1, 0, 1},
{-1, 0, -1},
{1, 0, -1},
//
{0, 1, 1},
{0, -1, 1},
{0, -1, -1},
{0, 1, -1},
//
{1, 1, 1},
{1, 1, -1},
{1, -1, 1},
{1, -1, -1},
{-1, 1, 1},
{-1, 1, -1},
{-1, -1, 1},
{-1, -1, -1},
}
for _, v := range dir {
x := node.x + v[0]
y := node.y + v[1]
z := node.z + v[2]
if _, exist := b.gMap[x]; !exist {
continue
}
if _, exist := b.gMap[x][y]; !exist {
continue
}
if _, exist := b.gMap[x][y][z]; !exist {
continue
}
neighborNode := b.gMap[x][y][z]
neighborList = append(neighborList, neighborNode)
}
return neighborList
}
func (b *BFS) GetPath() []*PathNode {
path := make([]*PathNode, 0)
if b.endPathNode.parent == nil {
return nil
}
node := b.endPathNode
for {
if node == nil {
break
}
path = append(path, node)
node = node.parent
}
if len(path) == 0 {
return nil
}
return path
}
func (b *BFS) Pathfinding() []MeshMapPos {
queue := NewALQueue[*PathNode]()
b.startPathNode.visit = true
queue.EnQueue(b.startPathNode)
for queue.Len() > 0 {
head := queue.DeQueue()
neighborList := b.GetNeighbor(head)
for _, neighbor := range neighborList {
if !neighbor.visit && neighbor.state != NODE_BLOCK {
neighbor.visit = true
neighbor.parent = head
queue.EnQueue(neighbor)
if neighbor.state == NODE_END {
break
}
}
}
}
path := b.GetPath()
if path == nil {
return nil
}
pathVectorList := make([]MeshMapPos, 0)
for i := len(path) - 1; i >= 0; i-- {
node := path[i]
pathVectorList = append(pathVectorList, MeshMapPos{
X: node.x,
Y: node.y,
Z: node.z,
})
}
return pathVectorList
}

View File

@@ -0,0 +1,7 @@
package alg
type MeshMapPos struct {
X int16
Y int16
Z int16
}

127
pkg/alg/queue.go Normal file
View File

@@ -0,0 +1,127 @@
package alg
type LinkList struct {
value any
frontNode *LinkList
nextNode *LinkList
}
// LinkListQueue 无界队列 每个元素可存储不同数据结构
type LLQueue struct {
headPtr *LinkList
tailPtr *LinkList
len uint64
}
func NewLLQueue() *LLQueue {
return &LLQueue{
headPtr: nil,
tailPtr: nil,
len: 0,
}
}
func (q *LLQueue) Len() uint64 {
return q.len
}
func (q *LLQueue) EnQueue(value any) {
if q.headPtr == nil || q.tailPtr == nil {
q.headPtr = new(LinkList)
q.tailPtr = q.headPtr
} else {
q.tailPtr.nextNode = new(LinkList)
q.tailPtr.nextNode.frontNode = q.tailPtr
q.tailPtr = q.tailPtr.nextNode
}
q.tailPtr.value = value
q.len++
}
func (q *LLQueue) DeQueue() any {
if q.Len() == 0 || q.headPtr == nil {
return nil
}
ret := q.headPtr.value
q.len--
q.headPtr = q.headPtr.nextNode
return ret
}
// ArrayListQueue 无界队列 泛型
type ALQueue[T any] struct {
array []T
}
func NewALQueue[T any]() *ALQueue[T] {
return &ALQueue[T]{
array: make([]T, 0),
}
}
func (q *ALQueue[T]) Len() uint64 {
return uint64(len(q.array))
}
func (q *ALQueue[T]) EnQueue(value T) {
q.array = append(q.array, value)
}
func (q *ALQueue[T]) DeQueue() T {
if q.Len() == 0 {
var null T
return null
}
ret := q.array[0]
q.array = q.array[1:]
return ret
}
// RingArrayQueue 有界队列 性能最好
type RAQueue[T any] struct {
ringArray []T
ringArrayLen uint64
headPtr uint64
tailPtr uint64
len uint64
}
func NewRAQueue[T any](size uint64) *RAQueue[T] {
return &RAQueue[T]{
ringArray: make([]T, size),
ringArrayLen: size,
headPtr: 0,
tailPtr: 0,
len: 0,
}
}
func (q *RAQueue[T]) Len() uint64 {
return q.len
}
func (q *RAQueue[T]) EnQueue(value T) {
if q.len >= q.ringArrayLen {
return
}
q.ringArray[q.tailPtr] = value
q.tailPtr++
if q.tailPtr >= q.ringArrayLen {
q.tailPtr = 0
}
q.len++
}
func (q *RAQueue[T]) DeQueue() T {
if q.Len() == 0 {
var null T
return null
}
ret := q.ringArray[q.headPtr]
q.headPtr++
if q.headPtr >= q.ringArrayLen {
q.headPtr = 0
}
q.len--
return ret
}

92
pkg/alg/queue_test.go Normal file
View File

@@ -0,0 +1,92 @@
package alg
import (
"fmt"
"testing"
)
func TestLLQueue(t *testing.T) {
queue := NewLLQueue()
queue.EnQueue(float32(100.123))
queue.EnQueue(uint8(66))
queue.EnQueue("aaa")
queue.EnQueue(int64(-123456789))
queue.EnQueue(true)
queue.EnQueue(5)
for queue.Len() > 0 {
value := queue.DeQueue()
fmt.Println(value)
}
}
func TestALQueue(t *testing.T) {
queue := NewALQueue[uint8]()
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(8)
queue.EnQueue(9)
for queue.Len() > 0 {
value := queue.DeQueue()
fmt.Println(value)
}
}
func TestRAQueue(t *testing.T) {
queue := NewRAQueue[uint8](1000)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(8)
queue.EnQueue(9)
for queue.Len() > 0 {
value := queue.DeQueue()
fmt.Println(value)
}
}
func BenchmarkLLQueue(b *testing.B) {
data := ""
for i := 0; i < 1024; i++ {
data += "X"
}
queue := NewLLQueue()
for i := 0; i < b.N; i++ {
for i := 0; i < 100; i++ {
queue.EnQueue(&data)
}
for i := 0; i < 100; i++ {
queue.DeQueue()
}
}
}
func BenchmarkALQueue(b *testing.B) {
data := ""
for i := 0; i < 1024; i++ {
data += "X"
}
queue := NewALQueue[*string]()
for i := 0; i < b.N; i++ {
for i := 0; i < 100; i++ {
queue.EnQueue(&data)
}
for i := 0; i < 100; i++ {
queue.DeQueue()
}
}
}
func BenchmarkRAQueue(b *testing.B) {
data := ""
for i := 0; i < 1024; i++ {
data += "X"
}
queue := NewRAQueue[*string](1000)
for i := 0; i < b.N; i++ {
for i := 0; i < 100; i++ {
queue.EnQueue(&data)
}
for i := 0; i < 100; i++ {
queue.DeQueue()
}
}
}

89
pkg/alg/snowflake.go Normal file
View File

@@ -0,0 +1,89 @@
package alg
import (
"sync"
"time"
)
// 雪花算法的基本实现
// snowflake ID 是一个64位的int数据 由四部分组成
// A-B-C-D
// A 1位 最高位不使用
// B 41位 时间戳
// C 10位 节点ID
// D 12位 毫秒内序列号
// 时间戳 节点ID 毫秒内序列号 位数可按需调整
const (
workerBits uint8 = 10 // 节点ID位数 2^10=1024
numberBits uint8 = 12 // 毫秒内序列号位数 2^12=4096
workerMax int64 = -1 ^ (-1 << workerBits) // 节点ID最大值
numberMax int64 = -1 ^ (-1 << numberBits) // 毫秒内序列号最大值
timeShift = workerBits + numberBits // 时间戳向左偏移量
workerShift = numberBits // 节点ID向左偏移量
/*
* 原始算法使用41位字节作为时间戳数值
* 大约68年也就是2038年就会用完
* 这里做个偏移以增加可用时间
* !!!这个一旦定义且开始生成ID后千万不要改了!!!
* 不然可能会生成相同的ID
*/
epoch int64 = 1657148827000 // 2022-07-07 07:07:07
)
type SnowflakeWorker struct {
lock sync.Mutex // 互斥锁
timestamp int64 // 记录时间戳
workerId int64 // 节点ID
number int64 // 当前毫秒内已经生成的ID序列号 从0开始累加
}
func NewSnowflakeWorker(workerId int64) *SnowflakeWorker {
if workerId < 0 || workerId > workerMax {
// worker id error
return nil
}
worker := &SnowflakeWorker{
timestamp: 0,
workerId: workerId,
number: 0,
}
return worker
}
func (s *SnowflakeWorker) GenId() int64 {
s.lock.Lock()
defer s.lock.Unlock()
// 当前毫秒时间戳
now := time.Now().UnixNano() / 1e6
if s.timestamp > now {
// 发生了时钟回拨
if s.timestamp-now > 1000 {
// 时钟回拨太严重
return -1
}
for now <= s.timestamp {
// 自旋等待当前时间超过上一次ID生成的时间
now = time.Now().UnixNano() / 1e6
}
}
if s.timestamp == now {
s.number++
if s.number > numberMax {
// 当前毫秒内生成ID数量超过限制
for now <= s.timestamp {
// 自旋等待
now = time.Now().UnixNano() / 1e6
}
}
}
if s.timestamp < now {
// 新的毫秒到来重置序列号和时间戳
s.number = 0
s.timestamp = now
}
// 生成ID
id := (now-epoch)<<timeShift | (s.workerId << workerShift) | (s.number)
return id
}

50
pkg/alg/snowflake_test.go Normal file
View File

@@ -0,0 +1,50 @@
package alg
import (
"fmt"
"sync"
"testing"
)
type UniqueID interface {
~int64 | ~string
}
func idDupCheck[T UniqueID](genIdFunc func() T) {
var wg sync.WaitGroup
totalIdList := make(map[int]*[]T)
for i := 0; i < 1000; i++ {
wg.Add(1)
_idList := make([]T, 0)
_idListPtr := &_idList
totalIdList[i] = _idListPtr
go func(idListPtr *[]T) {
defer wg.Done()
for ii := 0; ii < 10000; ii++ {
id := genIdFunc()
*idListPtr = append(*idListPtr, id)
}
}(_idListPtr)
}
wg.Wait()
dupCheck := make(map[T]bool)
for gid, idListPtr := range totalIdList {
for _, id := range *idListPtr {
value, exist := dupCheck[id]
if exist && value == true {
fmt.Printf("find dup id, gid: %v, id: %v\n", gid, id)
} else {
dupCheck[id] = true
}
}
}
fmt.Printf("check finish\n")
}
func TestSnowflakeGenId(t *testing.T) {
snowflake := NewSnowflakeWorker(1)
if snowflake == nil {
panic("create snowflake worker error")
}
idDupCheck(snowflake.GenId)
}

809
pkg/email/email.go Normal file
View File

@@ -0,0 +1,809 @@
// Package email is designed to provide an "email interface for humans."
// Designed to be robust and flexible, the email package aims to make sending email easy without getting in the way.
package email
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"math/big"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"net/smtp"
"net/textproto"
"os"
"path/filepath"
"strings"
"time"
"unicode"
)
const (
MaxLineLength = 76 // MaxLineLength is the maximum line length per RFC 2045
defaultContentType = "text/plain; charset=us-ascii" // defaultContentType is the default Content-Type according to RFC 2045, section 5.2
)
// ErrMissingBoundary is returned when there is no boundary given for a multipart entity
var ErrMissingBoundary = errors.New("No boundary found for multipart entity")
// ErrMissingContentType is returned when there is no "Content-Type" header for a MIME entity
var ErrMissingContentType = errors.New("No Content-Type found for MIME entity")
// Email is the type used for email messages
type Email struct {
ReplyTo []string
From string
To []string
Bcc []string
Cc []string
Subject string
Text []byte // Plaintext message (optional)
HTML []byte // Html message (optional)
Sender string // override From as SMTP envelope sender (optional)
Headers textproto.MIMEHeader
Attachments []*Attachment
ReadReceipt []string
}
// part is a copyable representation of a multipart.Part
type part struct {
header textproto.MIMEHeader
body []byte
}
// NewEmail creates an Email, and returns the pointer to it.
func NewEmail() *Email {
return &Email{Headers: textproto.MIMEHeader{}}
}
// trimReader is a custom io.Reader that will trim any leading
// whitespace, as this can cause email imports to fail.
type trimReader struct {
rd io.Reader
trimmed bool
}
// Read trims off any unicode whitespace from the originating reader
func (tr *trimReader) Read(buf []byte) (int, error) {
n, err := tr.rd.Read(buf)
if err != nil {
return n, err
}
if !tr.trimmed {
t := bytes.TrimLeftFunc(buf[:n], unicode.IsSpace)
tr.trimmed = true
n = copy(buf, t)
}
return n, err
}
func handleAddressList(v []string) []string {
res := []string{}
for _, a := range v {
w := strings.Split(a, ",")
for _, addr := range w {
decodedAddr, err := (&mime.WordDecoder{}).DecodeHeader(strings.TrimSpace(addr))
if err == nil {
res = append(res, decodedAddr)
} else {
res = append(res, addr)
}
}
}
return res
}
// NewEmailFromReader reads a stream of bytes from an io.Reader, r,
// and returns an email struct containing the parsed data.
// This function expects the data in RFC 5322 format.
func NewEmailFromReader(r io.Reader) (*Email, error) {
e := NewEmail()
s := &trimReader{rd: r}
tp := textproto.NewReader(bufio.NewReader(s))
// Parse the main headers
hdrs, err := tp.ReadMIMEHeader()
if err != nil {
return e, err
}
// Set the subject, to, cc, bcc, and from
for h, v := range hdrs {
switch h {
case "Subject":
e.Subject = v[0]
subj, err := (&mime.WordDecoder{}).DecodeHeader(e.Subject)
if err == nil && len(subj) > 0 {
e.Subject = subj
}
delete(hdrs, h)
case "To":
e.To = handleAddressList(v)
delete(hdrs, h)
case "Cc":
e.Cc = handleAddressList(v)
delete(hdrs, h)
case "Bcc":
e.Bcc = handleAddressList(v)
delete(hdrs, h)
case "Reply-To":
e.ReplyTo = handleAddressList(v)
delete(hdrs, h)
case "From":
e.From = v[0]
fr, err := (&mime.WordDecoder{}).DecodeHeader(e.From)
if err == nil && len(fr) > 0 {
e.From = fr
}
delete(hdrs, h)
}
}
e.Headers = hdrs
body := tp.R
// Recursively parse the MIME parts
ps, err := parseMIMEParts(e.Headers, body)
if err != nil {
return e, err
}
for _, p := range ps {
if ct := p.header.Get("Content-Type"); ct == "" {
return e, ErrMissingContentType
}
ct, _, err := mime.ParseMediaType(p.header.Get("Content-Type"))
if err != nil {
return e, err
}
// Check if part is an attachment based on the existence of the Content-Disposition header with a value of "attachment".
if cd := p.header.Get("Content-Disposition"); cd != "" {
cd, params, err := mime.ParseMediaType(p.header.Get("Content-Disposition"))
if err != nil {
return e, err
}
filename, filenameDefined := params["filename"]
if cd == "attachment" || (cd == "inline" && filenameDefined) {
_, err = e.Attach(bytes.NewReader(p.body), filename, ct)
if err != nil {
return e, err
}
continue
}
}
switch {
case ct == "text/plain":
e.Text = p.body
case ct == "text/html":
e.HTML = p.body
}
}
return e, nil
}
// parseMIMEParts will recursively walk a MIME entity and return a []mime.Part containing
// each (flattened) mime.Part found.
// It is important to note that there are no limits to the number of recursions, so be
// careful when parsing unknown MIME structures!
func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
var ps []*part
// If no content type is given, set it to the default
if _, ok := hs["Content-Type"]; !ok {
hs.Set("Content-Type", defaultContentType)
}
ct, params, err := mime.ParseMediaType(hs.Get("Content-Type"))
if err != nil {
return ps, err
}
// If it's a multipart email, recursively parse the parts
if strings.HasPrefix(ct, "multipart/") {
if _, ok := params["boundary"]; !ok {
return ps, ErrMissingBoundary
}
mr := multipart.NewReader(b, params["boundary"])
for {
var buf bytes.Buffer
p, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
return ps, err
}
if _, ok := p.Header["Content-Type"]; !ok {
p.Header.Set("Content-Type", defaultContentType)
}
subct, _, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
if err != nil {
return ps, err
}
if strings.HasPrefix(subct, "multipart/") {
sps, err := parseMIMEParts(p.Header, p)
if err != nil {
return ps, err
}
ps = append(ps, sps...)
} else {
var reader io.Reader
reader = p
const cte = "Content-Transfer-Encoding"
if p.Header.Get(cte) == "base64" {
reader = base64.NewDecoder(base64.StdEncoding, reader)
}
// Otherwise, just append the part to the list
// Copy the part data into the buffer
if _, err := io.Copy(&buf, reader); err != nil {
return ps, err
}
ps = append(ps, &part{body: buf.Bytes(), header: p.Header})
}
}
} else {
// If it is not a multipart email, parse the body content as a single "part"
switch hs.Get("Content-Transfer-Encoding") {
case "quoted-printable":
b = quotedprintable.NewReader(b)
case "base64":
b = base64.NewDecoder(base64.StdEncoding, b)
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, b); err != nil {
return ps, err
}
ps = append(ps, &part{body: buf.Bytes(), header: hs})
}
return ps, nil
}
// Attach is used to attach content from an io.Reader to the email.
// Required parameters include an io.Reader, the desired filename for the attachment, and the Content-Type
// The function will return the created Attachment for reference, as well as nil for the error, if successful.
func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) {
var buffer bytes.Buffer
if _, err = io.Copy(&buffer, r); err != nil {
return
}
at := &Attachment{
Filename: filename,
ContentType: c,
Header: textproto.MIMEHeader{},
Content: buffer.Bytes(),
}
e.Attachments = append(e.Attachments, at)
return at, nil
}
// AttachFile is used to attach content to the email.
// It attempts to open the file referenced by filename and, if successful, creates an Attachment.
// This Attachment is then appended to the slice of Email.Attachments.
// The function will then return the Attachment for reference, as well as nil for the error, if successful.
func (e *Email) AttachFile(filename string) (a *Attachment, err error) {
f, err := os.Open(filename)
if err != nil {
return
}
defer f.Close()
ct := mime.TypeByExtension(filepath.Ext(filename))
basename := filepath.Base(filename)
return e.Attach(f, basename, ct)
}
// msgHeaders merges the Email's various fields and custom headers together in a
// standards compliant way to create a MIMEHeader to be used in the resulting
// message. It does not alter e.Headers.
//
// "e"'s fields To, Cc, From, Subject will be used unless they are present in
// e.Headers. Unless set in e.Headers, "Date" will filled with the current time.
func (e *Email) msgHeaders() (textproto.MIMEHeader, error) {
res := make(textproto.MIMEHeader, len(e.Headers)+6)
if e.Headers != nil {
for _, h := range []string{"Reply-To", "To", "Cc", "From", "Subject", "Date", "Message-Id", "MIME-Version"} {
if v, ok := e.Headers[h]; ok {
res[h] = v
}
}
}
// Set headers if there are values.
if _, ok := res["Reply-To"]; !ok && len(e.ReplyTo) > 0 {
res.Set("Reply-To", strings.Join(e.ReplyTo, ", "))
}
if _, ok := res["To"]; !ok && len(e.To) > 0 {
res.Set("To", strings.Join(e.To, ", "))
}
if _, ok := res["Cc"]; !ok && len(e.Cc) > 0 {
res.Set("Cc", strings.Join(e.Cc, ", "))
}
if _, ok := res["Subject"]; !ok && e.Subject != "" {
res.Set("Subject", e.Subject)
}
if _, ok := res["Message-Id"]; !ok {
id, err := generateMessageID()
if err != nil {
return nil, err
}
res.Set("Message-Id", id)
}
// Date and From are required headers.
if _, ok := res["From"]; !ok {
res.Set("From", e.From)
}
if _, ok := res["Date"]; !ok {
res.Set("Date", time.Now().Format(time.RFC1123Z))
}
if _, ok := res["MIME-Version"]; !ok {
res.Set("MIME-Version", "1.0")
}
for field, vals := range e.Headers {
if _, ok := res[field]; !ok {
res[field] = vals
}
}
return res, nil
}
func writeMessage(buff io.Writer, msg []byte, multipart bool, mediaType string, w *multipart.Writer) error {
if multipart {
header := textproto.MIMEHeader{
"Content-Type": {mediaType + "; charset=UTF-8"},
"Content-Transfer-Encoding": {"quoted-printable"},
}
if _, err := w.CreatePart(header); err != nil {
return err
}
}
qp := quotedprintable.NewWriter(buff)
// Write the text
if _, err := qp.Write(msg); err != nil {
return err
}
return qp.Close()
}
func (e *Email) categorizeAttachments() (htmlRelated, others []*Attachment) {
for _, a := range e.Attachments {
if a.HTMLRelated {
htmlRelated = append(htmlRelated, a)
} else {
others = append(others, a)
}
}
return
}
// Bytes converts the Email object to a []byte representation, including all needed MIMEHeaders, boundaries, etc.
func (e *Email) Bytes() ([]byte, error) {
// TODO: better guess buffer size
buff := bytes.NewBuffer(make([]byte, 0, 4096))
headers, err := e.msgHeaders()
if err != nil {
return nil, err
}
htmlAttachments, otherAttachments := e.categorizeAttachments()
if len(e.HTML) == 0 && len(htmlAttachments) > 0 {
return nil, errors.New("there are HTML attachments, but no HTML body")
}
var (
isMixed = len(otherAttachments) > 0
isAlternative = len(e.Text) > 0 && len(e.HTML) > 0
isRelated = len(e.HTML) > 0 && len(htmlAttachments) > 0
)
var w *multipart.Writer
if isMixed || isAlternative || isRelated {
w = multipart.NewWriter(buff)
}
switch {
case isMixed:
headers.Set("Content-Type", "multipart/mixed;\r\n boundary="+w.Boundary())
case isAlternative:
headers.Set("Content-Type", "multipart/alternative;\r\n boundary="+w.Boundary())
case isRelated:
headers.Set("Content-Type", "multipart/related;\r\n boundary="+w.Boundary())
case len(e.HTML) > 0:
headers.Set("Content-Type", "text/html; charset=UTF-8")
headers.Set("Content-Transfer-Encoding", "quoted-printable")
default:
headers.Set("Content-Type", "text/plain; charset=UTF-8")
headers.Set("Content-Transfer-Encoding", "quoted-printable")
}
headerToBytes(buff, headers)
_, err = io.WriteString(buff, "\r\n")
if err != nil {
return nil, err
}
// Check to see if there is a Text or HTML field
if len(e.Text) > 0 || len(e.HTML) > 0 {
var subWriter *multipart.Writer
if isMixed && isAlternative {
// Create the multipart alternative part
subWriter = multipart.NewWriter(buff)
header := textproto.MIMEHeader{
"Content-Type": {"multipart/alternative;\r\n boundary=" + subWriter.Boundary()},
}
if _, err := w.CreatePart(header); err != nil {
return nil, err
}
} else {
subWriter = w
}
// Create the body sections
if len(e.Text) > 0 {
// Write the text
if err := writeMessage(buff, e.Text, isMixed || isAlternative, "text/plain", subWriter); err != nil {
return nil, err
}
}
if len(e.HTML) > 0 {
messageWriter := subWriter
var relatedWriter *multipart.Writer
if (isMixed || isAlternative) && len(htmlAttachments) > 0 {
relatedWriter = multipart.NewWriter(buff)
header := textproto.MIMEHeader{
"Content-Type": {"multipart/related;\r\n boundary=" + relatedWriter.Boundary()},
}
if _, err := subWriter.CreatePart(header); err != nil {
return nil, err
}
messageWriter = relatedWriter
} else if isRelated && len(htmlAttachments) > 0 {
relatedWriter = w
messageWriter = w
}
// Write the HTML
if err := writeMessage(buff, e.HTML, isMixed || isAlternative || isRelated, "text/html", messageWriter); err != nil {
return nil, err
}
if len(htmlAttachments) > 0 {
for _, a := range htmlAttachments {
a.setDefaultHeaders()
ap, err := relatedWriter.CreatePart(a.Header)
if err != nil {
return nil, err
}
// Write the base64Wrapped content to the part
base64Wrap(ap, a.Content)
}
if isMixed || isAlternative {
relatedWriter.Close()
}
}
}
if isMixed && isAlternative {
if err := subWriter.Close(); err != nil {
return nil, err
}
}
}
// Create attachment part, if necessary
for _, a := range otherAttachments {
a.setDefaultHeaders()
ap, err := w.CreatePart(a.Header)
if err != nil {
return nil, err
}
// Write the base64Wrapped content to the part
base64Wrap(ap, a.Content)
}
if isMixed || isAlternative || isRelated {
if err := w.Close(); err != nil {
return nil, err
}
}
return buff.Bytes(), nil
}
// Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
// This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
func (e *Email) Send(addr string, a smtp.Auth) error {
// Merge the To, Cc, and Bcc fields
to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
for i := 0; i < len(to); i++ {
addr, err := mail.ParseAddress(to[i])
if err != nil {
return err
}
to[i] = addr.Address
}
// Check to make sure there is at least one recipient and one "From" address
if e.From == "" || len(to) == 0 {
return errors.New("Must specify at least one From address and one To address")
}
sender, err := e.parseSender()
if err != nil {
return err
}
raw, err := e.Bytes()
if err != nil {
return err
}
return smtp.SendMail(addr, a, sender, to, raw)
}
// Select and parse an SMTP envelope sender address. Choose Email.Sender if set, or fallback to Email.From.
func (e *Email) parseSender() (string, error) {
if e.Sender != "" {
sender, err := mail.ParseAddress(e.Sender)
if err != nil {
return "", err
}
return sender.Address, nil
} else {
from, err := mail.ParseAddress(e.From)
if err != nil {
return "", err
}
return from.Address, nil
}
}
// SendWithTLS sends an email over tls with an optional TLS config.
//
// The TLS Config is helpful if you need to connect to a host that is used an untrusted
// certificate.
func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
// Merge the To, Cc, and Bcc fields
to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
for i := 0; i < len(to); i++ {
addr, err := mail.ParseAddress(to[i])
if err != nil {
return err
}
to[i] = addr.Address
}
// Check to make sure there is at least one recipient and one "From" address
if e.From == "" || len(to) == 0 {
return errors.New("Must specify at least one From address and one To address")
}
sender, err := e.parseSender()
if err != nil {
return err
}
raw, err := e.Bytes()
if err != nil {
return err
}
conn, err := tls.Dial("tcp", addr, t)
if err != nil {
return err
}
c, err := smtp.NewClient(conn, t.ServerName)
if err != nil {
return err
}
defer c.Close()
if err = c.Hello("localhost"); err != nil {
return err
}
if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
}
}
if err = c.Mail(sender); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(raw)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
// SendWithStartTLS sends an email over TLS using STARTTLS with an optional TLS config.
//
// The TLS Config is helpful if you need to connect to a host that is used an untrusted
// certificate.
func (e *Email) SendWithStartTLS(addr string, a smtp.Auth, t *tls.Config) error {
// Merge the To, Cc, and Bcc fields
to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
for i := 0; i < len(to); i++ {
addr, err := mail.ParseAddress(to[i])
if err != nil {
return err
}
to[i] = addr.Address
}
// Check to make sure there is at least one recipient and one "From" address
if e.From == "" || len(to) == 0 {
return errors.New("Must specify at least one From address and one To address")
}
sender, err := e.parseSender()
if err != nil {
return err
}
raw, err := e.Bytes()
if err != nil {
return err
}
// Taken from the standard library
// https://github.com/golang/go/blob/master/src/net/smtp/smtp.go#L328
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Hello("localhost"); err != nil {
return err
}
// Use TLS if available
if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(t); err != nil {
return err
}
}
if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
}
}
if err = c.Mail(sender); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(raw)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
// Attachment is a struct representing an email attachment.
// Based on the mime/multipart.FileHeader struct, Attachment contains the name, MIMEHeader, and content of the attachment in question
type Attachment struct {
Filename string
ContentType string
Header textproto.MIMEHeader
Content []byte
HTMLRelated bool
}
func (at *Attachment) setDefaultHeaders() {
contentType := "application/octet-stream"
if len(at.ContentType) > 0 {
contentType = at.ContentType
}
at.Header.Set("Content-Type", contentType)
if len(at.Header.Get("Content-Disposition")) == 0 {
disposition := "attachment"
if at.HTMLRelated {
disposition = "inline"
}
at.Header.Set("Content-Disposition", fmt.Sprintf("%s;\r\n filename=\"%s\"", disposition, at.Filename))
}
if len(at.Header.Get("Content-ID")) == 0 {
at.Header.Set("Content-ID", fmt.Sprintf("<%s>", at.Filename))
}
if len(at.Header.Get("Content-Transfer-Encoding")) == 0 {
at.Header.Set("Content-Transfer-Encoding", "base64")
}
}
// base64Wrap encodes the attachment content, and wraps it according to RFC 2045 standards (every 76 chars)
// The output is then written to the specified io.Writer
func base64Wrap(w io.Writer, b []byte) {
// 57 raw bytes per 76-byte base64 line.
const maxRaw = 57
// Buffer for each line, including trailing CRLF.
buffer := make([]byte, MaxLineLength+len("\r\n"))
copy(buffer[MaxLineLength:], "\r\n")
// Process raw chunks until there's no longer enough to fill a line.
for len(b) >= maxRaw {
base64.StdEncoding.Encode(buffer, b[:maxRaw])
w.Write(buffer)
b = b[maxRaw:]
}
// Handle the last chunk of bytes.
if len(b) > 0 {
out := buffer[:base64.StdEncoding.EncodedLen(len(b))]
base64.StdEncoding.Encode(out, b)
out = append(out, "\r\n"...)
w.Write(out)
}
}
// headerToBytes renders "header" to "buff". If there are multiple values for a
// field, multiple "Field: value\r\n" lines will be emitted.
func headerToBytes(buff io.Writer, header textproto.MIMEHeader) {
for field, vals := range header {
for _, subval := range vals {
// bytes.Buffer.Write() never returns an error.
io.WriteString(buff, field)
io.WriteString(buff, ": ")
// Write the encoded header if needed
switch {
case field == "Content-Type" || field == "Content-Disposition":
buff.Write([]byte(subval))
case field == "From" || field == "To" || field == "Cc" || field == "Bcc":
participants := strings.Split(subval, ",")
for i, v := range participants {
addr, err := mail.ParseAddress(v)
if err != nil {
continue
}
participants[i] = addr.String()
}
buff.Write([]byte(strings.Join(participants, ", ")))
default:
buff.Write([]byte(mime.QEncoding.Encode("UTF-8", subval)))
}
io.WriteString(buff, "\r\n")
}
}
}
var maxBigInt = big.NewInt(math.MaxInt64)
// generateMessageID generates and returns a string suitable for an RFC 2822
// compliant Message-ID, e.g.:
// <1444789264909237300.3464.1819418242800517193@DESKTOP01>
//
// The following parameters are used to generate a Message-ID:
// - The nanoseconds since Epoch
// - The calling PID
// - A cryptographically random int64
// - The sending hostname
func generateMessageID() (string, error) {
t := time.Now().UnixNano()
pid := os.Getpid()
rint, err := rand.Int(rand.Reader, maxBigInt)
if err != nil {
return "", err
}
h, err := os.Hostname()
// If we can't get the hostname, we'll use localhost
if err != nil {
h = "localhost.localdomain"
}
msgid := fmt.Sprintf("<%d.%d.%d@%s>", t, pid, rint, h)
return msgid, nil
}

932
pkg/email/email_test.go Normal file
View File

@@ -0,0 +1,932 @@
package email
import (
"fmt"
"strings"
"testing"
"bufio"
"bytes"
"crypto/rand"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"net/smtp"
"net/textproto"
)
func prepareEmail() *Email {
e := NewEmail()
e.From = "Jordan Wright <test@example.com>"
e.To = []string{"test@example.com"}
e.Bcc = []string{"test_bcc@example.com"}
e.Cc = []string{"test_cc@example.com"}
e.Subject = "Awesome Subject"
return e
}
func basicTests(t *testing.T, e *Email) *mail.Message {
raw, err := e.Bytes()
if err != nil {
t.Fatal("Failed to render message: ", e)
}
msg, err := mail.ReadMessage(bytes.NewBuffer(raw))
if err != nil {
t.Fatal("Could not parse rendered message: ", err)
}
expectedHeaders := map[string]string{
"To": "<test@example.com>",
"From": "\"Jordan Wright\" <test@example.com>",
"Cc": "<test_cc@example.com>",
"Subject": "Awesome Subject",
}
for header, expected := range expectedHeaders {
if val := msg.Header.Get(header); val != expected {
t.Errorf("Wrong value for message header %s: %v != %v", header, expected, val)
}
}
return msg
}
func TestEmailText(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, _, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "text/plain" {
t.Fatalf("Content-type expected \"text/plain\", not %v", mt)
}
}
func TestEmailWithHTMLAttachments(t *testing.T) {
e := prepareEmail()
// Set plain text to exercise "mime/alternative"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<html><body>This is a text.</body></html>")
// Set HTML attachment to exercise "mime/related".
attachment, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "image/png; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
attachment.HTMLRelated = true
b, err := e.Bytes()
if err != nil {
t.Fatal("Could not serialize e-mail:", err)
}
// Print the bytes for ocular validation and make sure no errors.
//fmt.Println(string(b))
// TODO: Verify the attachments.
s := &trimReader{rd: bytes.NewBuffer(b)}
tp := textproto.NewReader(bufio.NewReader(s))
// Parse the main headers
hdrs, err := tp.ReadMIMEHeader()
if err != nil {
t.Fatal("Could not parse the headers:", err)
}
// Recursively parse the MIME parts
ps, err := parseMIMEParts(hdrs, tp.R)
if err != nil {
t.Fatal("Could not parse the MIME parts recursively:", err)
}
plainTextFound := false
htmlFound := false
imageFound := false
if expected, actual := 3, len(ps); actual != expected {
t.Error("Unexpected number of parts. Expected:", expected, "Was:", actual)
}
for _, part := range ps {
// part has "header" and "body []byte"
cd := part.header.Get("Content-Disposition")
ct := part.header.Get("Content-Type")
if strings.Contains(ct, "image/png") && strings.HasPrefix(cd, "inline") {
imageFound = true
}
if strings.Contains(ct, "text/html") {
htmlFound = true
}
if strings.Contains(ct, "text/plain") {
plainTextFound = true
}
}
if !plainTextFound {
t.Error("Did not find plain text part.")
}
if !htmlFound {
t.Error("Did not find HTML part.")
}
if !imageFound {
t.Error("Did not find image part.")
}
}
func TestEmailWithHTMLAttachmentsHTMLOnly(t *testing.T) {
e := prepareEmail()
e.HTML = []byte("<html><body>This is a text.</body></html>")
// Set HTML attachment to exercise "mime/related".
attachment, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "image/png; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
attachment.HTMLRelated = true
b, err := e.Bytes()
if err != nil {
t.Fatal("Could not serialize e-mail:", err)
}
// Print the bytes for ocular validation and make sure no errors.
//fmt.Println(string(b))
// TODO: Verify the attachments.
s := &trimReader{rd: bytes.NewBuffer(b)}
tp := textproto.NewReader(bufio.NewReader(s))
// Parse the main headers
hdrs, err := tp.ReadMIMEHeader()
if err != nil {
t.Fatal("Could not parse the headers:", err)
}
if !strings.HasPrefix(hdrs.Get("Content-Type"), "multipart/related") {
t.Error("Envelope Content-Type is not multipart/related: ", hdrs["Content-Type"])
}
// Recursively parse the MIME parts
ps, err := parseMIMEParts(hdrs, tp.R)
if err != nil {
t.Fatal("Could not parse the MIME parts recursively:", err)
}
htmlFound := false
imageFound := false
if expected, actual := 2, len(ps); actual != expected {
t.Error("Unexpected number of parts. Expected:", expected, "Was:", actual)
}
for _, part := range ps {
// part has "header" and "body []byte"
ct := part.header.Get("Content-Type")
if strings.Contains(ct, "image/png") {
imageFound = true
}
if strings.Contains(ct, "text/html") {
htmlFound = true
}
}
if !htmlFound {
t.Error("Did not find HTML part.")
}
if !imageFound {
t.Error("Did not find image part.")
}
}
func TestEmailHTML(t *testing.T) {
e := prepareEmail()
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, _, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatalf("Content-type header is invalid: %#v", ct)
} else if mt != "text/html" {
t.Fatalf("Content-type expected \"text/html\", not %v", mt)
}
}
func TestEmailTextAttachment(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatalf("Invalid or missing boundary parameter: %#v", b)
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
text, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find text component of email: %s", err)
}
// Does the text portion match what we expect?
mt, _, err = mime.ParseMediaType(text.Header.Get("Content-type"))
if err != nil {
t.Fatal("Could not parse message's Content-Type")
} else if mt != "text/plain" {
t.Fatal("Message missing text/plain")
}
plainText, err := io.ReadAll(text)
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
t.Fatalf("Plain text is broken: %#q", plainText)
}
// Check attachments.
_, err = mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only text and one attachment!")
}
}
func TestEmailTextHtmlAttachment(t *testing.T) {
e := prepareEmail()
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatal("Unexpected empty boundary parameter")
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
text, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find text component of email: %s", err)
}
// Does the text portion match what we expect?
mt, params, err = mime.ParseMediaType(text.Header.Get("Content-type"))
if err != nil {
t.Fatal("Could not parse message's Content-Type")
} else if mt != "multipart/alternative" {
t.Fatal("Message missing multipart/alternative")
}
mpReader := multipart.NewReader(text, params["boundary"])
part, err := mpReader.NextPart()
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
plainText, err := io.ReadAll(part)
if err != nil {
t.Fatal("Could not read plain text component of message: ", err)
}
if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
t.Fatalf("Plain text is broken: %#q", plainText)
}
// Check attachments.
_, err = mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only text and one attachment!")
}
}
func TestEmailAttachment(t *testing.T) {
e := prepareEmail()
_, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
if err != nil {
t.Fatal("Could not add an attachment to the message: ", err)
}
msg := basicTests(t, e)
// Were the right headers set?
ct := msg.Header.Get("Content-type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
t.Fatal("Content-type header is invalid: ", ct)
} else if mt != "multipart/mixed" {
t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
}
b := params["boundary"]
if b == "" {
t.Fatal("Unexpected empty boundary parameter")
}
if len(params) != 1 {
t.Fatal("Unexpected content-type parameters")
}
// Is the generated message parsable?
mixed := multipart.NewReader(msg.Body, params["boundary"])
// Check attachments.
a, err := mixed.NextPart()
if err != nil {
t.Fatalf("Could not find attachment component of email: %s", err)
}
if !strings.HasPrefix(a.Header.Get("Content-Disposition"), "attachment") {
t.Fatalf("Content disposition is not attachment: %s", a.Header.Get("Content-Disposition"))
}
if _, err = mixed.NextPart(); err != io.EOF {
t.Error("Expected only one attachment!")
}
}
func TestHeaderEncoding(t *testing.T) {
cases := []struct {
field string
have string
want string
}{
{
field: "From",
have: "Needs Encóding <encoding@example.com>, Only ASCII <foo@example.com>",
want: "=?utf-8?q?Needs_Enc=C3=B3ding?= <encoding@example.com>, \"Only ASCII\" <foo@example.com>\r\n",
},
{
field: "To",
have: "Keith Moore <moore@cs.utk.edu>, Keld Jørn Simonsen <keld@dkuug.dk>",
want: "\"Keith Moore\" <moore@cs.utk.edu>, =?utf-8?q?Keld_J=C3=B8rn_Simonsen?= <keld@dkuug.dk>\r\n",
},
{
field: "Cc",
have: "Needs Encóding <encoding@example.com>, \"Test :)\" <test@localhost>",
want: "=?utf-8?q?Needs_Enc=C3=B3ding?= <encoding@example.com>, \"Test :)\" <test@localhost>\r\n",
},
{
field: "Subject",
have: "Subject with a 🐟",
want: "=?UTF-8?q?Subject_with_a_=F0=9F=90=9F?=\r\n",
},
{
field: "Subject",
have: "Subject with only ASCII",
want: "Subject with only ASCII\r\n",
},
}
buff := &bytes.Buffer{}
for _, c := range cases {
header := make(textproto.MIMEHeader)
header.Add(c.field, c.have)
buff.Reset()
headerToBytes(buff, header)
want := fmt.Sprintf("%s: %s", c.field, c.want)
got := buff.String()
if got != want {
t.Errorf("invalid utf-8 header encoding. \nwant:%#v\ngot: %#v", want, got)
}
}
}
func TestEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <jmwright798@gmail.com>", "also@example.com"},
From: "Jordan Wright <jmwright798@gmail.com>",
ReplyTo: []string{"Jordan Wright <jmwright798@gmail.com>"},
Cc: []string{"one@example.com", "Two <two@example.com>"},
Bcc: []string{"three@example.com", "Four <four@example.com>"},
Text: []byte("This is a test email with HTML Formatting. It also has very long lines so\nthat the content must be wrapped if using quoted-printable decoding.\n"),
HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: Test Subject
From: Jordan Wright <jmwright798@gmail.com>
Reply-To: Jordan Wright <jmwright798@gmail.com>
To: Jordan Wright <jmwright798@gmail.com>, also@example.com
Cc: one@example.com, Two <two@example.com>
Bcc: three@example.com, Four <four@example.com>
Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
--001a114fb3fc42fd6b051f834280
Content-Type: text/plain; charset=UTF-8
This is a test email with HTML Formatting. It also has very long lines so
that the content must be wrapped if using quoted-printable decoding.
--001a114fb3fc42fd6b051f834280
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
also has very long lines so that the content must be wrapped if using quote=
d-printable decoding.</div>
--001a114fb3fc42fd6b051f834280--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if len(e.To) != len(ex.To) {
t.Fatalf("Incorrect number of \"To\" addresses: %v != %v", len(e.To), len(ex.To))
}
if e.To[0] != ex.To[0] {
t.Fatalf("Incorrect \"To[0]\": %#q != %#q", e.To[0], ex.To[0])
}
if e.To[1] != ex.To[1] {
t.Fatalf("Incorrect \"To[1]\": %#q != %#q", e.To[1], ex.To[1])
}
if len(e.Cc) != len(ex.Cc) {
t.Fatalf("Incorrect number of \"Cc\" addresses: %v != %v", len(e.Cc), len(ex.Cc))
}
if e.Cc[0] != ex.Cc[0] {
t.Fatalf("Incorrect \"Cc[0]\": %#q != %#q", e.Cc[0], ex.Cc[0])
}
if e.Cc[1] != ex.Cc[1] {
t.Fatalf("Incorrect \"Cc[1]\": %#q != %#q", e.Cc[1], ex.Cc[1])
}
if len(e.Bcc) != len(ex.Bcc) {
t.Fatalf("Incorrect number of \"Bcc\" addresses: %v != %v", len(e.Bcc), len(ex.Bcc))
}
if e.Bcc[0] != ex.Bcc[0] {
t.Fatalf("Incorrect \"Bcc[0]\": %#q != %#q", e.Cc[0], ex.Cc[0])
}
if e.Bcc[1] != ex.Bcc[1] {
t.Fatalf("Incorrect \"Bcc[1]\": %#q != %#q", e.Bcc[1], ex.Bcc[1])
}
if len(e.ReplyTo) != len(ex.ReplyTo) {
t.Fatalf("Incorrect number of \"Reply-To\" addresses: %v != %v", len(e.ReplyTo), len(ex.ReplyTo))
}
if e.ReplyTo[0] != ex.ReplyTo[0] {
t.Fatalf("Incorrect \"ReplyTo\": %#q != %#q", e.ReplyTo[0], ex.ReplyTo[0])
}
}
func TestNonAsciiEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Anaïs <anais@example.org>"},
Cc: []string{"Patrik Fältström <paf@example.com>"},
From: "Mrs Valérie Dupont <valerie.dupont@example.com>",
Text: []byte("This is a test message!"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: =?UTF-8?Q?Test Subject?=
From: Mrs =?ISO-8859-1?Q?Val=C3=A9rie=20Dupont?= <valerie.dupont@example.com>
To: =?utf-8?q?Ana=C3=AFs?= <anais@example.org>
Cc: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@example.com>
Content-type: text/plain; charset=ISO-8859-1
This is a test message!`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if e.To[0] != ex.To[0] {
t.Fatalf("Incorrect \"To\": %#q != %#q", e.To, ex.To)
}
if e.Cc[0] != ex.Cc[0] {
t.Fatalf("Incorrect \"Cc\": %#q != %#q", e.Cc, ex.Cc)
}
}
func TestNonMultipartEmailFromReader(t *testing.T) {
ex := &Email{
Text: []byte("This is a test message!"),
Subject: "Example Subject (no MIME Type)",
Headers: textproto.MIMEHeader{},
}
ex.Headers.Add("Content-Type", "text/plain; charset=us-ascii")
ex.Headers.Add("Message-ID", "<foobar@example.com>")
raw := []byte(`From: "Foo Bar" <foobar@example.com>
Content-Type: text/plain
To: foobar@example.com
Subject: Example Subject (no MIME Type)
Message-ID: <foobar@example.com>
This is a test message!`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if ex.Subject != e.Subject {
t.Errorf("Incorrect subject. %#q != %#q\n", ex.Subject, e.Subject)
}
if !bytes.Equal(ex.Text, e.Text) {
t.Errorf("Incorrect body. %#q != %#q\n", ex.Text, e.Text)
}
if ex.Headers.Get("Message-ID") != e.Headers.Get("Message-ID") {
t.Errorf("Incorrect message ID. %#q != %#q\n", ex.Headers.Get("Message-ID"), e.Headers.Get("Message-ID"))
}
}
func TestBase64EmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <jmwright798@gmail.com>"},
From: "Jordan Wright <jmwright798@gmail.com>",
Text: []byte("This is a test email with HTML Formatting. It also has very long lines so that the content must be wrapped if using quoted-printable decoding."),
HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
}
raw := []byte(`
MIME-Version: 1.0
Subject: Test Subject
From: Jordan Wright <jmwright798@gmail.com>
To: Jordan Wright <jmwright798@gmail.com>
Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
--001a114fb3fc42fd6b051f834280
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
VGhpcyBpcyBhIHRlc3QgZW1haWwgd2l0aCBIVE1MIEZvcm1hdHRpbmcuIEl0IGFsc28gaGFzIHZl
cnkgbG9uZyBsaW5lcyBzbyB0aGF0IHRoZSBjb250ZW50IG11c3QgYmUgd3JhcHBlZCBpZiB1c2lu
ZyBxdW90ZWQtcHJpbnRhYmxlIGRlY29kaW5nLg==
--001a114fb3fc42fd6b051f834280
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
also has very long lines so that the content must be wrapped if using quote=
d-printable decoding.</div>
--001a114fb3fc42fd6b051f834280--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
}
func TestAttachmentEmailFromReader(t *testing.T) {
ex := &Email{
Subject: "Test Subject",
To: []string{"Jordan Wright <jmwright798@gmail.com>"},
From: "Jordan Wright <jmwright798@gmail.com>",
Text: []byte("Simple text body"),
HTML: []byte("<div dir=\"ltr\">Simple HTML body</div>\n"),
}
a, err := ex.Attach(bytes.NewReader([]byte("Let's just pretend this is raw JPEG data.")), "cat.jpeg", "image/jpeg")
if err != nil {
t.Fatalf("Error attaching image %s", err.Error())
}
b, err := ex.Attach(bytes.NewReader([]byte("Let's just pretend this is raw JPEG data.")), "cat-inline.jpeg", "image/jpeg")
if err != nil {
t.Fatalf("Error attaching inline image %s", err.Error())
}
raw := []byte(`
From: Jordan Wright <jmwright798@gmail.com>
Date: Thu, 17 Oct 2019 08:55:37 +0100
Mime-Version: 1.0
Content-Type: multipart/mixed;
boundary=35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
To: Jordan Wright <jmwright798@gmail.com>
Subject: Test Subject
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Type: multipart/alternative;
boundary=b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8
Simple text body
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8
<div dir=3D"ltr">Simple HTML body</div>
--b10ca5b1072908cceb667e8968d3af04503b7ab07d61c9f579c15b416d7c--
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: attachment;
filename="cat.jpeg"
Content-Id: <cat.jpeg>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: inline;
filename="cat-inline.jpeg"
Content-Id: <cat-inline.jpeg>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7--`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error creating email %s", err.Error())
}
if e.Subject != ex.Subject {
t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
}
if !bytes.Equal(e.Text, ex.Text) {
t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
}
if !bytes.Equal(e.HTML, ex.HTML) {
t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
}
if e.From != ex.From {
t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
}
if len(e.Attachments) != 2 {
t.Fatalf("Incorrect number of attachments %d != %d", len(e.Attachments), 1)
}
if e.Attachments[0].Filename != a.Filename {
t.Fatalf("Incorrect attachment filename %s != %s", e.Attachments[0].Filename, a.Filename)
}
if !bytes.Equal(e.Attachments[0].Content, a.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[0].Content, a.Content)
}
if e.Attachments[1].Filename != b.Filename {
t.Fatalf("Incorrect attachment filename %s != %s", e.Attachments[1].Filename, b.Filename)
}
if !bytes.Equal(e.Attachments[1].Content, b.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[1].Content, b.Content)
}
}
func ExampleGmail() {
e := NewEmail()
e.From = "Jordan Wright <test@gmail.com>"
e.To = []string{"test@example.com"}
e.Bcc = []string{"test_bcc@example.com"}
e.Cc = []string{"test_cc@example.com"}
e.Subject = "Awesome Subject"
e.Text = []byte("Text Body is, of course, supported!\n")
e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
}
func ExampleAttach() {
e := NewEmail()
e.AttachFile("test.txt")
}
func Test_base64Wrap(t *testing.T) {
file := "I'm a file long enough to force the function to wrap a\n" +
"couple of lines, but I stop short of the end of one line and\n" +
"have some padding dangling at the end."
encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
"dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
"ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
var buf bytes.Buffer
base64Wrap(&buf, []byte(file))
if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
}
}
// *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
func Test_quotedPrintEncode(t *testing.T) {
var buf bytes.Buffer
text := []byte("Dear reader!\n\n" +
"This is a test email to try and capture some of the corner cases that exist within\n" +
"the quoted-printable encoding.\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
expected := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist=\r\n" +
" within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
"s so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
" a fish: =F0=9F=90=9F\r\n")
qp := quotedprintable.NewWriter(&buf)
if _, err := qp.Write(text); err != nil {
t.Fatal("quotePrintEncode: ", err)
}
if err := qp.Close(); err != nil {
t.Fatal("Error closing writer", err)
}
if b := buf.Bytes(); !bytes.Equal(b, expected) {
t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
}
}
func TestMultipartNoContentType(t *testing.T) {
raw := []byte(`From: Mikhail Gusarov <dottedmag@dottedmag.net>
To: notmuch@notmuchmail.org
References: <20091117190054.GU3165@dottiness.seas.harvard.edu>
Date: Wed, 18 Nov 2009 01:02:38 +0600
Message-ID: <87iqd9rn3l.fsf@vertex.dottedmag>
MIME-Version: 1.0
Subject: Re: [notmuch] Working with Maildir storage?
Content-Type: multipart/mixed; boundary="===============1958295626=="
--===============1958295626==
Content-Type: multipart/signed; boundary="=-=-=";
micalg=pgp-sha1; protocol="application/pgp-signature"
--=-=-=
Content-Transfer-Encoding: quoted-printable
Twas brillig at 14:00:54 17.11.2009 UTC-05 when lars@seas.harvard.edu did g=
yre and gimble:
--=-=-=
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iQIcBAEBAgAGBQJLAvNOAAoJEJ0g9lA+M4iIjLYQAKp0PXEgl3JMOEBisH52AsIK
=/ksP
-----END PGP SIGNATURE-----
--=-=-=--
--===============1958295626==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Testing!
--===============1958295626==--
`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error when parsing email %s", err.Error())
}
if !bytes.Equal(e.Text, []byte("Testing!")) {
t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "Testing!")
}
}
func TestNoMultipartHTMLContentTypeBase64Encoding(t *testing.T) {
raw := []byte(`MIME-Version: 1.0
From: no-reply@example.com
To: tester@example.org
Date: 7 Jan 2021 03:07:44 -0800
Subject: Hello
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Message-Id: <20210107110744.547DD70532@example.com>
PGh0bWw+PGhlYWQ+PHRpdGxlPnRlc3Q8L3RpdGxlPjwvaGVhZD48Ym9keT5IZWxsbyB3
b3JsZCE8L2JvZHk+PC9odG1sPg==
`)
e, err := NewEmailFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("Error when parsing email %s", err.Error())
}
if !bytes.Equal(e.HTML, []byte("<html><head><title>test</title></head><body>Hello world!</body></html>")) {
t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "<html>...</html>")
}
}
// *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
func Test_quotedPrintDecode(t *testing.T) {
text := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist=\r\n" +
" within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
"s so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
" a fish: =F0=9F=90=9F\r\n")
expected := []byte("Dear reader!\r\n\r\n" +
"This is a test email to try and capture some of the corner cases that exist within\r\n" +
"the quoted-printable encoding.\r\n" +
"There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
"it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\r\n")
qp := quotedprintable.NewReader(bytes.NewReader(text))
got, err := io.ReadAll(qp)
if err != nil {
t.Fatal("quotePrintDecode: ", err)
}
if !bytes.Equal(got, expected) {
t.Errorf("quotedPrintDecode generated incorrect results: %#q != %#q", got, expected)
}
}
func Benchmark_base64Wrap(b *testing.B) {
// Reasonable base case; 128K random bytes
file := make([]byte, 128*1024)
if _, err := rand.Read(file); err != nil {
panic(err)
}
for i := 0; i <= b.N; i++ {
base64Wrap(io.Discard, file)
}
}
func TestParseSender(t *testing.T) {
var cases = []struct {
e Email
want string
haserr bool
}{
{
Email{From: "from@test.com"},
"from@test.com",
false,
},
{
Email{Sender: "sender@test.com", From: "from@test.com"},
"sender@test.com",
false,
},
{
Email{Sender: "bad_address_sender"},
"",
true,
},
{
Email{Sender: "good@sender.com", From: "bad_address_from"},
"good@sender.com",
false,
},
}
for i, testcase := range cases {
got, err := testcase.e.parseSender()
if got != testcase.want || (err != nil) != testcase.haserr {
t.Errorf(`%d: got %s != want %s or error "%t" != "%t"`, i+1, got, testcase.want, err != nil, testcase.haserr)
}
}
}

367
pkg/email/pool.go Normal file
View File

@@ -0,0 +1,367 @@
package email
import (
"crypto/tls"
"errors"
"io"
"net"
"net/mail"
"net/smtp"
"net/textproto"
"sync"
"syscall"
"time"
)
type Pool struct {
addr string
auth smtp.Auth
max int
created int
clients chan *client
rebuild chan struct{}
mut *sync.Mutex
lastBuildErr *timestampedErr
closing chan struct{}
tlsConfig *tls.Config
helloHostname string
}
type client struct {
*smtp.Client
failCount int
}
type timestampedErr struct {
err error
ts time.Time
}
const maxFails = 4
var (
ErrClosed = errors.New("pool closed")
ErrTimeout = errors.New("timed out")
)
func NewPool(address string, count int, auth smtp.Auth, opt_tlsConfig ...*tls.Config) (pool *Pool, err error) {
pool = &Pool{
addr: address,
auth: auth,
max: count,
clients: make(chan *client, count),
rebuild: make(chan struct{}),
closing: make(chan struct{}),
mut: &sync.Mutex{},
}
if len(opt_tlsConfig) == 1 {
pool.tlsConfig = opt_tlsConfig[0]
} else if host, _, e := net.SplitHostPort(address); e != nil {
return nil, e
} else {
pool.tlsConfig = &tls.Config{ServerName: host}
}
return
}
// go1.1 didn't have this method
func (c *client) Close() error {
return c.Text.Close()
}
// SetHelloHostname optionally sets the hostname that the Go smtp.Client will
// use when doing a HELLO with the upstream SMTP server. By default, Go uses
// "localhost" which may not be accepted by certain SMTP servers that demand
// an FQDN.
func (p *Pool) SetHelloHostname(h string) {
p.helloHostname = h
}
func (p *Pool) get(timeout time.Duration) *client {
select {
case c := <-p.clients:
return c
default:
}
if p.created < p.max {
p.makeOne()
}
var deadline <-chan time.Time
if timeout >= 0 {
deadline = time.After(timeout)
}
for {
select {
case c := <-p.clients:
return c
case <-p.rebuild:
p.makeOne()
case <-deadline:
return nil
case <-p.closing:
return nil
}
}
}
func shouldReuse(err error) bool {
// certainly not perfect, but might be close:
// - EOF: clearly, the connection went down
// - textproto.Errors were valid SMTP over a valid connection,
// but resulted from an SMTP error response
// - textproto.ProtocolErrors result from connections going down,
// invalid SMTP, that sort of thing
// - syscall.Errno is probably down connection/bad pipe, but
// passed straight through by textproto instead of becoming a
// ProtocolError
// - if we don't recognize the error, don't reuse the connection
// A false positive will probably fail on the Reset(), and even if
// not will eventually hit maxFails.
// A false negative will knock over (and trigger replacement of) a
// conn that might have still worked.
if err == io.EOF {
return false
}
switch err.(type) {
case *textproto.Error:
return true
case *textproto.ProtocolError, textproto.ProtocolError:
return false
case syscall.Errno:
return false
default:
return false
}
}
func (p *Pool) replace(c *client) {
p.clients <- c
}
func (p *Pool) inc() bool {
if p.created >= p.max {
return false
}
p.mut.Lock()
defer p.mut.Unlock()
if p.created >= p.max {
return false
}
p.created++
return true
}
func (p *Pool) dec() {
p.mut.Lock()
p.created--
p.mut.Unlock()
select {
case p.rebuild <- struct{}{}:
default:
}
}
func (p *Pool) makeOne() {
go func() {
if p.inc() {
if c, err := p.build(); err == nil {
p.clients <- c
} else {
p.lastBuildErr = &timestampedErr{err, time.Now()}
p.dec()
}
}
}()
}
func startTLS(c *client, t *tls.Config) (bool, error) {
if ok, _ := c.Extension("STARTTLS"); !ok {
return false, nil
}
if err := c.StartTLS(t); err != nil {
return false, err
}
return true, nil
}
func addAuth(c *client, auth smtp.Auth) (bool, error) {
if ok, _ := c.Extension("AUTH"); !ok {
return false, nil
}
if err := c.Auth(auth); err != nil {
return false, err
}
return true, nil
}
func (p *Pool) build() (*client, error) {
cl, err := smtp.Dial(p.addr)
if err != nil {
return nil, err
}
// Is there a custom hostname for doing a HELLO with the SMTP server?
if p.helloHostname != "" {
cl.Hello(p.helloHostname)
}
c := &client{cl, 0}
if _, err := startTLS(c, p.tlsConfig); err != nil {
c.Close()
return nil, err
}
if p.auth != nil {
if _, err := addAuth(c, p.auth); err != nil {
c.Close()
return nil, err
}
}
return c, nil
}
func (p *Pool) maybeReplace(err error, c *client) {
if err == nil {
c.failCount = 0
p.replace(c)
return
}
c.failCount++
if c.failCount >= maxFails {
goto shutdown
}
if !shouldReuse(err) {
goto shutdown
}
if err := c.Reset(); err != nil {
goto shutdown
}
p.replace(c)
return
shutdown:
p.dec()
c.Close()
}
func (p *Pool) failedToGet(startTime time.Time) error {
select {
case <-p.closing:
return ErrClosed
default:
}
if p.lastBuildErr != nil && startTime.Before(p.lastBuildErr.ts) {
return p.lastBuildErr.err
}
return ErrTimeout
}
// Send sends an email via a connection pulled from the Pool. The timeout may
// be <0 to indicate no timeout. Otherwise reaching the timeout will produce
// and error building a connection that occurred while we were waiting, or
// otherwise ErrTimeout.
func (p *Pool) Send(e *Email, timeout time.Duration) (err error) {
start := time.Now()
c := p.get(timeout)
if c == nil {
return p.failedToGet(start)
}
defer func() {
p.maybeReplace(err, c)
}()
recipients, err := addressLists(e.To, e.Cc, e.Bcc)
if err != nil {
return
}
msg, err := e.Bytes()
if err != nil {
return
}
from, err := emailOnly(e.From)
if err != nil {
return
}
if err = c.Mail(from); err != nil {
return
}
for _, recip := range recipients {
if err = c.Rcpt(recip); err != nil {
return
}
}
w, err := c.Data()
if err != nil {
return
}
if _, err = w.Write(msg); err != nil {
return
}
err = w.Close()
return
}
func emailOnly(full string) (string, error) {
addr, err := mail.ParseAddress(full)
if err != nil {
return "", err
}
return addr.Address, nil
}
func addressLists(lists ...[]string) ([]string, error) {
length := 0
for _, lst := range lists {
length += len(lst)
}
combined := make([]string, 0, length)
for _, lst := range lists {
for _, full := range lst {
addr, err := emailOnly(full)
if err != nil {
return nil, err
}
combined = append(combined, addr)
}
}
return combined, nil
}
// Close immediately changes the pool's state so no new connections will be
// created, then gets and closes the existing ones as they become available.
func (p *Pool) Close() {
close(p.closing)
for p.created > 0 {
c := <-p.clients
c.Quit()
p.dec()
}
}

186
pkg/endec/endec.go Normal file
View File

@@ -0,0 +1,186 @@
package endec
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"hash"
)
func RsaParsePubKey(pubKeyPem []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(pubKeyPem)
if block == nil {
return nil, errors.New("invalid rsa public key")
}
pubInfo, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pubKey := pubInfo.(*rsa.PublicKey)
return pubKey, nil
}
func RsaParsePubKeyByPrivKey(privKeyPem []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(privKeyPem)
if block == nil {
return nil, errors.New("invalid rsa private key")
}
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return &privKey.PublicKey, nil
}
func RsaParsePrivKey(privKeyPem []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privKeyPem)
if block == nil {
return nil, errors.New("invalid rsa private key")
}
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privKey, nil
}
func RsaEncrypt(rawData []byte, pubKey *rsa.PublicKey) (encData []byte, err error) {
return rsa.EncryptPKCS1v15(rand.Reader, pubKey, rawData)
}
func RsaDecrypt(encData []byte, privKey *rsa.PrivateKey) (decData []byte, err error) {
return rsa.DecryptPKCS1v15(rand.Reader, privKey, encData)
}
func RsaSign(rawData []byte, privKey *rsa.PrivateKey) (signData []byte, err error) {
msgHash := sha256.New()
_, err = msgHash.Write(rawData)
if err != nil {
return nil, err
}
msgHashSum := msgHash.Sum(nil)
signData, err = rsa.SignPKCS1v15(rand.Reader, privKey, crypto.SHA256, msgHashSum)
if err != nil {
return nil, err
}
return signData, nil
}
func RsaVerify(rawData []byte, signData []byte, pubKey *rsa.PublicKey) (ok bool, err error) {
msgHash := sha256.New()
_, err = msgHash.Write(rawData)
if err != nil {
return false, err
}
msgHashSum := msgHash.Sum(nil)
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, msgHashSum, signData)
if err != nil {
return false, err
}
return true, nil
}
func AesCFBEncrypt(rawData []byte, key []byte, iv []byte) (encData []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
encData = make([]byte, len(rawData))
if iv == nil {
iv = make([]byte, aes.BlockSize)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encData, rawData)
return encData, nil
}
func AesCFBDecrypt(encData []byte, key []byte, iv []byte) (decData []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if iv == nil {
iv = make([]byte, aes.BlockSize)
}
stream := cipher.NewCFBDecrypter(block, iv)
decData = make([]byte, len(encData))
stream.XORKeyStream(decData, encData)
return decData, nil
}
func AesCBCEncrypt(rawData []byte, key []byte, iv []byte) (encData []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
paddingChar := block.BlockSize() - len(rawData)%block.BlockSize()
paddingData := bytes.Repeat([]byte{byte(paddingChar)}, paddingChar)
rawData = append(rawData, paddingData...)
encData = make([]byte, len(rawData))
if iv == nil {
iv = make([]byte, aes.BlockSize)
}
blockMode := cipher.NewCBCEncrypter(block, iv)
blockMode.CryptBlocks(encData, rawData)
return encData, nil
}
func AesCBCDecrypt(encData []byte, key []byte, iv []byte) (decData []byte, err error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if iv == nil {
iv = make([]byte, aes.BlockSize)
}
blockMode := cipher.NewCBCDecrypter(block, iv)
decData = make([]byte, len(encData))
blockMode.CryptBlocks(decData, encData)
paddingChar := int(decData[len(decData)-1])
decData = decData[:len(decData)-paddingChar]
return decData, nil
}
func Sha1Str(inputStr string) string {
h := sha1.New()
return hashStr(h, inputStr)
}
func Sha256Str(inputStr string) string {
h := sha256.New()
return hashStr(h, inputStr)
}
func Md5Str(inputStr string) string {
h := md5.New()
return hashStr(h, inputStr)
}
func hashStr(h hash.Hash, inputStr string) string {
h.Write([]byte(inputStr))
return hex.EncodeToString(h.Sum(nil))
}
func Xor(data []byte, key []byte) {
for i := 0; i < len(data); i++ {
data[i] ^= key[i%len(key)]
}
}
func Hk4eAbilityHashCode(ability string) int32 {
hashCode := int32(0)
for i := 0; i < len(ability); i++ {
hashCode = int32(ability[i]) + 131*hashCode
}
return hashCode
}

29
pkg/endec/endec_test.go Normal file
View File

@@ -0,0 +1,29 @@
package endec
import (
"encoding/base64"
"fmt"
"testing"
)
var key []byte
func init() {
key, _ = base64.StdEncoding.DecodeString("NK6Ucg8hCXN9oPZFojqcAqYEY2DETZzt6oKrGmZwdOU=")
}
func TestAesCBC(t *testing.T) {
raw := []byte("fuck")
enc, _ := AesCBCEncrypt(raw, key, key[0:16])
fmt.Printf("enc: %v\n", enc)
dec, _ := AesCBCDecrypt(enc, key, key[0:16])
fmt.Printf("dec: %v\n", dec)
}
func TestAesCFB(t *testing.T) {
raw := []byte("fuck")
enc, _ := AesCFBEncrypt(raw, key, key[0:16])
fmt.Printf("enc: %v\n", enc)
dec, _ := AesCFBDecrypt(enc, key, key[0:16])
fmt.Printf("dec: %v\n", dec)
}

View File

@@ -0,0 +1,75 @@
package httpclient
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
)
var httpClient http.Client
func init() {
httpClient = http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
Timeout: time.Second * 60,
}
}
func Get[T any](url string, token string) (*T, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if token != "" {
req.Header.Set("Authorization", "Bearer"+" "+token)
}
rsp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
data, err := io.ReadAll(rsp.Body)
_ = rsp.Body.Close()
if err != nil {
return nil, err
}
responseData := new(T)
err = json.Unmarshal(data, responseData)
if err != nil {
return nil, err
}
return responseData, nil
}
func Post[T any](url string, body any, token string) (*T, error) {
reqData, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != "" {
req.Header.Set("Authorization", "Bearer"+" "+token)
}
rsp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
rspData, err := io.ReadAll(rsp.Body)
_ = rsp.Body.Close()
if err != nil {
return nil, err
}
responseData := new(T)
err = json.Unmarshal(rspData, responseData)
if err != nil {
return nil, err
}
return responseData, nil
}

220
pkg/logger/logger.go Normal file
View File

@@ -0,0 +1,220 @@
package logger
import (
"bytes"
"fmt"
"log"
"os"
"path"
"runtime"
"strconv"
"time"
)
const (
DEBUG int = 1
INFO int = 2
ERROR int = 3
UNKNOWN int = 4
)
const (
CONSOLE int = 1
FILE int = 2
BOTH int = 3
NEITHER int = 4
)
var LOG *Logger = nil
type Logger struct {
level int
method int
trackLine bool
file *os.File
logInfoChan chan *LogInfo
}
type LogInfo struct {
logLevel int
msg string
param []any
fileInfo string
funcInfo string
lineInfo int
goroutineId string
}
// 日志配置
type Config struct {
Level string `toml:"level"`
Method string `toml:"method"`
TrackLine bool `toml:"track_line"`
}
func InitLogger(name string, cfg Config) {
log.SetFlags(0)
LOG = new(Logger)
LOG.level = getLevelInt(cfg.Level)
LOG.method = getMethodInt(cfg.Method)
LOG.trackLine = cfg.TrackLine
LOG.logInfoChan = make(chan *LogInfo, 1000)
if LOG.method == FILE || LOG.method == BOTH {
file, err := os.OpenFile("./"+name+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
info := fmt.Sprintf("open log file error: %v\n", err)
panic(info)
}
LOG.file = file
}
go LOG.doLog()
}
func (l *Logger) doLog() {
for {
logInfo := <-l.logInfoChan
timeNow := time.Now()
timeNowStr := timeNow.Format("2006-01-02 15:04:05.000")
logHeader := "[" + timeNowStr + "]" + " " +
"[" + l.getLevelStr(logInfo.logLevel) + "]" + " "
if l.trackLine {
logHeader += "[" +
"line:" + logInfo.fileInfo + ":" + strconv.FormatInt(int64(logInfo.lineInfo), 10) +
" func:" + logInfo.funcInfo +
" goroutine:" + logInfo.goroutineId +
"]" + " "
}
logStr := logHeader + fmt.Sprintf(logInfo.msg, logInfo.param...) + "\n"
red := string([]byte{27, 91, 51, 49, 109})
reset := string([]byte{27, 91, 48, 109})
if l.method == CONSOLE {
if logInfo.logLevel == ERROR {
log.Print(red, logStr, reset)
} else {
log.Print(logStr)
}
} else if l.method == FILE {
_, _ = l.file.WriteString(logStr)
} else if l.method == BOTH {
if logInfo.logLevel == ERROR {
log.Print(red, logStr, reset)
} else {
log.Print(logStr)
}
_, _ = l.file.WriteString(logStr)
}
}
}
func (l *Logger) Debug(msg string, param ...any) {
if l.level > DEBUG {
return
}
logInfo := new(LogInfo)
logInfo.logLevel = DEBUG
logInfo.msg = msg
logInfo.param = param
if l.trackLine {
fileInfo, funcInfo, lineInfo := l.getLineInfo()
logInfo.fileInfo = fileInfo
logInfo.funcInfo = funcInfo
logInfo.lineInfo = lineInfo
logInfo.goroutineId = l.getGoroutineId()
}
l.logInfoChan <- logInfo
}
func (l *Logger) Info(msg string, param ...any) {
if l.level > INFO {
return
}
logInfo := new(LogInfo)
logInfo.logLevel = INFO
logInfo.msg = msg
logInfo.param = param
if l.trackLine {
fileInfo, funcInfo, lineInfo := l.getLineInfo()
logInfo.fileInfo = fileInfo
logInfo.funcInfo = funcInfo
logInfo.lineInfo = lineInfo
logInfo.goroutineId = l.getGoroutineId()
}
l.logInfoChan <- logInfo
}
func (l *Logger) Error(msg string, param ...any) {
if l.level > ERROR {
return
}
logInfo := new(LogInfo)
logInfo.logLevel = ERROR
logInfo.msg = msg
logInfo.param = param
if l.trackLine {
fileInfo, funcInfo, lineInfo := l.getLineInfo()
logInfo.fileInfo = fileInfo
logInfo.funcInfo = funcInfo
logInfo.lineInfo = lineInfo
logInfo.goroutineId = l.getGoroutineId()
}
l.logInfoChan <- logInfo
}
func getLevelInt(level string) (ret int) {
switch level {
case "DEBUG":
ret = DEBUG
case "INFO":
ret = INFO
case "ERROR":
ret = ERROR
default:
ret = UNKNOWN
}
return ret
}
func (l *Logger) getLevelStr(level int) (ret string) {
switch level {
case DEBUG:
ret = "DEBUG"
case INFO:
ret = "INFO"
case ERROR:
ret = "ERROR"
}
return ret
}
func getMethodInt(method string) (ret int) {
switch method {
case "CONSOLE":
ret = CONSOLE
case "FILE":
ret = FILE
case "BOTH":
ret = BOTH
default:
ret = NEITHER
}
return ret
}
func (l *Logger) getGoroutineId() (goroutineId string) {
staticInfo := make([]byte, 32)
runtime.Stack(staticInfo, false)
staticInfo = bytes.TrimPrefix(staticInfo, []byte("goroutine "))
staticInfo = staticInfo[:bytes.IndexByte(staticInfo, ' ')]
goroutineId = string(staticInfo)
return goroutineId
}
func (l *Logger) getLineInfo() (fileName string, funcName string, lineNo int) {
pc, file, line, ok := runtime.Caller(2)
if ok {
fileName = path.Base(file)
funcName = path.Base(runtime.FuncForPC(pc).Name())
lineNo = line
}
return
}

76
pkg/object/object.go Normal file
View File

@@ -0,0 +1,76 @@
package object
import (
"bytes"
"encoding/gob"
"fmt"
)
func DeepCopy(src, dest any) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dest)
}
func ConvBoolToInt64(v bool) int64 {
if v {
return 1
} else {
return 0
}
}
func ConvInt64ToBool(v int64) bool {
if v != 0 {
return true
} else {
return false
}
}
func ConvListToMap[T any](l []T) map[uint64]T {
ret := make(map[uint64]T)
for index, value := range l {
ret[uint64(index)] = value
}
return ret
}
func ConvMapToList[T any](m map[uint64]T) []T {
ret := make([]T, 0)
for _, value := range m {
ret = append(ret, value)
}
return ret
}
func IsUtf8String(value string) bool {
data := []byte(value)
for i := 0; i < len(data); {
str := fmt.Sprintf("%b", data[i])
num := 0
for num < len(str) {
if str[num] != '1' {
break
}
num++
}
if data[i]&0x80 == 0x00 {
i++
continue
} else if num > 2 {
i++
for j := 0; j < num-1; j++ {
if data[i]&0xc0 != 0x80 {
return false
}
i++
}
} else {
return false
}
}
return true
}

57
pkg/random/random.go Normal file
View File

@@ -0,0 +1,57 @@
package random
import (
"encoding/hex"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func GetRandomStr(strLen int) (str string) {
baseStr := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
for i := 0; i < strLen; i++ {
index := rand.Intn(len(baseStr))
str += string(baseStr[index])
}
return str
}
func GetRandomByte(len int) []byte {
ret := make([]byte, 0)
for i := 0; i < len; i++ {
r := uint8(rand.Intn(256))
ret = append(ret, r)
}
return ret
}
func GetRandomByteHexStr(len int) string {
return hex.EncodeToString(GetRandomByte(len))
}
func GetRandomInt32(min int32, max int32) int32 {
if max <= min {
return 0
}
r := rand.Int31n(max-min+1) + min
return r
}
func GetRandomFloat32(min float32, max float32) float32 {
if max <= min {
return 0.0
}
r := rand.Float32()*(max-min) + min
return r
}
func GetRandomFloat64(min float64, max float64) float64 {
if max <= min {
return 0.0
}
r := rand.Float64()*(max-min) + min
return r
}

11
pkg/random/random_test.go Normal file
View File

@@ -0,0 +1,11 @@
package random
import (
"fmt"
"testing"
)
func TestGetRandomStr(t *testing.T) {
str := GetRandomStr(16)
fmt.Println(str)
}

27
pkg/reflection/struct.go Normal file
View File

@@ -0,0 +1,27 @@
package reflection
import "reflect"
func ConvStructToMap(value any) map[string]any {
refType := reflect.TypeOf(value)
if refType.Kind() == reflect.Ptr {
refType = refType.Elem()
}
if refType.Kind() != reflect.Struct {
return nil
}
fieldNum := refType.NumField()
result := make(map[string]any)
nameList := make([]string, 0)
for i := 0; i < fieldNum; i++ {
nameList = append(nameList, refType.Field(i).Name)
}
refValue := reflect.ValueOf(value)
if refValue.Kind() == reflect.Ptr {
refValue = refValue.Elem()
}
for i := 0; i < fieldNum; i++ {
result[nameList[i]] = refValue.FieldByName(nameList[i]).Interface()
}
return result
}