echoip/iputil/ipstack/ipstack.go

73 lines
1.7 KiB
Go
Raw Normal View History

2023-09-23 03:21:58 +02:00
package ipstack
2023-09-23 02:15:10 +02:00
import (
"fmt"
"net"
"github.com/mpolden/echoip/iputil"
parser "github.com/mpolden/echoip/iputil/paser"
"github.com/qioalice/ipstack"
)
2023-09-23 03:21:58 +02:00
type IPStack struct {
2023-09-23 02:15:10 +02:00
response *ipstack.Response
}
2023-09-23 03:21:58 +02:00
func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) {
2023-09-23 03:28:49 +02:00
res, err := ipstack.IP(ip.String())
2023-09-23 02:15:10 +02:00
ips.response = res
2023-09-23 03:28:49 +02:00
if err != nil {
return parser.Response{}, err
2023-09-23 02:15:10 +02:00
}
ipDecimal := iputil.ToDecimal(ip)
parserResponse := parser.Response{
2023-09-23 03:28:49 +02:00
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,
2023-09-23 02:15:10 +02:00
}
if res.Timezone != nil {
parserResponse.Timezone = res.Timezone.ID
parserResponse.IsDayLightSavings = res.Timezone.IsDaylightSaving
}
if res.Security != nil {
parserResponse.IsProxy = res.Security.IsProxy
parserResponse.IsCrawler = res.Security.IsCrawler
parserResponse.CrawlerName = res.Security.CrawlerName
parserResponse.CrawlerType = res.Security.CrawlerType
parserResponse.IsTor = res.Security.IsTOR
parserResponse.ThreatLevel = res.Security.ThreatLevel
parserResponse.ThreatTypes = &res.Security.ThreatTypes
2023-09-23 02:15:10 +02:00
}
if res.Location != nil {
2023-09-23 03:28:49 +02:00
parserResponse.CountryEU = &res.Location.IsEU
2023-09-23 02:15:10 +02:00
}
if res.Connection != nil {
if res.Connection.ASN > 0 {
parserResponse.ASN = fmt.Sprintf("AS%d", res.Connection.ASN)
}
}
2023-09-23 03:28:49 +02:00
2023-09-23 02:15:10 +02:00
return parserResponse, nil
}
2023-09-23 03:21:58 +02:00
func (ips *IPStack) IsEmpty() bool {
2023-09-23 02:15:10 +02:00
return false
}