commit 030f2782a1d23735e5528423fca5eaded51b4569 Author: mitch Date: Mon Jun 20 16:07:41 2022 -0400 Initial Commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7717aa0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ports + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..c663174 --- /dev/null +++ b/main.go @@ -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 +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..be19d0a --- /dev/null +++ b/main_test.go @@ -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) + } +} diff --git a/portscan.go b/portscan.go new file mode 100644 index 0000000..22f811b --- /dev/null +++ b/portscan.go @@ -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() +} diff --git a/portscan_test.go b/portscan_test.go new file mode 100644 index 0000000..fc08c65 --- /dev/null +++ b/portscan_test.go @@ -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 +}