diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..654da7a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +go.mod diff --git a/imgs/clock.png b/imgs/clock.png new file mode 100644 index 0000000..bb99da0 Binary files /dev/null and b/imgs/clock.png differ diff --git a/imgs/goroutins.jpg b/imgs/goroutins.jpg new file mode 100644 index 0000000..79f5ee0 Binary files /dev/null and b/imgs/goroutins.jpg differ diff --git a/imgs/puente.png b/imgs/puente.png new file mode 100644 index 0000000..68c5a1d Binary files /dev/null and b/imgs/puente.png differ diff --git a/main.go b/main.go index f356e36..34d3271 100644 --- a/main.go +++ b/main.go @@ -1,36 +1,49 @@ package main import ( + "time" + "flag" "fmt" "net" "sync" ) +//IsOpen connect to ports +func IsOpen(ipstr string,port int,timeout time.Duration) bool{ + target:= fmt.Sprintf("%s:%d",ipstr,port) + conn, err := net.DialTimeout("tcp", target, timeout) + + if err != nil { + return false + } + defer conn.Close() + return true +} func main() { - hostname := "localhost" + // input user -ip "1.1.1.1" + var ipstr string + flag.StringVar(&ipstr, "ip", "localhost", "ip target") + flag.Parse() // worker pool channel - conSema := make(chan struct{}, 10) + timeout:=500*time.Millisecond var wg sync.WaitGroup + conSema:= make(chan bool,10) + for i := 1; i < 65535; i++ { wg.Add(1) - conSema <- struct{}{} + conSema <- true go func(port int) { - addr := fmt.Sprintf("%s:%d", hostname, port) - conn, err := net.Dial("tcp", addr) - if err != nil { - // handle error - fmt.Printf("port %d closed: %v\n", port, err) - } else { - fmt.Printf("port %d open\n", port) - conn.Close() + + if IsOpen(ipstr,port,timeout){ + fmt.Printf("Open port: %d \n",port) } + <-conSema wg.Done() }(i) } wg.Wait() - }