-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafelyx.go
208 lines (171 loc) · 5.59 KB
/
safelyx.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
package safelyx
import (
"bytes"
"encoding/json"
"net/http"
"time"
)
// SafeLinkResponse represents the response from the CheckLink function
type SafeLinkResponse struct {
URL string `json:"url"`
Result int `json:"result"`
ResultText string `json:"result_text"`
Date string `json:"date"`
Analysis LinkAnalysis `json:"analysis"`
ChecksRemaining int `json:"checks_remaining"`
}
type LinkAnalysis struct {
DomainReputation string `json:"domain_reputation"`
SourceCode string `json:"source_code"`
AntiVirus string `json:"anti_virus"`
}
// SafeEmailResponse represents the response from the CheckEmail function
type SafeEmailResponse struct {
Email string `json:"email"`
Result int `json:"result"`
ResultText string `json:"result_text"`
Date string `json:"date"`
Analysis EmailAnalysis `json:"analysis"`
ChecksRemaining int `json:"checks_remaining"`
}
type EmailAnalysis struct {
Address string `json:"address"`
DomainReputation string `json:"domain_reputation"`
MXRecords string `json:"mx_records"`
}
// SafeMessageResponse represents the response from the CheckMessage function
type SafeMessageResponse struct {
Message string `json:"message"`
Result int `json:"result"`
ResultText string `json:"result_text"`
Date string `json:"date"`
Analysis MessageAnalysis `json:"analysis"`
ChecksRemaining int `json:"checks_remaining"`
}
type MessageAnalysis struct {
Content string `json:"content"`
Sentiment string `json:"sentiment"`
Links []SimplifiedLinkResponse `json:"links"`
Emails []SimplifiedEmailResponse `json:"emails"`
}
type SimplifiedLinkResponse struct {
URL string `json:"url"`
Result int `json:"result"`
Date string `json:"date"`
Analysis LinkAnalysis `json:"analysis"`
}
type SimplifiedEmailResponse struct {
Email string `json:"email"`
Result int `json:"result"`
Date string `json:"date"`
Analysis EmailAnalysis `json:"analysis"`
}
// SafeImageResponse represents the response from the CheckImage function
type SafeImageResponse struct {
ImageURL string `json:"image_url"`
Result int `json:"result"`
ResultText string `json:"result_text"`
Date string `json:"date"`
Analysis ImageAnalysis `json:"analysis"`
ChecksRemaining int `json:"checks_remaining"`
}
type ImageAnalysis struct {
Description string `json:"description"`
Link SimplifiedLinkResponse `json:"link"`
}
// Client represents a Safelyx API client
type Client struct {
httpClient *http.Client
keyCode string
}
// NewClient creates a new Safelyx API client
func NewClient(keyCode string) *Client {
return &Client{
httpClient: &http.Client{
Timeout: 10 * time.Second,
},
keyCode: keyCode,
}
}
// CheckLink securely checks if a link is safe to click or visit
func (c *Client) CheckLink(link string) (*SafeLinkResponse, error) {
payload := map[string]interface{}{
"link": link,
"key_code": c.keyCode,
}
resp, err := c.makeRequest("https://safelyx.com/safe-link-checker", payload)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SafeLinkResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
// CheckEmail securely checks if an email address is legitimate
func (c *Client) CheckEmail(email string) (*SafeEmailResponse, error) {
payload := map[string]interface{}{
"email": email,
"key_code": c.keyCode,
}
resp, err := c.makeRequest("https://safelyx.com/safe-email-checker", payload)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SafeEmailResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
// CheckMessage securely checks if a message's content is safe
func (c *Client) CheckMessage(message string, skipLinkAndEmailChecks bool) (*SafeMessageResponse, error) {
payload := map[string]interface{}{
"message": message,
"skip_link_and_email_checks": skipLinkAndEmailChecks,
"key_code": c.keyCode,
}
resp, err := c.makeRequest("https://safelyx.com/safe-message-checker", payload)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SafeMessageResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
// CheckImage securely checks if an image is safe
func (c *Client) CheckImage(imageURL string) (*SafeImageResponse, error) {
payload := map[string]interface{}{
"image_url": imageURL,
"key_code": c.keyCode,
}
resp, err := c.makeRequest("https://safelyx.com/safe-image-checker", payload)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SafeImageResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) makeRequest(url string, payload interface{}) (*http.Response, error) {
jsonData, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
return c.httpClient.Do(req)
}