fix(web-proxy): move UA check into API block, small clean up

This commit is contained in:
Iris System 2023-03-01 06:11:08 +13:00
parent 7e606ca8b2
commit 7fffb7f65a

View File

@ -48,14 +48,6 @@ func init() {
type ProxyHandler struct{} type ProxyHandler struct{}
func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") == "" {
// please set a valid user-agent
rw.Header().Set("content-type", "application/json")
rw.WriteHeader(400)
rw.Write([]byte(`{"message":"A valid User-Agent header is required.","code":0}`))
return
}
remote, ok := remotes[r.Host] remote, ok := remotes[r.Host]
if !ok { if !ok {
// unknown domains redirect to landing page // unknown domains redirect to landing page
@ -65,7 +57,7 @@ func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.Host == "api.pluralkit.me" { if r.Host == "api.pluralkit.me" {
// root // root
if r.URL.Path == "" { if r.URL.Path == "" || r.URL.Path == "/" {
// api root path redirects to docs // api root path redirects to docs
http.Redirect(rw, r, "https://pluralkit.me/api/", http.StatusFound) http.Redirect(rw, r, "https://pluralkit.me/api/", http.StatusFound)
return return
@ -78,13 +70,16 @@ func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("Access-Control-Allow-Headers", "Content-Type, Authorization, sentry-trace, User-Agent") rw.Header().Add("Access-Control-Allow-Headers", "Content-Type, Authorization, sentry-trace, User-Agent")
rw.Header().Add("Access-Control-Max-Age", "86400") rw.Header().Add("Access-Control-Max-Age", "86400")
if r.Method == http.MethodOptions { if r.Header.Get("User-Agent") == "" {
rw.WriteHeader(200) // please set a valid user-agent
rw.Header().Set("content-type", "application/json")
rw.WriteHeader(400)
rw.Write([]byte(`{"message":"A valid User-Agent header is required.","code":0}`))
return return
} }
if r.URL.Path == "/" { if r.Method == http.MethodOptions {
http.Redirect(rw, r, "https://pluralkit.me/api", http.StatusFound) rw.WriteHeader(200)
return return
} }
@ -92,9 +87,11 @@ func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("content-type", "application/json") rw.Header().Set("content-type", "application/json")
rw.WriteHeader(410) rw.WriteHeader(410)
rw.Write([]byte(`{"message":"Unsupported API version","code":0}`)) rw.Write([]byte(`{"message":"Unsupported API version","code":0}`))
return
} }
if is_trying_to_use_v1_path_on_v2(r.URL.Path) { if is_trying_to_use_v1_path_on_v2(r.URL.Path) {
rw.Header().Set("content-type", "application/json")
rw.WriteHeader(400) rw.WriteHeader(400)
rw.Write([]byte(`{"message":"Invalid path for API version","code":0}`)) rw.Write([]byte(`{"message":"Invalid path for API version","code":0}`))
return return