mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-13 03:37:21 +01:00
add hostname lookup
This commit is contained in:
parent
ccd3c7a7bb
commit
3d14ea63ba
16
api/api.go
16
api/api.go
@ -21,6 +21,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
IP_HEADER = "x-ifconfig-ip"
|
IP_HEADER = "x-ifconfig-ip"
|
||||||
COUNTRY_HEADER = "x-ifconfig-country"
|
COUNTRY_HEADER = "x-ifconfig-country"
|
||||||
|
HOSTNAME_HEADER = "x-ifconfig-hostname"
|
||||||
TEXT_PLAIN = "text/plain; charset=utf-8"
|
TEXT_PLAIN = "text/plain; charset=utf-8"
|
||||||
APPLICATION_JSON = "application/json"
|
APPLICATION_JSON = "application/json"
|
||||||
)
|
)
|
||||||
@ -28,9 +29,10 @@ const (
|
|||||||
var cliUserAgentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$")
|
var cliUserAgentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$")
|
||||||
|
|
||||||
type API struct {
|
type API struct {
|
||||||
db *geoip2.Reader
|
db *geoip2.Reader
|
||||||
CORS bool
|
CORS bool
|
||||||
Template string
|
ReverseLookup bool
|
||||||
|
Template string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *API { return &API{} }
|
func New() *API { return &API{} }
|
||||||
@ -197,6 +199,14 @@ func (a *API) requestFilter(next http.Handler) http.Handler {
|
|||||||
r.Header.Set(COUNTRY_HEADER, country)
|
r.Header.Set(COUNTRY_HEADER, country)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if a.ReverseLookup {
|
||||||
|
hostname, err := net.LookupAddr(ip.String())
|
||||||
|
if err != nil {
|
||||||
|
r.Header.Set(HOSTNAME_HEADER, err.Error())
|
||||||
|
} else {
|
||||||
|
r.Header.Set(HOSTNAME_HEADER, strings.Join(hostname, ", "))
|
||||||
|
}
|
||||||
|
}
|
||||||
if a.CORS {
|
if a.CORS {
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
10
main.go
10
main.go
@ -11,10 +11,11 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var opts struct {
|
var opts struct {
|
||||||
DBPath string `short:"f" long:"file" description:"Path to GeoIP database" value-name:"FILE" default:""`
|
DBPath string `short:"f" long:"file" description:"Path to GeoIP database" value-name:"FILE" default:""`
|
||||||
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
|
Listen string `short:"l" long:"listen" description:"Listening address" value-name:"ADDR" default:":8080"`
|
||||||
CORS bool `short:"x" long:"cors" description:"Allow requests from other domains" default:"false"`
|
CORS bool `short:"x" long:"cors" description:"Allow requests from other domains" default:"false"`
|
||||||
Template string `short:"t" long:"template" description:"Path to template" default:"index.html"`
|
ReverseLookup bool `short:"r" long:"reverselookup" description:"Perform reverse hostname lookups" default:"false"`
|
||||||
|
Template string `short:"t" long:"template" description:"Path to template" default:"index.html"`
|
||||||
}
|
}
|
||||||
_, err := flags.ParseArgs(&opts, os.Args)
|
_, err := flags.ParseArgs(&opts, os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -32,6 +33,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.CORS = opts.CORS
|
a.CORS = opts.CORS
|
||||||
|
a.ReverseLookup = opts.ReverseLookup
|
||||||
a.Template = opts.Template
|
a.Template = opts.Template
|
||||||
|
|
||||||
log.Printf("Listening on %s", opts.Listen)
|
log.Printf("Listening on %s", opts.Listen)
|
||||||
|
Loading…
Reference in New Issue
Block a user