mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2024-12-22 07:28:00 +00:00
Add CI and go-report badges. Add comments to make golint happy
This commit is contained in:
parent
9b10052cd1
commit
2d330a99b7
@ -1,5 +1,8 @@
|
||||
# What is my IP address
|
||||
|
||||
[![CI](https://github.com/dcarrillo/whatismyip/workflows/CI/badge.svg)](https://github.com/dcarrillo/whatismyip/actions)
|
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/dcarrillo/whatismyip)](https://goreportcard.com/report/github.com/dcarrillo/whatismyip)
|
||||
|
||||
- [What is my IP address](#what-is-my-ip-address)
|
||||
- [Features](#features)
|
||||
- [Endpoints](#endpoints)
|
||||
|
@ -1,3 +1,4 @@
|
||||
package core
|
||||
|
||||
// Version to be defined on build time
|
||||
var Version = "tobedefined"
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HeadersToSortedString shorts and dumps http.Header to a string separated by \n
|
||||
func HeadersToSortedString(headers http.Header) string {
|
||||
var output string
|
||||
|
||||
@ -31,6 +32,7 @@ func HeadersToSortedString(headers http.Header) string {
|
||||
return output
|
||||
}
|
||||
|
||||
// GetLogFormatter returns our custom log format
|
||||
func GetLogFormatter(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n",
|
||||
param.ClientIP,
|
||||
@ -57,9 +59,8 @@ func normalizeLog(log interface{}) interface{} {
|
||||
case []string:
|
||||
if len(v) == 0 {
|
||||
return "-"
|
||||
} else {
|
||||
return strings.Join(v, ", ")
|
||||
}
|
||||
return strings.Join(v, ", ")
|
||||
}
|
||||
|
||||
return log
|
||||
|
@ -33,7 +33,10 @@ type settings struct {
|
||||
|
||||
const defaultAddress = ":8080"
|
||||
|
||||
// ErrVersion is the custom error triggered when -version flag is passed
|
||||
var ErrVersion = errors.New("setting: version requested")
|
||||
|
||||
// App is the var with the parsed settings
|
||||
var App = settings{
|
||||
// hard-coded for the time being
|
||||
Server: serverSettings{
|
||||
@ -42,6 +45,7 @@ var App = settings{
|
||||
},
|
||||
}
|
||||
|
||||
// Setup initializes the App object parsing the flags
|
||||
func Setup(args []string) (output string, err error) {
|
||||
flags := flag.NewFlagSet("whatismyip", flag.ContinueOnError)
|
||||
var buf bytes.Buffer
|
||||
|
@ -7,10 +7,12 @@ import (
|
||||
"github.com/oschwald/maxminddb-golang"
|
||||
)
|
||||
|
||||
// Record is the interface to be implemented for record operations
|
||||
type Record interface {
|
||||
LookUp(ip net.IP)
|
||||
}
|
||||
|
||||
// GeoRecord is the model for City database
|
||||
type GeoRecord struct {
|
||||
Country struct {
|
||||
ISOCode string `maxminddb:"iso_code"`
|
||||
@ -29,6 +31,7 @@ type GeoRecord struct {
|
||||
} `maxminddb:"postal"`
|
||||
}
|
||||
|
||||
// ASNRecord is the model for ASN database
|
||||
type ASNRecord struct {
|
||||
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
|
||||
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
|
||||
@ -51,11 +54,13 @@ func openMMDB(path string) *maxminddb.Reader {
|
||||
return db
|
||||
}
|
||||
|
||||
// Setup opens all Geolite2 databases
|
||||
func Setup(cityPath string, asnPath string) {
|
||||
db.city = openMMDB(cityPath)
|
||||
db.asn = openMMDB(asnPath)
|
||||
}
|
||||
|
||||
// CloseDBs unmaps from memory and frees resources to the filesystem
|
||||
func CloseDBs() {
|
||||
log.Printf("Closing dbs...")
|
||||
if err := db.city.Close(); err != nil {
|
||||
@ -66,6 +71,7 @@ func CloseDBs() {
|
||||
}
|
||||
}
|
||||
|
||||
// LookUp an IP and get city data
|
||||
func (record *GeoRecord) LookUp(ip net.IP) error {
|
||||
err := db.city.Lookup(ip, record)
|
||||
if err != nil {
|
||||
@ -75,6 +81,7 @@ func (record *GeoRecord) LookUp(ip net.IP) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LookUp an IP and get ASN data
|
||||
func (record *ASNRecord) LookUp(ip net.IP) error {
|
||||
err := db.asn.Lookup(ip, record)
|
||||
if err != nil {
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
|
||||
const userAgentPattern = `curl|wget|libwww-perl|python|ansible-httpget|HTTPie|WindowsPowerShell|http_request|Go-http-client|^$`
|
||||
|
||||
// JSONResponse maps data as json
|
||||
type JSONResponse struct {
|
||||
IP string `json:"ip"`
|
||||
IPVersion byte `json:"ip_version"`
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SetupTemplate reads and parses a template from file
|
||||
func SetupTemplate(r *gin.Engine) {
|
||||
if setting.App.TemplatePath == "" {
|
||||
t, _ := template.New("home").Parse(home)
|
||||
@ -18,6 +19,7 @@ func SetupTemplate(r *gin.Engine) {
|
||||
}
|
||||
}
|
||||
|
||||
// Setup defines the endpoints
|
||||
func Setup(r *gin.Engine) {
|
||||
r.GET("/", getRoot)
|
||||
r.GET("/client-port", getClientPortAsString)
|
||||
|
@ -7,10 +7,12 @@ import (
|
||||
"github.com/dcarrillo/whatismyip/models"
|
||||
)
|
||||
|
||||
// Geo defines a base type for lookups
|
||||
type Geo struct {
|
||||
IP net.IP
|
||||
}
|
||||
|
||||
// LookUpCity queries the database for city data related to the given IP
|
||||
func (g *Geo) LookUpCity() *models.GeoRecord {
|
||||
record := &models.GeoRecord{}
|
||||
err := record.LookUp(g.IP)
|
||||
@ -22,6 +24,7 @@ func (g *Geo) LookUpCity() *models.GeoRecord {
|
||||
return record
|
||||
}
|
||||
|
||||
// LookUpASN queries the database for ASN data related to the given IP
|
||||
func (g *Geo) LookUpASN() *models.ASNRecord {
|
||||
record := &models.ASNRecord{}
|
||||
err := record.LookUp(g.IP)
|
||||
|
Loading…
Reference in New Issue
Block a user