Commit: f60d6989d2d27538162e3c051f9a9d221dccac54 Parent: eb8fe7aadf7993ed40881c8ed50e75a9488ce044 Author: Vi Grey Date: 2024-06-18 12:25 UTC Summary: Add HTTP Error HTML content CHANGELOG.txt | 6 +++++ src/augelmir.go | 2 +- src/http.go | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 100 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index eff26a0..42fdb0d 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## [0.0.2] - 2024-06-18 + +### Added +- HTTP Error HTML content + + ## [0.0.1] - 2024-06-18 ### Added diff --git a/src/augelmir.go b/src/augelmir.go index dac640c..d47688b 100644 --- a/src/augelmir.go +++ b/src/augelmir.go @@ -6,7 +6,7 @@ import ( ) const ( - VERSION = "0.0.1" + VERSION = "0.0.2" ) func init() { diff --git a/src/http.go b/src/http.go index 27de702..ba8381f 100644 --- a/src/http.go +++ b/src/http.go @@ -3,6 +3,7 @@ package main import ( "crypto/tls" "fmt" + "io" "net" "net/http" "net/http/httputil" @@ -14,14 +15,70 @@ import ( ) var ( - httpStatuses = []int{ - 100, 101, 102, 103, - 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, - 300, 301, 302, 303, 304, 305, 307, 308, - 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, - 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, - 422, 423, 424, 425, 426, 428, 429, 431, 451, - 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, + httpStatuses = map[int]string{ + 100: "Continue", + 101: "Switching Protocols", + 102: "Processing", + 103: "Early Hints", + 200: "OK", + 201: "Created", + 202: "Accepted", + 203: "Non-Authorative Information", + 204: "No Content", + 205: "Reset Content", + 206: "Partial Content", + 207: "Multi-Status", + 208: "Already Reported", + 226: "IM Used", + 300: "Multiple Choices", + 301: "Moved Permanently", + 302: "Found", + 303: "See Other", + 304: "Not Modified", + 305: "Use Proxy", + 306: "Switch Proxy", + 307: "Temporary Redirect", + 308: "Permanent Redirect", + 400: "Bad Request", + 401: "Unauthorized", + 402: "Payment Required", + 403: "Forbidden", + 404: "Not Found", + 405: "Method Not Allowed", + 406: "Not Acceptable", + 407: "Proxy Authentication Required", + 408: "Request Timeout", + 409: "Conflict", + 410: "Gone", + 411: "Length Required", + 412: "Precondition Failed", + 413: "Payload Too Large", + 414: "URI Too Long", + 415: "Unsupported Media Type", + 416: "Range Not Satisfiable", + 417: "Expectation Failed", + 418: "I'm a teapot", + 421: "Misdirected Request", + 422: "Unprocessable Content", + 423: "Locked", + 424: "Failed Dependency", + 425: "Too Early", + 426: "Upgrade Required", + 428: "Precondition Required", + 429: "Too Many Requests", + 431: "Request Header Fields Too Large", + 451: "Unavailable For Legal Reasons", + 500: "Internal Server Error", + 501: "Not Implemented", + 502: "Bad Gateway", + 503: "Service Unavailable", + 504: "Gateway Timeout", + 505: "HTTP Version Not Supported", + 506: "Variant Also Negotiates", + 507: "Insufficient Storage", + 508: "Loop Detected", + 510: "Not Extended", + 511: "Network Authentication Required", } tlsVersions = map[string]uint16{ "1.2": 0x0303, @@ -61,16 +118,9 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { break } } - var validStatus bool - for _, s := range httpStatuses { - if status == s { - validStatus = true - break - } - } // If no status set and no web_roots and no proxy_address, kill TCP connection // If error, close HTTP connection "normally" - if !validStatus && len(webRoots) == 0 && proxyAddress == "" { + if len(httpStatuses[status]) == 0 && len(webRoots) == 0 && proxyAddress == "" { // Create HTTP hijacker if hijacker, ok := w.(http.Hijacker); ok { // Get raw TCP connection of HTTP connection @@ -90,16 +140,36 @@ func httpHandler(w http.ResponseWriter, r *http.Request) { for _, header := range headers { w.Header().Set(header.Key, header.Value) } - if validStatus { + if len(webRoots) > 0 { + status = serveFile(w, r, webRoots, index, optionalHTMLExt) + } else if len(httpStatuses[status]) != 0 { w.WriteHeader(status) } - if len(webRoots) > 0 { - serveFile(w, r, webRoots, index, optionalHTMLExt) - return + if status > 399 && status < 600 && len(httpStatuses[status]) > 0 { + serveHTTPError(w, r, status) } } -func serveFile(w http.ResponseWriter, r *http.Request, webRoots []string, index, optionalHTMLExt bool) { +func serveHTTPError(w http.ResponseWriter, r *http.Request, status int) { + w.WriteHeader(status) + io.WriteString(w, fmt.Sprintf("\n"+ + ""+ + " "+ + " \n"+ + " \n"+ + " \n"+ + " HTTP Error: %d - %s\n"+ + " \n"+ + " \n"+ + "

HTTP Error: %d

\n"+ + "

%s

\n"+ + "
\n"+ + " Back Home\n"+ + " \n"+ + "", status, httpStatuses[status], status, httpStatuses[status])) +} + +func serveFile(w http.ResponseWriter, r *http.Request, webRoots []string, index, optionalHTMLExt bool) (status int) { var tryPaths []string for _, webRoot := range webRoots { unescapedPath, _ := url.PathUnescape(r.URL.Path) @@ -122,7 +192,8 @@ func serveFile(w http.ResponseWriter, r *http.Request, webRoots []string, index, return } } - w.WriteHeader(404) + status = 404 + return } func newProxy(targetHost string) (*httputil.ReverseProxy, error) {