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