ports/portscan.go

34 lines
618 B
Go
Raw Normal View History

2022-06-20 20:07:41 +00:00
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()
}