TCP 扫描器

单线程

func main() {
	for i := 22; i < 1000; i++ {
		address := fmt.Sprintf("192.168.141.135:%d", i)
		connec, err := net.Dial("tcp", address)
		if err != nil {
			fmt.Println(i, "端口打开失败")
			continue
		}
		defer connec.Close()
		fmt.Println(i, "端口打开成功")
	}
}

使用单线程问题是比较慢。扫描完大概是半个小时

多线程

func main() {
	var wg sync.WaitGroup
	for i := 22; i < 5535; i++ {
		wg.Add(1)
		go func(j int) {
			defer wg.Done()
			address := fmt.Sprintf("192.168.141.135:%d", j)
			conn, err := net.Dial("tcp", address)
			if err != nil {
				// fmt.Println(address, "端口关闭了")
				return
			}
			defer conn.Close()
			fmt.Println(address, "端口打开了")
		}(i)
	}
	wg.Wait()
}

线程池

工作的结构如下

代码

  • 主线程和处理的 go 应该是并发执行。
func work(port chan int, wg *sync.WaitGroup, result chan int) {
	for p := range port {
		defer wg.Done()
		fmt.Println(p)
		address := fmt.Sprintf("192.168.141.135:%d", p) //扫描地址
		conn, err := net.Dial("tcp", address)
		if err != nil {
			result <- 0 //
			continue
		}
		defer conn.Close() //关闭
		result <- p        //把端口传回去就可以了
	}
}

func main() {
	ports := make(chan int, 100) //带有100个缓冲池
	results := make(chan int)    //这个只有main自己干,他就不需要缓冲
	openport, closeport := make([]int, 1), make([]int, 1)
	var wg sync.WaitGroup
	for i := 0; i < cap(ports); i++ {
		go work(ports, &wg, results) //相当于搞了 100 个干活的。
	}

	go func() {
		for i := 1; i < 1024; i++ {
			wg.Add(1)
			ports <- i
		}
	}()

	for i := 1; i < 1024; i++ {
		port := <-results //返回的数据
		if port != 0 {
			openport = append(openport, port)
		} else {
			closeport = append(closeport, port)
		}
	}

	sort.Ints(openport)
	fmt.Println(openport)
	wg.Wait()
	close(ports) //关闭 port
}

利用线程池计算一个数的平方

package main

import (
	"fmt"
	"time"
)

var (
	a chan int
	b chan int
)

func work(a1, a2 chan int) {
	time.Sleep(time.Second * 2)
	for an := range a1 {
		a2 <- an * an
	}
}

func main() {
	a = make(chan int, 101)
	b = make(chan int)
	for i := 0; i < 101; i++ {
		go work(a, b)
	}
	for i := 0; i < 101; i++ {
		a <- i
	}
	for i := 0; i < 101; i++ {
		ret := <-b
		fmt.Println(ret)
	}
}
posted @ 2021-09-04 17:27  沧海一声笑rush  阅读(55)  评论(0编辑  收藏  举报