github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_shutdown.go (about) 1 package nodes 2 3 import ( 4 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" 5 "github.com/TeaOSLab/EdgeNode/internal/remotelogs" 6 "github.com/TeaOSLab/EdgeNode/internal/utils" 7 "github.com/iwind/TeaGo/Tea" 8 "net/http" 9 "os" 10 "path" 11 "strings" 12 ) 13 14 // 调用临时关闭页面 15 func (this *HTTPRequest) doShutdown() { 16 var shutdown = this.web.Shutdown 17 if shutdown == nil { 18 return 19 } 20 21 if len(shutdown.BodyType) == 0 || shutdown.BodyType == serverconfigs.HTTPPageBodyTypeURL { 22 // URL 23 if urlSchemeRegexp.MatchString(shutdown.URL) { 24 this.doURL(http.MethodGet, shutdown.URL, "", shutdown.Status, true) 25 return 26 } 27 28 // URL为空,则显示文本 29 if len(shutdown.URL) == 0 { 30 // 自定义响应Headers 31 if shutdown.Status > 0 { 32 this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status) 33 this.writer.WriteHeader(shutdown.Status) 34 } else { 35 this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK) 36 this.writer.WriteHeader(http.StatusOK) 37 } 38 _, _ = this.writer.WriteString("The site have been shutdown.") 39 return 40 } 41 42 // 从本地文件中读取 43 var realpath = path.Clean(shutdown.URL) 44 if !strings.HasPrefix(realpath, "/pages/") && !strings.HasPrefix(realpath, "pages/") { // only files under "/pages/" can be used 45 var msg = "404 page not found: '" + shutdown.URL + "'" 46 this.writer.WriteHeader(http.StatusNotFound) 47 _, _ = this.writer.Write([]byte(msg)) 48 return 49 } 50 var file = Tea.Root + Tea.DS + shutdown.URL 51 fp, err := os.Open(file) 52 if err != nil { 53 var msg = "404 page not found: '" + shutdown.URL + "'" 54 this.writer.WriteHeader(http.StatusNotFound) 55 _, _ = this.writer.Write([]byte(msg)) 56 return 57 } 58 59 defer func() { 60 _ = fp.Close() 61 }() 62 63 // 自定义响应Headers 64 if shutdown.Status > 0 { 65 this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status) 66 this.writer.WriteHeader(shutdown.Status) 67 } else { 68 this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK) 69 this.writer.WriteHeader(http.StatusOK) 70 } 71 var buf = utils.BytePool1k.Get() 72 _, err = utils.CopyWithFilter(this.writer, fp, buf.Bytes, func(p []byte) []byte { 73 return []byte(this.Format(string(p))) 74 }) 75 utils.BytePool1k.Put(buf) 76 if err != nil { 77 if !this.canIgnore(err) { 78 remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "write to client failed: "+err.Error()) 79 } 80 } else { 81 this.writer.SetOk() 82 } 83 } else if shutdown.BodyType == serverconfigs.HTTPPageBodyTypeHTML { 84 // 自定义响应Headers 85 if shutdown.Status > 0 { 86 this.ProcessResponseHeaders(this.writer.Header(), shutdown.Status) 87 this.writer.WriteHeader(shutdown.Status) 88 } else { 89 this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK) 90 this.writer.WriteHeader(http.StatusOK) 91 } 92 93 _, err := this.writer.WriteString(this.Format(shutdown.Body)) 94 if err != nil { 95 if !this.canIgnore(err) { 96 remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "write to client failed: "+err.Error()) 97 } 98 } else { 99 this.writer.SetOk() 100 } 101 } else if shutdown.BodyType == serverconfigs.HTTPPageBodyTypeRedirectURL { 102 var newURL = shutdown.URL 103 if len(newURL) == 0 { 104 newURL = "/" 105 } 106 107 if shutdown.Status > 0 && httpStatusIsRedirect(shutdown.Status) { 108 httpRedirect(this.writer, this.RawReq, newURL, shutdown.Status) 109 } else { 110 httpRedirect(this.writer, this.RawReq, newURL, http.StatusTemporaryRedirect) 111 } 112 this.writer.SetOk() 113 } 114 }