34 lines
618 B
Go
34 lines
618 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func connection(port string, ip string, portWG *sync.WaitGroup) (bool, error) {
|
||
|
connString := ip + ":" + port
|
||
|
timeout := time.Second * time.Duration(5)
|
||
|
conn, err := net.DialTimeout("tcp", connString, timeout)
|
||
|
if err != nil {
|
||
|
defer portWG.Done()
|
||
|
return false, err
|
||
|
}
|
||
|
conn.Close()
|
||
|
fmt.Println(port)
|
||
|
defer portWG.Done()
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
func scanIp(ports []string, ip string, hostWG *sync.WaitGroup) {
|
||
|
var portWG sync.WaitGroup
|
||
|
|
||
|
for i := range ports {
|
||
|
portWG.Add(1)
|
||
|
go connection(ports[i], ip, &portWG)
|
||
|
}
|
||
|
portWG.Wait()
|
||
|
defer hostWG.Done()
|
||
|
}
|