(u string, added map[string]bool, excluded map[string]bool)
| 1354 | } |
| 1355 | |
| 1356 | func CrawlUrls(u string, added map[string]bool, excluded map[string]bool) []string { |
| 1357 | webStruct, err := GetWebsite(u, false, false) // get body without cachebuster. TODO use response w/o cachebuster from recon, so it doesn't have to be fetched again |
| 1358 | if err != nil { |
| 1359 | msg := fmt.Sprintf("Error while crawling %s: %s\n", u, err.Error()) |
| 1360 | Print(msg, Red) |
| 1361 | return []string{} |
| 1362 | } |
| 1363 | bodyReader := strings.NewReader(webStruct.Body) |
| 1364 | tokenizer := html.NewTokenizer(bodyReader) |
| 1365 | |
| 1366 | var urls []string |
| 1367 | |
| 1368 | eof := false |
| 1369 | for !eof { |
| 1370 | tokentype := tokenizer.Next() |
| 1371 | |
| 1372 | switch tokentype { |
| 1373 | case html.StartTagToken, html.SelfClosingTagToken: |
| 1374 | |
| 1375 | token := tokenizer.Token() |
| 1376 | |
| 1377 | if token.Data == "a" || token.Data == "link" { |
| 1378 | for _, a := range token.Attr { |
| 1379 | if a.Key == "href" { |
| 1380 | //if strings.HasSuffix(a.Val, ".css") && strings.Contains(a.Val, ".css?") { // TODO: Flag to exclude css files from thorough scanning |
| 1381 | // break |
| 1382 | //} |
| 1383 | urls = addUrl(urls, a.Val, added, excluded) |
| 1384 | break |
| 1385 | } |
| 1386 | } |
| 1387 | } else if token.Data == "script" { |
| 1388 | for _, a := range token.Attr { |
| 1389 | if a.Key == "src" { |
| 1390 | urls = addUrl(urls, a.Val, added, excluded) |
| 1391 | break |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | // When EOF is reached a html.ErrorToken appears |
| 1397 | case html.ErrorToken: |
| 1398 | err := tokenizer.Err() |
| 1399 | if err == io.EOF { |
| 1400 | eof = true |
| 1401 | break |
| 1402 | } |
| 1403 | msg := fmt.Sprintf("error tokenizing HTML: %+v", tokenizer.Err()) |
| 1404 | Print(msg, Yellow) |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | // redirect |
| 1409 | if webStruct.Headers["Location"] != nil { |
| 1410 | if h := webStruct.Headers["Location"][0]; h != "" { |
| 1411 | urls = addUrl(urls, h, added, excluded) |
| 1412 | } |
| 1413 | } |
no test coverage detected