Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bitmaskit committed Feb 18, 2020
1 parent 9105be4 commit 4e2c92a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
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
24 changes: 24 additions & 0 deletions README.md
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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/bitmaskit/go-portscanner

go 1.13
68 changes: 68 additions & 0 deletions main.go
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)
}

0 comments on commit 4e2c92a

Please sign in to comment.