From 78818ebbd9f2c069aebcba0d2e57eb6b0a676f23 Mon Sep 17 00:00:00 2001 From: George Shammas Date: Mon, 16 Nov 2015 21:16:06 +0000 Subject: [PATCH] Add the ability to test local port reachability from the world --- main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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())