You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.4 KiB
58 lines
1.4 KiB
package dial
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/fatih/color"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
func PortDial(address, port, protocol string, timeout int) {
|
|
red := color.New(color.FgRed).SprintFunc()
|
|
yellow := color.New(color.FgYellow).SprintFunc()
|
|
green := color.New(color.FgGreen).SprintFunc()
|
|
blue := color.New(color.FgBlue).SprintFunc()
|
|
ip := digIp(address)
|
|
if ip == "" {
|
|
fmt.Printf("%v ping: %v: Name or service not known\n", blue(protocol), red(address))
|
|
return
|
|
}
|
|
fmt.Printf("%v ping %v (%v) via port %v (timeout: %vs)...\n", blue(protocol), address, ip, port, timeout)
|
|
dialTarget := ip + ":" + port
|
|
|
|
for {
|
|
startTime := time.Now()
|
|
conn, err := net.DialTimeout(protocol, dialTarget, time.Duration(timeout)*time.Second)
|
|
endTime := time.Now()
|
|
elapsedTime := float64(endTime.Sub(startTime)) / float64(time.Millisecond)
|
|
if err != nil {
|
|
fmt.Printf("%s via %v:%v time=%v ms\n", red("TIMEOUT"), ip, port, elapsedTime)
|
|
time.Sleep(time.Duration(timeout) * time.Second)
|
|
|
|
} else {
|
|
err := conn.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if elapsedTime <= 500 {
|
|
fmt.Printf("%s via %v:%v time=%v ms\n", green("OK"), ip, port, elapsedTime)
|
|
} else {
|
|
fmt.Printf("%s via %v:%v time=%v ms\n", yellow("OK"), ip, port, elapsedTime)
|
|
}
|
|
|
|
time.Sleep(time.Duration(1) * time.Second)
|
|
continue
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func digIp(address string) string {
|
|
ip, err := net.ResolveIPAddr("ip", address)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return ip.String()
|
|
}
|