Only lookup hostname if the results are going to need it.
This commit is contained in:
parent
8f3934c13a
commit
5e97b47197
12
main.go
12
main.go
|
@ -40,6 +40,15 @@ func Logger() gin.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func stringInSlice(a string, list []string) bool {
|
||||
for _, b := range list {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func mainHandler(c *gin.Context) {
|
||||
fields := strings.Split(c.Params.ByName("field"), ".")
|
||||
ip, err := net.ResolveTCPAddr("tcp", c.Req.RemoteAddr)
|
||||
|
@ -56,12 +65,15 @@ func mainHandler(c *gin.Context) {
|
|||
c.Set("referer", c.Req.Header.Get("Referer"))
|
||||
c.Set("forwarded", c.Req.Header.Get("X-Forwarded-For"))
|
||||
|
||||
// Only lookup hostname if the results are going to need it.
|
||||
if stringInSlice(fields[0], []string{"", "all", "host"}) {
|
||||
hostnames, err := net.LookupAddr(ip.IP.String())
|
||||
if err != nil {
|
||||
c.Set("host", "")
|
||||
} else {
|
||||
c.Set("host", hostnames[0])
|
||||
}
|
||||
}
|
||||
|
||||
wantsJSON := false
|
||||
if len(fields) >= 2 && fields[1] == "json" {
|
||||
|
|
Loading…
Reference in New Issue