diff --git a/main.go b/main.go index b60a9de..52b7bcd 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "path" "strings" "time" + "strconv" "github.com/gin-gonic/gin" ) @@ -50,6 +51,14 @@ func stringInSlice(a string, list []string) bool { return false } +func testRemoteTCPPort(address string) bool { + _, err := net.DialTimeout("tcp", address, 3 * time.Second) + if err != nil { + return false + } + return true +} + func mainHandler(c *gin.Context) { fields := strings.Split(c.Params.ByName("field"), ".") ip, err := net.ResolveTCPAddr("tcp", c.Request.RemoteAddr) @@ -62,6 +71,19 @@ func mainHandler(c *gin.Context) { ip.IP = cfIP } + if fields[0] == "porttest" { + if len(fields) >= 2 { + if port, err := strconv.Atoi(fields[1]); err == nil && port > 0 && port <= 65535 { + c.String(200, fmt.Sprintln(testRemoteTCPPort(ip.IP.String() + ":" + fields[1]))) + } else { + c.String(400, "Invalid Port Number") + } + } else { + c.String(400, "Need Port") + } + return + } + c.Set("ip", ip.IP.String()) c.Set("port", ip.Port) c.Set("ua", c.Request.UserAgent())