-
Notifications
You must be signed in to change notification settings - Fork 14
/
getFavicon.go
94 lines (89 loc) · 2.15 KB
/
getFavicon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package fingerprint
import (
"crypto/tls"
"fmt"
"github.com/youki992/VscanPlus/pkg"
"github.com/youki992/VscanPlus/pkg/httpx/common/stringz"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"time"
)
func xegexpjs(reg string, resp string) (reslut1 [][]string) {
reg1 := regexp.MustCompile(reg)
if reg1 == nil {
log.Println("regexp err")
return nil
}
result1 := reg1.FindAllStringSubmatch(resp, -1)
return result1
}
func getfavicon(httpbody string, turl string) string {
faviconpaths := xegexpjs(`href="(.*?favicon....)"`, httpbody)
var faviconpath string
u, err := url.Parse(turl)
if err != nil {
panic(err)
}
turl = u.Scheme + "://" + u.Host
if len(faviconpaths) > 0 {
fav := faviconpaths[0][1]
if fav[:2] == "//" {
faviconpath = "http:" + fav
} else {
if fav[:4] == "http" {
faviconpath = fav
} else {
faviconpath = turl + "/" + fav
}
}
} else {
faviconpath = turl + "/favicon.ico"
}
return favicohash(faviconpath)
}
func favicohash(host string) string {
timeout := time.Duration(8 * time.Second)
var tr *http.Transport
if pkg.HttpProxy != "" {
uri, _ := url.Parse(pkg.HttpProxy)
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
Proxy: http.ProxyURL(uri),
}
} else {
tr = &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
}
}
client := http.Client{
Timeout: timeout,
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse /* 不进入重定向 */
},
}
resp, err := client.Get(host)
if err != nil {
//log.Println("favicon client error:", err)
return "0"
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
//log.Println("favicon file read error: ", err)
return "0"
}
faviconMMH3 := fmt.Sprintf("%d", stringz.FaviconHash(body))
return faviconMMH3
} else {
return "0"
}
}