mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-05 01:20:26 +08:00
请求源站错误时增加503、504错误
This commit is contained in:
@@ -121,7 +121,7 @@ func (this *HTTPRequest) Do() {
|
|||||||
// Web配置
|
// Web配置
|
||||||
err := this.configureWeb(this.Server.Web, true, 0)
|
err := this.configureWeb(this.Server.Web, true, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
this.doEnd()
|
this.doEnd()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (this *HTTPRequest) doAuth() (shouldStop bool) {
|
|||||||
return writer.StatusCode(), nil
|
return writer.StatusCode(), nil
|
||||||
}, this.Format)
|
}, this.Format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.write502(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if b {
|
if b {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package nodes
|
package nodes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,30 +18,15 @@ func (this *HTTPRequest) write404() {
|
|||||||
_, _ = this.writer.Write([]byte(msg))
|
_, _ = this.writer.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HTTPRequest) write500(err error) {
|
func (this *HTTPRequest) write50x(err error, statusCode int) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.addError(err)
|
this.addError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
statusCode := http.StatusInternalServerError
|
|
||||||
if this.doPage(statusCode) {
|
if this.doPage(statusCode) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.processResponseHeaders(statusCode)
|
this.processResponseHeaders(statusCode)
|
||||||
this.writer.WriteHeader(statusCode)
|
this.writer.WriteHeader(statusCode)
|
||||||
_, _ = this.writer.Write([]byte(http.StatusText(statusCode)))
|
_, _ = this.writer.Write([]byte(types.String(statusCode) + " " + http.StatusText(statusCode)))
|
||||||
}
|
|
||||||
|
|
||||||
func (this *HTTPRequest) write502(err error) {
|
|
||||||
if err != nil {
|
|
||||||
this.addError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
statusCode := http.StatusBadGateway
|
|
||||||
if this.doPage(statusCode) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
this.processResponseHeaders(statusCode)
|
|
||||||
this.writer.WriteHeader(statusCode)
|
|
||||||
_, _ = this.writer.Write([]byte("502 Bad Gateway"))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/iwind/gofcgi/pkg/fcgi"
|
"github.com/iwind/gofcgi/pkg/fcgi"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -80,7 +81,7 @@ func (this *HTTPRequest) doFastcgi() (shouldStop bool) {
|
|||||||
|
|
||||||
client, err := fcgi.SharedPool(fastcgi.Network(), fastcgi.RealAddress(), uint(poolSize)).Client()
|
client, err := fcgi.SharedPool(fastcgi.Network(), fastcgi.RealAddress(), uint(poolSize)).Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,13 +159,13 @@ func (this *HTTPRequest) doFastcgi() (shouldStop bool) {
|
|||||||
|
|
||||||
resp, stderr, err := client.Call(fcgiReq)
|
resp, stderr, err := client.Call(fcgiReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(stderr) > 0 {
|
if len(stderr) > 0 {
|
||||||
err := errors.New("Fastcgi Error: " + strings.TrimSpace(string(stderr)) + " script: " + maps.NewMap(params).GetString("SCRIPT_FILENAME"))
|
err := errors.New("Fastcgi Error: " + strings.TrimSpace(string(stderr)) + " script: " + maps.NewMap(params).GetString("SCRIPT_FILENAME"))
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -36,7 +37,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
if origin == nil {
|
if origin == nil {
|
||||||
err := errors.New(this.requestPath() + ": no available backends for reverse proxy")
|
err := errors.New(this.requestPath() + ": no available backends for reverse proxy")
|
||||||
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
||||||
this.write502(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.origin = origin // 设置全局变量是为了日志等处理
|
this.origin = origin // 设置全局变量是为了日志等处理
|
||||||
@@ -56,7 +57,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
if origin.Addr == nil {
|
if origin.Addr == nil {
|
||||||
err := errors.New(this.requestPath() + ": origin '" + strconv.FormatInt(origin.Id, 10) + "' does not has a address")
|
err := errors.New(this.requestPath() + ": origin '" + strconv.FormatInt(origin.Id, 10) + "' does not has a address")
|
||||||
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
||||||
this.write502(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.RawReq.URL.Scheme = origin.Addr.Protocol.Primary().Scheme()
|
this.RawReq.URL.Scheme = origin.Addr.Protocol.Primary().Scheme()
|
||||||
@@ -143,7 +144,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
client, err := SharedHTTPClientPool.Client(this.RawReq, origin, originAddr)
|
client, err := SharedHTTPClientPool.Client(this.RawReq, origin, originAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
|
||||||
this.write502(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,13 +159,23 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// 客户端取消请求,则不提示
|
// 客户端取消请求,则不提示
|
||||||
httpErr, ok := err.(*url.Error)
|
httpErr, ok := err.(*url.Error)
|
||||||
if !ok || httpErr.Err != context.Canceled {
|
if !ok {
|
||||||
// TODO 如果超过最大失败次数,则下线
|
|
||||||
SharedOriginStateManager.Fail(origin, this.reverseProxy, func() {
|
SharedOriginStateManager.Fail(origin, this.reverseProxy, func() {
|
||||||
this.reverseProxy.ResetScheduling()
|
this.reverseProxy.ResetScheduling()
|
||||||
})
|
})
|
||||||
|
this.write50x(err, http.StatusBadGateway)
|
||||||
this.write502(err)
|
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': "+err.Error())
|
||||||
|
} else if httpErr.Err != context.Canceled {
|
||||||
|
SharedOriginStateManager.Fail(origin, this.reverseProxy, func() {
|
||||||
|
this.reverseProxy.ResetScheduling()
|
||||||
|
})
|
||||||
|
if httpErr.Timeout() {
|
||||||
|
this.write50x(err, http.StatusGatewayTimeout)
|
||||||
|
} else if httpErr.Temporary() {
|
||||||
|
this.write50x(err, http.StatusServiceUnavailable)
|
||||||
|
} else {
|
||||||
|
this.write50x(err, http.StatusBadGateway)
|
||||||
|
}
|
||||||
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': "+err.Error())
|
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': "+err.Error())
|
||||||
} else {
|
} else {
|
||||||
// 是否为客户端方面的错误
|
// 是否为客户端方面的错误
|
||||||
@@ -178,7 +189,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !isClientError {
|
if !isClientError {
|
||||||
this.write502(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if resp != nil && resp.Body != nil {
|
if resp != nil && resp.Body != nil {
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func (this *HTTPRequest) doRoot() (isBreak bool) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
logs.Error(err)
|
logs.Error(err)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ func (this *HTTPRequest) doRoot() (isBreak bool) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
logs.Error(err)
|
logs.Error(err)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -283,8 +283,7 @@ func (this *HTTPRequest) doRoot() (isBreak bool) {
|
|||||||
|
|
||||||
reader, err := os.OpenFile(filePath, os.O_RDONLY, 0444)
|
reader, err := os.OpenFile(filePath, os.O_RDONLY, 0444)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
logs.Error(err)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func (this *HTTPRequest) doURL(method string, url string, host string, statusCod
|
|||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(errors.New(req.URL.String() + ": " + err.Error()))
|
logs.Error(errors.New(req.URL.String() + ": " + err.Error()))
|
||||||
this.write500(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package nodes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/iwind/TeaGo/logs"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -43,8 +42,7 @@ func (this *HTTPRequest) doWebsocket() {
|
|||||||
// TODO 增加N次错误重试,重试的时候需要尝试不同的源站
|
// TODO 增加N次错误重试,重试的时候需要尝试不同的源站
|
||||||
originConn, err := OriginConnect(this.origin, this.RawReq.RemoteAddr)
|
originConn, err := OriginConnect(this.origin, this.RawReq.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
this.write500(err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -53,15 +51,13 @@ func (this *HTTPRequest) doWebsocket() {
|
|||||||
|
|
||||||
err = this.RawReq.Write(originConn)
|
err = this.RawReq.Write(originConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
this.write50x(err, http.StatusBadGateway)
|
||||||
this.write500(err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
clientConn, _, err := this.writer.Hijack()
|
clientConn, _, err := this.writer.Hijack()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error(err)
|
this.write50x(err, http.StatusInternalServerError)
|
||||||
this.write500(err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user