Skip to content

Commit

Permalink
Merge pull request #91 from IlluminatiFish/patch-1
Browse files Browse the repository at this point in the history
Better discord webhook regex using https://github.com/oriser/regroup
  • Loading branch information
ehsandeep authored Oct 26, 2021
2 parents b3cede6 + bd6da83 commit 03b36c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
26 changes: 13 additions & 13 deletions pkg/providers/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(`(?P<scheme>https?):\/\/(?P<domain>(?:ptb\.|canary\.)?discord(?:app)?\.com)\/api(?:\/)?(?P<api_version>v\d{1,2})?\/webhooks\/(?P<webhook_identifier>\d{17,19})\/(?P<webhook_token>[\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)
Expand Down

0 comments on commit 03b36c7

Please sign in to comment.