dpde7365 2016-07-17 14:59
浏览 35
已采纳

扫描goroutine中的端口

I am currently in the proces of learning go. To do that, I am making a relatively simple portscanner.

The issue I am facing is that it takes a considerable amount of time to scan these ports. The behaviour i am having is that, if i scan ports (defined as an array of int32 (protobuf doesnt support int16), NOT using goroutines works, but is quite slow when scanning more then 5 ports as you can imagine, since it is not running in parallel.

To achieve parallelism, I came up with the following bits of code (explanation + issue comes after code):

//entry point for port scanning
var results []*portscan.ScanResult
//len(splitPorts) is the given string (see benchmark below) chopped up in an int32 slice
ch := make(chan *portscan.ScanResult, len(splitPorts))

var wg sync.WaitGroup
for _, port := range splitPorts {
    connect(ip, port, req.Timeout, ch, &wg)
}
wg.Wait()

for elem := range ch {
    results = append(results, elem)
}

// go routine
func connect(ip string, port, timeout int32, ch chan *portscan.ScanResult, wg *sync.WaitGroup) {
    wg.Add(1)
    go func() {
        res := &portscan.ScanResult{
            Port:   port,
            IsOpen: false,
        }
        conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Duration(timeout)*time.Millisecond)

        if err == nil {
            conn.Close()
            res.IsOpen = true
        }
        ch <- res
        wg.Done()
    }()
}

So protobuf prepared me a struct which looks as follows:

type ScanResult struct {
    Port   int32 `protobuf:"varint,1,opt,name=port" json:"port,omitempty"`
    IsOpen bool  `protobuf:"varint,2,opt,name=isOpen" json:"isOpen,omitempty"`
}

As seen in the first line of the code snippet, I have defined a slice, to hold all results, the idea is that my application scans ports in parallel and when that is done, send the results to whoever is interested.

However, using this code, the program gets stuck.

I run this benchmark to test its performance:

func BenchmarkPortScan(b *testing.B) {
  request := &portscan.ScanPortsRequest{
      Ip:        "62.129.139.214",
      PortRange: "20,21,22,23",
      Timeout:   500,
  }

  svc := newService()

  for i := 0; i < b.N; i++ {
      svc.ScanPorts(nil, request)
  }
}

What is the cause for it to get stuck. Does looking at this code give anything away?

So in short, what I want my end result to be is that each port is scanned in a different go routine, and when they all finished, everything comes together in a resulting slice of ScanResult.

I hope I have been clear and provided enough information for you guys to help me.

Oh, and im especially looking for pointers, and the learning bit, not persee working code samples.

  • 写回答

2条回答 默认 最新

  • dongmibeng5885 2016-07-17 15:30
    关注

    You need to close the channel after wg.Wait(). Otherwise your range for loop gets stuck.

    Other than that your code looks fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b