Add the ability to test local port reachability from the world
This commit is contained in:
parent
91af7c833b
commit
78818ebbd9
22
main.go
22
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())
|
||||
|
|
Loading…
Reference in New Issue