Created
January 26, 2018 14:30
-
-
Save CarpeNoctem/3817a6083de741032da8bbe42712dba5 to your computer and use it in GitHub Desktop.
Dividing work in a go (golang) port scanner
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
func ScanRangeTwo(min, max, num_threads int) (open_ports []string) { | |
open_ch := make(chan []string, num_threads) | |
high := min - 1 | |
for i := 0; i < num_threads; i++ { | |
low := high + 1 | |
high = min + (i+1)*(max-min)/num_threads | |
//fmt.Printf("Child #%d should scan %d through %d\n", i+1, low, high) | |
go CheckRange(open_ch, low, high) | |
} | |
for i := 0; i < num_threads; i++ { | |
result := <-open_ch | |
open_ports = append(open_ports, result...) | |
} | |
return open_ports | |
} |
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
func ScanRange(lower, upper int64) (open_ports []string) { | |
open_ch := make(chan []string, threads) | |
num_ports := int(upper - lower + 1) | |
ports_per_thread := int(math.Ceil(float64(num_ports) / float64(threads))) | |
fmt.Println("Ports per thread:", ports_per_thread) | |
for i := 0; i < threads; i++ { | |
child_lower := i*ports_per_thread + int(lower) | |
child_upper := i*ports_per_thread + ports_per_thread | |
if child_upper > num_ports { | |
child_upper = num_ports | |
} | |
if child_lower > num_ports { | |
child_lower = num_ports | |
} | |
//fmt.Printf("Child number %d about to try lower: %d - upper: %d\n", i, child_lower, child_upper) | |
go CheckRange(open_ch, child_lower, child_upper) | |
} | |
waiting := threads | |
for waiting > 0 { | |
//fmt.Println("Waiting...", waiting) | |
result := <-open_ch | |
open_ports = append(open_ports, result...) | |
waiting-- | |
} | |
//fmt.Println("All children finished.") | |
return open_ports | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment