updated functions, templates

This commit is contained in:
Paul Lecuq 2020-01-20 13:57:11 +01:00
parent bbc0b707e8
commit f77f3f95a9
4 changed files with 16 additions and 18 deletions

View File

@ -2,7 +2,7 @@ package main
import (
"flag"
"fmt"
"fmt"
"net"
"net/http"
"os"
@ -15,7 +15,7 @@ import (
)
// GetIPInfo returns IP address informations
func GetIPInfo(c echo.Context) (ip IP, err error) {
func (ip *IP) GetIPInfo(c echo.Context) (err error) {
if c.QueryParam("ip") != "" {
ip.IP = c.QueryParam("ip")
} else {
@ -24,26 +24,25 @@ func GetIPInfo(c echo.Context) (ip IP, err error) {
err = ip.CheckIPAddress()
if err != nil {
log.Println(fmt.Sprintf("Error checking IP address %s",ip.IP))
return err
}
err = ip.GetHostname()
if err != nil {
log.Println(fmt.Sprintf("Error checking revers DNS for IP address %s",ip.IP))
log.Println(err)
}
return
}
// Dip return webpage
// Dip returns webpage
func Dip(c echo.Context) (err error) {
var ip IP
ip, err = GetIPInfo(c)
fmt.Println(err)
if err != nil {
return c.Render(http.StatusInternalServerError, "error.html", nil)
}
err = ip.GetIPInfo(c)
if err != nil {
return c.Render(http.StatusInternalServerError, "error.html", err)
}
if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) {
return c.Render(http.StatusOK, "index.html", ip)
@ -62,7 +61,7 @@ func Static(c echo.Context) (err error) {
func (ip *IP) CheckIPAddress() (err error) {
addr := net.ParseIP(ip.IP)
if addr == nil {
return fmt.Errorf("Supplied string isn't an IP address")
return fmt.Errorf("Supplied string isn't an IP address \"%s\"", ip.IP)
}
return
}
@ -71,7 +70,7 @@ func (ip *IP) CheckIPAddress() (err error) {
func (ip *IP) GetHostname() (err error) {
revip, err := net.LookupAddr(ip.IP)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Supplied address %s doesn't have a valid reverse DNS entry", ip.IP))
return fmt.Errorf(fmt.Sprintf("Supplied address \"%s\" doesn't have a valid reverse DNS entry", ip.IP))
}
ip.Hostname = revip[0]
return

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"html/template"
"io"
"strings"
@ -32,7 +31,6 @@ func BuildTemplates(box packr.Box) (builttemplates *Template, err error) {
tmpl := template.New("template")
for _, filename := range box.List() {
fmt.Println(filename)
tmplContent, _ := box.FindString(filename)
tmpl.New(filename).Parse(tmplContent)
}

View File

@ -4,5 +4,6 @@
</head>
<body>
<h1>Error</h1>
<p>{{ . }} isn't a IP address</p>
</body>
</html>

View File

@ -4,7 +4,7 @@
{{ template "head.html" .}}
</head>
<body>
<h1>IP: {{ .IP }}</h1>
<h2>Reverse DNS: {{ .Hostname }}</h2>
<h1>IP : {{ .IP }}</h1>
<h2>Reverse DNS : {{ .Hostname }}</h2>
</body>
</html>