@@ -32,7 +32,6 @@ type IS struct {
3232
3333 submitid string
3434
35- endpoint * url.URL
3635 httpClient * http.Client
3736 torClient * http.Client
3837}
@@ -65,15 +64,18 @@ func init() {
6564func (wbrc * Archiver ) Wayback (links []string ) (map [string ]string , error ) {
6665 collects , results := make (map [string ]string ), make (map [string ]string )
6766 for _ , link := range links {
68- if ! helper .IsURL (link ) {
69- logger .Info (link + " is invalid url." )
70- continue
67+ if helper .IsURL (link ) {
68+ collects [link ] = link
7169 }
72- collects [link ] = link
70+ }
71+ if len (collects ) == 0 {
72+ return results , fmt .Errorf ("Not found" )
7373 }
7474
75- done := make (chan bool , 1 )
76- torClient , err := newTorClient (done )
75+ ctx , cancel := context .WithCancel (context .Background ())
76+ defer cancel ()
77+ torClient , t , err := newTorClient (ctx )
78+ defer closeTor (t )
7779 if err != nil {
7880 logger .Error ("%v" , err )
7981 }
@@ -102,11 +104,6 @@ func (wbrc *Archiver) Wayback(links []string) (map[string]string, error) {
102104 }
103105 wg .Wait ()
104106
105- // Close tor connection
106- defer func () {
107- done <- true
108- }()
109-
110107 if len (results ) == 0 {
111108 return results , fmt .Errorf ("No results" )
112109 }
@@ -123,11 +120,13 @@ func (wbrc *Archiver) Playback(links []string) (map[string]string, error) {
123120 }
124121 }
125122 if len (collects ) == 0 {
126- return results , fmt .Errorf ("No found URL " )
123+ return results , fmt .Errorf ("Not found" )
127124 }
128125
129- done := make (chan bool , 1 )
130- torClient , err := newTorClient (done )
126+ ctx , cancel := context .WithCancel (context .Background ())
127+ defer cancel ()
128+ torClient , t , err := newTorClient (ctx )
129+ defer closeTor (t )
131130 if err != nil {
132131 logger .Error ("%v" , err )
133132 }
@@ -156,19 +155,14 @@ func (wbrc *Archiver) Playback(links []string) (map[string]string, error) {
156155 }
157156 wg .Wait ()
158157
159- // Close tor connection
160- defer func () {
161- done <- true
162- }()
163-
164158 if len (results ) == 0 {
165159 return results , fmt .Errorf ("No results" )
166160 }
167161
168162 return results , nil
169163}
170164func (is * IS ) archive (uri string , ch chan <- string ) {
171- _ , err := is .getValidDomain ()
165+ endpoint , err := is .getValidDomain ()
172166 if err != nil {
173167 ch <- fmt .Sprint ("archive.today is unavailable." )
174168 return
@@ -182,14 +176,14 @@ func (is *IS) archive(uri string, ch chan<- string) {
182176 "anyway" : {anyway },
183177 "url" : {uri },
184178 }
185- domain := is . endpoint .String ()
179+ domain := endpoint .String ()
186180 req , err := http .NewRequest ("POST" , domain + "/submit/" , strings .NewReader (data .Encode ()))
187181 req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
188182 req .Header .Add ("Content-Length" , strconv .Itoa (len (data .Encode ())))
189183 req .Header .Add ("User-Agent" , userAgent )
190184 req .Header .Add ("Referer" , domain )
191185 req .Header .Add ("Origin" , domain )
192- req .Header .Add ("Host" , is . endpoint .Hostname ())
186+ req .Header .Add ("Host" , endpoint .Hostname ())
193187 req .Header .Add ("Cookie" , is .getCookie ())
194188 resp , err := is .httpClient .Do (req )
195189 if err != nil {
@@ -286,6 +280,7 @@ func (is *IS) getSubmitID(url string) (string, error) {
286280}
287281
288282func (is * IS ) getValidDomain () (* url.URL , error ) {
283+ var endpoint * url.URL
289284 // get valid domain and submitid
290285 r := func (domains []string ) {
291286 for _ , domain := range domains {
@@ -294,8 +289,8 @@ func (is *IS) getValidDomain() (*url.URL, error) {
294289 if err != nil {
295290 continue
296291 }
297- is .endpoint , _ = url .Parse (h )
298292 is .submitid = id
293+ endpoint , _ = url .Parse (h )
299294 break
300295 }
301296 }
@@ -307,28 +302,28 @@ func (is *IS) getValidDomain() (*url.URL, error) {
307302 r ([]string {onion })
308303 }
309304
310- if is . endpoint == nil || is .submitid == "" {
305+ if endpoint == nil || is .submitid == "" {
311306 r (domains )
312- if is . endpoint == nil || is .submitid == "" {
307+ if endpoint == nil || is .submitid == "" {
313308 return nil , fmt .Errorf ("archive.today is unavailable." )
314309 }
315310 }
316311
317- return is . endpoint , nil
312+ return endpoint , nil
318313}
319314
320315func (is * IS ) search (uri string , ch chan <- string ) {
321- _ , err := is .getValidDomain ()
316+ endpoint , err := is .getValidDomain ()
322317 if err != nil {
323318 ch <- fmt .Sprint ("archive.today is unavailable." )
324319 return
325320 }
326321
327- domain := is . endpoint .String ()
322+ domain := endpoint .String ()
328323 req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/%s" , domain , uri ), nil )
329324 req .Header .Add ("User-Agent" , userAgent )
330325 req .Header .Add ("Referer" , domain )
331- req .Header .Add ("Host" , is . endpoint .Hostname ())
326+ req .Header .Add ("Host" , endpoint .Hostname ())
332327 resp , err := is .httpClient .Do (req )
333328 if err != nil {
334329 ch <- fmt .Sprint (err )
@@ -344,7 +339,7 @@ func (is *IS) search(uri string, ch chan<- string) {
344339
345340 target , exists := doc .Find ("#row0 > .TEXT-BLOCK > a" ).Attr ("href" )
346341 if ! exists {
347- ch <- "No found"
342+ ch <- "Not found"
348343 return
349344 }
350345
0 commit comments