Skip to content

Commit

Permalink
[*]解决部分端口扫描时间过长的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lcvvvv committed Apr 11, 2021
1 parent 1598f54 commit b1564a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 24 additions & 1 deletion gonmap.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gonmap

import (
"context"
"errors"
"fmt"
"strings"
"time"
)

var NMAP *Nmap
Expand Down Expand Up @@ -69,6 +71,27 @@ type Nmap struct {
finger *Finger
}

func (n *Nmap) SafeScan(ip string, port int, timeout time.Duration) *PortInfomation {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
resChan := make(chan *PortInfomation)
go n.safeScanSub(ip, port, ctx, resChan)
for {
select {
case <-ctx.Done():
return newPortInfo()
case res := <-resChan:
return res
}
}
}

func (n *Nmap) safeScanSub(ip string, port int, ctx context.Context, resChan chan *PortInfomation) {
r := n.Scan(ip, port)
resChan <- r
ctx.Done()
}

func (n *Nmap) Scan(ip string, port int) *PortInfomation {
n.target.host = ip
n.target.port = port
Expand Down Expand Up @@ -113,7 +136,7 @@ func (n *Nmap) getPortInfo(p *probe, target *target, tls bool) *PortInfomation {
return portinfo.CLOSED()
}
if strings.Contains(err.Error(), "close") {
return nil
return portinfo
}
//fmt.Println(err)
return portinfo
Expand Down
7 changes: 5 additions & 2 deletions gonmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package gonmap
import (
"fmt"
"testing"
"time"
)

func TestGonmap(t *testing.T) {

status := Init(9)
fmt.Printf("[INFO] 成功加载探针:[%d]个,指纹[%d]条\n", status["PROBE"], status["MATCH"])
fmt.Printf("[INFO] 本次扫描将使用探针:[%d]个,指纹[%d]条\n", status["USED_PROBE"], status["USED_MATCH"])
n := New()
r := n.Scan("10.158.3.42", 443)
fmt.Printf("%s\t%s\t%s\n", n.target.uri, r.Service(), r.Info())
r := n.SafeScan("10.158.3.42", 139, 5*time.Second)
fmt.Printf("%s\t%s\t%s\t%s\n", n.target.uri, r.status, r.Service(), r.Info())

//for i := 1; i <= 10000; i++ {
// fmt.Println("开始探测端口:",i)
// fmt.Println(n.Scan("192.168.217.1", 139))
Expand Down

0 comments on commit b1564a0

Please sign in to comment.