Files
EdgeNode/internal/nodes/http_request_shutdown.go

115 lines
3.3 KiB
Go
Raw Normal View History

2020-09-26 08:07:07 +08:00
package nodes
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"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"
"path"
"strings"
2020-09-26 08:07:07 +08:00
)
// 调用临时关闭页面
func (this *HTTPRequest) doShutdown() {
var shutdown = this.web.Shutdown
2020-09-26 08:07:07 +08:00
if shutdown == nil {
return
}
if len(shutdown.BodyType) == 0 || shutdown.BodyType == serverconfigs.HTTPPageBodyTypeURL {
// URL
if urlSchemeRegexp.MatchString(shutdown.URL) {
this.doURL(http.MethodGet, shutdown.URL, "", shutdown.Status, true)
return
}
2021-10-10 10:35:05 +08:00
// URL为空则显示文本
if len(shutdown.URL) == 0 {
// 自定义响应Headers
if shutdown.Status > 0 {
this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status)
2021-10-10 10:35:05 +08:00
this.writer.WriteHeader(shutdown.Status)
} else {
this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK)
2021-10-10 10:35:05 +08:00
this.writer.WriteHeader(http.StatusOK)
}
_, _ = this.writer.WriteString("The site have been shutdown.")
2021-10-10 10:35:05 +08:00
return
}
// 从本地文件中读取
var realpath = path.Clean(shutdown.URL)
if !strings.HasPrefix(realpath, "/pages/") && !strings.HasPrefix(realpath, "pages/") { // only files under "/pages/" can be used
var msg = "404 page not found: '" + shutdown.URL + "'"
this.writer.WriteHeader(http.StatusNotFound)
_, _ = this.writer.Write([]byte(msg))
return
}
var file = Tea.Root + Tea.DS + shutdown.URL
2021-10-10 10:35:05 +08:00
fp, err := os.Open(file)
if err != nil {
var msg = "404 page not found: '" + shutdown.URL + "'"
2021-10-10 10:35:05 +08:00
this.writer.WriteHeader(http.StatusNotFound)
_, _ = this.writer.Write([]byte(msg))
2021-10-10 10:35:05 +08:00
return
}
defer func() {
_ = fp.Close()
}()
2020-09-26 08:07:07 +08:00
// 自定义响应Headers
if shutdown.Status > 0 {
this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status)
2020-09-26 08:07:07 +08:00
this.writer.WriteHeader(shutdown.Status)
} else {
this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK)
2020-09-26 08:07:07 +08:00
this.writer.WriteHeader(http.StatusOK)
}
var buf = utils.BytePool1k.Get()
2024-04-15 09:26:00 +08:00
_, err = utils.CopyWithFilter(this.writer, fp, buf.Bytes, func(p []byte) []byte {
2021-10-10 10:35:05 +08:00
return []byte(this.Format(string(p)))
})
2021-12-19 11:32:26 +08:00
utils.BytePool1k.Put(buf)
2020-09-26 08:07:07 +08:00
if err != nil {
2021-10-10 10:35:05 +08:00
if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "write to client failed: "+err.Error())
}
} else {
this.writer.SetOk()
2020-09-26 08:07:07 +08:00
}
} else if shutdown.BodyType == serverconfigs.HTTPPageBodyTypeHTML {
2021-10-10 10:35:05 +08:00
// 自定义响应Headers
if shutdown.Status > 0 {
this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status)
2021-10-10 10:35:05 +08:00
this.writer.WriteHeader(shutdown.Status)
} else {
this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK)
2021-10-10 10:35:05 +08:00
this.writer.WriteHeader(http.StatusOK)
}
2021-10-10 10:35:05 +08:00
_, err := this.writer.WriteString(this.Format(shutdown.Body))
if err != nil {
if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "write to client failed: "+err.Error())
}
} else {
this.writer.SetOk()
}
} else if shutdown.BodyType == serverconfigs.HTTPPageBodyTypeRedirectURL {
var newURL = shutdown.URL
if len(newURL) == 0 {
newURL = "/"
}
if shutdown.Status > 0 && httpStatusIsRedirect(shutdown.Status) {
httpRedirect(this.writer, this.RawReq, newURL, shutdown.Status)
} else {
httpRedirect(this.writer, this.RawReq, newURL, http.StatusTemporaryRedirect)
}
this.writer.SetOk()
2020-09-26 08:07:07 +08:00
}
}