Initial Commit

This commit is contained in:
mitch 2022-06-20 16:07:41 -04:00
commit 030f2782a1
5 changed files with 134 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module ports
go 1.17

34
main.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"flag"
"fmt"
"strconv"
"sync"
)
func main() {
remoteIpPtr := flag.String("remote", "", "Remote Server")
flag.Parse()
ports := flag.Args()
remoteIp := *remoteIpPtr
fmt.Println(remoteIp)
var hostWG sync.WaitGroup
ports = portsSetup(ports)
go scanIp(ports, remoteIp, &hostWG)
hostWG.Add(1)
hostWG.Wait()
fmt.Println("Process complete.")
}
func portsSetup(ports []string) []string {
if len(ports) == 0 {
for i := 1; i < 65535; i++ {
ports = append(ports, strconv.FormatInt(int64(i), 10))
}
}
return ports
}

39
main_test.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"reflect"
"strconv"
"testing"
)
func TestPortsSetup(t *testing.T) {
t.Run("Single Port", func(t *testing.T) {
want := []string{"1"}
got := portsSetup(want)
assertEqual(t, got, want)
})
t.Run("Multi Port", func(t *testing.T) {
want := []string{"1", "2"}
got := portsSetup(want)
assertEqual(t, got, want)
})
t.Run("Empty Port", func(t *testing.T) {
var ports, want []string
got := portsSetup(ports)
for i := 1; i < 65535; i++ {
want = append(want, strconv.FormatInt(int64(i), 10))
}
assertEqual(t, got, want)
})
}
func assertEqual(t testing.TB, got, want []string) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

33
portscan.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
"net"
"sync"
"time"
)
func connection(port string, ip string, portWG *sync.WaitGroup) (bool, error) {
connString := ip + ":" + port
timeout := time.Second * time.Duration(5)
conn, err := net.DialTimeout("tcp", connString, timeout)
if err != nil {
defer portWG.Done()
return false, err
}
conn.Close()
fmt.Println(port)
defer portWG.Done()
return true, nil
}
func scanIp(ports []string, ip string, hostWG *sync.WaitGroup) {
var portWG sync.WaitGroup
for i := range ports {
portWG.Add(1)
go connection(ports[i], ip, &portWG)
}
portWG.Wait()
defer hostWG.Done()
}

25
portscan_test.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"net"
"testing"
)
func TestConnection(t *testing.T) {
t.Run("Test Connection", func(t *testing.T) {
})
}
func TestScanIp(t *testing.T) {
t.Run("Test Scan IP", func(t *testing.T) {
})
}
type MockConn struct {
net.Conn
}
func (m *MockConn) Read(b []byte) (int, error) {
return 0, nil
}