Skip to content

Commit

Permalink
refacto: tool check
Browse files Browse the repository at this point in the history
  • Loading branch information
mlcdf committed Dec 10, 2022
1 parent 77705bd commit 4855200
Showing 1 changed file with 56 additions and 79 deletions.
135 changes: 56 additions & 79 deletions internal/command/tool_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ func (c *CheckCommand) Synopsis() string {
return "Perform various check on your website"
}

type checks struct {
ipv4 string
ipv6 string

protocol string
validCert string
enforceHTTPS string
custom404 string
}

type setCheck func(c *checks)

func (c *CheckCommand) Run(args []string) int {
var hosting string
var domain string
Expand Down Expand Up @@ -84,57 +72,60 @@ func (c *CheckCommand) Run(args []string) int {
}
}

fc := make(chan setCheck, 5)
ch := &check{wg: new(sync.WaitGroup), httpClient: c.HTTPClient}
ch.wg.Add(5)

wg := new(sync.WaitGroup)
wg.Add(5)

go checkRedirect(fc, wg, domain)
go checkValidCert(fc, wg, domain)
go checkProtocol(fc, wg, domain)
go checkCustom404Page(fc, wg, c.HTTPClient, domain)
go ch.checkEnforceHTTPS(domain)
go ch.checkValidCert(domain)
go ch.checkProtocol(domain)
go ch.checCustom404(domain)

hostingInfo, err := client.GetHosting(hosting)
if err != nil {
return c.View.PrintErr(err)
}

go checkIP(fc, wg, hostingInfo, domain)
go ch.checkIP(hostingInfo, domain)

wg.Wait()
close(fc)

_checks := &checks{}
for ch := range fc {
ch(_checks)
}
ch.wg.Wait()

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()

fmt.Fprintf(w, cmdutil.Bold("DNS")+"\t\t\n")
fmt.Fprintf(w, " Record A \t=\t%s\n", _checks.ipv4)
fmt.Fprintf(w, " Record AAAA \t=\t%s\n", _checks.ipv6)
fmt.Fprintf(w, " Record A \t=\t%s\n", ch.ipv4)
fmt.Fprintf(w, " Record AAAA \t=\t%s\n", ch.ipv6)
fmt.Fprintf(w, "\t\t\n")
fmt.Fprintf(w, cmdutil.Bold("HTTP")+"\t\t\n")

fmt.Fprintf(w, " Protocol\t=\t%s\n", _checks.protocol)
fmt.Fprintf(w, " Valid certificate\t=\t%s\n", _checks.validCert)
fmt.Fprintf(w, " Enforce HTTPS\t=\t%s\n", _checks.enforceHTTPS)
fmt.Fprintf(w, " Custom 404 page\t=\t%s\n", _checks.custom404)
fmt.Fprintf(w, " Protocol\t=\t%s\n", ch.protocol)
fmt.Fprintf(w, " Valid certificate\t=\t%s\n", ch.validCert)
fmt.Fprintf(w, " Enforce HTTPS\t=\t%s\n", ch.enforceHTTPS)
fmt.Fprintf(w, " Custom 404 page\t=\t%s\n", ch.custom404)

return 0
}

func checkIP(e chan setCheck, wg *sync.WaitGroup, hosting *api.HostingInfo, domain string) {
defer wg.Done()
type check struct {
wg *sync.WaitGroup
httpClient *http.Client

ipv4 string
ipv6 string

protocol string
validCert string
enforceHTTPS string
custom404 string
}

func (c *check) checkIP(hosting *api.HostingInfo, domain string) {
defer c.wg.Done()

ips, err := net.LookupIP(domain)
if err != nil {
e <- func(c *checks) {
c.ipv4 = "failed to lookup IP"
c.ipv6 = "failed to lookup IP"
}
c.ipv4 = "failed to lookup IP"
c.ipv6 = "failed to lookup IP"
return
}

Expand All @@ -155,10 +146,8 @@ func checkIP(e chan setCheck, wg *sync.WaitGroup, hosting *api.HostingInfo, doma
}
}

e <- func(c *checks) {
c.ipv4 = yesno(ipv4)
c.ipv6 = yesno(ipv6)
}
c.ipv4 = yesno(ipv4)
c.ipv6 = yesno(ipv6)
}

func yesno(value bool) string {
Expand All @@ -169,95 +158,83 @@ func yesno(value bool) string {
return "no"
}

func checkValidCert(e chan setCheck, wg *sync.WaitGroup, domain string) {
defer wg.Done()
func (c *check) checkValidCert(domain string) {
defer c.wg.Done()

conn, err := tls.Dial("tcp", fmt.Sprintf("%s:443", domain), nil)
if err == nil {
err = conn.VerifyHostname(domain)
if err != nil {
e <- func(c *checks) {
c.validCert = fmt.Sprintf("no, hostname doesn't match: %s", err)
}
return
c.validCert = fmt.Sprintf("no, hostname doesn't match: %s", err)
}

e <- func(c *checks) {
c.validCert = "yes"
}
c.validCert = "yes"
return
}

e <- func(c *checks) {
c.validCert = fmt.Sprintf("no, %s", err)
}
c.validCert = fmt.Sprintf("no, %s", err)
}

func checkRedirect(e chan setCheck, wg *sync.WaitGroup, domain string) {
defer wg.Done()
func (c *check) checkEnforceHTTPS(domain string) {
defer c.wg.Done()

url := fmt.Sprintf("http://%s", domain)

noRedirectClient := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
noRedirectClient := c.httpClient
noRedirectClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { return nil }

res, err := noRedirectClient.Get(url)
if err != nil {
e <- func(c *checks) { c.enforceHTTPS = err.Error() }
c.enforceHTTPS = err.Error()
return
}

res.Body.Close()

if (res.StatusCode == 301 || res.StatusCode == 302) &&
strings.Contains(res.Header.Get("location"), fmt.Sprintf("https://%s", domain)) {
e <- func(c *checks) { c.enforceHTTPS = "yes" }
c.enforceHTTPS = "yes"
return
}

e <- func(c *checks) { c.enforceHTTPS = "no" }
c.enforceHTTPS = "no"
}

func checkProtocol(e chan setCheck, wg *sync.WaitGroup, domain string) {
defer wg.Done()
func (c *check) checkProtocol(domain string) {
defer c.wg.Done()

res, err := http.Get("https://" + domain)
if err != nil {
e <- func(c *checks) { c.protocol = err.Error() }
c.protocol = err.Error()
return
}
res.Body.Close()

e <- func(c *checks) {
c.protocol = res.Proto
}
c.protocol = res.Proto
}

func checkCustom404Page(e chan setCheck, wg *sync.WaitGroup, httpClient *http.Client, domain string) {
defer wg.Done()
func (c *check) checCustom404(domain string) {
defer c.wg.Done()

res, err := httpClient.Get(fmt.Sprintf("https://%s/thispagedoesnotexists", domain))
res, err := c.httpClient.Get(fmt.Sprintf("https://%s/thispagedoesnotexists", domain))
if err != nil {
e <- func(c *checks) { c.custom404 = err.Error() }
c.custom404 = err.Error()
return
}

defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
e <- func(c *checks) { c.custom404 = err.Error() }
c.custom404 = err.Error()
return
}

if strings.Contains(string(body), "<title>404 Not Found</title>") &&
strings.Contains(string(body), "<p>The requested URL was not found on this server.</p>") {
e <- func(c *checks) { c.custom404 = "no" }
c.custom404 = "no"
return
}

e <- func(c *checks) { c.custom404 = "yes" }
c.custom404 = "yes"
}

0 comments on commit 4855200

Please sign in to comment.