-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (58 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// IPT
// Author: Daniel Debny - github.com/debek
// Year: 24.12.2023
// Description: An application for testing TCP/UDP connection to a given host and port.
package main
import (
"fmt"
"net"
"os"
"time"
)
const (
Red = "\033[31m"
Green = "\033[32m"
Reset = "\033[0m"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: ipt <ip> <port> [<timeout>]")
os.Exit(1)
}
ip := os.Args[1]
port := os.Args[2]
timeout := "5s"
if len(os.Args) == 4 {
timeout = os.Args[3] + "s"
}
timeoutDuration, err := time.ParseDuration(timeout)
if err != nil {
fmt.Printf(Red+"Error: Invalid timeout format: '%s'. Please provide time in format like '5s' for 5 seconds.\n"+Reset, timeout)
os.Exit(1)
}
startTime := time.Now()
fmt.Printf("[%s] START: Connection Test to %s:%s with a timeout of %s.\n", startTime.Format("2006-01-02 15:04:05"), ip, port, timeoutDuration)
for {
currentTime := time.Now()
err := checkConnection(ip, port, timeoutDuration)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
fmt.Printf(Red+"[%s] FAILURE: Connection to %s:%s timed out (possible firewall rejection)\n"+Reset, currentTime.Format("2006-01-02 15:04:05"), ip, port)
} else {
fmt.Printf(Red+"[%s] FAILURE: Connection to %s:%s failed (service not running or blocked)\n"+Reset, currentTime.Format("2006-01-02 15:04:05"), ip, port)
}
} else {
fmt.Printf(Green+"[%s] SUCCESS: Connected to %s:%s\n"+Reset, currentTime.Format("2006-01-02 15:04:05"), ip, port)
}
time.Sleep(1 * time.Second)
}
}
func checkConnection(ip string, port string, timeout time.Duration) error {
address := net.JoinHostPort(ip, port)
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return err
}
defer conn.Close()
return nil
}