resource.go 2.3 KB

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