PageRenderTime 34ms CodeModel.GetById 10ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ip.go

https://gitlab.com/voxxit/gogeoip2
Go | 98 lines | 87 code | 11 blank | 0 comment | 4 complexity | 7c68c32ec60cf2d930d72b2c3f4672de MD5 | raw file
 1package api
 2
 3import (
 4	"fmt"
 5	"net"
 6)
 7
 8type IPConfig struct {
 9	Addr             string
10	RequestLatitude  string
11	RequestLongitude string
12}
13
14type IP struct {
15	IP              string   `json:"ip"`
16	Scores          *Scores  `json:"scores"`
17	ASNum           uint     `json:"asnum,omitempty"`
18	City            string   `json:"city"`
19	Country         string   `json:"country"`
20	CountryISO      string   `json:"country-iso"`
21	Continent       string   `json:"continent"`
22	Latitude        float64  `json:"lat"`
23	Longitude       float64  `json:"lng"`
24	TimeZone        string   `json:"time-zone"`
25	Distance        float64  `json:"distance,omitempty"`
26	Hostnames       []string `json:"hostnames,omitempty"`
27	ASOrg           string   `json:"asorg,omitempty"`
28	ISP             string   `json:"isp,omitempty"`
29	Org             string   `json:"org,omitempty"`
30	CIDRReport      string   `json:"cidr-report"`
31	CleanTalkReport string   `json:"cleantalk-report"`
32	IPInfoReport    string   `json:"ipinfo-report"`
33	PeeringDBReport string   `json:"peering-db-report"`
34}
35
36func LookupIP(db *IPDatabase, config *IPConfig) (*IP, error) {
37	parsedIP := net.ParseIP(config.Addr)
38
39	record, err := db.Isp.ISP(parsedIP)
40	if err != nil {
41		return nil, err
42	}
43
44	location, err := db.City.City(parsedIP)
45	if err != nil {
46		return nil, err
47	}
48
49	hostnames, _ := net.LookupAddr(config.Addr)
50
51	distance := distanceTo(
52		location.Location.Latitude,
53		location.Location.Longitude,
54		config.RequestLatitude,
55		config.RequestLongitude,
56	)
57
58	scores := NewScores(&ScoresInput{
59		Distance:    distance,
60		CountryCode: location.Country.IsoCode,
61	})
62
63	ip := &IP{
64		Scores:     scores,
65		Latitude:   location.Location.Latitude,
66		Longitude:  location.Location.Longitude,
67		IP:         config.Addr,
68		Distance:   distance,
69		TimeZone:   location.Location.TimeZone,
70		City:       location.City.Names["en"],
71		Country:    location.Country.Names["en"],
72		CountryISO: location.Country.IsoCode,
73		Continent:  location.Continent.Code,
74		Hostnames:  hostnames,
75		ASNum:      record.AutonomousSystemNumber,
76		ASOrg:      record.AutonomousSystemOrganization,
77		ISP:        record.ISP,
78		Org:        record.Organization,
79		CIDRReport: fmt.Sprintf(
80			"http://www.cidr-report.org/cgi-bin/as-report?as=AS%d&view=2.0",
81			record.AutonomousSystemNumber,
82		),
83		CleanTalkReport: fmt.Sprintf(
84			"https://cleantalk.org/blacklists/AS%d",
85			record.AutonomousSystemNumber,
86		),
87		IPInfoReport: fmt.Sprintf(
88			"http://ipinfo.io/AS%d",
89			record.AutonomousSystemNumber,
90		),
91		PeeringDBReport: fmt.Sprintf(
92			"https://beta.peeringdb.com/api/asn/%d",
93			record.AutonomousSystemNumber,
94		),
95	}
96
97	return ip, nil
98}