-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamalytics.go
113 lines (92 loc) · 2.64 KB
/
scamalytics.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
package middleware
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"time"
"github.com/eirka/eirka-libs/config"
e "github.com/eirka/eirka-libs/errors"
"github.com/gin-gonic/gin"
)
// Scamalytics check ip with scamalytics
func Scamalytics() gin.HandlerFunc {
return func(c *gin.Context) {
// check ip against scamalytics
err := CheckScamalytics(c.ClientIP())
if err == e.ErrBlacklist {
c.JSON(http.StatusBadRequest, gin.H{"error_message": "IP is on spam blacklist"})
c.Error(err).SetMeta("Scamalytics.CheckScamalytics")
c.Abort()
return
}
c.Next()
}
}
type ScamalyticsResponse struct {
Status string `json:"status"`
Mode string `json:"mode"`
IP string `json:"ip"`
Score int `json:"score"`
Risk string `json:"risk"`
URL string `json:"url"`
Credits struct {
Used int `json:"used"`
Remaining int `json:"remaining"`
LastSyncTimestampUtc string `json:"last_sync_timestamp_utc"`
SecondsElapsedSinceLastSync int `json:"seconds_elapsed_since_last_sync"`
Note string `json:"note"`
} `json:"credits"`
Exec string `json:"exec"`
}
// CheckScamalytics will query blacklist api for IP
func CheckScamalytics(ip string) (err error) {
if len(ip) == 0 {
return errors.New("no ip provided")
}
queryValues := url.Values{}
queryValues.Set("ip", ip)
queryValues.Set("key", config.Settings.Scamalytics.Key)
// construct the api request
scamalyticsEndpoint := &url.URL{
Scheme: "https",
Host: config.Settings.Scamalytics.Endpoint,
Path: config.Settings.Scamalytics.Path,
RawQuery: queryValues.Encode(),
}
// our http request
req, err := http.NewRequest(http.MethodGet, scamalyticsEndpoint.String(), nil)
if err != nil {
return errors.New("error creating scamalytics request")
}
// set ua header
req.Header.Set("User-Agent", "Eirka/1.2")
// a client with a timeout
var netClient = &http.Client{
Timeout: time.Second * 10,
}
// do the request
// TODO: add errors here to a system log
resp, err := netClient.Do(req)
if err != nil {
return errors.New("error reaching scamalytics")
}
defer resp.Body.Close()
// read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.New("error parsing scamalytics response")
}
scamalyticsData := ScamalyticsResponse{}
// unmarshal into struct
err = json.Unmarshal(body, &scamalyticsData)
if err != nil {
return errors.New("error parsing scamalytics data")
}
// check if the spammer confidence level is over our setting
if scamalyticsData.Score > config.Settings.Scamalytics.Score {
return e.ErrBlacklist
}
return
}