forked from hyper0x/goc2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
125 lines (115 loc) · 2.93 KB
/
helper.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
package scheduler
import (
"errors"
"fmt"
"regexp"
"strings"
anlz "webcrawler/analyzer"
base "webcrawler/base"
dl "webcrawler/downloader"
ipl "webcrawler/itempipeline"
mdw "webcrawler/middleware"
)
func generateChannelManager(channelArgs base.ChannelArgs) mdw.ChannelManager {
return mdw.NewChannelManager(channelArgs)
}
func generatePageDownloaderPool(
poolSize uint32,
httpClientGenerator GenHttpClient) (dl.PageDownloaderPool, error) {
dlPool, err := dl.NewPageDownloaderPool(
poolSize,
func() dl.PageDownloader {
return dl.NewPageDownloader(httpClientGenerator())
},
)
if err != nil {
return nil, err
}
return dlPool, nil
}
func generateAnalyzerPool(poolSize uint32) (anlz.AnalyzerPool, error) {
analyzerPool, err := anlz.NewAnalyzerPool(
poolSize,
func() anlz.Analyzer {
return anlz.NewAnalyzer()
},
)
if err != nil {
return nil, err
}
return analyzerPool, nil
}
func generateItemPipeline(itemProcessors []ipl.ProcessItem) ipl.ItemPipeline {
return ipl.NewItemPipeline(itemProcessors)
}
// 生成组件实例代号。
func generateCode(prefix string, id uint32) string {
return fmt.Sprintf("%s-%d", prefix, id)
}
// 解析组件实例代号。
func parseCode(code string) []string {
result := make([]string, 2)
var codePrefix string
var id string
index := strings.Index(code, "-")
if index > 0 {
codePrefix = code[:index]
id = code[index+1:]
} else {
codePrefix = code
}
result[0] = codePrefix
result[1] = id
return result
}
var regexpForIp = regexp.MustCompile(`((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))`)
var regexpForDomains = []*regexp.Regexp{
// *.xx or *.xxx.xx
regexp.MustCompile(`\.(com|com\.\w{2})$`),
regexp.MustCompile(`\.(gov|gov\.\w{2})$`),
regexp.MustCompile(`\.(net|net\.\w{2})$`),
regexp.MustCompile(`\.(org|org\.\w{2})$`),
// *.xx
regexp.MustCompile(`\.me$`),
regexp.MustCompile(`\.biz$`),
regexp.MustCompile(`\.info$`),
regexp.MustCompile(`\.name$`),
regexp.MustCompile(`\.mobi$`),
regexp.MustCompile(`\.so$`),
regexp.MustCompile(`\.asia$`),
regexp.MustCompile(`\.tel$`),
regexp.MustCompile(`\.tv$`),
regexp.MustCompile(`\.cc$`),
regexp.MustCompile(`\.co$`),
regexp.MustCompile(`\.\w{2}$`),
}
func getPrimaryDomain(host string) (string, error) {
host = strings.TrimSpace(host)
if host == "" {
return "", errors.New("The host is empty!")
}
if regexpForIp.MatchString(host) {
return host, nil
}
var suffixIndex int
for _, re := range regexpForDomains {
pos := re.FindStringIndex(host)
if pos != nil {
suffixIndex = pos[0]
break
}
}
if suffixIndex > 0 {
var pdIndex int
firstPart := host[:suffixIndex]
index := strings.LastIndex(firstPart, ".")
if index < 0 {
pdIndex = 0
} else {
pdIndex = index + 1
}
return host[pdIndex:], nil
} else {
return "", errors.New("Unrecognized host!")
}
}