-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### Go template | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
### Example user template template | ||
/go-portscanner | ||
### Example user template | ||
|
||
# IntelliJ project files | ||
.idea | ||
*.iml | ||
out | ||
gen |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# go-portscanner | ||
Port scanner written in Go | ||
|
||
## Requirements | ||
Installed Go | ||
|
||
## Usage | ||
|
||
```shell script | ||
$ git clone git@github.com:bitmaskit/go-portscanner | ||
$ cd go-portscanner | ||
$ go build . | ||
``` | ||
|
||
Addr - address you want to scan | ||
|
||
From - starting port | ||
|
||
To - ending port | ||
|
||
W - number of workers(threads). More threads = faster scan* | ||
```shell script | ||
$ ./go-portscanner -addr=localhost -from=10 -to=444 -w=1000 | ||
``` | ||
|
||
*Based on PC configuration |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/bitmaskit/go-portscanner | ||
|
||
go 1.13 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"net" | ||
"sort" | ||
"time" | ||
) | ||
|
||
var ( | ||
addr string | ||
from int | ||
to int | ||
wCnt int | ||
results = make(chan int) | ||
) | ||
|
||
func worker(addr string, ports chan int) { | ||
var address string | ||
for p := range ports { | ||
address = fmt.Sprintf("%s:%d", addr, p) | ||
conn, err := net.DialTimeout("tcp", address, 5*time.Second) | ||
if err != nil { | ||
results <- 0 | ||
continue | ||
} else { | ||
_ = conn.Close() | ||
results <- p | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
flag.StringVar(&addr, "addr", "localhost", "'addr' indicates address you want to scan.") | ||
flag.IntVar(&from, "from", 1, "'from' indicates starting port.") | ||
flag.IntVar(&to, "to", 65535, "'to' indicates ending port.") | ||
flag.IntVar(&wCnt, "w", 100, "'w' indicates workers count. The more workers you have the faster scan will be. But ") | ||
flag.Parse() | ||
|
||
fmt.Printf("Creating %d workers to scan ports.\n", wCnt) | ||
|
||
ports := make(chan int, wCnt) | ||
for i := 0; i < wCnt; i++ { | ||
go worker(addr, ports) | ||
} | ||
fmt.Printf("%d workers to scan ports.\n", wCnt) | ||
go func() { | ||
fmt.Printf("Scanning %s from %d to %d\n", addr, from, to) | ||
for i := from; i <= to; i++ { | ||
ports <- i | ||
} | ||
}() | ||
|
||
var openPorts []int | ||
for i := from; i < to; i++ { | ||
port := <-results | ||
if port != 0 { | ||
openPorts = append(openPorts, port) | ||
} | ||
} | ||
sort.Ints(openPorts) | ||
for _, port := range openPorts { | ||
fmt.Printf("%d opened \n", port) | ||
} | ||
close(ports) | ||
close(results) | ||
} |