From 79e3d6a9bef9bf1420cfc4cb06e944835e7fa9a5 Mon Sep 17 00:00:00 2001 From: Ethan Knowlton Date: Fri, 22 Sep 2023 21:28:49 -0400 Subject: [PATCH] gofmt -- hopefuly what works --- cmd/echoip/main.go | 16 ++++++++-------- html/index.html | 3 +-- http/http.go | 4 ++-- iputil/geo/geo.go | 35 +++++++++++++++++----------------- iputil/ipstack/ipstack.go | 40 +++++++++++++++++++-------------------- iputil/paser/parser.go | 37 ++++++++++++++++++------------------ 6 files changed, 67 insertions(+), 68 deletions(-) diff --git a/cmd/echoip/main.go b/cmd/echoip/main.go index 1f64543..e66ca70 100644 --- a/cmd/echoip/main.go +++ b/cmd/echoip/main.go @@ -48,29 +48,29 @@ func main() { var headers multiValueFlag flag.Var(&headers, "H", "Header to trust for remote IP, if present (e.g. X-Real-IP)") flag.Parse() - + if len(flag.Args()) != 0 { flag.Usage() return } var parser parser.Parser - if (*service == "geoip") { + if *service == "geoip" { geo, err := geo.Open(*countryFile, *cityFile, *asnFile) if err != nil { log.Fatal(err) } parser = &geo - } - - if (*service == "ipstack") { - if err := ipstackApi.Init(ipstackApiKey); err != nil { + } + + if *service == "ipstack" { + if err := ipstackApi.Init(ipstackApiKey); err != nil { log.Fatal(err) } ips := ipstack.IPStack{} parser = &ips - } - + } + cache := http.NewCache(*cacheSize) server := http.New(parser, cache, *profile) server.IPHeaders = headers diff --git a/html/index.html b/html/index.html index f669309..4a06056 100644 --- a/html/index.html +++ b/html/index.html @@ -172,8 +172,7 @@

This information is provided from the GeoLite2 database created by MaxMind, available from - www.maxmind.com -

+ www.maxmind.com

{{ end}} {{ if .Service == "ipstack" }}

This information is provided from diff --git a/http/http.go b/http/http.go index 3321bf1..9693d2b 100644 --- a/http/http.go +++ b/http/http.go @@ -296,7 +296,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro } var data = struct { - parser.Response + parser.Response Host string BoxLatTop float64 BoxLatBottom float64 @@ -316,7 +316,7 @@ func (s *Server) DefaultHandler(w http.ResponseWriter, r *http.Request) *appErro s.LookupPort != nil, s.Sponsor, } - + if err := t.Execute(w, &data); err != nil { return internalServerError(err) } diff --git a/iputil/geo/geo.go b/iputil/geo/geo.go index 2a0a8be..0f97a6b 100644 --- a/iputil/geo/geo.go +++ b/iputil/geo/geo.go @@ -81,23 +81,24 @@ func (g *geoip) Parse(ip net.IP, hostname string) (parser.Response, error) { autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber) } return parser.Response{ - Service: "ipstack", - IP: ip, - IPDecimal: ipDecimal, - Country: country.Name, - CountryISO: country.ISO, - CountryEU: country.IsEU, - RegionName: city.RegionName, - RegionCode: city.RegionCode, - MetroCode: city.MetroCode, - PostalCode: city.PostalCode, - City: city.Name, - Latitude: city.Latitude, - Longitude: city.Longitude, - Timezone: city.Timezone, - ASN: autonomousSystemNumber, - ASNOrg: asn.AutonomousSystemOrganization, - Hostname: hostname, + UsingGeoIP: true, + UsingIPStack: false, + IP: ip, + IPDecimal: ipDecimal, + Country: country.Name, + CountryISO: country.ISO, + CountryEU: country.IsEU, + RegionName: city.RegionName, + RegionCode: city.RegionCode, + MetroCode: city.MetroCode, + PostalCode: city.PostalCode, + City: city.Name, + Latitude: city.Latitude, + Longitude: city.Longitude, + Timezone: city.Timezone, + ASN: autonomousSystemNumber, + ASNOrg: asn.AutonomousSystemOrganization, + Hostname: hostname, }, nil } diff --git a/iputil/ipstack/ipstack.go b/iputil/ipstack/ipstack.go index 6e2cd23..fa9b63d 100644 --- a/iputil/ipstack/ipstack.go +++ b/iputil/ipstack/ipstack.go @@ -14,32 +14,30 @@ type IPStack struct { } func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) { - res, err := ipstack.IP(ip.String()); + res, err := ipstack.IP(ip.String()) ips.response = res - if err != nil { - return parser.Response{}, err + if err != nil { + return parser.Response{}, err } fmt.Printf("%+v\n", res) - ipDecimal := iputil.ToDecimal(ip) - - parserResponse := parser.Response{ - Service: "ipstack", - Latitude: float64(res.Latitide), - Longitude: float64(res.Longitude), - Hostname: hostname, - IP: ip, - IPDecimal: ipDecimal, - Country: res.CountryName, - CountryISO: res.CountryCode, - RegionName: res.RegionName, - RegionCode: res.RegionCode, - MetroCode: 0, - PostalCode: res.Zip, - City: res.City, + UsingGeoIP: false, + UsingIPStack: true, + Latitude: float64(res.Latitide), + Longitude: float64(res.Longitude), + Hostname: hostname, + IP: ip, + IPDecimal: ipDecimal, + Country: res.CountryName, + CountryISO: res.CountryCode, + RegionName: res.RegionName, + RegionCode: res.RegionCode, + MetroCode: 0, + PostalCode: res.Zip, + City: res.City, } if res.Timezone != nil { @@ -47,7 +45,7 @@ func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) { } if res.Location != nil { - parserResponse.CountryEU = &res.Location.IsEU; + parserResponse.CountryEU = &res.Location.IsEU } if res.Connection != nil { @@ -55,7 +53,7 @@ func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) { parserResponse.ASN = fmt.Sprintf("AS%d", res.Connection.ASN) } } - + return parserResponse, nil } diff --git a/iputil/paser/parser.go b/iputil/paser/parser.go index 4c5f21a..4cf731a 100644 --- a/iputil/paser/parser.go +++ b/iputil/paser/parser.go @@ -13,22 +13,23 @@ type Parser interface { } type Response struct { - Service string `json:"database"` - IP net.IP `json:"ip"` - IPDecimal *big.Int `json:"ip_decimal"` - Country string `json:"country,omitempty"` - CountryISO string `json:"country_iso,omitempty"` - CountryEU *bool `json:"country_eu,omitempty"` - RegionName string `json:"region_name,omitempty"` - RegionCode string `json:"region_code,omitempty"` - MetroCode uint `json:"metro_code,omitempty"` - PostalCode string `json:"zip_code,omitempty"` - City string `json:"city,omitempty"` - Latitude float64 `json:"latitude,omitempty"` - Longitude float64 `json:"longitude,omitempty"` - Timezone string `json:"time_zone,omitempty"` - ASN string `json:"asn,omitempty"` - ASNOrg string `json:"asn_org,omitempty"` - Hostname string `json:"hostname,omitempty"` - UserAgent *useragent.UserAgent `json:"user_agent,omitempty"` + UsingGeoIP bool `json:"UsingGeoIP"` + UsingIPStack bool `json:"UsingIPStack"` + IP net.IP `json:"ip"` + IPDecimal *big.Int `json:"ip_decimal"` + Country string `json:"country,omitempty"` + CountryISO string `json:"country_iso,omitempty"` + CountryEU *bool `json:"country_eu,omitempty"` + RegionName string `json:"region_name,omitempty"` + RegionCode string `json:"region_code,omitempty"` + MetroCode uint `json:"metro_code,omitempty"` + PostalCode string `json:"zip_code,omitempty"` + City string `json:"city,omitempty"` + Latitude float64 `json:"latitude,omitempty"` + Longitude float64 `json:"longitude,omitempty"` + Timezone string `json:"time_zone,omitempty"` + ASN string `json:"asn,omitempty"` + ASNOrg string `json:"asn_org,omitempty"` + Hostname string `json:"hostname,omitempty"` + UserAgent *useragent.UserAgent `json:"user_agent,omitempty"` }