Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
MX record and TCP port checks added
Browse files Browse the repository at this point in the history
  • Loading branch information
MFAshby committed May 6, 2019
1 parent 8bd2fa3 commit 23d0f8a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
30 changes: 30 additions & 0 deletions templates/healthchecks.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
{{ define "content" }}
<div>
<h3>TCP ports</h3>
<p>
{{ if eq "" .FailingPorts }}
All the required ports are open &#x2714;
{{ else }}
Some ports are not open &#x2717;
check your firewall for ports {{ .FailingPorts }}
{{ end }}
</p>
<h3>DNS records</h3>
<h4>MX</h4>
<p>
{{ if eq .MxRecordShouldBe .MxRecordIs }}
Your MX record is OK &#x2714;
{{ else }}
Your MX record is wrong &#x2717;<br/>
It should be:
<form class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="mx-host">Host</label>
<input id="mx-host" class="copyable" readonly type="text" value="@"/>
</div>

<div class="pure-control-group">
<label for="mx-value">Value</label>
<input id="mx-value" class="copyable" readonly type="text" value="{{.MxRecordShouldBe}}"/>
</div>
</form>
{{ end }}
</p>
<h4>DKIM</h4>
<p>
{{ if eq .DkimRecordShouldBe .DkimRecordIs }}
Expand Down
47 changes: 47 additions & 0 deletions web/healthchecks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"fmt"
"henrymail/config"
"henrymail/dkim"
"henrymail/models"
Expand All @@ -26,19 +27,27 @@ func (wa *wa) healthChecks(w http.ResponseWriter, r *http.Request, u *models.Use
return
}
dkimActual := fetchDkimActual()
mxActual := fetchMxActual()
mxExpected := config.GetString(config.ServerName) + "." // MX records expect a trailing dot

data := struct {
layoutData
DkimRecordIs string
DkimRecordShouldBe string
SpfRecordIs string
SpfRecordShouldBe string
MxRecordIs string
MxRecordShouldBe string
FailingPorts string
}{
layoutData: *ld,
DkimRecordIs: dkimActual,
DkimRecordShouldBe: dkimExpected,
SpfRecordIs: spfActual,
SpfRecordShouldBe: spfExpected,
MxRecordIs: mxActual,
MxRecordShouldBe: mxExpected,
FailingPorts: fetchFailingPorts(),
}
wa.healthChecksView.render(w, data)
}
Expand Down Expand Up @@ -69,3 +78,41 @@ func fetchSpfActual() string {
}
return spfActual
}

func fetchMxActual() string {
mxActual := ""
mxes, e := net.LookupMX(config.GetString(config.Domain))
if e != nil {
mxActual = e.Error()
} else {
mxesLen := len(mxes)
if mxesLen != 1 {
// Should be just 1 MX record
mxActual = fmt.Sprintf("Expecting exactly 1 mx record, found %d", mxesLen)
} else {
// MX record should be referring to this server
mxActual = mxes[0].Host
}
}
return mxActual
}

func fetchFailingPorts() string {
// Should be able to open a socket on these ports, to ourselves
portsTest := []string{
"25",
"143",
"443",
"587",
}
var failedPorts []string
for _, port := range portsTest {
con, e := net.Dial("tcp", config.GetString(config.ServerName)+":"+port)
if e != nil {
failedPorts = append(failedPorts, port)
} else {
_ = con.Close()
}
}
return strings.Join(failedPorts, ",")
}

0 comments on commit 23d0f8a

Please sign in to comment.