Skip to content

Commit bf9279c

Browse files
committed
feat: add URL check via FishFish
1 parent 7103486 commit bf9279c

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

check/FishFish.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package check
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"net/url"
9+
"strings"
10+
)
11+
12+
type FishFishReport struct {
13+
Category string `json:"category"`
14+
}
15+
16+
const fishFishUrl = "https://api.fishfish.gg/v1/domains/%s"
17+
18+
func HasFishFishWarning(urls []string, clientIdentifier string) bool {
19+
userAgent := "Matrix Guardian Bot (" + clientIdentifier + ")"
20+
for _, u := range urls {
21+
if checkFfSingleUrl(u, userAgent) {
22+
return true
23+
}
24+
}
25+
return false
26+
}
27+
28+
func checkFfSingleUrl(u string, userAgent string) bool {
29+
if !strings.HasPrefix(u, "http") {
30+
u = "http://" + u
31+
}
32+
parsedUrl, err := url.Parse(u)
33+
if err != nil || parsedUrl.Host == "" {
34+
return false
35+
}
36+
report := FishFishReport{}
37+
responseCode, err := getJson(fmt.Sprintf(fishFishUrl, parsedUrl.Hostname()), userAgent, &report)
38+
if err != nil || responseCode != 200 {
39+
return false
40+
}
41+
return report.Category != "safe"
42+
}
43+
44+
func getJson(u string, userAgent string, target *FishFishReport) (int, error) {
45+
req, err := http.NewRequest("GET", u, nil)
46+
req.Header = http.Header{
47+
"Content-Type": []string{"application/json"},
48+
"User-Agent": []string{userAgent},
49+
}
50+
resp, err := http.DefaultClient.Do(req)
51+
if err != nil {
52+
return -1, err
53+
}
54+
defer func(Body io.ReadCloser) {
55+
_ = Body.Close()
56+
}(resp.Body)
57+
return resp.StatusCode, json.NewDecoder(resp.Body).Decode(target)
58+
}

check/VirusTotal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
func HasVirusTotalWarning(key string, urls []string) bool {
1010
vtClient := vt.NewClient(key)
1111
for _, url := range urls {
12-
if checkSingleUrl(vtClient, url) {
12+
if checkVtSingleUrl(vtClient, url) {
1313
return true
1414
}
1515
}
1616
return false
1717
}
1818

19-
func checkSingleUrl(client *vt.Client, url string) bool {
19+
func checkVtSingleUrl(client *vt.Client, url string) bool {
2020
urlId := base64.RawURLEncoding.EncodeToString([]byte(url))
2121
report, err := client.Get(vt.URL("urls/%s", urlId))
2222
if report == nil || err != nil {

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func onProtectedRoomMessage(client *mautrix.Client, ctx context.Context, evt *ev
154154
}
155155
reg := regexp.MustCompile(filter.RegexUrl)
156156
urls := reg.FindAllString(evt.Content.AsMessage().Body, -1)
157+
if len(urls) == 0 {
158+
// TODO send read indicator
159+
return
160+
}
157161
if config.useUrlFilter && filter.IsUrlFiltered(database, urls) {
158162
redactMessage(client, ctx, evt, "found blocklisted URL")
159163
return
@@ -162,6 +166,10 @@ func onProtectedRoomMessage(client *mautrix.Client, ctx context.Context, evt *ev
162166
redactMessage(client, ctx, evt, "found suspicious URL (VirusTotal)")
163167
return
164168
}
169+
if config.useUrlCheckFf && check.HasFishFishWarning(urls, client.UserID.String()) {
170+
redactMessage(client, ctx, evt, "found suspicious URL (FishFish)")
171+
return
172+
}
165173
_, err := client.SendReaction(ctx, evt.RoomID, evt.ID, "🛡️")
166174
if err != nil {
167175
return

0 commit comments

Comments
 (0)