mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2024-12-22 14:27:59 +00:00
55 lines
994 B
Go
55 lines
994 B
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/dcarrillo/whatismyip/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type JSONScanResponse struct {
|
|
IP string `json:"ip"`
|
|
Port int `json:"port"`
|
|
Reachable bool `json:"reachable"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
func scanTCPPort(ctx *gin.Context) {
|
|
port, err := strconv.Atoi(ctx.Params.ByName("port"))
|
|
if err == nil && (port < 1 || port > 65535) {
|
|
err = fmt.Errorf("%d is not a valid port number", port)
|
|
}
|
|
if err != nil {
|
|
ctx.JSON(http.StatusBadRequest, JSONScanResponse{
|
|
Reason: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
add := net.TCPAddr{
|
|
IP: net.ParseIP(ctx.ClientIP()),
|
|
Port: port,
|
|
}
|
|
|
|
scan := service.PortScanner{
|
|
Address: &add,
|
|
}
|
|
|
|
isOpen, err := scan.IsPortOpen()
|
|
reason := ""
|
|
if err != nil {
|
|
reason = err.Error()
|
|
}
|
|
|
|
response := JSONScanResponse{
|
|
IP: ctx.ClientIP(),
|
|
Port: port,
|
|
Reachable: isOpen,
|
|
Reason: reason,
|
|
}
|
|
ctx.JSON(http.StatusOK, response)
|
|
}
|