Skip to content

Commit

Permalink
Handle upstream connection error. Show 502 to user
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipok committed Mar 3, 2024
1 parent 119a7fe commit 5f1ea10
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
76 changes: 76 additions & 0 deletions 502.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 502 - Bad Gateway</title>
<style>
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
padding-top: 15vh;
font-family: Arial, sans-serif;
text-align: center;
background-color: #f9f9f9;
}

h1 {
color: #151515;
}

p {
color: #222;
}

footer {
margin-top: auto;
padding: 10px 0;
width: 100%;
position: fixed;
bottom: 8px;
text-align: center;
}

.github-link {
display: inline-block;
padding: 10px;
color: #444;
text-decoration: none;
border-radius: 5px;
}

.github-link:hover {
background-color: #C5C5C5;
}
</style>
</head>

<body>
<h1>Error 502 - Bad Gateway</h1>
<p>Oops! The server received an invalid response from the upstream server.</p>

<footer>
<a href="https://github.com/Jipok/jauth" class="github-link">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
<svg width="30" height="30" viewBox="0 0 24 24" style="vertical-align: middle;" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Dribbble-Light-Preview" transform="translate(-140.000000, -7559.000000)" fill="#000000">
<g id="icons" transform="translate(56.000000, 160.000000)">
<path
d="M94,7399 C99.523,7399 104,7403.59 104,7409.253 C104,7413.782 101.138,7417.624 97.167,7418.981 C96.66,7419.082 96.48,7418.762 96.48,7418.489 C96.48,7418.151 96.492,7417.047 96.492,7415.675 C96.492,7414.719 96.172,7414.095 95.813,7413.777 C98.04,7413.523 100.38,7412.656 100.38,7408.718 C100.38,7407.598 99.992,7406.684 99.35,7405.966 C99.454,7405.707 99.797,7404.664 99.252,7403.252 C99.252,7403.252 98.414,7402.977 96.505,7404.303 C95.706,7404.076 94.85,7403.962 94,7403.958 C93.15,7403.962 92.295,7404.076 91.497,7404.303 C89.586,7402.977 88.746,7403.252 88.746,7403.252 C88.203,7404.664 88.546,7405.707 88.649,7405.966 C88.01,7406.684 87.619,7407.598 87.619,7408.718 C87.619,7412.646 89.954,7413.526 92.175,7413.785 C91.889,7414.041 91.63,7414.493 91.54,7415.156 C90.97,7415.418 89.522,7415.871 88.63,7414.304 C88.63,7414.304 88.101,7413.319 87.097,7413.247 C87.097,7413.247 86.122,7413.234 87.029,7413.87 C87.029,7413.87 87.684,7414.185 88.139,7415.37 C88.139,7415.37 88.726,7417.2 91.508,7416.58 C91.513,7417.437 91.522,7418.245 91.522,7418.489 C91.522,7418.76 91.338,7419.077 90.839,7418.982 C86.865,7417.627 84,7413.783 84,7409.253 C84,7403.59 88.478,7399 94,7399"
id="github-[#142]">
</path>
</g>
</g>
</g>
</svg>
Jauth
</a>
</footer>
</body>

</html>
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"compress/gzip"
"crypto/sha256"
"embed"
_ "embed"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -79,11 +79,14 @@ var (
// Next comment will embed `index.html` file into executable during compile time
//
//go:embed index.html
var embed_index_html embed.FS
var embed_index_html string

//go:embed NotInWhitelist.html
var NotInWhitelist_PAGE string

//go:embed 502.html
var embed_502_html []byte

func main() {
// Config filename can be provided via command line
config_file := "jauth.toml"
Expand Down Expand Up @@ -142,15 +145,13 @@ func main() {
}

// Load login page. There is a built-in and the user can provide his own
var raw_index_page []byte
raw_index_page := []byte(embed_index_html)
if cfg.CustomPage != "" {
raw_index_page, err = os.ReadFile(cfg.CustomPage)
if err != nil {
log.Fatal(err)
}
log.Print("Using custom login page: ", cfg.CustomPage)
} else {
raw_index_page, err = embed_index_html.ReadFile("index.html")
}
if err != nil {
log.Fatal(err)
}

// Add Domain_Info for non specified domains, ip address
Expand Down Expand Up @@ -253,6 +254,10 @@ func blue(in string) string {
return fmt.Sprintf("\033[0;34m%s\033[0;0m", in)
}

func yellow(in string) string {
return fmt.Sprintf("\033[0;33m%s\033[0;0m", in)
}

func expandTilde(path string) string {
// Expand tilde ~ to home dir
user_dir, err := os.UserHomeDir()
Expand Down
5 changes: 5 additions & 0 deletions ssl-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func startWebServer() {
r.SetXForwarded()
r.Out.Host = r.In.Host
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
log.Printf(yellow("User can't access https://%s - ")+"%s", r.Host, err)
w.WriteHeader(http.StatusInternalServerError)
w.Write(embed_502_html)
},
}
// See auth-handler.go
mux := newAuthMux(proxy)
Expand Down

0 comments on commit 5f1ea10

Please sign in to comment.