-
Notifications
You must be signed in to change notification settings - Fork 4
/
is.go
300 lines (255 loc) · 6.86 KB
/
is.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package is
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/cretz/bine/tor"
"github.com/wabarc/logger"
)
const timeout = 30 * time.Second
var (
userAgent = "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"
onion = "http://archiveiya74codqgiixo33q62qlrqtkgmcitqx5u2oeqnmn5bpcbiyd.onion" // archivecaslytosk.onion
domains = []string{
"https://archive.ph",
"https://archive.today",
"https://archive.is",
"https://archive.li",
"https://archive.vn",
"https://archive.fo",
"https://archive.md",
}
)
// Archiver represents an archiver that can be used to submit and search for
// archived versions of web pages on archive.today.
type Archiver struct {
*http.Client
torClient *http.Client
tor *tor.Tor
// Cookie string for setting cookies in requests.
Cookie string
anyway string
}
type IS struct {
submitid string
}
func init() {
debug := os.Getenv("DEBUG")
if debug == "true" || debug == "1" || debug == "on" {
logger.EnableDebug()
}
}
// NewArchiver returns a Archiver struct with the specified HTTP client and Tor network client.
// It's the responsibility of the caller to call CloseTor when it is no longer needed.
func NewArchiver(client *http.Client) *Archiver {
arc := &Archiver{anyway: "1"}
if client == nil {
client = &http.Client{Timeout: timeout}
}
// client.CheckRedirect = noRedirect
arc.Client = client
torClient, tor, err := newTorClient(context.Background())
if err != nil {
return arc
}
if torClient != nil {
arc.torClient = torClient
}
arc.tor = tor
return arc
}
// CloseTor closes the Tor client if it exists.
func (arc *Archiver) CloseTor() {
if arc.tor != nil {
_ = closeTor(arc.tor)
}
}
// Do implements the http.Do method to execute an HTTP request using the embedded HTTP
// client or the Tor client as a fallback if the primary client fails to execute the request.
func (arc *Archiver) Do(req *http.Request) (resp *http.Response, err error) {
if strings.HasSuffix(req.URL.Hostname(), ".onion") {
goto tryTor
}
resp, err = arc.Client.Do(req)
if err != nil {
goto tryTor
}
return
tryTor:
if arc.torClient != nil {
return arc.torClient.Do(req)
}
return
}
// Wayback is the handle of saving webpages to archive.is
func (arc *Archiver) Wayback(ctx context.Context, in *url.URL) (dst string, err error) {
is := &IS{}
dst, err = arc.archive(ctx, is, in)
if err != nil {
return
}
dst = strings.Replace(dst, onion, "https://archive.today", 1)
dst = regexp.MustCompile(`\/again\?url=.*`).ReplaceAllString(dst, "")
return
}
// Playback handle searching archived webpages from archive.is
func (arc *Archiver) Playback(ctx context.Context, in *url.URL) (dst string, err error) {
is := &IS{}
dst, err = arc.search(ctx, is, in)
if err != nil {
return
}
dst = strings.Replace(dst, onion, "https://archive.today", 1)
return
}
func (arc *Archiver) archive(ctx context.Context, is *IS, u *url.URL) (string, error) {
endpoint, err := arc.getValidDomain(is)
if err != nil {
return "", fmt.Errorf("archive.today is unavailable.")
}
uri := u.String()
data := url.Values{
"submitid": {is.submitid},
"anyway": {arc.anyway},
"url": {uri},
}
domain := endpoint.String()
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, domain+"/submit/", strings.NewReader(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Referer", domain)
req.Header.Add("Origin", domain)
req.Header.Add("Host", endpoint.Hostname())
req.Header.Add("Cookie", getCookie())
resp, err := arc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
code := resp.StatusCode / 100
if code == 1 || code == 4 || code == 5 {
final := fmt.Sprintf("%s?url=%s", domain, uri)
return final, nil
}
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
return "", err
}
// When use anyway parameter.
refresh := resp.Header.Get("Refresh")
if len(refresh) > 0 {
r := strings.Split(refresh, ";url=")
if len(r) == 2 {
return r[1], nil
}
}
loc := resp.Header.Get("location")
if len(loc) > 2 {
u, err := url.Parse(loc)
if err == nil && strings.HasPrefix("archive", u.Hostname()) {
return loc, nil
}
}
// Redirect to final url if page saved.
final := resp.Request.URL.String()
if len(final) > 0 && !strings.Contains(final, "/submit/") {
return final, nil
}
return fmt.Sprintf("%s/timegate/%s", domain, uri), nil
}
func noRedirect(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
func getCookie() string {
return os.Getenv("ARCHIVE_COOKIE")
}
func (arc *Archiver) getSubmitID(url string) (string, error) {
r := strings.NewReader("")
req, _ := http.NewRequest("GET", url, r)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Cookie", getCookie())
resp, err := arc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("status code error: %d", resp.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
id, exists := doc.Find("input[name=submitid]").First().Attr("value")
if !exists {
return "", fmt.Errorf("submitid not found")
}
return id, nil
}
func (arc *Archiver) getValidDomain(is *IS) (*url.URL, error) {
var endpoint *url.URL
// get valid domain and submitid
try := func(domains ...string) {
for _, domain := range domains {
id, err := arc.getSubmitID(domain)
if err != nil {
continue
}
is.submitid = id
endpoint, _ = url.Parse(domain)
break
}
}
if endpoint == nil || is.submitid == "" {
try(domains...)
if endpoint != nil && is.submitid != "" {
return endpoint, nil
}
}
// Try request over Tor hidden service.
if arc.torClient != nil {
try(onion)
}
if endpoint == nil {
return nil, fmt.Errorf("not found valid domain")
}
return endpoint, nil
}
func (arc *Archiver) search(ctx context.Context, is *IS, in *url.URL) (string, error) {
endpoint, err := arc.getValidDomain(is)
if err != nil {
return "", fmt.Errorf("archive.today is unavailable.")
}
domain := endpoint.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/%s", domain, in), nil)
if err != nil {
return "", err
}
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Referer", domain)
req.Header.Add("Host", endpoint.Hostname())
resp, err := arc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
target, exists := doc.Find("#row0 > .TEXT-BLOCK > a").Attr("href")
if !exists {
return "", fmt.Errorf("Not found")
}
return target, nil
}