Skip to content

Commit

Permalink
fix: add check for valid IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
strideynet committed Mar 14, 2022
1 parent d739aa8 commit 05e2a6c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions backend/api_v1/rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type rdnsParams struct {

func rdns(g group, log *zap.Logger, dnsServer string) {
g.GET("/:ip", func(ctx *gin.Context) {
ip := ctx.Param("ip")

isJson := ctx.ContentType() == "application/json"
var params rdnsParams
if err := ctx.BindQuery(&params); err != nil {
Expand All @@ -31,6 +29,19 @@ func rdns(g group, log *zap.Logger, dnsServer string) {
return
}

ip := ctx.Param("ip")
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
if ctx.ContentType() == "application/json" {
ctx.JSON(400, map[string]string{
"message": "Provided IP is not valid",
})
} else {
ctx.String(400, "Provided IP is not valid")
}
return
}

if !params.Trace {
hosts, err := net.LookupAddr(ip)
if err != nil || len(hosts) == 0 {
Expand All @@ -54,7 +65,6 @@ func rdns(g group, log *zap.Logger, dnsServer string) {
return
}

ipAddr := net.ParseIP(ip)
result, err := dnsLib.LookupRDNS(
log, ipAddr, dnsServer,
)
Expand Down

0 comments on commit 05e2a6c

Please sign in to comment.