2022-11-18 02:54:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var metric = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: "pk_http_requests",
|
|
|
|
Buckets: []float64{.1, .25, 1, 2.5, 5, 20},
|
|
|
|
},
|
|
|
|
[]string{"domain", "method", "status", "route"},
|
|
|
|
)
|
|
|
|
|
|
|
|
func proxyTo(host string) *httputil.ReverseProxy {
|
|
|
|
rp := httputil.NewSingleHostReverseProxy(&url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: host,
|
|
|
|
RawQuery: "",
|
|
|
|
})
|
|
|
|
rp.ModifyResponse = logTimeElapsed
|
|
|
|
return rp
|
|
|
|
}
|
|
|
|
|
2022-11-23 03:24:36 +00:00
|
|
|
var systemsRegex = regexp.MustCompile("systems/[^/{}]+")
|
|
|
|
var membersRegex = regexp.MustCompile("members/[^/{}]+")
|
|
|
|
var groupsRegex = regexp.MustCompile("groups/[^/{}]+")
|
|
|
|
var switchesRegex = regexp.MustCompile("switches/[^/{}]+")
|
|
|
|
var guildsRegex = regexp.MustCompile("guilds/[^/{}]+")
|
|
|
|
var messagesRegex = regexp.MustCompile("messages/[^/{}]+")
|
2022-11-18 02:54:37 +00:00
|
|
|
|
|
|
|
func cleanPath(host, path string) string {
|
|
|
|
if host != "api.pluralkit.me" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-12-03 11:59:36 +00:00
|
|
|
path = strings.ToLower(path)
|
|
|
|
|
2022-11-23 03:24:36 +00:00
|
|
|
if !(strings.HasPrefix(path, "/v2") || strings.HasPrefix(path, "/private")) {
|
2022-11-18 02:54:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-11-23 03:24:36 +00:00
|
|
|
path = systemsRegex.ReplaceAllString(path, "systems/{systemRef}")
|
|
|
|
path = membersRegex.ReplaceAllString(path, "members/{memberRef}")
|
|
|
|
path = groupsRegex.ReplaceAllString(path, "groups/{groupRef}")
|
|
|
|
path = switchesRegex.ReplaceAllString(path, "switches/{switchRef}")
|
|
|
|
path = guildsRegex.ReplaceAllString(path, "guilds/{guild_id}")
|
|
|
|
path = messagesRegex.ReplaceAllString(path, "messages/{message_id}")
|
2022-11-18 02:54:37 +00:00
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireEnv(key string) string {
|
|
|
|
if val, ok := os.LookupEnv(key); !ok {
|
|
|
|
panic("missing `" + key + "` in environment")
|
|
|
|
} else {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 12:00:41 +00:00
|
|
|
|
|
|
|
func is_trying_to_use_v1_path_on_v2(path string) bool {
|
|
|
|
return strings.HasPrefix(path, "/v2/s/") ||
|
|
|
|
strings.HasPrefix(path, "/v2/m/") ||
|
|
|
|
strings.HasPrefix(path, "/v2/a/") ||
|
|
|
|
strings.HasPrefix(path, "/v2/msg/") ||
|
|
|
|
path == "/v2/s" ||
|
|
|
|
path == "/v2/m"
|
|
|
|
}
|