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/http"
"github.com/mpolden/echoip/iputil" "github.com/mpolden/echoip/iputil"
"github.com/mpolden/echoip/iputil/geo" "github.com/mpolden/echoip/iputil/geo"
"github.com/mpolden/echoip/iputil/ipstack"
parser "github.com/mpolden/echoip/iputil/paser" parser "github.com/mpolden/echoip/iputil/paser"
"github.com/mpolden/echoip/iputil/stack" ipstackApi "github.com/qioalice/ipstack"
"github.com/qioalice/ipstack"
) )
type multiValueFlag []string type multiValueFlag []string
@ -33,7 +33,7 @@ func init() {
func main() { func main() {
var ipstackApiKey string 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") flag.StringVar(&ipstackApiKey, "S", "", "IP Stack API Key")
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")
@ -55,7 +55,7 @@ func main() {
} }
var parser parser.Parser var parser parser.Parser
if (*database == "geoip") { if (*service == "geoip") {
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)
@ -63,11 +63,11 @@ func main() {
parser = &geo parser = &geo
} }
if (*database == "ipstack") { if (*service == "ipstack") {
if err := ipstack.Init(ipstackApiKey); err != nil { if err := ipstackApi.Init(ipstackApiKey); err != nil {
log.Fatal(err) log.Fatal(err)
} }
ips := stack.IPstackParser{} ips := ipstack.IPStack{}
parser = &ips parser = &ips
} }

View File

@ -168,13 +168,18 @@
</tr> </tr>
{{ end }} {{ end }} {{ end }} {{ end }}
</table> </table>
{{ if .Country }} {{ if .Country }} {{ if .Service == "geoip" }}
<p> <p>
This information is provided from the GeoLite2 database created by This information is provided from the GeoLite2 database created by
MaxMind, available from MaxMind, available from
<a href="https://www.maxmind.com">www.maxmind.com</a> <a href="https://www.maxmind.com">www.maxmind.com</a>
</p> </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"> <div class="pure-u-1 pure-u-md-1-1">
<h2>Map</h2> <h2>Map</h2>
<iframe <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) autonomousSystemNumber = fmt.Sprintf("AS%d", asn.AutonomousSystemNumber)
} }
return parser.Response{ return parser.Response{
Service: "ipstack",
IP: ip, IP: ip,
IPDecimal: ipDecimal, IPDecimal: ipDecimal,
Country: country.Name, Country: country.Name,

View File

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

View File

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