forked from suosi-inc/go-pkg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
168 lines (137 loc) · 4.12 KB
/
detect.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package spider
import (
"bytes"
"errors"
"net/url"
"github.com/PuerkitoBio/goquery"
"github.com/suosi-inc/go-pkg-spider/extract"
"github.com/x-funs/go-fun"
)
const (
DetectDomainRemoveTags = "script,noscript,style,iframe,link,svg"
)
type DomainRes struct {
Domain string
HomeDomain string
Scheme string
Charset CharsetRes
Lang LangRes
Country string
Province string
Category string
Title string
TitleClean string
Description string
Icp string
State bool
StatusCode int
ContentCount int
ListCount int
SubDomains map[string]bool
}
// DetectDomain 域名探测
// DomainRes.State true 和 err nil 表示探测成功
// DomainRes.State true 可能会返回 err, 如 doc 解析失败
// DomainRes.State false 时根据 StatusCode 判断是请求是否成功或请求成功但响应失败(如404)
func DetectDomain(domain string, timeout int, retry int) (*DomainRes, error) {
if retry == 0 {
retry = 1
}
for i := 0; i < retry; i++ {
domainRes, err := DetectDomainDo(domain, timeout)
if domainRes.StatusCode != 0 || err == nil {
return domainRes, err
}
}
domainRes := &DomainRes{}
return domainRes, errors.New("ErrorDomainDetect")
}
func DetectDomainDo(domain string, timeout int) (*DomainRes, error) {
if timeout == 0 {
timeout = 10000
}
domainRes := &DomainRes{}
req := &HttpReq{
HttpReq: &fun.HttpReq{
MaxContentLength: 10 * 1024 * 1024,
MaxRedirect: 3,
},
ForceTextContentType: true,
}
scheme := "http"
homes := []string{"www", ""}
for _, home := range homes {
var urlStr string
var homeDomain string
if home != "" {
homeDomain = home + fun.DOT + domain
urlStr = scheme + "://" + homeDomain
} else {
homeDomain = domain
urlStr = scheme + "://" + homeDomain
}
resp, err := HttpGetResp(urlStr, req, timeout)
domainRes.StatusCode = resp.StatusCode
if resp.Success && err == nil {
domainRes.Domain = domain
// 如果发生了跳转, 则重新设置 homeDomain, 前提是还是同一个主域名
domainRes.HomeDomain = homeDomain
requestHostname := resp.RequestURL.Hostname()
if domainRes.HomeDomain != requestHostname {
requestTopDomain := extract.DomainTop(requestHostname)
if requestTopDomain != domain {
return domainRes, errors.New("ErrorRedirect:" + requestHostname)
}
domainRes.HomeDomain = requestHostname
}
// 如果发生了协议跳转, 则重新设置 scheme
domainRes.Scheme = scheme
if domainRes.Scheme != resp.RequestURL.Scheme {
domainRes.Scheme = resp.RequestURL.Scheme
}
// 字符集
domainRes.Charset = resp.Charset
// 解析 HTML
u, _ := url.Parse(urlStr)
doc, docErr := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body))
if docErr == nil {
doc.Find(DefaultDocRemoveTags).Remove()
// 中国 ICP 解析
icp, province := extract.Icp(doc)
if icp != "" && province != "" {
domainRes.Country = "中国"
domainRes.Icp = icp
domainRes.Province = extract.ProvinceShortMap[province]
}
// 语言
langRes := Lang(doc, resp.Charset.Charset, false)
domainRes.Lang = langRes
// 尽可能的探测一些信息国家/省份/类别
if domainRes.Country == "" {
country, province, category := extract.MetaFromHost(u.Hostname(), langRes.Lang)
domainRes.Country = country
domainRes.Province = province
domainRes.Category = category
}
// 标题摘要
domainRes.Title = extract.WebTitle(doc, 0)
domainRes.TitleClean = extract.WebTitleClean(domainRes.Title, langRes.Lang)
domainRes.Description = extract.WebDescription(doc, 0)
// 站内链接
linkTitles, _ := extract.WebLinkTitles(doc, resp.RequestURL, true)
// 链接分类
links, subDomains := extract.LinkTypes(linkTitles, langRes.Lang, nil)
domainRes.ContentCount = len(links.Content)
domainRes.ListCount = len(links.List)
domainRes.SubDomains = subDomains
domainRes.State = true
return domainRes, nil
} else {
return domainRes, errors.New("ErrorDocParse")
}
} else {
return domainRes, err
}
}
return domainRes, errors.New("ErrorDomainDetect")
}