custom error page enabled for default for non-exist routes and invalid host

This commit is contained in:
yusing 2024-09-28 11:45:01 +08:00
parent 9a6a66f5a8
commit da6a2756fa
5 changed files with 59 additions and 70 deletions

View file

@ -71,8 +71,13 @@ Example:
Hot-reloading is **supported**, you can **edit**, **rename** or **delete** files **without restarting**. Changes will be reflected after page reload Hot-reloading is **supported**, you can **edit**, **rename** or **delete** files **without restarting**. Changes will be reflected after page reload
Error page will be served if: Error page will be served if:
- status code is not in range of 200 to 300
- content type is `text/html`, `application/xhtml+xml` or `text/plain` - route does not exist or domain does not match any of `match_domains`
or
- status code is not in range of 200 to 300, and
- content type is `text/html`, `application/xhtml+xml` or `text/plain`
Error page will be served: Error page will be served:

View file

@ -23,3 +23,5 @@ var (
return clone return clone
}() }()
) )
const StaticFilePathPrefix = "/$gperrorpage/"

View file

@ -1,9 +1,7 @@
package route package route
import ( import (
"encoding/json"
"fmt" "fmt"
"slices"
"sync" "sync"
"net/http" "net/http"
@ -11,6 +9,7 @@ import (
"strings" "strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/yusing/go-proxy/internal/api/v1/error_page"
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/docker/idlewatcher" "github.com/yusing/go-proxy/internal/docker/idlewatcher"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
@ -172,20 +171,18 @@ func (u *URL) MarshalText() (text []byte, err error) {
func ProxyHandler(w http.ResponseWriter, r *http.Request) { func ProxyHandler(w http.ResponseWriter, r *http.Request) {
mux, err := findMuxFunc(r.Host) mux, err := findMuxFunc(r.Host)
if err != nil { if err != nil {
logrus.Error(E.Failure("request"). if !middleware.ServeStaticErrorPageFile(w, r) {
Subjectf("%s %s", r.Method, r.URL.String()). logrus.Error(E.Failure("request").
With(err)) Subjectf("%s %s", r.Method, r.URL.String()).
acceptTypes := r.Header.Values("Accept") With(err))
switch { errorPage, ok := error_page.GetErrorPageByStatus(http.StatusNotFound)
case slices.Contains(acceptTypes, "text/html"): if ok {
w.WriteHeader(http.StatusNotFound)
case slices.Contains(acceptTypes, "application/json"): w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusNotFound) w.Write(errorPage)
json.NewEncoder(w).Encode(map[string]string{ } else {
"error": err.Error(), http.Error(w, err.Error(), http.StatusNotFound)
}) }
default:
http.Error(w, err.Error(), http.StatusNotFound)
} }
return return
} }

View file

@ -10,40 +10,15 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/yusing/go-proxy/internal/api/v1/error_page" "github.com/yusing/go-proxy/internal/api/v1/error_page"
"github.com/yusing/go-proxy/internal/common"
gpHTTP "github.com/yusing/go-proxy/internal/http" gpHTTP "github.com/yusing/go-proxy/internal/http"
) )
const staticFilePathPrefix = "/$gperrorpage/"
var CustomErrorPage = &Middleware{ var CustomErrorPage = &Middleware{
before: func(next http.Handler, w ResponseWriter, r *Request) { before: func(next http.Handler, w ResponseWriter, r *Request) {
path := r.URL.Path if !ServeStaticErrorPageFile(w, r) {
if path != "" && path[0] != '/' { next.ServeHTTP(w, r)
path = "/" + path
} }
if strings.HasPrefix(path, staticFilePathPrefix) {
filename := path[len(staticFilePathPrefix):]
file, ok := error_page.GetStaticFile(filename)
if !ok {
http.NotFound(w, r)
errPageLogger.Errorf("unable to load resource %s", filename)
} else {
ext := filepath.Ext(filename)
switch ext {
case ".html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
case ".js":
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
case ".css":
w.Header().Set("Content-Type", "text/css; charset=utf-8")
default:
errPageLogger.Errorf("unexpected file type %q for %s", ext, filename)
}
w.Write(file)
}
return
}
next.ServeHTTP(w, r)
}, },
modifyResponse: func(resp *Response) error { modifyResponse: func(resp *Response) error {
// only handles non-success status code and html/plain content type // only handles non-success status code and html/plain content type
@ -67,4 +42,34 @@ var CustomErrorPage = &Middleware{
}, },
} }
func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) bool {
path := r.URL.Path
if path != "" && path[0] != '/' {
path = "/" + path
}
if strings.HasPrefix(path, common.StaticFilePathPrefix) {
filename := path[len(common.StaticFilePathPrefix):]
file, ok := error_page.GetStaticFile(filename)
if !ok {
errPageLogger.Errorf("unable to load resource %s", filename)
return false
} else {
ext := filepath.Ext(filename)
switch ext {
case ".html":
w.Header().Set("Content-Type", "text/html; charset=utf-8")
case ".js":
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
case ".css":
w.Header().Set("Content-Type", "text/css; charset=utf-8")
default:
errPageLogger.Errorf("unexpected file type %q for %s", ext, filename)
}
w.Write(file)
return true
}
}
return false
}
var errPageLogger = logrus.WithField("middleware", "error_page") var errPageLogger = logrus.WithField("middleware", "error_page")

View file

@ -64,8 +64,7 @@
"port": {}, "port": {},
"no_tls_verify": {}, "no_tls_verify": {},
"path_patterns": {}, "path_patterns": {},
"set_headers": {}, "middlewares": {}
"hide_headers": {}
}, },
"additionalProperties": false, "additionalProperties": false,
"allOf": [ "allOf": [
@ -121,24 +120,8 @@
} }
] ]
}, },
"set_headers": { "middlewares": {
"type": "object", "type": "object"
"description": "Proxy headers to set",
"additionalProperties": {
"items": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"hide_headers": {
"type": "array",
"description": "Proxy headers to hide",
"items": {
"type": "string"
}
} }
} }
}, },
@ -156,10 +139,7 @@
"path_patterns": { "path_patterns": {
"not": true "not": true
}, },
"set_headers": { "middlewares": {
"not": true
},
"hide_headers": {
"not": true "not": true
} }
}, },