2020-09-26 08:07:07 +08:00
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-05-22 17:31:26 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2021-06-06 23:42:11 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
2021-09-21 09:29:17 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
2020-09-26 08:07:07 +08:00
|
|
|
|
"github.com/iwind/TeaGo/Tea"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
2023-07-07 11:50:10 +08:00
|
|
|
|
"path"
|
|
|
|
|
|
"strings"
|
2020-09-26 08:07:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-01 15:44:36 +08:00
|
|
|
|
const defaultPageContentType = "text/html; charset=utf-8"
|
|
|
|
|
|
|
2020-09-26 08:07:07 +08:00
|
|
|
|
// 请求特殊页面
|
|
|
|
|
|
func (this *HTTPRequest) doPage(status int) (shouldStop bool) {
|
|
|
|
|
|
if len(this.web.Pages) == 0 {
|
2023-05-22 17:31:26 +08:00
|
|
|
|
// 集群自定义页面
|
|
|
|
|
|
if this.nodeConfig != nil && this.ReqServer != nil {
|
|
|
|
|
|
var httpPagesPolicy = this.nodeConfig.FindHTTPPagesPolicyWithClusterId(this.ReqServer.ClusterId)
|
|
|
|
|
|
if httpPagesPolicy != nil && httpPagesPolicy.IsOn && len(httpPagesPolicy.Pages) > 0 {
|
|
|
|
|
|
return this.doPageLookup(httpPagesPolicy.Pages, status)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-26 08:07:07 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-22 17:31:26 +08:00
|
|
|
|
// 查找当前网站自定义页面
|
|
|
|
|
|
shouldStop = this.doPageLookup(this.web.Pages, status)
|
|
|
|
|
|
if shouldStop {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 集群自定义页面
|
|
|
|
|
|
if this.nodeConfig != nil && this.ReqServer != nil {
|
|
|
|
|
|
var httpPagesPolicy = this.nodeConfig.FindHTTPPagesPolicyWithClusterId(this.ReqServer.ClusterId)
|
|
|
|
|
|
if httpPagesPolicy != nil && httpPagesPolicy.IsOn && len(httpPagesPolicy.Pages) > 0 {
|
|
|
|
|
|
return this.doPageLookup(httpPagesPolicy.Pages, status)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *HTTPRequest) doPageLookup(pages []*serverconfigs.HTTPPageConfig, status int) (shouldStop bool) {
|
|
|
|
|
|
for _, page := range pages {
|
2020-09-26 08:07:07 +08:00
|
|
|
|
if page.Match(status) {
|
2023-11-10 16:36:35 +08:00
|
|
|
|
if len(page.BodyType) == 0 || page.BodyType == serverconfigs.HTTPPageBodyTypeURL {
|
2023-06-16 09:56:37 +08:00
|
|
|
|
if urlSchemeRegexp.MatchString(page.URL) {
|
2021-11-05 14:10:43 +08:00
|
|
|
|
var newStatus = page.NewStatus
|
|
|
|
|
|
if newStatus <= 0 {
|
|
|
|
|
|
newStatus = status
|
|
|
|
|
|
}
|
|
|
|
|
|
this.doURL(http.MethodGet, page.URL, "", newStatus, true)
|
2021-10-10 10:35:05 +08:00
|
|
|
|
return true
|
|
|
|
|
|
} else {
|
2023-07-07 11:50:10 +08:00
|
|
|
|
var realpath = path.Clean(page.URL)
|
|
|
|
|
|
if !strings.HasPrefix(realpath, "/pages/") && !strings.HasPrefix(realpath, "pages/") { // only files under "/pages/" can be used
|
|
|
|
|
|
var msg = "404 page not found: '" + page.URL + "'"
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2023-07-07 11:50:10 +08:00
|
|
|
|
this.writer.WriteHeader(http.StatusNotFound)
|
|
|
|
|
|
_, _ = this.writer.Write([]byte(msg))
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
var file = Tea.Root + Tea.DS + realpath
|
2021-10-10 10:35:05 +08:00
|
|
|
|
fp, err := os.Open(file)
|
|
|
|
|
|
if err != nil {
|
2023-07-07 11:50:10 +08:00
|
|
|
|
var msg = "404 page not found: '" + page.URL + "'"
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2021-10-10 10:35:05 +08:00
|
|
|
|
this.writer.WriteHeader(http.StatusNotFound)
|
2023-07-07 11:50:10 +08:00
|
|
|
|
_, _ = this.writer.Write([]byte(msg))
|
2021-10-10 10:35:05 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
2023-07-07 11:50:10 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
_ = fp.Close()
|
|
|
|
|
|
}()
|
2020-09-26 08:07:07 +08:00
|
|
|
|
|
2021-10-16 12:21:28 +08:00
|
|
|
|
stat, err := fp.Stat()
|
|
|
|
|
|
if err != nil {
|
2023-07-07 11:50:10 +08:00
|
|
|
|
var msg = "404 could not read page content: '" + page.URL + "'"
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2021-10-16 12:21:28 +08:00
|
|
|
|
this.writer.WriteHeader(http.StatusNotFound)
|
2023-07-07 11:50:10 +08:00
|
|
|
|
_, _ = this.writer.Write([]byte(msg))
|
2021-10-16 12:21:28 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 10:35:05 +08:00
|
|
|
|
// 修改状态码
|
|
|
|
|
|
if page.NewStatus > 0 {
|
|
|
|
|
|
// 自定义响应Headers
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2023-06-11 10:46:20 +08:00
|
|
|
|
this.ProcessResponseHeaders(this.writer.Header(), page.NewStatus)
|
2022-02-15 14:55:49 +08:00
|
|
|
|
this.writer.Prepare(nil, stat.Size(), page.NewStatus, true)
|
2021-10-10 10:35:05 +08:00
|
|
|
|
this.writer.WriteHeader(page.NewStatus)
|
|
|
|
|
|
} else {
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2023-06-11 10:46:20 +08:00
|
|
|
|
this.ProcessResponseHeaders(this.writer.Header(), status)
|
2022-02-15 14:55:49 +08:00
|
|
|
|
this.writer.Prepare(nil, stat.Size(), status, true)
|
2021-10-10 10:35:05 +08:00
|
|
|
|
this.writer.WriteHeader(status)
|
|
|
|
|
|
}
|
2023-07-07 11:50:10 +08:00
|
|
|
|
var buf = utils.BytePool1k.Get()
|
2021-10-10 10:35:05 +08:00
|
|
|
|
_, err = utils.CopyWithFilter(this.writer, fp, buf, func(p []byte) []byte {
|
|
|
|
|
|
return []byte(this.Format(string(p)))
|
|
|
|
|
|
})
|
2021-12-19 11:32:26 +08:00
|
|
|
|
utils.BytePool1k.Put(buf)
|
2021-10-10 10:35:05 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if !this.canIgnore(err) {
|
|
|
|
|
|
remotelogs.Warn("HTTP_REQUEST_PAGE", "write to client failed: "+err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.writer.SetOk()
|
|
|
|
|
|
}
|
2020-09-26 08:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 10:35:05 +08:00
|
|
|
|
return true
|
2023-11-10 16:36:35 +08:00
|
|
|
|
} else if page.BodyType == serverconfigs.HTTPPageBodyTypeHTML {
|
2021-12-02 14:46:40 +08:00
|
|
|
|
// 这里需要实现设置Status,因为在Format()中可以获取${status}等变量
|
|
|
|
|
|
if page.NewStatus > 0 {
|
|
|
|
|
|
this.writer.statusCode = page.NewStatus
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.writer.statusCode = status
|
|
|
|
|
|
}
|
2021-10-16 12:21:28 +08:00
|
|
|
|
var content = this.Format(page.Body)
|
|
|
|
|
|
|
2020-09-26 08:07:07 +08:00
|
|
|
|
// 修改状态码
|
|
|
|
|
|
if page.NewStatus > 0 {
|
|
|
|
|
|
// 自定义响应Headers
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2023-06-11 10:46:20 +08:00
|
|
|
|
this.ProcessResponseHeaders(this.writer.Header(), page.NewStatus)
|
2022-02-15 14:55:49 +08:00
|
|
|
|
this.writer.Prepare(nil, int64(len(content)), page.NewStatus, true)
|
2020-09-26 08:07:07 +08:00
|
|
|
|
this.writer.WriteHeader(page.NewStatus)
|
|
|
|
|
|
} else {
|
2023-08-01 15:44:36 +08:00
|
|
|
|
this.writer.Header().Set("Content-Type", defaultPageContentType)
|
2023-06-11 10:46:20 +08:00
|
|
|
|
this.ProcessResponseHeaders(this.writer.Header(), status)
|
2022-02-15 14:55:49 +08:00
|
|
|
|
this.writer.Prepare(nil, int64(len(content)), status, true)
|
2020-09-26 08:07:07 +08:00
|
|
|
|
this.writer.WriteHeader(status)
|
|
|
|
|
|
}
|
2021-10-10 10:35:05 +08:00
|
|
|
|
|
2021-10-16 12:21:28 +08:00
|
|
|
|
_, err := this.writer.WriteString(content)
|
2020-09-26 08:07:07 +08:00
|
|
|
|
if err != nil {
|
2021-06-06 23:42:11 +08:00
|
|
|
|
if !this.canIgnore(err) {
|
|
|
|
|
|
remotelogs.Warn("HTTP_REQUEST_PAGE", "write to client failed: "+err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.writer.SetOk()
|
2020-09-26 08:07:07 +08:00
|
|
|
|
}
|
2021-10-10 10:35:05 +08:00
|
|
|
|
return true
|
2023-11-10 16:36:35 +08:00
|
|
|
|
} else if page.BodyType == serverconfigs.HTTPPageBodyTypeRedirectURL {
|
|
|
|
|
|
var newURL = page.URL
|
|
|
|
|
|
if len(newURL) == 0 {
|
|
|
|
|
|
newURL = "/"
|
|
|
|
|
|
}
|
|
|
|
|
|
if page.NewStatus > 0 && httpStatusIsRedirect(page.NewStatus) {
|
|
|
|
|
|
httpRedirect(this.writer, this.RawReq, newURL, page.NewStatus)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
httpRedirect(this.writer, this.RawReq, newURL, http.StatusTemporaryRedirect)
|
|
|
|
|
|
}
|
|
|
|
|
|
this.writer.SetOk()
|
|
|
|
|
|
return true
|
2020-09-26 08:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|