resource.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vhost
  15. import (
  16. "bytes"
  17. "io"
  18. "net/http"
  19. "os"
  20. "github.com/fatedier/frp/pkg/util/log"
  21. "github.com/fatedier/frp/pkg/util/version"
  22. )
  23. var NotFoundPagePath = ""
  24. const (
  25. NotFound = `<!DOCTYPE html>
  26. <html>
  27. <head>
  28. <title>Not Found</title>
  29. <style>
  30. body {
  31. width: 35em;
  32. margin: 0 auto;
  33. font-family: Tahoma, Verdana, Arial, sans-serif;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <h1>The page you requested was not found.</h1>
  39. <p>Sorry, the page you are looking for is currently unavailable.<br/>
  40. Please try again later.</p>
  41. <p>The server is powered by <a href="https://github.com/fatedier/frp">frp</a>.</p>
  42. <p><em>Faithfully yours, frp.</em></p>
  43. </body>
  44. </html>
  45. `
  46. )
  47. func getNotFoundPageContent() []byte {
  48. var (
  49. buf []byte
  50. err error
  51. )
  52. if NotFoundPagePath != "" {
  53. buf, err = os.ReadFile(NotFoundPagePath)
  54. if err != nil {
  55. log.Warnf("read custom 404 page error: %v", err)
  56. buf = []byte(NotFound)
  57. }
  58. } else {
  59. buf = []byte(NotFound)
  60. }
  61. return buf
  62. }
  63. func NotFoundResponse() *http.Response {
  64. header := make(http.Header)
  65. header.Set("server", "frp/"+version.Full())
  66. header.Set("Content-Type", "text/html")
  67. content := getNotFoundPageContent()
  68. res := &http.Response{
  69. Status: "Not Found",
  70. StatusCode: 404,
  71. Proto: "HTTP/1.1",
  72. ProtoMajor: 1,
  73. ProtoMinor: 1,
  74. Header: header,
  75. Body: io.NopCloser(bytes.NewReader(content)),
  76. ContentLength: int64(len(content)),
  77. }
  78. return res
  79. }