Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webhooks and reactions 404 issues #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/sirupsen/logrus"
)

var client *http.Client
Expand Down Expand Up @@ -213,6 +215,15 @@ func GetBotUser(token string) (*BotUserResponse, error) {
}

func doDiscordReq(ctx context.Context, path string, method string, body io.ReadCloser, header http.Header, query string) (*http.Response, error) {
route := GetMetricsPath(path)
if route == "/channels/!/messages/!/reactions/!/!" {
segs := strings.Split(path, "/")
unescaped, _ := url.PathUnescape(segs[7])
if segs[7] == unescaped {
segs[7] = url.PathEscape(segs[7])
}
path = strings.Join(segs, "/")
}
discordReq, err := http.NewRequestWithContext(ctx, method, "https://discord.com"+path+"?"+query, body)
if err != nil {
return nil, err
Expand All @@ -229,7 +240,6 @@ func doDiscordReq(ctx context.Context, path string, method string, body io.ReadC
}

if err == nil {
route := GetMetricsPath(path)
status := discordResp.Status
method := discordResp.Request.Method
elapsed := time.Since(startTime).Seconds()
Expand Down
17 changes: 13 additions & 4 deletions lib/queue.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package lib

import (
"bytes"
"context"
"errors"
"github.com/Clever/leakybucket"
"github.com/Clever/leakybucket/memory"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/Clever/leakybucket"
"github.com/Clever/leakybucket/memory"
"github.com/sirupsen/logrus"
)

type QueueItem struct {
Expand Down Expand Up @@ -272,6 +276,11 @@ func return401(item *QueueItem) {
item.doneChan <- nil
}

func isUnknownWebhook(_body io.ReadCloser) bool {
body, _ := ioutil.ReadAll(_body);
return bytes.Contains(body, []byte("\"code\": 10015"))
}

func isInteraction(url string) bool {
parts := strings.Split(strings.SplitN(url, "?", 1)[0], "/")
for _, p := range parts {
Expand Down Expand Up @@ -348,7 +357,7 @@ func (q *RequestQueue) subscribe(ch *QueueChannel, path string, pathHash uint64)
}).Warn("Unexpected 429")
}

if resp.StatusCode == 404 && strings.HasPrefix(path, "/webhooks/") && !isInteraction(item.Req.URL.String()) {
if resp.StatusCode == 404 && isUnknownWebhook(resp.Body) && !isInteraction(item.Req.URL.String()) {
logger.WithFields(logrus.Fields{
"bucket": path,
"route": item.Req.URL.String(),
Expand Down
Loading