Last active
June 20, 2020 18:19
-
-
Save goujonbe/d3b6b62da9b48ecfda8195abd1f1ffb0 to your computer and use it in GitHub Desktop.
A TCP port scanner that uses goroutines and Channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"sort" | |
) | |
func worker(ports, results chan int) { | |
for p := range ports { | |
address := fmt.Sprintf("scanme.nmap.org:%d", p) | |
conn, err := net.Dial("tcp", address) | |
if err != nil { | |
results <- 0 | |
continue | |
} | |
conn.Close() | |
results <- p | |
} | |
} | |
func main() { | |
// this channel will receive ports to be scanned | |
ports := make(chan int, 100) | |
// this channel will receive results of scanning | |
results := make(chan int) | |
// create a slice to store the results so that they can be sorted later. | |
var openports []int | |
// create a pool of workers | |
for i := 0; i < cap(ports); i++ { | |
go worker(ports, results) | |
} | |
// send ports to be scanned | |
go func() { | |
for i := 1; i <= 1024; i++ { | |
ports <- i | |
} | |
}() | |
for i := 0; i < 1024; i++ { | |
port := <-results | |
if port != 0 { | |
openports = append(openports, port) | |
} | |
} | |
// After all the work has been completed, close the channels | |
close(ports) | |
close(results) | |
// sort open port numbers | |
sort.Ints(openports) | |
for _, port := range openports { | |
fmt.Printf("%d open\n", port) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment