Skip to content

Commit

Permalink
feat: add URL check via FishFish
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Dec 20, 2024
1 parent 7103486 commit bf9279c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
58 changes: 58 additions & 0 deletions check/FishFish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package check

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)

type FishFishReport struct {
Category string `json:"category"`
}

const fishFishUrl = "https://api.fishfish.gg/v1/domains/%s"

func HasFishFishWarning(urls []string, clientIdentifier string) bool {
userAgent := "Matrix Guardian Bot (" + clientIdentifier + ")"
for _, u := range urls {
if checkFfSingleUrl(u, userAgent) {
return true
}
}
return false
}

func checkFfSingleUrl(u string, userAgent string) bool {
if !strings.HasPrefix(u, "http") {
u = "http://" + u
}
parsedUrl, err := url.Parse(u)
if err != nil || parsedUrl.Host == "" {
return false
}
report := FishFishReport{}
responseCode, err := getJson(fmt.Sprintf(fishFishUrl, parsedUrl.Hostname()), userAgent, &report)
if err != nil || responseCode != 200 {
return false
}
return report.Category != "safe"
}

func getJson(u string, userAgent string, target *FishFishReport) (int, error) {
req, err := http.NewRequest("GET", u, nil)
req.Header = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{userAgent},
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return -1, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
return resp.StatusCode, json.NewDecoder(resp.Body).Decode(target)
}
4 changes: 2 additions & 2 deletions check/VirusTotal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
func HasVirusTotalWarning(key string, urls []string) bool {
vtClient := vt.NewClient(key)
for _, url := range urls {
if checkSingleUrl(vtClient, url) {
if checkVtSingleUrl(vtClient, url) {
return true
}
}
return false
}

func checkSingleUrl(client *vt.Client, url string) bool {
func checkVtSingleUrl(client *vt.Client, url string) bool {
urlId := base64.RawURLEncoding.EncodeToString([]byte(url))
report, err := client.Get(vt.URL("urls/%s", urlId))
if report == nil || err != nil {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func onProtectedRoomMessage(client *mautrix.Client, ctx context.Context, evt *ev
}
reg := regexp.MustCompile(filter.RegexUrl)
urls := reg.FindAllString(evt.Content.AsMessage().Body, -1)
if len(urls) == 0 {
// TODO send read indicator
return
}
if config.useUrlFilter && filter.IsUrlFiltered(database, urls) {
redactMessage(client, ctx, evt, "found blocklisted URL")
return
Expand All @@ -162,6 +166,10 @@ func onProtectedRoomMessage(client *mautrix.Client, ctx context.Context, evt *ev
redactMessage(client, ctx, evt, "found suspicious URL (VirusTotal)")
return
}
if config.useUrlCheckFf && check.HasFishFishWarning(urls, client.UserID.String()) {
redactMessage(client, ctx, evt, "found suspicious URL (FishFish)")
return
}
_, err := client.SendReaction(ctx, evt.RoomID, evt.ID, "🛡️")
if err != nil {
return
Expand Down

0 comments on commit bf9279c

Please sign in to comment.