Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6000ae2

Browse files
initial commit
1 parent f83d20c commit 6000ae2

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module turbo-scanner
2+
3+
go 1.17

main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"sort"
7+
)
8+
9+
func main() {
10+
if runtime.GOOS == "linux" {
11+
linuxSetup()
12+
}
13+
14+
ports := make(chan int, 1000)
15+
results := make(chan int)
16+
var openports []int
17+
18+
for i := 0; i < cap(ports); i++ {
19+
go routine(ports, results)
20+
}
21+
22+
go func() {
23+
for i := 1; i <= 65535; i++ {
24+
ports <- i
25+
}
26+
}()
27+
28+
for i := 0; i < 65535; i++ {
29+
port := <-results
30+
if port != 0 {
31+
openports = append(openports, port)
32+
}
33+
}
34+
35+
close(ports)
36+
close(results)
37+
sort.Ints(openports)
38+
for _, port := range openports {
39+
fmt.Printf("%d open\n", port)
40+
if runtime.GOOS == "windows" {
41+
fmt.Println(winService(&port))
42+
} else if runtime.GOOS == "darwin" {
43+
fmt.Println(macService(&port))
44+
} else if runtime.GOOS == "linux" {
45+
fmt.Println(linuxService(&port))
46+
}
47+
}
48+
49+
}

routine/routine.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Package routine provides port scanning functionality.
2+
package routine
3+
4+
import (
5+
"fmt"
6+
"net"
7+
)
8+
9+
// Routine takes a host and scans 65,535 tcp ports on the respective host.
10+
func Routine(host *string, ports, results chan int) {
11+
for port := range ports {
12+
address := fmt.Sprintf("%s:%d", *host, port)
13+
conn, err := net.Dial("tcp", address)
14+
if err != nil {
15+
results <- 0
16+
continue
17+
}
18+
conn.Close()
19+
results <- port
20+
}
21+
}

services/services.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Package services provides service specific OS functionality.
2+
package services
3+
4+
import (
5+
"log"
6+
"os/exec"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
// WinService takes a port to obtain the running service on it.
12+
// It returns the Windows service name and respective details.
13+
func WinService(port *int) string {
14+
arg0 := "Get-Process -Id (Get-NetTCPConnection -LocalPort "
15+
arg1 := strconv.Itoa(*port)
16+
arg2 := ").OwningProcess"
17+
args := arg0 + arg1 + arg2
18+
out, _ := exec.Command("powershell", "/C", args).Output()
19+
trimmedOut := strings.TrimSpace(string(out))
20+
trimmedOut += "\n"
21+
return string(trimmedOut)
22+
}
23+
24+
// LinuxService takes a port to obtain the running service on it.
25+
// It returns the Debian-based Linux service name and respective details.
26+
func LinuxService(port *int) string {
27+
arg0 := "netstat -tlnp | grep "
28+
arg1 := strconv.Itoa(*port)
29+
args := arg0 + arg1
30+
out, err := exec.Command("sh", "-c", args).Output()
31+
if err != nil {
32+
log.Fatal("sudo apt install net-tools")
33+
}
34+
return string(out)
35+
}
36+
37+
// MacService takes a port to obtain the running service on it.
38+
// It returns the Mac service name and respective details.
39+
func MacService(port *int) string {
40+
arg0 := "lsof -i :"
41+
arg1 := strconv.Itoa(*port)
42+
args := arg0 + arg1
43+
out, _ := exec.Command("sh", "-c", args).Output()
44+
return string(out)
45+
}

setup/setup.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Package setup provides OS specific setup functionality.
2+
package setup
3+
4+
import "os/exec"
5+
6+
// LinuxSetup installs the net-tools library to ensure netstat has the ability to run.
7+
// It returns any necessary info to stdin from the result of the OS install.
8+
func LinuxSetup() string {
9+
arg0 := "sudo apt install net-tools"
10+
out, _ := exec.Command("sh", "-c", arg0).Output()
11+
return string(out)
12+
}

0 commit comments

Comments
 (0)