From de773d2b03499b301d8c82deaece9ce51a125248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E6=8C=AF=E8=AE=AD?= <5552123@qq.com> Date: Tue, 14 Jan 2020 16:24:02 +0800 Subject: [PATCH] scan --- .gitignore | 1 + go.mod | 2 ++ go.sum | 2 ++ main.go | 7 ++----- scan/scan.go | 26 ++++++++++++++++---------- 5 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index b45c28f..caf14ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea log +.DS_Store diff --git a/go.mod b/go.mod index f135320..5dbef87 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/xs25cn/scanPort go 1.13 + +require github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ec543bf --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d h1:4ianvxb8s3oyizgjuWWxGuTAUU+6JStcvj6BuHS4PVY= +github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d/go.mod h1:5G2EjwzgZUPnnReoKvPWVneT8APYbyKkihDVAHUi0II= diff --git a/main.go b/main.go index 45b77b2..944acd4 100644 --- a/main.go +++ b/main.go @@ -33,11 +33,8 @@ func main() { lib.Mkdir(*path) //初始化 - scanIP := scan.ScanIp{ - Debug: true, - Timeout: *timeout, - Process: *process, - } + scanIP:=scan.NewScanIp(*timeout,*process,true) + ips, err := scanIP.GetAllIp(*ip) if err != nil { fmt.Println(err.Error()) diff --git a/scan/scan.go b/scan/scan.go index 563542c..4211276 100644 --- a/scan/scan.go +++ b/scan/scan.go @@ -14,9 +14,17 @@ import ( //ip 扫描 type ScanIp struct { - Debug bool - Timeout int - Process int + debug bool + timeout int + process int +} + +func NewScanIp(timeout int,process int,debug bool) *ScanIp { + return &ScanIp{ + debug:debug, + timeout:timeout, + process:process, + } } //获取开放端口号 @@ -30,14 +38,14 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string) []int { ) ports, _ := s.getAllPort(port) total = len(ports) - if total < s.Process { + if total < s.process { pageCount = total } else { - pageCount = s.Process + pageCount = s.process } num = int(math.Ceil(float64(total) / float64(pageCount))) - s.sendLog(fmt.Sprintf("%v 【%v】需要扫描端口总数:%v 个,总协程:%v 个,每个协程处理:%v 个,超时时间:%v毫秒", time.Now().Format("2006-01-02 15:04:05"), ip, total, pageCount, num, s.Timeout)) + s.sendLog(fmt.Sprintf("%v 【%v】需要扫描端口总数:%v 个,总协程:%v 个,每个协程处理:%v 个,超时时间:%v毫秒", time.Now().Format("2006-01-02 15:04:05"), ip, total, pageCount, num, s.timeout)) start := time.Now() all := map[int][]int{} for i := 1; i <= pageCount; i++ { @@ -70,9 +78,7 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string) []int { }(v, k) } wg.Wait() - s.sendLog(fmt.Sprintf("%v 【%v】扫描结束,执行时长%.3fs , 所有开放的端口:%v", time.Now().Format("2006-01-02 15:04:05"), ip, time.Since(start).Seconds(), openPorts)) - time.Sleep(time.Second * 1) return openPorts } @@ -117,7 +123,7 @@ func (s *ScanIp) GetAllIp(ip string) ([]string, error) { //记录日志 func (s *ScanIp) sendLog(str string) { - if s.Debug == true { + if s.debug == true { fmt.Println(str) } } @@ -168,7 +174,7 @@ func (s *ScanIp) filterPort(str string) (int, error) { //查看端口号是否打开 func (s *ScanIp) isOpen(ip string, port int) bool { - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Millisecond*time.Duration(s.Timeout)) + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Millisecond*time.Duration(s.timeout)) if err != nil { if strings.Contains(err.Error(),"too many open files"){ fmt.Println("连接数超出系统限制!"+err.Error())