-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from krystal/rdns
rdns tracing support
- Loading branch information
Showing
5 changed files
with
122 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,75 @@ | ||
package api_v1 | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/gin-gonic/gin" | ||
dnsLib "github.com/krystal/krystal-network-tools/backend/dns" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func rdns(g group) { | ||
type rdnsParams struct { | ||
// Trace is used to define if the DNS record should be traced all the way to the nameserver. | ||
Trace bool `form:"trace"` | ||
} | ||
|
||
func rdns(g group, log *zap.Logger, dnsServer string) { | ||
g.GET("/:ip", func(ctx *gin.Context) { | ||
ip := ctx.Param("ip") | ||
hosts, err := net.LookupAddr(ip) | ||
if err != nil || len(hosts) == 0 { | ||
if ctx.ContentType() == "application/json" { | ||
|
||
isJson := ctx.ContentType() == "application/json" | ||
var params rdnsParams | ||
if err := ctx.BindQuery(¶ms); err != nil { | ||
if isJson { | ||
ctx.JSON(400, map[string]string{ | ||
"message": "Failed to find IP", | ||
"message": err.Error(), | ||
}) | ||
} else { | ||
ctx.String(400, "Failed to find IP") | ||
ctx.String(400, "unable to parse query params: %s", err.Error()) | ||
} | ||
return | ||
} | ||
|
||
if !params.Trace { | ||
hosts, err := net.LookupAddr(ip) | ||
if err != nil || len(hosts) == 0 { | ||
if ctx.ContentType() == "application/json" { | ||
ctx.JSON(400, map[string]string{ | ||
"message": "Failed to find IP", | ||
}) | ||
} else { | ||
ctx.String(400, "Failed to find IP") | ||
} | ||
return | ||
} | ||
if ctx.ContentType() == "application/json" { | ||
ctx.JSON(200, map[string]string{ | ||
"hostname": hosts[0], | ||
}) | ||
} else { | ||
ctx.String(200, hosts[0]) | ||
} | ||
|
||
return | ||
} | ||
|
||
result, err := dnsLib.LookupRDNS( | ||
log, ip, dnsServer, | ||
) | ||
if err != nil { | ||
ctx.Error(&gin.Error{ | ||
Type: gin.ErrorTypePublic, | ||
Err: fmt.Errorf("failed to perform dns lookup: %v", err), | ||
}) | ||
return | ||
} | ||
if ctx.ContentType() == "application/json" { | ||
ctx.JSON(200, map[string]string{ | ||
"hostname": hosts[0], | ||
ctx.JSON(200, map[string]interface{}{ | ||
"trace": result, | ||
}) | ||
} else { | ||
ctx.String(200, hosts[0]) | ||
ctx.String(200, result.String()) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters