-
Notifications
You must be signed in to change notification settings - Fork 39
/
enola.go
154 lines (128 loc) · 2.88 KB
/
enola.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
package enola
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"golang.org/x/sync/semaphore"
)
const RequestTimeout time.Duration = time.Second * 20
type Website struct {
ErrorType string `json:"errorType"`
ErrorMessage interface{} `json:"errorMsg"`
URL string `json:"url"`
UrlMain string `json:"urlMain"`
UsernameClaimed string `json:"username_claimed"`
UsernameUnclaimed string `json:"username_unclaimed"`
}
type Enola struct {
Data map[string]Website
Site string
Ctx context.Context
}
type Result struct {
Name string `json:"name"`
URL string `json:"url"`
Found bool `json:"found"`
}
//go:embed data.json
var d []byte
func New(ctx context.Context) (Enola, error) {
var data map[string]Website
err := json.Unmarshal(d, &data)
if err != nil {
return Enola{}, fmt.Errorf("error: %v", ErrDataFileIsNotAValidJson)
}
return Enola{
Data: data,
Ctx: ctx,
}, nil
}
func (s *Enola) SetSite(site string) *Enola {
s.Site = site
return s
}
func (s *Enola) ListCount() int { return len(s.Data) }
func (s *Enola) List() map[string]Website { return s.Data }
func (s *Enola) Check(username string) (<-chan Result, error) {
ch := make(chan Result)
data := map[string]Website{}
if s.Site != "" {
for k, v := range s.Data {
if strings.Contains(strings.ToLower(k), strings.Trim(strings.ToLower(s.Site), " ")) {
data[k] = v
}
}
// if site is not found in the list
if len(data) == 0 {
return nil, fmt.Errorf("error: %v", ErrSiteNotFound)
}
}
if len(data) == 0 {
data = s.Data
}
ctx := context.Background()
sem := semaphore.NewWeighted(20)
go func() {
for key, value := range data {
if err := sem.Acquire(ctx, 1); err != nil {
fmt.Println(err)
}
go func(key string, value Website) {
defer sem.Release(1)
url := strings.ReplaceAll(value.URL, "{}", username)
res := Result{
Name: key,
URL: url,
Found: false,
}
client := http.DefaultClient
client.Timeout = RequestTimeout
if value.ErrorType == "status_code" {
resp, err := client.Get(url)
if err != nil {
ch <- res
return
}
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
res.Found = true
ch <- res
return
}
ch <- res
return
}
if value.ErrorType == "message" {
resp, err := client.Get(url)
if err != nil {
ch <- res
return
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
ch <- res
return
}
valueString, ok := value.ErrorMessage.(string)
if !ok {
ch <- res
return
}
if !strings.Contains(string(bodyBytes), valueString) {
res.Found = true
ch <- res
return
}
ch <- res
}
}(key, value)
}
}()
return ch, nil
}