mirror of https://github.com/mpolden/echoip
Merge pull request #2 from levelsoftware/adding-extra-ipstack-fields
Added extra IP Stack fields
This commit is contained in:
commit
07b45691b6
|
@ -35,6 +35,8 @@ func main() {
|
||||||
var ipstackApiKey string
|
var ipstackApiKey string
|
||||||
service := flag.String("d", "geoip", "Which database to use, 'ipstack' or 'geoip'")
|
service := flag.String("d", "geoip", "Which database to use, 'ipstack' or 'geoip'")
|
||||||
flag.StringVar(&ipstackApiKey, "S", "", "IP Stack API Key")
|
flag.StringVar(&ipstackApiKey, "S", "", "IP Stack API Key")
|
||||||
|
ipStackEnableSecurityModule := flag.Bool("x", false, "Enable security module for IP Stack ( must have security module, aka. non-free account. )")
|
||||||
|
ipStackUseHttps := flag.Bool("h", false, "Use HTTPS for IP Stack ( only non-free accounts )")
|
||||||
countryFile := flag.String("f", "", "Path to GeoIP country database")
|
countryFile := flag.String("f", "", "Path to GeoIP country database")
|
||||||
cityFile := flag.String("c", "", "Path to GeoIP city database")
|
cityFile := flag.String("c", "", "Path to GeoIP city database")
|
||||||
asnFile := flag.String("a", "", "Path to GeoIP ASN database")
|
asnFile := flag.String("a", "", "Path to GeoIP ASN database")
|
||||||
|
@ -56,6 +58,7 @@ func main() {
|
||||||
|
|
||||||
var parser parser.Parser
|
var parser parser.Parser
|
||||||
if *service == "geoip" {
|
if *service == "geoip" {
|
||||||
|
log.Print("Using GeoIP for IP database")
|
||||||
geo, err := geo.Open(*countryFile, *cityFile, *asnFile)
|
geo, err := geo.Open(*countryFile, *cityFile, *asnFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -64,7 +67,17 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *service == "ipstack" {
|
if *service == "ipstack" {
|
||||||
if err := ipstackApi.Init(ipstackApiKey); err != nil {
|
log.Print("Using GeoIP for IP database")
|
||||||
|
if *ipStackEnableSecurityModule {
|
||||||
|
log.Print("Enable Security Module ( Requires Professional Plus account )")
|
||||||
|
}
|
||||||
|
enableSecurity := ipstackApi.ParamEnableSecurity(*ipStackEnableSecurityModule)
|
||||||
|
apiKey := ipstackApi.ParamToken(ipstackApiKey)
|
||||||
|
useHttps := ipstackApi.ParamUseHTTPS(*ipStackUseHttps)
|
||||||
|
if *ipStackUseHttps {
|
||||||
|
log.Print("Use IP Stack HTTPS API ( Requires non-free account )")
|
||||||
|
}
|
||||||
|
if err := ipstackApi.Init(apiKey, enableSecurity, useHttps); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
ips := ipstack.IPStack{}
|
ips := ipstack.IPStack{}
|
||||||
|
|
|
@ -136,6 +136,11 @@
|
||||||
<th scope="row">Timezone</th>
|
<th scope="row">Timezone</th>
|
||||||
<td>{{ .Timezone }}</td>
|
<td>{{ .Timezone }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
{{ end }} {{ if .IsDayLightSavings }}
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Is Daylight Savings?</th>
|
||||||
|
<td>{{ .IsDayLightSavings }}</td>
|
||||||
|
</tr>
|
||||||
{{ end }} {{ if .ASN }}
|
{{ end }} {{ if .ASN }}
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">ASN</th>
|
<th scope="row">ASN</th>
|
||||||
|
@ -166,7 +171,44 @@
|
||||||
<th scope="row">User agent: Raw</th>
|
<th scope="row">User agent: Raw</th>
|
||||||
<td>{{ .UserAgent.RawValue }}</td>
|
<td>{{ .UserAgent.RawValue }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{ end }} {{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
{{ if .IPStackSecurityEnabled }}
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Is Proxy?</th>
|
||||||
|
<td>{{ .IsProxy }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Is Crawler?</th>
|
||||||
|
<td>{{ .IsCrawler }}</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
{{ if .IsCrawler }}
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Crawler Name</th>
|
||||||
|
<td>{{ .CrawlerName }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Crawler Type</th>
|
||||||
|
<td>{{ .CrawlerType }}</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Is Tor?</th>
|
||||||
|
<td>{{ .IsTor }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Threat Level</th>
|
||||||
|
<td>{{ .ThreatLevel }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Threat Types</th>
|
||||||
|
<td>{{ .ThreatTypes }}</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
</table>
|
</table>
|
||||||
{{ if .Country }} {{ if .UsingGeoIP }}
|
{{ if .Country }} {{ if .UsingGeoIP }}
|
||||||
<p>
|
<p>
|
||||||
|
|
|
@ -163,7 +163,7 @@ func TestDisabledHandlers(t *testing.T) {
|
||||||
{s.URL + "/country", "404 page not found", 404},
|
{s.URL + "/country", "404 page not found", 404},
|
||||||
{s.URL + "/country-iso", "404 page not found", 404},
|
{s.URL + "/country-iso", "404 page not found", 404},
|
||||||
{s.URL + "/city", "404 page not found", 404},
|
{s.URL + "/city", "404 page not found", 404},
|
||||||
{s.URL + "/json", "{\n \"UsingGeoIP\": true,\n \"UsingIPStack\": false,\n \"ip\": \"127.0.0.1\",\n \"ip_decimal\": 2130706433\n}", 200},
|
{s.URL + "/json", "{\n \"UsingGeoIP\": true,\n \"UsingIPStack\": false,\n \"IPStackSecurityEnabled\": false,\n \"ip\": \"127.0.0.1\",\n \"ip_decimal\": 2130706433\n}", 200},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -189,7 +189,7 @@ func TestJSONHandlers(t *testing.T) {
|
||||||
out string
|
out string
|
||||||
status int
|
status int
|
||||||
}{
|
}{
|
||||||
{s.URL, "{\n \"UsingGeoIP\": true,\n \"UsingIPStack\": false,\n \"ip\": \"127.0.0.1\",\n \"ip_decimal\": 2130706433,\n \"country\": \"Elbonia\",\n \"country_iso\": \"EB\",\n \"country_eu\": false,\n \"region_name\": \"North Elbonia\",\n \"region_code\": \"1234\",\n \"metro_code\": 1234,\n \"zip_code\": \"1234\",\n \"city\": \"Bornyasherk\",\n \"latitude\": 63.416667,\n \"longitude\": 10.416667,\n \"time_zone\": \"Europe/Bornyasherk\",\n \"asn\": \"AS59795\",\n \"asn_org\": \"Hosting4Real\",\n \"hostname\": \"localhost\",\n \"user_agent\": {\n \"product\": \"curl\",\n \"version\": \"7.2.6.0\",\n \"raw_value\": \"curl/7.2.6.0\"\n }\n}", 200},
|
{s.URL, "{\n \"UsingGeoIP\": true,\n \"UsingIPStack\": false,\n \"IPStackSecurityEnabled\": false,\n \"ip\": \"127.0.0.1\",\n \"ip_decimal\": 2130706433,\n \"country\": \"Elbonia\",\n \"country_iso\": \"EB\",\n \"country_eu\": false,\n \"region_name\": \"North Elbonia\",\n \"region_code\": \"1234\",\n \"metro_code\": 1234,\n \"zip_code\": \"1234\",\n \"city\": \"Bornyasherk\",\n \"latitude\": 63.416667,\n \"longitude\": 10.416667,\n \"time_zone\": \"Europe/Bornyasherk\",\n \"asn\": \"AS59795\",\n \"asn_org\": \"Hosting4Real\",\n \"hostname\": \"localhost\",\n \"user_agent\": {\n \"product\": \"curl\",\n \"version\": \"7.2.6.0\",\n \"raw_value\": \"curl/7.2.6.0\"\n }\n}", 200},
|
||||||
{s.URL + "/port/foo", "{\n \"status\": 400,\n \"error\": \"invalid port: foo\"\n}", 400},
|
{s.URL + "/port/foo", "{\n \"status\": 400,\n \"error\": \"invalid port: foo\"\n}", 400},
|
||||||
{s.URL + "/port/0", "{\n \"status\": 400,\n \"error\": \"invalid port: 0\"\n}", 400},
|
{s.URL + "/port/0", "{\n \"status\": 400,\n \"error\": \"invalid port: 0\"\n}", 400},
|
||||||
{s.URL + "/port/65537", "{\n \"status\": 400,\n \"error\": \"invalid port: 65537\"\n}", 400},
|
{s.URL + "/port/65537", "{\n \"status\": 400,\n \"error\": \"invalid port: 65537\"\n}", 400},
|
||||||
|
|
|
@ -81,24 +81,25 @@ func (g *geoip) Parse(ip net.IP, hostname string) (parser.Response, error) {
|
||||||
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
|
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
|
||||||
}
|
}
|
||||||
return parser.Response{
|
return parser.Response{
|
||||||
UsingGeoIP: true,
|
UsingGeoIP: true,
|
||||||
UsingIPStack: false,
|
UsingIPStack: false,
|
||||||
IP: ip,
|
IPStackSecurityEnabled: false,
|
||||||
IPDecimal: ipDecimal,
|
IP: ip,
|
||||||
Country: country.Name,
|
IPDecimal: ipDecimal,
|
||||||
CountryISO: country.ISO,
|
Country: country.Name,
|
||||||
CountryEU: country.IsEU,
|
CountryISO: country.ISO,
|
||||||
RegionName: city.RegionName,
|
CountryEU: country.IsEU,
|
||||||
RegionCode: city.RegionCode,
|
RegionName: city.RegionName,
|
||||||
MetroCode: city.MetroCode,
|
RegionCode: city.RegionCode,
|
||||||
PostalCode: city.PostalCode,
|
MetroCode: city.MetroCode,
|
||||||
City: city.Name,
|
PostalCode: city.PostalCode,
|
||||||
Latitude: city.Latitude,
|
City: city.Name,
|
||||||
Longitude: city.Longitude,
|
Latitude: city.Latitude,
|
||||||
Timezone: city.Timezone,
|
Longitude: city.Longitude,
|
||||||
ASN: autonomousSystemNumber,
|
Timezone: city.Timezone,
|
||||||
ASNOrg: asn.AutonomousSystemOrganization,
|
ASN: autonomousSystemNumber,
|
||||||
Hostname: hostname,
|
ASNOrg: asn.AutonomousSystemOrganization,
|
||||||
|
Hostname: hostname,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package ipstack
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/mpolden/echoip/iputil"
|
"github.com/mpolden/echoip/iputil"
|
||||||
parser "github.com/mpolden/echoip/iputil/paser"
|
parser "github.com/mpolden/echoip/iputil/paser"
|
||||||
|
@ -41,6 +42,21 @@ func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) {
|
||||||
|
|
||||||
if res.Timezone != nil {
|
if res.Timezone != nil {
|
||||||
parserResponse.Timezone = res.Timezone.ID
|
parserResponse.Timezone = res.Timezone.ID
|
||||||
|
parserResponse.IsDayLightSavings = res.Timezone.IsDaylightSaving
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Security != nil {
|
||||||
|
parserResponse.IPStackSecurityEnabled = true
|
||||||
|
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
|
||||||
|
|
||||||
|
if !reflect.ValueOf(&res.Security.ThreatTypes).IsNil() {
|
||||||
|
parserResponse.ThreatTypes = &res.Security.ThreatTypes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.Location != nil {
|
if res.Location != nil {
|
||||||
|
|
|
@ -13,23 +13,33 @@ type Parser interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
UsingGeoIP bool `json:"UsingGeoIP"`
|
UsingGeoIP bool `json:"UsingGeoIP"`
|
||||||
UsingIPStack bool `json:"UsingIPStack"`
|
UsingIPStack bool `json:"UsingIPStack"`
|
||||||
IP net.IP `json:"ip"`
|
IPStackSecurityEnabled bool `json:"IPStackSecurityEnabled"`
|
||||||
IPDecimal *big.Int `json:"ip_decimal"`
|
IP net.IP `json:"ip"`
|
||||||
Country string `json:"country,omitempty"`
|
IPDecimal *big.Int `json:"ip_decimal"`
|
||||||
CountryISO string `json:"country_iso,omitempty"`
|
Country string `json:"country,omitempty"`
|
||||||
CountryEU *bool `json:"country_eu,omitempty"`
|
CountryISO string `json:"country_iso,omitempty"`
|
||||||
RegionName string `json:"region_name,omitempty"`
|
CountryEU *bool `json:"country_eu,omitempty"`
|
||||||
RegionCode string `json:"region_code,omitempty"`
|
RegionName string `json:"region_name,omitempty"`
|
||||||
MetroCode uint `json:"metro_code,omitempty"`
|
RegionCode string `json:"region_code,omitempty"`
|
||||||
PostalCode string `json:"zip_code,omitempty"`
|
MetroCode uint `json:"metro_code,omitempty"`
|
||||||
City string `json:"city,omitempty"`
|
PostalCode string `json:"zip_code,omitempty"`
|
||||||
Latitude float64 `json:"latitude,omitempty"`
|
City string `json:"city,omitempty"`
|
||||||
Longitude float64 `json:"longitude,omitempty"`
|
Latitude float64 `json:"latitude,omitempty"`
|
||||||
Timezone string `json:"time_zone,omitempty"`
|
Longitude float64 `json:"longitude,omitempty"`
|
||||||
ASN string `json:"asn,omitempty"`
|
Timezone string `json:"time_zone,omitempty"`
|
||||||
ASNOrg string `json:"asn_org,omitempty"`
|
IsDayLightSavings bool `json:"is_daylight_savings,omitempty"`
|
||||||
Hostname string `json:"hostname,omitempty"`
|
ASN string `json:"asn,omitempty"`
|
||||||
UserAgent *useragent.UserAgent `json:"user_agent,omitempty"`
|
ASNOrg string `json:"asn_org,omitempty"`
|
||||||
|
Hostname string `json:"hostname,omitempty"`
|
||||||
|
UserAgent *useragent.UserAgent `json:"user_agent,omitempty"`
|
||||||
|
CurrencyCode string `json:"currency_code,omitempty"`
|
||||||
|
IsProxy bool `json:"is_proxy,omitempty"`
|
||||||
|
IsCrawler bool `json:"is_crawler,omitempty"`
|
||||||
|
CrawlerName string `json:"crawler_name,omitempty"`
|
||||||
|
CrawlerType string `json:"crawler_type,omitempty"`
|
||||||
|
IsTor bool `json:"is_tor,omitempty"`
|
||||||
|
ThreatLevel string `json:"threat_level,omitempty"`
|
||||||
|
ThreatTypes *interface{} `json:"threat_types,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue