mirror of https://github.com/mpolden/echoip
upadted template, added https option for ipstack
This commit is contained in:
parent
81aa484762
commit
410002ff47
|
@ -35,6 +35,8 @@ func main() {
|
|||
var ipstackApiKey string
|
||||
service := flag.String("d", "geoip", "Which database to use, 'ipstack' or 'geoip'")
|
||||
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")
|
||||
cityFile := flag.String("c", "", "Path to GeoIP city database")
|
||||
asnFile := flag.String("a", "", "Path to GeoIP ASN database")
|
||||
|
@ -56,6 +58,7 @@ func main() {
|
|||
|
||||
var parser parser.Parser
|
||||
if *service == "geoip" {
|
||||
log.Print("Using GeoIP for IP database")
|
||||
geo, err := geo.Open(*countryFile, *cityFile, *asnFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -64,7 +67,17 @@ func main() {
|
|||
}
|
||||
|
||||
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)
|
||||
}
|
||||
ips := ipstack.IPStack{}
|
||||
|
|
|
@ -173,18 +173,17 @@
|
|||
</tr>
|
||||
{{ end }}
|
||||
|
||||
{{ if .IsProxy }}
|
||||
{{ if .IPStackSecurityEnabled }}
|
||||
<tr>
|
||||
<th scope="row">Is Proxy?</th>
|
||||
<td>{{ .IsProxy }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
{{ if .IsCrawler }}
|
||||
<tr>
|
||||
<th scope="row">Is Crawler?</th>
|
||||
<td>{{ .IsCrawler }}</td>
|
||||
</tr>
|
||||
|
||||
{{ if .IsCrawler }}
|
||||
<tr>
|
||||
<th scope="row">Crawler Name</th>
|
||||
<td>{{ .CrawlerName }}</td>
|
||||
|
@ -195,21 +194,14 @@
|
|||
</tr>
|
||||
{{ end }}
|
||||
|
||||
{{ if .IsTor }}
|
||||
<tr>
|
||||
<th scope="row">Is Tor?</th>
|
||||
<td>{{ .IsTor }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
{{ if .ThreatLevel }}
|
||||
<tr>
|
||||
<th scope="row">Threat Level</th>
|
||||
<td>{{ .ThreatLevel }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
{{ if .ThreatTypes }}
|
||||
<tr>
|
||||
<th scope="row">Threat Types</th>
|
||||
<td>{{ .ThreatTypes }}</td>
|
||||
|
|
|
@ -163,7 +163,7 @@ func TestDisabledHandlers(t *testing.T) {
|
|||
{s.URL + "/country", "404 page not found", 404},
|
||||
{s.URL + "/country-iso", "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 {
|
||||
|
@ -189,7 +189,7 @@ func TestJSONHandlers(t *testing.T) {
|
|||
out string
|
||||
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/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},
|
||||
|
|
|
@ -83,6 +83,7 @@ func (g *geoip) Parse(ip net.IP, hostname string) (parser.Response, error) {
|
|||
return parser.Response{
|
||||
UsingGeoIP: true,
|
||||
UsingIPStack: false,
|
||||
IPStackSecurityEnabled: false,
|
||||
IP: ip,
|
||||
IPDecimal: ipDecimal,
|
||||
Country: country.Name,
|
||||
|
|
|
@ -3,6 +3,7 @@ package ipstack
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
"github.com/mpolden/echoip/iputil"
|
||||
parser "github.com/mpolden/echoip/iputil/paser"
|
||||
|
@ -45,14 +46,18 @@ func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) {
|
|||
}
|
||||
|
||||
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 {
|
||||
parserResponse.CountryEU = &res.Location.IsEU
|
||||
|
|
|
@ -15,6 +15,7 @@ type Parser interface {
|
|||
type Response struct {
|
||||
UsingGeoIP bool `json:"UsingGeoIP"`
|
||||
UsingIPStack bool `json:"UsingIPStack"`
|
||||
IPStackSecurityEnabled bool `json:"IPStackSecurityEnabled"`
|
||||
IP net.IP `json:"ip"`
|
||||
IPDecimal *big.Int `json:"ip_decimal"`
|
||||
Country string `json:"country,omitempty"`
|
||||
|
|
Loading…
Reference in New Issue