frps: support custom_404_page

This commit is contained in:
fatedier 2019-04-25 12:29:34 +08:00
parent 6a1f15b25e
commit 0dfd3a421c
5 changed files with 39 additions and 3 deletions

View File

@ -65,3 +65,6 @@ subdomain_host = frps.com
# if tcp stream multiplexing is used, default is true # if tcp stream multiplexing is used, default is true
tcp_mux = true tcp_mux = true
# custom 404 page for HTTP requests
# custom_404_page = /path/to/404.html

View File

@ -69,6 +69,7 @@ type ServerCommonConf struct {
Token string `json:"token"` Token string `json:"token"`
SubDomainHost string `json:"subdomain_host"` SubDomainHost string `json:"subdomain_host"`
TcpMux bool `json:"tcp_mux"` TcpMux bool `json:"tcp_mux"`
Custom404Page string `json:"custom_404_page"`
AllowPorts map[int]struct{} AllowPorts map[int]struct{}
MaxPoolCount int64 `json:"max_pool_count"` MaxPoolCount int64 `json:"max_pool_count"`
@ -104,6 +105,7 @@ func GetDefaultServerConf() *ServerCommonConf {
MaxPortsPerClient: 0, MaxPortsPerClient: 0,
HeartBeatTimeout: 90, HeartBeatTimeout: 90,
UserConnTimeout: 10, UserConnTimeout: 10,
Custom404Page: "",
} }
} }
@ -293,6 +295,10 @@ func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (c
cfg.TcpMux = true cfg.TcpMux = true
} }
if tmpStr, ok = conf.Get("common", "custom_404_page"); ok {
cfg.Custom404Page = tmpStr
}
if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok { if tmpStr, ok = conf.Get("common", "heartbeat_timeout"); ok {
v, errRet := strconv.ParseInt(tmpStr, 10, 64) v, errRet := strconv.ParseInt(tmpStr, 10, 64)
if errRet != nil { if errRet != nil {

View File

@ -108,6 +108,9 @@ func NewService() (svr *Service, err error) {
return return
} }
// Init 404 not found page
vhost.NotFoundPagePath = cfg.Custom404Page
var ( var (
httpMuxOn bool httpMuxOn bool
httpsMuxOn bool httpsMuxOn bool

View File

@ -15,13 +15,18 @@
package vhost package vhost
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
frpLog "github.com/fatedier/frp/utils/log"
"github.com/fatedier/frp/utils/version" "github.com/fatedier/frp/utils/version"
) )
var (
NotFoundPagePath = ""
)
const ( const (
NotFound = `<!DOCTYPE html> NotFound = `<!DOCTYPE html>
<html> <html>
@ -46,10 +51,28 @@ Please try again later.</p>
` `
) )
func getNotFoundPageContent() []byte {
var (
buf []byte
err error
)
if NotFoundPagePath != "" {
buf, err = ioutil.ReadFile(NotFoundPagePath)
if err != nil {
frpLog.Warn("read custom 404 page error: %v", err)
buf = []byte(NotFound)
}
} else {
buf = []byte(NotFound)
}
return buf
}
func notFoundResponse() *http.Response { func notFoundResponse() *http.Response {
header := make(http.Header) header := make(http.Header)
header.Set("server", "frp/"+version.Full()) header.Set("server", "frp/"+version.Full())
header.Set("Content-Type", "text/html") header.Set("Content-Type", "text/html")
res := &http.Response{ res := &http.Response{
Status: "Not Found", Status: "Not Found",
StatusCode: 404, StatusCode: 404,
@ -57,7 +80,7 @@ func notFoundResponse() *http.Response {
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 0, ProtoMinor: 0,
Header: header, Header: header,
Body: ioutil.NopCloser(strings.NewReader(NotFound)), Body: ioutil.NopCloser(bytes.NewReader(getNotFoundPageContent())),
} }
return res return res
} }

View File

@ -254,7 +254,8 @@ func (p *ReverseProxy) serveHTTP(rw http.ResponseWriter, req *http.Request) {
if err != nil { if err != nil {
p.logf("http: proxy error: %v", err) p.logf("http: proxy error: %v", err)
rw.WriteHeader(http.StatusNotFound) rw.WriteHeader(http.StatusNotFound)
rw.Write([]byte(NotFound))
rw.Write(getNotFoundPageContent())
return return
} }