Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.mod
Binary file added imgs/clock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/goroutins.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/puente.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()

}