-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstruct.go
166 lines (129 loc) · 2.9 KB
/
construct.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
package main
import (
"fmt"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"time"
"Graaf/graph"
"golang.org/x/net/html"
"golang.org/x/net/publicsuffix"
)
const (
baseURL string = ""
maxDepth uint16 = 1
)
var wg = sync.WaitGroup{}
var g = graph.Digraph{
Vertices: map[string]*graph.Vertex{},
Lock: sync.RWMutex{},
}
var client = &http.Client{
Timeout: 1 * time.Second,
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var depth uint16
start := time.Now()
baseTLD, _ := parseTLD(baseURL)
t := graph.Vertex{
Element: baseTLD,
In: []*graph.Vertex{},
Out: []*graph.Vertex{},
Lock: sync.RWMutex{},
}
g.AddVertex(&t)
wg.Add(1)
go retrieve(baseURL, depth, &t)
wg.Wait()
fmt.Println("operation took", time.Since(start))
g.Serialize("graph.txt")
}
func retrieve(src string, d uint16, p *graph.Vertex) {
defer wg.Done()
if d == maxDepth {
return
}
response, err := client.Get(src)
if err != nil {
return
}
fmt.Println(src)
tokenizer := html.NewTokenizer(response.Body)
defer response.Body.Close()
links := map[string]struct{}{}
Loop:
for {
token := tokenizer.Next()
switch token {
case html.ErrorToken:
break Loop
case html.StartTagToken:
t := tokenizer.Token()
isAnchor := t.Data == "a"
if isAnchor {
for _, attribute := range t.Attr {
if attribute.Key == "href" {
base, err := parseBaseURL(attribute.Val)
if err == nil {
links[base] = struct{}{}
}
}
}
}
}
}
for link := range links {
destTLD, _ := parseTLD(link)
t := graph.Vertex{
Element: destTLD,
In: []*graph.Vertex{p},
Out: []*graph.Vertex{},
Lock: sync.RWMutex{},
}
v, ok := g.GetVertex(destTLD)
if !ok {
g.AddVertex(&t)
p.AddOutgoing(&t)
wg.Add(1)
go retrieve(link, d+1, &t)
} else if p.Element != destTLD {
v.AddIncoming(p)
}
}
}
func parseBaseURL(u string) (string, error) {
v, err := url.Parse(u)
if err == nil && (v.Scheme == "https" || v.Scheme == "http") {
return v.Scheme + "://" + v.Hostname(), nil
}
return "", fmt.Errorf("domain could not be parsed")
}
func parseTLD(u string) (string, error) {
v, err := url.Parse(u)
if err == nil {
// ignore non-http[s] schemes
if v.Scheme == "http" || v.Scheme == "https" {
h := v.Hostname()
sep := "."
// check if hostname is ip address
p := strings.Join(strings.Split(h, sep), "")
if _, err := strconv.Atoi(p); err == nil {
return h, err
}
extension, _ := publicsuffix.PublicSuffix(h)
i := strings.LastIndex(h, extension)
if i > 1 { // else invalid url hostname format due to parsing
r := strings.Split(h[:i-1], sep) // get domain components
t := r[len(r)-1] + sep + extension // get root domain + extension
return t, err
}
return "", fmt.Errorf("incorrect URL format")
}
return "", fmt.Errorf("wrong scheme")
}
return "", fmt.Errorf("could not parse domain")
}