diff --git a/lib/tun2/server.go b/lib/tun2/server.go
index 74694a5..d191372 100644
--- a/lib/tun2/server.go
+++ b/lib/tun2/server.go
@@ -433,6 +433,39 @@ func (s *Server) RemoveConn(connection *Connection) {
})
}
+func gen502Page(req *http.Request) *http.Response {
+ template := `
no backends connectedno backends connected
Please ensure a backend is running for ${HOST}. This is request ID ${REQ_ID}.
`
+
+ resbody := []byte(os.Expand(template, func(in string) string {
+ switch in {
+ case "HOST":
+ return req.Host
+ case "REQ_ID":
+ return req.Header.Get("X-Request-Id")
+ }
+
+ return ""
+ }))
+ reshdr := req.Header
+ reshdr.Set("Content-Type", "text/html; charset=utf-8")
+
+ resp := &http.Response{
+ Status: fmt.Sprintf("%d Bad Gateway", http.StatusBadGateway),
+ StatusCode: http.StatusBadGateway,
+ Body: ioutil.NopCloser(bytes.NewBuffer(resbody)),
+
+ Proto: req.Proto,
+ ProtoMajor: req.ProtoMajor,
+ ProtoMinor: req.ProtoMinor,
+ Header: reshdr,
+ ContentLength: int64(len(resbody)),
+ Close: true,
+ Request: req,
+ }
+
+ return resp
+}
+
// RoundTrip sends a HTTP request to a backend and then returns its response.
func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
var conns []*Connection
@@ -448,36 +481,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
"uri": req.RequestURI,
})
- template := `no backends connectedno backends connected
Please ensure a backend is running for ${HOST}. This is request ID ${REQ_ID}.
`
-
- resbody := []byte(os.Expand(template, func(in string) string {
- switch in {
- case "HOST":
- return req.Host
- case "REQ_ID":
- return req.Header.Get("X-Request-Id")
- }
-
- return ""
- }))
- reshdr := req.Header
- reshdr.Set("Content-Type", "text/html; charset=utf-8")
-
- resp := &http.Response{
- Status: fmt.Sprintf("%d Bad Gateway", http.StatusBadGateway),
- StatusCode: http.StatusBadGateway,
- Body: ioutil.NopCloser(bytes.NewBuffer(resbody)),
-
- Proto: req.Proto,
- ProtoMajor: req.ProtoMajor,
- ProtoMinor: req.ProtoMinor,
- Header: reshdr,
- ContentLength: int64(len(resbody)),
- Close: true,
- Request: req,
- }
-
- return resp, nil
+ return gen502Page(req), nil
}
}
@@ -496,7 +500,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
"uri": req.RequestURI,
})
- return nil, ErrNoSuchBackend
+ return gen502Page(req), nil
}
c := goodConns[rand.Intn(len(goodConns))]