resource.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. frpLog "github.com/fatedier/frp/pkg/util/log"
  21. "github.com/fatedier/frp/pkg/util/version"
  22. )
  23. var (
  24. NotFoundPagePath = ""
  25. )
  26. const (
  27. NotFound = `<!DOCTYPE html>
  28. <html>
  29. <head>
  30. <title>Not Found</title>
  31. <style>
  32. body {
  33. width: 35em;
  34. margin: 0 auto;
  35. font-family: Tahoma, Verdana, Arial, sans-serif;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <h1>The page you requested was not found.</h1>
  41. <p>Sorry, the page you are looking for is currently unavailable.<br/>
  42. Please try again later.</p>
  43. <p>The server is powered by <a href="https://github.com/fatedier/frp">frp</a>.</p>
  44. <p><em>Faithfully yours, frp.</em></p>
  45. </body>
  46. </html>
  47. `
  48. )
  49. func getNotFoundPageContent() []byte {
  50. var (
  51. buf []byte
  52. err error
  53. )
  54. if NotFoundPagePath != "" {
  55. buf, err = os.ReadFile(NotFoundPagePath)
  56. if err != nil {
  57. frpLog.Warn("read custom 404 page error: %v", err)
  58. buf = []byte(NotFound)
  59. }
  60. } else {
  61. buf = []byte(NotFound)
  62. }
  63. return buf
  64. }
  65. func notFoundResponse() *http.Response {
  66. header := make(http.Header)
  67. header.Set("server", "frp/"+version.Full())
  68. header.Set("Content-Type", "text/html")
  69. res := &http.Response{
  70. Status: "Not Found",
  71. StatusCode: 404,
  72. Proto: "HTTP/1.0",
  73. ProtoMajor: 1,
  74. ProtoMinor: 0,
  75. Header: header,
  76. Body: io.NopCloser(bytes.NewReader(getNotFoundPageContent())),
  77. }
  78. return res
  79. }
  80. func noAuthResponse() *http.Response {
  81. header := make(map[string][]string)
  82. header["WWW-Authenticate"] = []string{`Basic realm="Restricted"`}
  83. res := &http.Response{
  84. Status: "401 Not authorized",
  85. StatusCode: 401,
  86. Proto: "HTTP/1.1",
  87. ProtoMajor: 1,
  88. ProtoMinor: 1,
  89. Header: header,
  90. }
  91. return res
  92. }