-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (114 loc) · 2.49 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
package main
import (
"encoding/xml"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/schollz/progressbar/v3"
"github.com/urfave/cli/v2"
)
var counter Counter
type UrlSet struct {
XMLName xml.Name `xml:"urlset"`
Urls []Url `xml:"url"`
}
type Url struct {
Loc string `xml:"loc"`
NewLoc string
}
func makeRequest(url string, wg *sync.WaitGroup, bar *progressbar.ProgressBar) {
client := http.Client{}
response, err := client.Get(url)
if err != nil {
fmt.Println(err)
}
defer func() {
respBodyClose := response.Body.Close()
if respBodyClose != nil {
fmt.Print(respBodyClose.Error())
return
}
}()
barError := bar.Add(1)
if barError != nil {
fmt.Printf(barError.Error())
}
defer wg.Done()
counter.Add(response.Status, 1)
}
func getXmlFromUrl(xmlUrl string) []byte {
resp, getErr := http.Get(xmlUrl)
if getErr != nil {
fmt.Printf(getErr.Error())
}
body, err := io.ReadAll(resp.Body)
defer func() {
respErr := resp.Body.Close()
if respErr != nil {
fmt.Printf(respErr.Error())
}
}()
if err != nil {
fmt.Printf(err.Error())
}
return body
}
func printSyncMap(counter *Counter) {
i := 0
counter.m.Range(func(key, value interface{}) bool {
int64val, _ := counter.Get(key.(string))
fmt.Printf("\t[%d] key: %v, value: %d\n", i, key, int64val)
i++
return true
})
}
func start(url string) {
bar := progressbar.NewOptions(-1,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetPredictTime(true),
progressbar.OptionShowIts(),
progressbar.OptionShowCount(),
progressbar.OptionClearOnFinish(),
progressbar.OptionSetDescription("[yellow]Trying redirects...[reset]"))
data := getXmlFromUrl(url)
var urlSet UrlSet
var wg sync.WaitGroup
defer func() {
finishError := bar.Finish()
println(fmt.Errorf(finishError.Error()))
}()
unmarshalErr := xml.Unmarshal(data, &urlSet)
if unmarshalErr != nil {
fmt.Printf(unmarshalErr.Error())
}
for _, v := range urlSet.Urls {
r := rand.Intn(1000000)
time.Sleep(time.Duration(r) * time.Microsecond)
url := strings.Replace(v.Loc, "watch", "oroloi", -1)
v.NewLoc = url
wg.Add(1)
go makeRequest(url, &wg, bar)
}
wg.Wait()
printSyncMap(&counter)
}
func main() {
app := &cli.App{
Name: "redtest",
Usage: "testing redirects",
Action: func(cCtx *cli.Context) error {
fmt.Printf("Running for %s\n", cCtx.Args().Get(0))
start(cCtx.Args().Get(0))
return nil
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf(err.Error())
}
}