之前也写过几个,但今天这个写的爽,学到了些新的东西。
主要用了ants及chromedp.
代码如下:
package main
import (
"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 int
url string
}
var resLiveUrl []liveUrl
func taskFuncWrapper(url, dict string, wg *sync.WaitGroup) taskFunc {
return func() {
dirFuzz(url, dict)
wg.Done()
}
}
func main() {
runTimes := 20
p, _ := 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.WaitGroup
for 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 + dict
client := &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 []string
for {
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 element
var buf []byte
// capture entire browser viewport, returning png with quality=90
if 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
}
代码潦草,记录一下。
文章评论