-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
237 lines (199 loc) · 4.74 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"flag"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/simonfrey/proxyfy"
"log"
"math/rand"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Url struct {
url string
typ int
}
type SafeMap struct {
v []Url
seenUrls map[string]int
baseUrl string
domainUrl string
queue chan string
mux sync.Mutex
}
var wgQuery sync.WaitGroup
var internalUrls, useProxy bool
func (c *SafeMap) Add(url string, urlType int) {
c.mux.Lock()
defer c.mux.Unlock()
if internalUrls && strings.HasPrefix(url, "/") {
url = c.domainUrl + url
}
if strings.Index(url, c.baseUrl) == 0 && c.seenUrls[url] == 0 {
c.v = append(c.v, Url{url, urlType})
c.seenUrls[url] = 1
// log.Println(len(c.v), ":", url)
if urlType == 1 {
wgQuery.Add(1)
//Enque newly found url
go func() { c.queue <- url }()
}
}
}
func (c *SafeMap) GetSingleElement() (*Url, int) {
c.mux.Lock()
defer c.mux.Unlock()
if len(c.v) == 0 {
return nil, 0
}
// Randomize slice sorting
rand.Shuffle(len(c.v), func(i, j int) {
c.v[i], c.v[j] = c.v[j], c.v[i]
})
// pop first element from slice c.v
u := c.v[0]
if len(c.v) > 0 {
c.v = c.v[1:]
}
return &u, len(c.v)
}
func main() {
//Setup expected flags
useProxyPtr := flag.Bool("p", false, "Proxyfy the requests")
internalUrlsPtr := flag.Bool("i", false, "Also use interal urls e.g. /hello/world")
sleepBetweenRequests := flag.Bool("s", true, "Sleep between add request to not be flagged internet archive")
//Parse commandline args
flag.Parse()
args := flag.Args()
if len(args) < 1 {
log.Fatal("Please specify the page you want to save. Form: http[s]://[yourwebsite.com]")
}
//Check if the url is valid and parse it into right format
urlStruct, err := url.Parse(args[0])
if err != nil {
log.Fatal(err)
}
cUrl := urlStruct.String()
pU, err := url.Parse(cUrl)
if err != nil {
log.Fatal(err)
}
dUrl := pU.Scheme + "://" + pU.Host
useProxy = *useProxyPtr
internalUrls = *internalUrlsPtr
log.Printf("\n Save URL: %s\n Use Proxy: %t\n Crawl internal urls: %t\n\n\n", cUrl, useProxy, internalUrls)
gimmeConfig := proxyfy.GimmeProxyConfig{
Protocol: "http",
Get: true,
Post: true,
SupportsHTTPS: true,
MinSpeed: 500,
}
proxyfy := proxyfy.NewProxyfyAdvancedConfig(gimmeConfig)
//Setup Chanel
queue := make(chan string)
uMap := &SafeMap{
v: make([]Url, 0),
seenUrls: make(map[string]int),
baseUrl: cUrl,
domainUrl: dUrl,
queue: queue,
}
//Add first baseurl to queue
uMap.Add(cUrl, 1)
//Close queue after all urls have been processed
processing := true
go func() {
wgQuery.Wait()
close(queue)
processing = false
}()
//Endless loop to range over channel
go func() {
for sUrl := range queue {
if *sleepBetweenRequests {
sleepTime := time.Duration(rand.Intn(10))
time.Sleep(sleepTime)
}
analyzeUrl(sUrl, uMap, proxyfy)
}
}()
//Internet archive only allows single connection. So we have to do the request slowly
for processing {
for {
u, remainingElements := uMap.GetSingleElement()
if u == nil {
break
}
if u.typ == 2 || u.typ == 1 {
fmt.Println("Remaining Elements: ", remainingElements)
addUrl(u.url, proxyfy, *sleepBetweenRequests)
}
}
}
log.Println("Done")
}
func analyzeUrl(sUrl string, uMap *SafeMap, proxyfy *proxyfy.Proxyfy) {
var err error
var res *http.Response
if useProxy {
res, err = proxyfy.Get(sUrl)
} else {
res, err = http.Get(sUrl)
}
if err != nil {
//log.Fatal(err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
//log.Printf("status code error: %d %s\n", res.StatusCode, res.Status)
return
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
//log.Println(err)
return
}
// use CSS selector found with the browser inspector
// for each, use index and item
doc.Find("body a").Each(func(index int, item *goquery.Selection) {
linkTag := item
link, _ := linkTag.Attr("href")
uMap.Add(strings.Split(link, "#")[0], 1)
})
doc.Find("body img").Each(func(index int, item *goquery.Selection) {
linkTag := item
link, _ := linkTag.Attr("src")
uMap.Add(strings.Split(link, "#")[0], 2)
})
wgQuery.Done()
}
func addUrl(sUrl string, proxyfy *proxyfy.Proxyfy, sleepBetweenRequests bool) {
baseUrl := "https://web.archive.org/save/"
for i := 0; i < 50; i++ {
if sleepBetweenRequests {
sleepTime := time.Duration(rand.Intn(5) + 5)
time.Sleep(sleepTime)
}
log.Println("[", i, "] Try for ", sUrl)
var err error
var res *http.Response
if useProxy {
res, err = proxyfy.Get(baseUrl + sUrl)
} else {
res, err = http.Get(baseUrl + sUrl)
}
if err != nil {
log.Println(err)
continue
}
if res.StatusCode == 200 {
break
}
}
log.Printf("Added: %s \n\n", sUrl)
}