mirror of https://github.com/mpolden/echoip
Fix indention
This commit is contained in:
parent
f77fb4fbc0
commit
b6acdacddd
|
@ -16,7 +16,7 @@ func main() {
|
||||||
var opts struct {
|
var opts struct {
|
||||||
CountryDBPath string `short:"f" long:"country-db" description:"Path to GeoIP country database" value-name:"FILE" default:""`
|
CountryDBPath string `short:"f" long:"country-db" description:"Path to GeoIP country database" value-name:"FILE" default:""`
|
||||||
CityDBPath string `short:"c" long:"city-db" description:"Path to GeoIP city database" value-name:"FILE" default:""`
|
CityDBPath string `short:"c" long:"city-db" description:"Path to GeoIP city database" value-name:"FILE" default:""`
|
||||||
ASNDBPath string `short:"a" long:"asn-db" description:"Path to GeoIP ASN database" value-name:"FILE" default:""`
|
ASNDBPath string `short:"a" long:"asn-db" description:"Path to GeoIP ASN database" value-name:"FILE" default:""`
|
||||||
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
|
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
|
||||||
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
|
ReverseLookup bool `short:"r" long:"reverse-lookup" description:"Perform reverse hostname lookups"`
|
||||||
PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"`
|
PortLookup bool `short:"p" long:"port-lookup" description:"Enable port lookup"`
|
||||||
|
|
47
http/http.go
47
http/http.go
|
@ -29,16 +29,16 @@ type Server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
IP net.IP `json:"ip"`
|
IP net.IP `json:"ip"`
|
||||||
IPDecimal uint64 `json:"ip_decimal"`
|
IPDecimal uint64 `json:"ip_decimal"`
|
||||||
Country string `json:"country,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
CountryISO string `json:"country_iso,omitempty"`
|
CountryISO string `json:"country_iso,omitempty"`
|
||||||
IsInEuropeanUnion bool `json:"is_in_european_union,omitempty"`
|
IsInEuropeanUnion bool `json:"is_in_european_union,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
Hostname string `json:"hostname,omitempty"`
|
Hostname string `json:"hostname,omitempty"`
|
||||||
LocationLatitude float64 `json:"location_latitude,omitempty"`
|
LocationLatitude float64 `json:"location_latitude,omitempty"`
|
||||||
LocationLongitude float64 `json:"location_longitude,omitempty"`
|
LocationLongitude float64 `json:"location_longitude,omitempty"`
|
||||||
AutonomousSystemNumber string `json:"asn_number,omitempty"`
|
AutonomousSystemNumber string `json:"asn_number,omitempty"`
|
||||||
AutonomousSystemOrganization string `json:"asn_organization,omitempty"`
|
AutonomousSystemOrganization string `json:"asn_organization,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,6 @@ func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
remoteIP = ip.String()
|
remoteIP = ip.String()
|
||||||
}
|
}
|
||||||
fmt.Printf("IP: %v\n", ip)
|
|
||||||
}
|
}
|
||||||
if remoteIP == "" {
|
if remoteIP == "" {
|
||||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
@ -68,7 +67,6 @@ func ipFromRequest(header string, r *http.Request) (net.IP, error) {
|
||||||
}
|
}
|
||||||
remoteIP = host
|
remoteIP = host
|
||||||
}
|
}
|
||||||
fmt.Printf("Remote: %v\n", remoteIP)
|
|
||||||
ip := net.ParseIP(remoteIP)
|
ip := net.ParseIP(remoteIP)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return nil, fmt.Errorf("could not parse IP: %s", remoteIP)
|
return nil, fmt.Errorf("could not parse IP: %s", remoteIP)
|
||||||
|
@ -90,20 +88,20 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
|
||||||
hostname, _ = s.LookupAddr(ip)
|
hostname, _ = s.LookupAddr(ip)
|
||||||
}
|
}
|
||||||
var autonomousSystemNumber string
|
var autonomousSystemNumber string
|
||||||
if(asn.AutonomousSystemNumber > 0) {
|
if asn.AutonomousSystemNumber > 0 {
|
||||||
autonomousSystemNumber = "AS" + strconv.FormatUint(uint64(asn.AutonomousSystemNumber), 10);
|
autonomousSystemNumber = "AS" + strconv.FormatUint(uint64(asn.AutonomousSystemNumber), 10);
|
||||||
}
|
}
|
||||||
return Response{
|
return Response{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
IPDecimal: ipDecimal,
|
IPDecimal: ipDecimal,
|
||||||
Country: country.Name,
|
Country: country.Name,
|
||||||
CountryISO: country.ISO,
|
CountryISO: country.ISO,
|
||||||
IsInEuropeanUnion: country.IsInEuropeanUnion,
|
IsInEuropeanUnion: country.IsInEuropeanUnion,
|
||||||
City: city.Name,
|
City: city.Name,
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
LocationLatitude: city.Latitude,
|
LocationLatitude: city.Latitude,
|
||||||
LocationLongitude: city.Longitude,
|
LocationLongitude: city.Longitude,
|
||||||
AutonomousSystemNumber: autonomousSystemNumber,
|
AutonomousSystemNumber: autonomousSystemNumber,
|
||||||
AutonomousSystemOrganization: asn.AutonomousSystemOrganization,
|
AutonomousSystemOrganization: asn.AutonomousSystemOrganization,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -280,7 +278,6 @@ func (s *Server) Handler() http.Handler {
|
||||||
// JSON
|
// JSON
|
||||||
r.Route("GET", "/", s.JSONHandler).Header("Accept", jsonMediaType)
|
r.Route("GET", "/", s.JSONHandler).Header("Accept", jsonMediaType)
|
||||||
r.Route("GET", "/json", s.JSONHandler)
|
r.Route("GET", "/json", s.JSONHandler)
|
||||||
|
|
||||||
r.RoutePrefix("GET", "/json/", s.JSONHandler)
|
r.RoutePrefix("GET", "/json/", s.JSONHandler)
|
||||||
|
|
||||||
// CLI
|
// CLI
|
||||||
|
@ -312,4 +309,4 @@ func (s *Server) ListenAndServe(addr string) error {
|
||||||
func FloatToString(input_num float64) string {
|
func FloatToString(input_num float64) string {
|
||||||
// to convert a float number to a string
|
// to convert a float number to a string
|
||||||
return strconv.FormatFloat(input_num, 'f', 6, 64)
|
return strconv.FormatFloat(input_num, 'f', 6, 64)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,26 +15,26 @@ type Client interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Country struct {
|
type Country struct {
|
||||||
Name string
|
Name string
|
||||||
ISO string
|
ISO string
|
||||||
IsInEuropeanUnion bool
|
IsInEuropeanUnion bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type City struct {
|
type City struct {
|
||||||
Name string
|
Name string
|
||||||
Latitude float64
|
Latitude float64
|
||||||
Longitude float64
|
Longitude float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type ASN struct {
|
type ASN struct {
|
||||||
AutonomousSystemNumber uint
|
AutonomousSystemNumber uint
|
||||||
AutonomousSystemOrganization string
|
AutonomousSystemOrganization string
|
||||||
}
|
}
|
||||||
|
|
||||||
type geoip struct {
|
type geoip struct {
|
||||||
country *geoip2.Reader
|
country *geoip2.Reader
|
||||||
city *geoip2.Reader
|
city *geoip2.Reader
|
||||||
asn *geoip2.Reader
|
asn *geoip2.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(countryDB, cityDB, asnDB string) (Client, error) {
|
func New(countryDB, cityDB, asnDB string) (Client, error) {
|
||||||
|
@ -132,4 +132,4 @@ func (g *geoip) ASN(ip net.IP) (ASN, error) {
|
||||||
|
|
||||||
func (g *geoip) IsEmpty() bool {
|
func (g *geoip) IsEmpty() bool {
|
||||||
return g.country == nil && g.city == nil
|
return g.country == nil && g.city == nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue