From 782fa9d0b00890f317cc98e57bbea66ef4eeaccf Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Tue, 24 Dec 2024 23:01:59 -0500 Subject: [PATCH] refactor: replace VT library with own logic to get rid of not really useful dependency --- check/VirusTotal.go | 28 ++++++++++++++++++++-------- go.mod | 2 -- go.sum | 9 --------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/check/VirusTotal.go b/check/VirusTotal.go index 86b2d6e..754149d 100644 --- a/check/VirusTotal.go +++ b/check/VirusTotal.go @@ -7,13 +7,14 @@ import ( "encoding/hex" "encoding/json" "fmt" - "github.com/VirusTotal/vt-go" "io" "mime/multipart" "net/http" ) -const endpointFile = "https://www.virustotal.com/api/v3/files/%s" +const endpointBase = "https://www.virustotal.com/api/v3/" +const endpointUrl = endpointBase + "urls/%s" +const endpointFile = endpointBase + "files/%s" func newRequest(key string, method string, url string, body io.ReadCloser) (*http.Request, error) { var req *http.Request @@ -47,24 +48,35 @@ func newRequest(key string, method string, url string, body io.ReadCloser) (*htt } func HasVirusTotalWarning(key string, urls []string) bool { - vtClient := vt.NewClient(key) for _, url := range urls { - if checkVtSingleUrl(vtClient, url) { + if checkVtSingleUrl(key, url) { return true } } return false } -func checkVtSingleUrl(client *vt.Client, url string) bool { +func checkVtSingleUrl(key string, url string) bool { urlId := base64.RawURLEncoding.EncodeToString([]byte(url)) - report, err := client.Get(vt.URL("urls/%s", urlId)) + req, err := newRequest(key, http.MethodGet, fmt.Sprintf(endpointUrl, urlId), nil) + if err != nil { + return false + } + report, err := http.DefaultClient.Do(req) if report == nil || err != nil { return false } + var body []byte + if body, err = io.ReadAll(report.Body); err != nil { + return false + } var result map[string]interface{} - err = json.Unmarshal(report.Data, &result) - attributes := result["attributes"] + err = json.Unmarshal(body, &result) + data := result["data"] + if _, valid := data.(map[string]interface{}); !valid { + return false + } + attributes := data.(map[string]interface{})["attributes"] if _, valid := attributes.(map[string]interface{}); !valid { return false } diff --git a/go.mod b/go.mod index b68d2b1..82cab86 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module matrix-guardian go 1.23 require ( - github.com/VirusTotal/vt-go v1.0.1 github.com/mattn/go-sqlite3 v1.14.24 maunium.net/go/mautrix v0.22.0 ) @@ -13,7 +12,6 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/rs/zerolog v1.33.0 // indirect - github.com/thedevsaddam/gojsonq/v2 v2.5.2 // indirect github.com/tidwall/gjson v1.18.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index 9fc205e..17e90c1 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,6 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/VirusTotal/vt-go v1.0.1 h1:rj/qugIY8XNC6ogwOaeJAGCOsb3nmY63+yuDMtHOx0Q= -github.com/VirusTotal/vt-go v1.0.1/go.mod h1:u1+HeRyl/gQs67eDgVEWNE7+x+zCyXhdtNVrRJR5YPE= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -21,12 +18,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0= -github.com/thedevsaddam/gojsonq/v2 v2.5.2/go.mod h1:bv6Xa7kWy82uT0LnXPE2SzGqTj33TAEeR560MdJkiXs= github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= @@ -48,8 +41,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mautrix v0.22.0 h1:nLrnLYiMyFV6qZPqpkNogkOPgm2dQTYiQXlu9Nc3rz8=