mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2026-07-23 21:35:47 +00:00
2e32a20f60
* refactor: export Settings type, Setup returns value * refactor: resolver.Setup takes explicit Settings struct * refactor: GetHeadersWithoutTrustedHeaders takes explicit header params * refactor: server constructors take narrow config * refactor: Router struct with handler methods, remove geoSvc global * refactor: wire DI through main, remove setting.App references * refactor: remove App global, use returned Settings * refactor: update router tests for DI * chore: fix lint issues — rename ServerTimeouts to Timeouts, fix shadowed variables
63 lines
1.1 KiB
Go
63 lines
1.1 KiB
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 (rt *Router) 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
|
|
}
|
|
|
|
ip := net.ParseIP(ctx.ClientIP())
|
|
if ip == nil {
|
|
ctx.JSON(http.StatusBadRequest, JSONScanResponse{
|
|
Reason: "client ip could not be determined",
|
|
})
|
|
return
|
|
}
|
|
|
|
add := net.TCPAddr{
|
|
IP: ip,
|
|
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)
|
|
}
|