Skip to content

Commit

Permalink
Merge branch 'tierpod:master' into upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
hdholm authored Mar 9, 2024
2 parents de5d329 + b4ccc49 commit 42b9e5e
Show file tree
Hide file tree
Showing 147 changed files with 52,629 additions and 984 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:

steps:

- name: Set up Go 1.20
uses: actions/setup-go@v3
- name: Set up Go 1.22
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.22"
id: go

- name: Check out code into the Go module directory
Expand All @@ -31,7 +31,7 @@ jobs:
make release
- name: Create release and upload assets if tag is defined
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ GIT_VER := $(shell basename $(GITHUB_REF))-$(shell date +%Y%m%d)
endif
LDFLAGS := -ldflags "-X main.version=$(GIT_VER)"

CGO_ENABLED := 0

.PHONY: test
test:
find ./cmd ./pkg -type f -name '*.go' | xargs gofmt -l -e
Expand All @@ -17,10 +19,10 @@ test:
go test -mod=vendor ./cmd/... ./pkg/...

.PHONY: build
build: test $(NAME)
build: test bin/$(NAME)

bin/$(NAME):
go build -mod=vendor -v $(LDFLAGS) -o $@ ./cmd/$(NAME)
CGO_ENABLED=$(CGO_ENABLED) go build -mod=vendor -v $(LDFLAGS) -o $@ ./cmd/$(NAME)

.PHONY: clean
clean:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Support input formats:

* **.zip** file: zipped dmarc report in xml format

* **.eml** file: an electronic mail format or email saved in plain text - dovecot-report-converter
tries to extract .xml, .gz or .zip attachments from found eml files to `input.dir`

Support output formats:

* **html_static** output file is a HTML, generated from builtin template htmlStaticTmpl (consts.go).
Expand Down Expand Up @@ -185,17 +188,17 @@ Building from sources
2. Download sources:

```bash
go get -u github.com/tierpod/dmarc-report-converter
git clone https://github.com/tierpod/dmarc-report-converter.git
```

3. Build binary and create installation archive:

```bash
cd $HOME/go/src/github.com/tierpod/dmarc-report-converter
cd dmarc-report-converter
make release
```

4. Installation archive will be places inside _release_ directory. Also, if you want to test
4. Installation archive will be places inside _dist_ directory. Also, if you want to test
dmarc-report-converter without installation, you can execute:

```bash
Expand Down
6 changes: 3 additions & 3 deletions cmd/dmarc-report-converter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"time"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (o *Output) isStdout() bool {
func loadConfig(path string) (*config, error) {
var c config

data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +88,7 @@ func loadConfig(path string) (*config, error) {
if c.Output.ExternalTemplate == "" {
return nil, fmt.Errorf("'output.external_template' must be configured to use external_template output")
}
data, err := ioutil.ReadFile(c.Output.ExternalTemplate)
data, err := os.ReadFile(c.Output.ExternalTemplate)
if err != nil {
return nil, err
}
Expand Down
31 changes: 31 additions & 0 deletions cmd/dmarc-report-converter/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ func (c *filesConverter) ConvertWrite() error {
}

func (c *filesConverter) find() error {
emlFiles, err := filepath.Glob(filepath.Join(c.cfg.Input.Dir, "*.eml"))
if err != nil {
return err
}
if len(emlFiles) > 0 {
log.Printf("[INFO] files: found %d eml file(s), extract attachments to %v", len(emlFiles), c.cfg.Input.Dir)
for _, eml := range emlFiles {
br, err := os.Open(eml)
if err != nil {
log.Printf("[ERROR] files: unable to extract attachments from %v", eml)
continue
}

isSuccess, err := extractAttachment(br, c.cfg.Input.Dir)
if err != nil {
log.Printf("[ERROR] files: %v, skip", err)
continue
}

if isSuccess && c.cfg.Input.Delete {
log.Printf("[DEBUG] files: delete %v", eml)
err := os.Remove(eml)
if err != nil {
log.Printf("[ERROR] files: %v", err)
continue
}
}
br.Close()
}
}

files, err := filepath.Glob(filepath.Join(c.cfg.Input.Dir, "*.*"))
if err != nil {
return err
Expand Down
107 changes: 61 additions & 46 deletions cmd/dmarc-report-converter/imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,53 +86,10 @@ func fetchIMAPAttachments(cfg *config) error {
return fmt.Errorf("server didn't return message body")
}

// create a new mail reader
mr, err := mail.CreateReader(br)
isSuccess, err := extractAttachment(br, cfg.Input.Dir)
if err != nil {
return err
}

// process each message's part
isSuccess := false
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Printf("[ERROR] imap: can't read next part: %v, skip", err)
break
}

switch h := p.Header.(type) {
case mail.AttachmentHeader:
// this is an attachment
filename, err := h.Filename()
if err != nil {
log.Printf("[ERROR] imap: %v, skip", err)
continue
}
log.Printf("[INFO] imap: found attachment: %v", filename)

outFile := filepath.Join(cfg.Input.Dir, filename)
log.Printf("[INFO] imap: save attachment to: %v", outFile)
f, err := os.Create(outFile)
if err != nil {
log.Printf("[ERROR] imap: %v, skip", err)
continue
}

_, err = io.Copy(f, p.Body)
if err != nil {
log.Printf("[ERROR] imap: %v, skip", err)
continue
}
err = f.Close()
if err != nil {
log.Printf("[ERROR] imap: %v, skip", err)
continue
}
isSuccess = true
}
log.Printf("[ERROR] imap: %v, skip", err)
continue
}

if isSuccess && cfg.Input.IMAP.Delete {
Expand Down Expand Up @@ -168,3 +125,61 @@ func fetchIMAPAttachments(cfg *config) error {

return nil
}

func extractAttachment(r io.Reader, inputDir string) (bool, error) {
// Create a new mail reader
mr, err := mail.CreateReader(r)
if err != nil {
return false, err
}

// process each message's part
isSuccess := false
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Printf("[ERROR] imap: can't read next part: %v, skip", err)
break
}

switch h := p.Header.(type) {
case *mail.AttachmentHeader:
// this is an attachment
filename, err := h.Filename()
if err != nil {
log.Printf("[ERROR] extractAttachment: %v, skip", err)
continue
}
if filename == "" {
log.Printf("[WARN] extractAttachment: found attachment with empty filename, skip")
continue
}

log.Printf("[INFO] extractAttachment: found attachment: %v", filename)

outFile := filepath.Join(inputDir, filename)
log.Printf("[INFO] extractAttachment: save attachment to: %v", outFile)
f, err := os.Create(outFile)
if err != nil {
log.Printf("[ERROR] extractAttachment: %v, skip", err)
continue
}

_, err = io.Copy(f, p.Body)
if err != nil {
log.Printf("[ERROR] extractAttachment: %v, skip", err)
continue
}
err = f.Close()
if err != nil {
log.Printf("[ERROR] extractAttachment: %v, skip", err)
continue
}
isSuccess = true
}
}

return isSuccess, err
}
1 change: 1 addition & 0 deletions cmd/dmarc-report-converter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"

_ "github.com/emersion/go-message/charset"
"github.com/hashicorp/logutils"
)

Expand Down
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/tierpod/dmarc-report-converter
go 1.13

require (
github.com/emersion/go-imap v1.0.0-beta.1.0.20180319181901-b63c7c7011cb
github.com/emersion/go-message v0.9.2-0.20180321173051-51445bfdc558
github.com/emersion/go-sasl v0.0.0-20161116183048-7e096a0a6197 // indirect
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe // indirect
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3
golang.org/x/text v0.3.8 // indirect
gopkg.in/yaml.v2 v2.2.1
github.com/emersion/go-imap v1.2.1
github.com/emersion/go-message v0.18.0
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
github.com/hashicorp/logutils v1.0.0
gopkg.in/yaml.v2 v2.4.0
)
38 changes: 24 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
github.com/emersion/go-imap v1.0.0-beta.1.0.20180319181901-b63c7c7011cb h1:vrU9clcMQcyh/5CEKvIpig1zRmtDmyMcBY08MhhpvD8=
github.com/emersion/go-imap v1.0.0-beta.1.0.20180319181901-b63c7c7011cb/go.mod h1:oydmHwiyv92ZOiNfQY9BDax5heePWN8P2+W1B2T6qjc=
github.com/emersion/go-message v0.9.2-0.20180321173051-51445bfdc558 h1:lPY6awq1rK4xEF2Qfw7vV2HczOWQRul8v8xEmTtRxDs=
github.com/emersion/go-message v0.9.2-0.20180321173051-51445bfdc558/go.mod h1:m3cK90skCWxm5sIMs1sXxly4Tn9Plvcf6eayHZJ1NzM=
github.com/emersion/go-sasl v0.0.0-20161116183048-7e096a0a6197 h1:rDJPbyliyym8ZL/Wt71kdolp6yaD4fLIQz638E6JEt0=
github.com/emersion/go-sasl v0.0.0-20161116183048-7e096a0a6197/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg=
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3 h1:oD64EFjELI9RY9yoWlfua58r+etdnoIC871z+rr6lkA=
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/emersion/go-imap v1.2.1 h1:+s9ZjMEjOB8NzZMVTM3cCenz2JrQIGGo5j1df19WjTA=
github.com/emersion/go-imap v1.2.1/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
github.com/emersion/go-message v0.18.0 h1:7LxAXHRpSeoO/Wom3ZApVZYG7c3d17yCScYce8WiXA8=
github.com/emersion/go-message v0.18.0/go.mod h1:Zi69ACvzaoV/MBnrxfVBPV3xWEuCmC2nEN39oJF4B8A=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
6 changes: 5 additions & 1 deletion pkg/dmarc/dmarc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (r *Report) CalculateStats() {
}
}
s.Failed = s.All - s.Passed
s.PassedPercent = math.Round((float64(s.Passed) / float64(s.All)) * 100)
if s.All == 0 {
s.PassedPercent = 0
} else {
s.PassedPercent = math.Round((float64(s.Passed) / float64(s.All)) * 100)
}

r.MessagesStats = *s
}
Expand Down
Loading

0 comments on commit 42b9e5e

Please sign in to comment.