diff --git a/go.mod b/go.mod index 6d60f8e..d6b646b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,12 @@ module DockerST go 1.22 + +require github.com/schollz/progressbar/v3 v3.14.4 + +require ( + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/term v0.20.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9af5570 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/schollz/progressbar/v3 v3.14.4 h1:W9ZrDSJk7eqmQhd3uxFNNcTr0QL+xuGNI9dEMrw0r74= +github.com/schollz/progressbar/v3 v3.14.4/go.mod h1:aT3UQ7yGm+2ZjeXPqsjTenwL3ddUiuZ0kfQ/2tHlyNI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= diff --git a/task/ip.go b/task/ip.go index f5b0667..17a7057 100644 --- a/task/ip.go +++ b/task/ip.go @@ -8,7 +8,6 @@ import ( "math/rand" "net" "net/http" - "sync" "time" ) @@ -37,7 +36,7 @@ var ( type IPRangeList struct { Ips []*net.IPAddr unusedIpCount int - wg sync.WaitGroup + delays []IPDelay } func init() { @@ -50,6 +49,7 @@ func CreateData() *IPRangeList { return &IPRangeList{ Ips: ips, unusedIpCount: 0, + delays: []IPDelay{}, } } diff --git a/task/tcpping.go b/task/tcpping.go index 6be0046..a17f76b 100644 --- a/task/tcpping.go +++ b/task/tcpping.go @@ -3,7 +3,10 @@ package task import ( "fmt" "net" + "sync" "time" + + "github.com/schollz/progressbar/v3" ) var ( @@ -13,24 +16,45 @@ var ( TcpConnectTimeOut = time.Second * 1 ) +type IPDelay struct { + IP *net.IPAddr + Delay time.Duration +} + func (p *IPRangeList) Run() *IPRangeList { + var wg sync.WaitGroup ch := make(chan struct{}, Routines) + + // 创建进度条 + bar := progressbar.NewOptions(len(p.Ips), + progressbar.OptionSetWidth(50), + progressbar.OptionSetDescription("测试IP中"), + progressbar.OptionShowCount(), + progressbar.OptionShowIts(), + ) + for _, ip := range p.Ips { - p.wg.Add(1) + wg.Add(1) ch <- struct{}{} // 控制最大并发数 go func(ip *net.IPAddr) { - defer p.wg.Done() + defer wg.Done() defer func() { <-ch }() // 释放并发控制 success, duration := TCPing(ip) - if success { - p.unusedIpCount++ - } else { - // 删除 + if success && duration > 0 { + p.delays = append(p.delays, IPDelay{IP: ip, Delay: duration}) } - fmt.Printf("IP: %s, Success: %t, Duration: %v\n", ip.String(), success, duration) + err := bar.Add(1) + if err != nil { + return + } // 更新进度条 }(ip) } - // 多线程执行 + wg.Wait() + err := bar.Finish() + if err != nil { + return nil + } + fmt.Printf("调用成功,可用的IP数量: %d\n", len(p.delays)) return p } @@ -46,7 +70,11 @@ func TCPing(ip *net.IPAddr) (bool, time.Duration) { if err != nil { return false, 0 } - defer conn.Close() + defer func(conn net.Conn) { + err = conn.Close() + if err != nil { + } + }(conn) duration := time.Since(startTime) return true, duration }