之前也写过几个,但今天这个写的爽,学到了些新的东西。
主要用了ants及chromedp.
代码如下:
package mainimport ("bufio""crypto/md5""fmt""context""github.com/panjf2000/ants/v2""io/ioutil""log""net/http""os""sync""github.com/chromedp/chromedp""time")// 目录扫描并截图type taskFunc func()type liveUrl struct {status inturl string}var resLiveUrl []liveUrlfunc taskFuncWrapper(url, dict string, wg *sync.WaitGroup) taskFunc {return func() {dirFuzz(url, dict)wg.Done()}}func main() {runTimes := 20p, _ := ants.NewPool(runTimes)defer p.Release()log.Println("开始运行")dict,err1 := readFile("E:\\dicc.txt")if err1 != nil {log.Println(err1)}log.Println("字典总数: ",len(dict))log.Println("运行线程: ",runTimes)var wg sync.WaitGroupfor i := 0; i < len(dict); i++ {wg.Add(1)p.Submit(taskFuncWrapper("http://192.168.227.129/",dict[i],&wg))}wg.Wait()log.Println("运行完成")fmt.Println(resLiveUrl)}func dirFuzz(url, dict string){//log.Println(url, dict)defer func() {recover()}()tmpUrl := url + dictclient := &http.Client{}// 创建请求req, _ := http.NewRequest("HEAD", tmpUrl, nil)req.Header.Set("User-Agent", "test")resp, err := client.Do(req)if resp.StatusCode == 200 || resp.StatusCode == 301 || resp.StatusCode == 302{resLiveUrl = append(resLiveUrl, liveUrl{resp.StatusCode,tmpUrl})fmt.Println(tmpUrl,resp.StatusCode)screen(tmpUrl)}if err != nil {// 格式化返回错误log.Println("请求出错:",err)}// 延迟处理的函数defer func() {resp.Body.Close()}()}func screen(url string){options := []chromedp.ExecAllocatorOption{chromedp.Flag("headless", true),chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),}options = append(chromedp.DefaultExecAllocatorOptions[:], options...)allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), options...)defer cancel()// 创建chrome示例ctx, cancel := chromedp.NewContext(allocCtx)defer cancel()ctx, cancel = context.WithTimeout(ctx, 5*time.Second)defer cancel()screenshot(ctx,url,MD5(url))}func readFile(filePath string)([]string,error){file,err := os.Open(filePath)if err != nil{return nil, err}defer file.Close()buf := bufio.NewScanner(file)var lineArr []stringfor {if !buf.Scan() {break}line := buf.Text()lineArr = append(lineArr,line)}return lineArr,err}func screenshot(ctx context.Context, url, name string) []byte{// capture screenshot of an elementvar buf []byte// capture entire browser viewport, returning png with quality=90if err := chromedp.Run(ctx, fullScreenshotHandle(url, 90, &buf)); err != nil {log.Fatal(err)}if err := ioutil.WriteFile(name+"_fullScreenshot.png", buf, 0o644); err != nil {log.Fatal(err)}log.Printf(name+"_fullScreenshot.png")return buf}// fullScreenshotHandle takes a screenshot of the entire browser viewport.func fullScreenshotHandle(urlstr string, quality int, res *[]byte) chromedp.Tasks {return chromedp.Tasks{chromedp.Navigate(urlstr),chromedp.FullScreenshot(res, quality),}}func MD5(str string) string {data := []byte(str)has := md5.Sum(data)md5str := fmt.Sprintf("%x", has)return md5str}

代码潦草,记录一下。
文章评论