diff --git a/elton.go b/elton.go index 613b514..78e826f 100644 --- a/elton.go +++ b/elton.go @@ -38,6 +38,7 @@ import ( "time" "github.com/vicanso/hes" + intranetip "github.com/vicanso/intranet-ip" ) const ( @@ -161,6 +162,11 @@ func NewGroup(path string, handlerList ...Handler) *Group { } } +// IsIntranet check ip is intranet +func IsIntranet(ip string) bool { + return intranetip.Is(net.ParseIP(ip)) +} + // SetFunctionName set function name func (e *Elton) SetFunctionName(fn interface{}, name string) { p := reflect.ValueOf(fn).Pointer() diff --git a/elton_test.go b/elton_test.go index 6697c35..e5fa751 100644 --- a/elton_test.go +++ b/elton_test.go @@ -38,6 +38,14 @@ import ( "github.com/vicanso/hes" ) +func TestIntranet(t *testing.T) { + assert := assert.New(t) + + assert.True(IsIntranet("127.0.0.1")) + assert.True(IsIntranet("192.168.1.1")) + assert.False(IsIntranet("1.1.1.1")) +} + func TestSkipper(t *testing.T) { c := &Context{ Committed: true, @@ -296,7 +304,7 @@ func TestHandle(t *testing.T) { resp := httptest.NewRecorder() e.ServeHTTP(resp, req) assert.Equal(http.StatusBadRequest, resp.Code, "default hes error status code should be 400") - assert.Equal("message=abcd", resp.Body.String()) + assert.Equal("statusCode=400, message=abcd", resp.Body.String()) }) t.Run("get routers", func(t *testing.T) { diff --git a/go.mod b/go.mod index 99fd392..63bd26e 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,9 @@ go 1.12 require ( github.com/stretchr/testify v1.7.0 - github.com/tidwall/gjson v1.6.7 + github.com/tidwall/gjson v1.6.8 github.com/vicanso/fresh v1.0.0 - github.com/vicanso/hes v0.3.0 + github.com/vicanso/hes v0.3.5 github.com/vicanso/intranet-ip v0.0.1 github.com/vicanso/keygrip v1.1.0 ) diff --git a/go.sum b/go.sum index 943f8cd..74746c6 100644 --- a/go.sum +++ b/go.sum @@ -3,21 +3,20 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tidwall/gjson v1.6.7 h1:Mb1M9HZCRWEcXQ8ieJo7auYyyiSux6w9XN3AdTpxJrE= -github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= +github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w= +github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/vicanso/fresh v1.0.0 h1:u3ykbW6SYW5CbI6rx1lZCVfWKVIptvL6/2KOnrTKsTY= github.com/vicanso/fresh v1.0.0/go.mod h1:gr1RKSFxQ1OnQHzUMBHCigifni7KrXveJjWCTlPjICA= -github.com/vicanso/hes v0.3.0 h1:atgM8+nBBdti7Ru0R84I7yh4fY4Yy9Ykq0K5EhMk77c= -github.com/vicanso/hes v0.3.0/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo= +github.com/vicanso/hes v0.3.5 h1:QPJRP4ks7W7gpIgzOzb6aaUafY8qWjmlcFdgOiqLflY= +github.com/vicanso/hes v0.3.5/go.mod h1:B0l1NIQM/nYw7owAd+hyHuNnAD8Nsx0T6duhVxmXUBY= github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0= github.com/vicanso/intranet-ip v0.0.1/go.mod h1:bqQ6VUhxdz0ipSb1kzd6aoZStlp+pB7CTlVmVhgLAxA= github.com/vicanso/keygrip v1.1.0 h1:B+qtNe/vvgZbYlkbJC4cOsbY9MzNp23+m2+ZybRy45Q= diff --git a/middleware/error_test.go b/middleware/error_test.go index bdedcc6..5c9255c 100644 --- a/middleware/error_test.go +++ b/middleware/error_test.go @@ -107,7 +107,7 @@ func TestErrorHandler(t *testing.T) { return c }, fn: defaultErrorHandler, - result: bytes.NewBufferString(`category=elton-error, message=abcd`), + result: bytes.NewBufferString(`statusCode=500, category=elton-error, message=abcd`), cacheControl: "public, max-age=300", contentType: "text/plain; charset=utf-8", }, diff --git a/middleware/proxy_test.go b/middleware/proxy_test.go index d367a74..9175396 100644 --- a/middleware/proxy_test.go +++ b/middleware/proxy_test.go @@ -171,9 +171,10 @@ func TestProxy(t *testing.T) { Transport: &http.Transport{}, }), err: &hes.Error{ - Category: ErrProxyCategory, - Message: "dial tcp 127.0.0.1:443: connect: connection refused", - Exception: true, + Category: ErrProxyCategory, + StatusCode: 400, + Message: "dial tcp 127.0.0.1:443: connect: connection refused", + Exception: true, }, target: "https://127.0.0.1", }, diff --git a/middleware/recover_test.go b/middleware/recover_test.go index 38d200a..1da5d25 100644 --- a/middleware/recover_test.go +++ b/middleware/recover_test.go @@ -60,7 +60,7 @@ func TestRecoverResponseText(t *testing.T) { e.ServeHTTP(resp, req) assert.Equal(http.StatusInternalServerError, resp.Code) - assert.Equal("category=elton-recover, message=abc", resp.Body.String()) + assert.Equal("statusCode=500, category=elton-recover, message=abc", resp.Body.String()) assert.True(ctx.Committed) assert.True(catchError) for _, key := range keys { diff --git a/middleware/responder_test.go b/middleware/responder_test.go index 6fb20df..a08c53e 100644 --- a/middleware/responder_test.go +++ b/middleware/responder_test.go @@ -105,9 +105,10 @@ func TestResponder(t *testing.T) { return c }, err: &hes.Error{ - Message: "json: unsupported type: func()", - Category: ErrResponderCategory, - Exception: true, + Message: "json: unsupported type: func()", + StatusCode: 500, + Category: ErrResponderCategory, + Exception: true, }, }, // response string diff --git a/middleware/static_serve_test.go b/middleware/static_serve_test.go index 25f0a44..8684a38 100644 --- a/middleware/static_serve_test.go +++ b/middleware/static_serve_test.go @@ -205,7 +205,7 @@ func TestStaticServe(t *testing.T) { } return c }, - err: errors.New("category=elton-static-serve, message=abcd"), + err: errors.New("statusCode=500, category=elton-static-serve, message=abcd"), }, // image {