/
main.go
181 lines (139 loc) · 3.08 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
"golang.org/x/net/html"
gv "github.com/awalterschulze/gographviz"
)
type edge struct {
from, to string
}
var (
graph *gv.Graph
// A list of URLs already handled
handled []string
// A list of edges already handled
handledEdges []edge
// The maxmimum depth allowed
maxDepth = 4
// The maximum number of nodes to generate
maxNodes = 32
// The current amount of nodes
nodeCount = 0
// The timeout for each GET request, in milliseconds
timeout = 7500
)
func main() {
url := flag.String("url", "https://golang.org", "the URL to analyse")
flag.IntVar(&maxDepth, "depth", 4, "the maximum depth to go into the tree")
flag.IntVar(&maxNodes, "nodes", 32, "the maximum amount of nodes to generate")
flag.IntVar(&timeout, "timeout", 7500, "the timeout on the GET requests")
flag.Parse()
graph = gv.NewGraph()
graph.SetDir(true)
graph.Name = "web"
graph.AddAttr("web", "rankdir", "LR")
if err := analyseURL(*url, 0); err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
if err := generateImage(); err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(2)
}
}
func analyseURL(u string, depth int) error {
if nodeCount >= maxNodes || depth > maxDepth {
return nil
}
for _, h := range handled {
if h == u {
return nil
}
}
nodeCount++
fmt.Printf(strings.Repeat(" ", depth)+"% 5d: %s\n", nodeCount, u)
handled = append(handled, u)
if err := graph.AddNode("web", quote(u), nil); err != nil {
return err
}
client := http.Client{
Timeout: time.Millisecond * time.Duration(timeout),
}
resp, err := client.Get(u)
if err != nil {
if err == http.ErrServerClosed {
return nil
}
return err
}
doc, err := html.Parse(resp.Body)
if err != nil {
return err
}
base, err := url.Parse(u)
if err != nil {
return err
}
var walk func(*html.Node) error
walk = func(n *html.Node) error {
if n.Type == html.ElementNode && n.Data == "a" {
for _, attr := range n.Attr {
if attr.Key == "href" {
parsed, err := url.Parse(attr.Val)
if err != nil {
return err
}
attr.Val = base.ResolveReference(parsed).String()
if err := analyseURL(attr.Val, depth+1); err != nil {
return err
}
connect(quote(attr.Val), quote(u))
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if err := walk(c); err != nil {
return err
}
}
return nil
}
return walk(doc)
}
func connect(from, to string) {
if graph.IsNode(from) {
for _, e := range handledEdges {
if e.from == from && e.to == to {
return
}
}
handledEdges = append(handledEdges, edge{
from: from,
to: to,
})
graph.AddEdge(from, to, true, map[string]string{
"dir": "reversed",
})
}
}
func quote(s string) string {
return fmt.Sprintf(`"%s"`, s)
}
func generateImage() error {
fmt.Println("generating image...")
cmd := exec.Command("dot", "-Tsvg", "-oout.svg")
cmd.Stdin = strings.NewReader(graph.String())
out, err := cmd.Output()
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}