Add the ability to test local port reachability from the world

This commit is contained in:
George Shammas 2015-11-16 21:16:06 +00:00
parent 91af7c833b
commit 78818ebbd9
1 changed files with 22 additions and 0 deletions

22
main.go
View File

@ -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())