diff --git a/go.mod b/go.mod index 4c223b1..e4f112a 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/mattn/go-isatty v0.0.13 // indirect github.com/onsi/ginkgo v1.16.4 // indirect github.com/onsi/gomega v1.10.5 // indirect + github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 github.com/pkg/errors v0.9.1 github.com/projectdiscovery/goflags v0.0.7 github.com/projectdiscovery/gologger v1.1.4 diff --git a/go.sum b/go.sum index f9f91b2..4a989fa 100644 --- a/go.sum +++ b/go.sum @@ -193,6 +193,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 h1:Qd1Ml+uEhpesT8Og0ysEhu5+DGhbhW+qxjapH8t1Kvs= +github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263/go.mod h1:odkMeLkWS8G6+WP2z3Pn2vkzhPSvBtFhAUYTKXAtZMQ= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -245,6 +247,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/pkg/providers/discord/discord.go b/pkg/providers/discord/discord.go index ca56f6d..880c6fd 100644 --- a/pkg/providers/discord/discord.go +++ b/pkg/providers/discord/discord.go @@ -2,14 +2,13 @@ package discord import ( "fmt" - "regexp" - "strings" "github.com/containrrr/shoutrrr" "github.com/pkg/errors" "github.com/projectdiscovery/gologger" "github.com/projectdiscovery/notify/pkg/utils" "go.uber.org/multierr" + "github.com/oriser/regroup" ) type Provider struct { @@ -40,21 +39,22 @@ func (p *Provider) Send(message, CliFormat string) error { for _, pr := range p.Discord { msg := utils.FormatMessage(message, utils.SelectFormat(CliFormat, pr.DiscordFormat)) - discordRegex := regexp.MustCompile(`https://(discord.com|discordapp.com)/api/webhooks/`) - - discordTokens := strings.TrimPrefix(pr.DiscordWebHookURL, discordRegex.FindString(pr.DiscordWebHookURL)) - tokens := strings.Split(discordTokens, "/") - if len(tokens) < 2 { + + discordWebhookRegex := regroup.MustCompile(`(?Phttps?):\/\/(?P(?:ptb\.|canary\.)?discord(?:app)?\.com)\/api(?:\/)?(?Pv\d{1,2})?\/webhooks\/(?P\d{17,19})\/(?P[\w\-]{68})`) + matchedGroups, err := discordWebhookRegex.Groups(pr.DiscordWebHookURL) + + if err != nil { err := fmt.Errorf("incorrect discord configuration for id: %s ", pr.ID) DiscordErr = multierr.Append(DiscordErr, err) continue } - webhookID, token := tokens[0], tokens[1] - url := fmt.Sprintf("discord://%s@%s?splitlines=no", token, webhookID) - err := shoutrrr.Send(url, msg) - if err != nil { - err = errors.Wrap(err, fmt.Sprintf("failed to send discord notification for id: %s ", pr.ID)) - DiscordErr = multierr.Append(DiscordErr, err) + + webhookID, webhookToken := matchedGroups["webhook_identifier"], matchedGroups["webhook_token"] + url := fmt.Sprintf("discord://%s@%s?splitlines=no", webhookToken, webhookID) + sendErr := shoutrrr.Send(url, msg) + if sendErr != nil { + sendErr = errors.Wrap(sendErr, fmt.Sprintf("failed to send discord notification for id: %s ", pr.ID)) + DiscordErr = multierr.Append(DiscordErr, sendErr) continue } gologger.Verbose().Msgf("discord notification sent for id: %s", pr.ID)