特殊页面可以直接使用HTML

This commit is contained in:
GoEdgeLab
2021-10-10 10:35:05 +08:00
parent fd0b093d58
commit b8db8ac71a
2 changed files with 109 additions and 66 deletions

View File

@@ -1,6 +1,7 @@
package nodes package nodes
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -20,6 +21,7 @@ func (this *HTTPRequest) doPage(status int) (shouldStop bool) {
for _, page := range this.web.Pages { for _, page := range this.web.Pages {
if page.Match(status) { if page.Match(status) {
if len(page.BodyType) == 0 || page.BodyType == shared.BodyTypeURL {
if urlPrefixRegexp.MatchString(page.URL) { if urlPrefixRegexp.MatchString(page.URL) {
this.doURL(http.MethodGet, page.URL, "", page.NewStatus, true) this.doURL(http.MethodGet, page.URL, "", page.NewStatus, true)
return true return true
@@ -66,6 +68,27 @@ func (this *HTTPRequest) doPage(status int) (shouldStop bool) {
} }
return true return true
} else if page.BodyType == shared.BodyTypeHTML {
// 修改状态码
if page.NewStatus > 0 {
// 自定义响应Headers
this.processResponseHeaders(page.NewStatus)
this.writer.WriteHeader(page.NewStatus)
} else {
this.processResponseHeaders(status)
this.writer.WriteHeader(status)
}
_, err := this.writer.WriteString(this.Format(page.Body))
if err != nil {
if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_PAGE", "write to client failed: "+err.Error())
}
} else {
this.writer.SetOk()
}
return true
}
} }
} }
return false return false

View File

@@ -1,6 +1,7 @@
package nodes package nodes
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
@@ -21,7 +22,8 @@ func (this *HTTPRequest) doShutdown() {
return return
} }
// URL为空则显示文本 TODO 未来可以自定义文本 if len(shutdown.BodyType) == 0 || shutdown.BodyType == shared.BodyTypeURL {
// URL为空则显示文本
if len(shutdown.URL) == 0 { if len(shutdown.URL) == 0 {
// 自定义响应Headers // 自定义响应Headers
if shutdown.Status > 0 { if shutdown.Status > 0 {
@@ -40,7 +42,6 @@ func (this *HTTPRequest) doShutdown() {
} }
// 从本地文件中读取 // 从本地文件中读取
// TODO 支持从数据库中读取文件
file := Tea.Root + Tea.DS + shutdown.URL file := Tea.Root + Tea.DS + shutdown.URL
fp, err := os.Open(file) fp, err := os.Open(file)
if err != nil { if err != nil {
@@ -80,4 +81,23 @@ func (this *HTTPRequest) doShutdown() {
if err != nil { if err != nil {
remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "close file failed: "+err.Error()) remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "close file failed: "+err.Error())
} }
} else if shutdown.BodyType == shared.BodyTypeHTML {
// 自定义响应Headers
if shutdown.Status > 0 {
this.processResponseHeaders(shutdown.Status)
this.writer.WriteHeader(shutdown.Status)
} else {
this.processResponseHeaders(http.StatusOK)
this.writer.WriteHeader(http.StatusOK)
}
_, 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()
}
}
} }