2019-12-22 18:20:45 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-01-21 18:42:34 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-12-22 18:20:45 +01:00
|
|
|
"net"
|
2020-01-19 17:30:10 +01:00
|
|
|
"net/http"
|
2019-12-22 18:20:45 +01:00
|
|
|
"os"
|
2020-01-19 17:30:10 +01:00
|
|
|
"strings"
|
2019-12-22 18:20:45 +01:00
|
|
|
|
2020-01-26 16:38:57 +01:00
|
|
|
"github.com/gobuffalo/packr/v2"
|
2019-12-22 18:20:45 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/likexian/whois-go"
|
|
|
|
whoisparser "github.com/likexian/whois-parser-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetIPInfo returns IP address informations
|
2020-01-20 13:57:11 +01:00
|
|
|
func (ip *IP) GetIPInfo(c echo.Context) (err error) {
|
2019-12-22 18:20:45 +01:00
|
|
|
if c.QueryParam("ip") != "" {
|
|
|
|
ip.IP = c.QueryParam("ip")
|
|
|
|
} else {
|
|
|
|
ip.IP = c.RealIP()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ip.CheckIPAddress()
|
|
|
|
if err != nil {
|
2020-01-21 18:42:34 +01:00
|
|
|
return err
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ip.GetHostname()
|
|
|
|
if err != nil {
|
2020-01-21 18:42:34 +01:00
|
|
|
log.Println(err)
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:57:11 +01:00
|
|
|
// Dip returns webpage
|
2020-01-19 17:30:10 +01:00
|
|
|
func Dip(c echo.Context) (err error) {
|
2019-12-22 18:20:45 +01:00
|
|
|
var ip IP
|
2020-01-19 17:30:10 +01:00
|
|
|
|
2020-01-21 18:42:34 +01:00
|
|
|
ip.GetIPInfo(c)
|
2019-12-22 18:20:45 +01:00
|
|
|
|
2020-01-19 17:30:10 +01:00
|
|
|
if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) {
|
|
|
|
return c.Render(http.StatusOK, "index.html", ip)
|
|
|
|
} else {
|
|
|
|
return c.JSON(http.StatusOK, ip)
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:38:57 +01:00
|
|
|
// Static ...
|
|
|
|
func Static(box *packr.Box, c echo.Context) (err error) {
|
|
|
|
name := c.Param("*")
|
|
|
|
cnt, _ := box.FindString(name)
|
|
|
|
if strings.HasSuffix(name, ".js") {
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript)
|
|
|
|
} else if strings.HasSuffix(name, ".css") {
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, "text/css")
|
|
|
|
}
|
|
|
|
return c.String(http.StatusOK, cnt)
|
2020-01-19 17:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckIPAddress check if ip is an IP address, return err if there's an error
|
2019-12-22 18:20:45 +01:00
|
|
|
func (ip *IP) CheckIPAddress() (err error) {
|
|
|
|
addr := net.ParseIP(ip.IP)
|
|
|
|
if addr == nil {
|
2020-01-20 13:57:11 +01:00
|
|
|
return fmt.Errorf("Supplied string isn't an IP address \"%s\"", ip.IP)
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHostname assign ip.Hostname value
|
|
|
|
func (ip *IP) GetHostname() (err error) {
|
|
|
|
revip, err := net.LookupAddr(ip.IP)
|
|
|
|
if err != nil {
|
2020-01-21 18:42:34 +01:00
|
|
|
return err
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|
|
|
|
ip.Hostname = revip[0]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:30:10 +01:00
|
|
|
// GetWhois not implemented yet
|
2019-12-22 18:20:45 +01:00
|
|
|
func (ip *IP) GetWhois() (err error) {
|
2020-01-19 17:30:10 +01:00
|
|
|
resultraw, err := whois.Whois("example.com")
|
2019-12-22 18:20:45 +01:00
|
|
|
//resultraw, err := whois.Whois(ip.IP)
|
|
|
|
fmt.Println(resultraw)
|
|
|
|
result, err := whoisparser.Parse(resultraw)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage displays possible arguments
|
|
|
|
func Usage() {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:30:10 +01:00
|
|
|
// IP defines dip main struct
|
2019-12-22 18:20:45 +01:00
|
|
|
type IP struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Hostname string `json:"hostname"`
|
2020-01-26 16:38:57 +01:00
|
|
|
City string `json:"city"`
|
|
|
|
Country string `json:"country"`
|
2019-12-22 18:20:45 +01:00
|
|
|
}
|