some refactoring, more to go

This commit is contained in:
Ethan Knowlton 2023-09-22 21:21:58 -04:00
parent 799eea7d12
commit 08a55bc67f
5 changed files with 21 additions and 13 deletions

View File

@ -10,9 +10,9 @@ import (
"github.com/mpolden/echoip/http"
"github.com/mpolden/echoip/iputil"
"github.com/mpolden/echoip/iputil/geo"
"github.com/mpolden/echoip/iputil/ipstack"
parser "github.com/mpolden/echoip/iputil/paser"
"github.com/mpolden/echoip/iputil/stack"
"github.com/qioalice/ipstack"
ipstackApi "github.com/qioalice/ipstack"
)
type multiValueFlag []string
@ -33,7 +33,7 @@ func init() {
func main() {
var ipstackApiKey string
database := 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")
countryFile := flag.String("f", "", "Path to GeoIP country database")
cityFile := flag.String("c", "", "Path to GeoIP city database")
@ -55,7 +55,7 @@ func main() {
}
var parser parser.Parser
if (*database == "geoip") {
if (*service == "geoip") {
geo, err := geo.Open(*countryFile, *cityFile, *asnFile)
if err != nil {
log.Fatal(err)
@ -63,11 +63,11 @@ func main() {
parser = &geo
}
if (*database == "ipstack") {
if err := ipstack.Init(ipstackApiKey); err != nil {
if (*service == "ipstack") {
if err := ipstackApi.Init(ipstackApiKey); err != nil {
log.Fatal(err)
}
ips := stack.IPstackParser{}
ips := ipstack.IPStack{}
parser = &ips
}

View File

@ -168,13 +168,18 @@
</tr>
{{ end }} {{ end }}
</table>
{{ if .Country }}
{{ if .Country }} {{ if .Service == "geoip" }}
<p>
This information is provided from the GeoLite2 database created by
MaxMind, available from
<a href="https://www.maxmind.com">www.maxmind.com</a>
</p>
{{ end }} {{ if .Latitude }}
{{ end}} {{ if .Service == "ipstack" }}
<p>
This information is provided from
<a href="https://www.ipstack.com">www.ipstack.com</a>
</p>
{{ end}} {{ end }} {{ if .Latitude }}
<div class="pure-u-1 pure-u-md-1-1">
<h2>Map</h2>
<iframe

View File

@ -81,6 +81,7 @@ func (g *geoip) Parse(ip net.IP, hostname string) (parser.Response, error) {
autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
}
return parser.Response{
Service: "ipstack",
IP: ip,
IPDecimal: ipDecimal,
Country: country.Name,

View File

@ -1,4 +1,4 @@
package stack
package ipstack
import (
"fmt"
@ -9,11 +9,11 @@ import (
"github.com/qioalice/ipstack"
)
type IPstackParser struct {
type IPStack struct {
response *ipstack.Response
}
func (ips *IPstackParser) Parse(ip net.IP, hostname string) (parser.Response, error) {
func (ips *IPStack) Parse(ip net.IP, hostname string) (parser.Response, error) {
res, err := ipstack.IP(ip.String());
ips.response = res
if err != nil {
@ -27,6 +27,7 @@ func (ips *IPstackParser) Parse(ip net.IP, hostname string) (parser.Response, er
parserResponse := parser.Response{
Service: "ipstack",
Latitude: float64(res.Latitide),
Longitude: float64(res.Longitude),
Hostname: hostname,
@ -58,6 +59,6 @@ func (ips *IPstackParser) Parse(ip net.IP, hostname string) (parser.Response, er
return parserResponse, nil
}
func (ips *IPstackParser) IsEmpty() bool {
func (ips *IPStack) IsEmpty() bool {
return false
}

View File

@ -13,6 +13,7 @@ type Parser interface {
}
type Response struct {
Service string `json:"database"`
IP net.IP `json:"ip"`
IPDecimal *big.Int `json:"ip_decimal"`
Country string `json:"country,omitempty"`