goroutine2KBgoroutinegoroutinegoroutinegoroutine

1.循环扫描

goroutine
package main

import (
 "fmt"
 "net"
 "time"
)

func main() {
 fmt.Println("TCP端口扫描启动...")
 start := time.Now()
 for i := 1; i <= 20; i++ {
  address := fmt.Sprintf("192.168.0.109:%d", i)
  conn, err := net.Dial("tcp", address)
  if err != nil {
   fmt.Printf(" %s 关闭\n", address)
   continue
  }
  conn.Close()
  fmt.Printf(" %s 打开\n", address)
 }

 elasped := time.Since(start) / 1e9
 fmt.Printf("\n 经过了%d秒 \n", elasped)
}

TCP端口扫描启动...
 192.168.0.109:1 关闭
 192.168.0.109:2 关闭
 192.168.0.109:3 关闭
 192.168.0.109:4 关闭
 192.168.0.109:5 关闭
 192.168.0.109:6 关闭
 192.168.0.109:7 关闭
 192.168.0.109:8 关闭
 192.168.0.109:9 关闭
 192.168.0.109:10 关闭
 192.168.0.109:11 关闭
 192.168.0.109:12 关闭
 192.168.0.109:13 关闭
 192.168.0.109:14 关闭
 192.168.0.109:15 关闭
 192.168.0.109:16 关闭
 192.168.0.109:17 关闭
 192.168.0.109:18 关闭
 192.168.0.109:19 关闭
 192.168.0.109:20 关闭

 经过了40秒

2.并发扫描

goroutine
package main

import (
 "fmt"
 "net"
 "sync"
 "time"
)

var wg sync.WaitGroup

func main() {
 fmt.Println("TCP端口扫描启动...")
 start := time.Now()
 for i := 1; i <= 20; i++ {
  wg.Add(1)
  go func(i int) {
   defer wg.Done()
   address := fmt.Sprintf("192.168.0.109:%d", i)
   conn, err := net.Dial("tcp", address)
   if err != nil {
    fmt.Printf(" %s 关闭\n", address)
    return
   }
   conn.Close()
   fmt.Printf(" %s 打开\n", address)
  }(i)
 }
 wg.Wait()
 elasped := time.Since(start) / 1e9
 fmt.Printf("经过了%d秒", elasped)
}

TCP端口扫描启动...
 192.168.0.109:4 关闭
 192.168.0.109:6 关闭 
 192.168.0.109:11 关闭
 192.168.0.109:7 关闭 
 192.168.0.109:8 关闭 
 192.168.0.109:14 关闭
 192.168.0.109:3 关闭
 192.168.0.109:20 关闭
 192.168.0.109:16 关闭
 192.168.0.109:12 关闭
 192.168.0.109:1 关闭
 192.168.0.109:17 关闭
 192.168.0.109:18 关闭
 192.168.0.109:13 关闭
 192.168.0.109:9 关闭
 192.168.0.109:10 关闭
 192.168.0.109:5 关闭
 192.168.0.109:15 关闭
 192.168.0.109:19 关闭
 192.168.0.109:2 关闭
经过了2秒
1~6553521s
package main

import (
 "fmt"
 "net"
 "sync"
 "time"
)

var wg sync.WaitGroup

func main() {
 fmt.Println("TCP端口扫描启动...")
 start := time.Now()
 for i := 1; i <= 65535; i++ {
  wg.Add(1)
  go func(i int) {
   defer wg.Done()
   address := fmt.Sprintf("192.168.0.109:%d", i)
   conn, err := net.Dial("tcp", address)
   if err != nil {
    fmt.Printf(" %s 关闭\n", address)
    return
   }
   conn.Close()
   fmt.Printf(" %s 打开\n", address)
  }(i)
 }
 wg.Wait()
 elasped := time.Since(start) / 1e9
 fmt.Printf("\n 经过了%d秒 \n", elasped)
}

 ...
 192.168.0.109:39702 关闭
 192.168.0.109:37282 关闭
 192.168.0.109:39490 关闭
 192.168.0.109:39795 关闭
 192.168.0.109:39613 关闭
 192.168.0.109:39707 关闭
 192.168.0.109:39723 关闭
 192.168.0.109:39732 关闭
 192.168.0.109:39806 关闭
 192.168.0.109:39682 关闭
 192.168.0.109:39809 关闭
 192.168.0.109:39663 关闭
 192.168.0.109:40006 关闭
 192.168.0.109:39712 关闭
 192.168.0.109:39804 关闭
 192.168.0.109:39731 关闭
 192.168.0.109:39701 关闭
 192.168.0.109:39810 关闭
 192.168.0.109:39725 关闭
 192.168.0.109:39805 关闭
 192.168.0.109:39713 关闭
 192.168.0.109:39489 关闭
 192.168.0.109:39791 关闭
 192.168.0.109:39640 关闭
 192.168.0.109:39667 关闭
 192.168.0.109:39661 关闭

 经过了21秒
goroutine

3.goroutine pool(池)

goroutine100w1个亿IP噌噌噌
goroutinemgoroutinechannelchannelnmgoroutine

同样是端口扫描任务,代码改造如下:

package main

import (
 "fmt"
 "net"
)

func main() {
 fmt.Println("TCP端口扫描启动...")
 fmt.Println("下列端口状态为打开:")
 n := make(chan int, 100)
 results := make(chan int, 100)
    
    //创建20000个goroutine
 for m := 0; m < 20000; m++ {
  go worker(n, results)
 }
    
    //channel代表了任务数量
 for i := 1; i < 65535; i++ {
  n <- i
 }
    close(n)
    
    //结果
 for port := range results {
  fmt.Println(port)
 }

}

func worker(ports <-chan int, results chan<- int) {
 for port := range ports {
  address := fmt.Sprintf("192.168.0.109:%d", port)
  conn, err := net.Dial("tcp", address)
  if err != nil {
   // fmt.Printf(" %s 关闭\n", address)
   continue
  }
  conn.Close()
  // fmt.Printf(" %s 打开\n", address)
  results <- port
 }
}

TCP端口扫描启动...
下列端口状态为打开:
80
81   
139  
902  
443  
445  
135  
912  
1433 
2383 
2179 
3306 
5357 
5040 
7680 
8080 
33060
43094
43095
49672
49664
49667
49666
49670
49665
50272