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