Adding upstream version 2.52.6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
a960158181
commit
6d002e9543
441 changed files with 95392 additions and 0 deletions
88
middleware/proxy/config.go
Normal file
88
middleware/proxy/config.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// Config defines the config for middleware.
|
||||
type Config struct {
|
||||
// Next defines a function to skip this middleware when returned true.
|
||||
//
|
||||
// Optional. Default: nil
|
||||
Next func(c *fiber.Ctx) bool
|
||||
|
||||
// Servers defines a list of <scheme>://<host> HTTP servers,
|
||||
//
|
||||
// which are used in a round-robin manner.
|
||||
// i.e.: "https://foobar.com, http://www.foobar.com"
|
||||
//
|
||||
// Required
|
||||
Servers []string
|
||||
|
||||
// ModifyRequest allows you to alter the request
|
||||
//
|
||||
// Optional. Default: nil
|
||||
ModifyRequest fiber.Handler
|
||||
|
||||
// ModifyResponse allows you to alter the response
|
||||
//
|
||||
// Optional. Default: nil
|
||||
ModifyResponse fiber.Handler
|
||||
|
||||
// Timeout is the request timeout used when calling the proxy client
|
||||
//
|
||||
// Optional. Default: 1 second
|
||||
Timeout time.Duration
|
||||
|
||||
// Per-connection buffer size for requests' reading.
|
||||
// This also limits the maximum header size.
|
||||
// Increase this buffer if your clients send multi-KB RequestURIs
|
||||
// and/or multi-KB headers (for example, BIG cookies).
|
||||
ReadBufferSize int
|
||||
|
||||
// Per-connection buffer size for responses' writing.
|
||||
WriteBufferSize int
|
||||
|
||||
// tls config for the http client.
|
||||
TlsConfig *tls.Config //nolint:stylecheck,revive // TODO: Rename to "TLSConfig" in v3
|
||||
|
||||
// Client is custom client when client config is complex.
|
||||
// Note that Servers, Timeout, WriteBufferSize, ReadBufferSize and TlsConfig
|
||||
// will not be used if the client are set.
|
||||
Client *fasthttp.LBClient
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
ModifyRequest: nil,
|
||||
ModifyResponse: nil,
|
||||
Timeout: fasthttp.DefaultLBClientTimeout,
|
||||
}
|
||||
|
||||
// configDefault function to set default values
|
||||
func configDefault(config ...Config) Config {
|
||||
// Return default config if nothing provided
|
||||
if len(config) < 1 {
|
||||
return ConfigDefault
|
||||
}
|
||||
|
||||
// Override default config
|
||||
cfg := config[0]
|
||||
|
||||
// Set default values
|
||||
if cfg.Timeout <= 0 {
|
||||
cfg.Timeout = ConfigDefault.Timeout
|
||||
}
|
||||
|
||||
// Set default values
|
||||
if len(cfg.Servers) == 0 && cfg.Client == nil {
|
||||
panic("Servers cannot be empty")
|
||||
}
|
||||
return cfg
|
||||
}
|
267
middleware/proxy/proxy.go
Normal file
267
middleware/proxy/proxy.go
Normal file
|
@ -0,0 +1,267 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// New is deprecated
|
||||
func New(config Config) fiber.Handler {
|
||||
log.Warn("[PROXY] proxy.New is deprecated, please use proxy.Balancer instead")
|
||||
return Balancer(config)
|
||||
}
|
||||
|
||||
// Balancer creates a load balancer among multiple upstream servers
|
||||
func Balancer(config Config) fiber.Handler {
|
||||
// Set default config
|
||||
cfg := configDefault(config)
|
||||
|
||||
// Load balanced client
|
||||
lbc := &fasthttp.LBClient{}
|
||||
// Note that Servers, Timeout, WriteBufferSize, ReadBufferSize and TlsConfig
|
||||
// will not be used if the client are set.
|
||||
if config.Client == nil {
|
||||
// Set timeout
|
||||
lbc.Timeout = cfg.Timeout
|
||||
// Scheme must be provided, falls back to http
|
||||
for _, server := range cfg.Servers {
|
||||
if !strings.HasPrefix(server, "http") {
|
||||
server = "http://" + server
|
||||
}
|
||||
|
||||
u, err := url.Parse(server)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
client := &fasthttp.HostClient{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
Addr: u.Host,
|
||||
|
||||
ReadBufferSize: config.ReadBufferSize,
|
||||
WriteBufferSize: config.WriteBufferSize,
|
||||
|
||||
TLSConfig: config.TlsConfig,
|
||||
}
|
||||
|
||||
lbc.Clients = append(lbc.Clients, client)
|
||||
}
|
||||
} else {
|
||||
// Set custom client
|
||||
lbc = config.Client
|
||||
}
|
||||
|
||||
// Return new handler
|
||||
return func(c *fiber.Ctx) error {
|
||||
// Don't execute middleware if Next returns true
|
||||
if cfg.Next != nil && cfg.Next(c) {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// Set request and response
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
|
||||
// Don't proxy "Connection" header
|
||||
req.Header.Del(fiber.HeaderConnection)
|
||||
|
||||
// Modify request
|
||||
if cfg.ModifyRequest != nil {
|
||||
if err := cfg.ModifyRequest(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
req.SetRequestURI(utils.UnsafeString(req.RequestURI()))
|
||||
|
||||
// Forward request
|
||||
if err := lbc.Do(req, res); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Don't proxy "Connection" header
|
||||
res.Header.Del(fiber.HeaderConnection)
|
||||
|
||||
// Modify response
|
||||
if cfg.ModifyResponse != nil {
|
||||
if err := cfg.ModifyResponse(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Return nil to end proxying if no error
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var client = &fasthttp.Client{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
}
|
||||
|
||||
var lock sync.RWMutex
|
||||
|
||||
// WithTlsConfig update http client with a user specified tls.config
|
||||
// This function should be called before Do and Forward.
|
||||
// Deprecated: use WithClient instead.
|
||||
//
|
||||
//nolint:stylecheck,revive // TODO: Rename to "WithTLSConfig" in v3
|
||||
func WithTlsConfig(tlsConfig *tls.Config) {
|
||||
client.TLSConfig = tlsConfig
|
||||
}
|
||||
|
||||
// WithClient sets the global proxy client.
|
||||
// This function should be called before Do and Forward.
|
||||
func WithClient(cli *fasthttp.Client) {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
client = cli
|
||||
}
|
||||
|
||||
// Forward performs the given http request and fills the given http response.
|
||||
// This method will return an fiber.Handler
|
||||
func Forward(addr string, clients ...*fasthttp.Client) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
return Do(c, addr, clients...)
|
||||
}
|
||||
}
|
||||
|
||||
// Do performs the given http request and fills the given http response.
|
||||
// This method can be used within a fiber.Handler
|
||||
func Do(c *fiber.Ctx, addr string, clients ...*fasthttp.Client) error {
|
||||
return doAction(c, addr, func(cli *fasthttp.Client, req *fasthttp.Request, resp *fasthttp.Response) error {
|
||||
return cli.Do(req, resp)
|
||||
}, clients...)
|
||||
}
|
||||
|
||||
// DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects.
|
||||
// When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.
|
||||
// This method can be used within a fiber.Handler
|
||||
func DoRedirects(c *fiber.Ctx, addr string, maxRedirectsCount int, clients ...*fasthttp.Client) error {
|
||||
return doAction(c, addr, func(cli *fasthttp.Client, req *fasthttp.Request, resp *fasthttp.Response) error {
|
||||
return cli.DoRedirects(req, resp, maxRedirectsCount)
|
||||
}, clients...)
|
||||
}
|
||||
|
||||
// DoDeadline performs the given request and waits for response until the given deadline.
|
||||
// This method can be used within a fiber.Handler
|
||||
func DoDeadline(c *fiber.Ctx, addr string, deadline time.Time, clients ...*fasthttp.Client) error {
|
||||
return doAction(c, addr, func(cli *fasthttp.Client, req *fasthttp.Request, resp *fasthttp.Response) error {
|
||||
return cli.DoDeadline(req, resp, deadline)
|
||||
}, clients...)
|
||||
}
|
||||
|
||||
// DoTimeout performs the given request and waits for response during the given timeout duration.
|
||||
// This method can be used within a fiber.Handler
|
||||
func DoTimeout(c *fiber.Ctx, addr string, timeout time.Duration, clients ...*fasthttp.Client) error {
|
||||
return doAction(c, addr, func(cli *fasthttp.Client, req *fasthttp.Request, resp *fasthttp.Response) error {
|
||||
return cli.DoTimeout(req, resp, timeout)
|
||||
}, clients...)
|
||||
}
|
||||
|
||||
func doAction(
|
||||
c *fiber.Ctx,
|
||||
addr string,
|
||||
action func(cli *fasthttp.Client, req *fasthttp.Request, resp *fasthttp.Response) error,
|
||||
clients ...*fasthttp.Client,
|
||||
) error {
|
||||
var cli *fasthttp.Client
|
||||
|
||||
// set local or global client
|
||||
if len(clients) != 0 {
|
||||
cli = clients[0]
|
||||
} else {
|
||||
lock.RLock()
|
||||
cli = client
|
||||
lock.RUnlock()
|
||||
}
|
||||
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
originalURL := utils.CopyString(c.OriginalURL())
|
||||
defer req.SetRequestURI(originalURL)
|
||||
|
||||
copiedURL := utils.CopyString(addr)
|
||||
req.SetRequestURI(copiedURL)
|
||||
// NOTE: if req.isTLS is true, SetRequestURI keeps the scheme as https.
|
||||
// Reference: https://github.com/gofiber/fiber/issues/1762
|
||||
if scheme := getScheme(utils.UnsafeBytes(copiedURL)); len(scheme) > 0 {
|
||||
req.URI().SetSchemeBytes(scheme)
|
||||
}
|
||||
|
||||
req.Header.Del(fiber.HeaderConnection)
|
||||
if err := action(cli, req, res); err != nil {
|
||||
return err
|
||||
}
|
||||
res.Header.Del(fiber.HeaderConnection)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getScheme(uri []byte) []byte {
|
||||
i := bytes.IndexByte(uri, '/')
|
||||
if i < 1 || uri[i-1] != ':' || i == len(uri)-1 || uri[i+1] != '/' {
|
||||
return nil
|
||||
}
|
||||
return uri[:i-1]
|
||||
}
|
||||
|
||||
// DomainForward performs an http request based on the given domain and populates the given http response.
|
||||
// This method will return an fiber.Handler
|
||||
func DomainForward(hostname, addr string, clients ...*fasthttp.Client) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
host := string(c.Request().Host())
|
||||
if host == hostname {
|
||||
return Do(c, addr+c.OriginalURL(), clients...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type roundrobin struct {
|
||||
sync.Mutex
|
||||
|
||||
current int
|
||||
pool []string
|
||||
}
|
||||
|
||||
// this method will return a string of addr server from list server.
|
||||
func (r *roundrobin) get() string {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
if r.current >= len(r.pool) {
|
||||
r.current %= len(r.pool)
|
||||
}
|
||||
|
||||
result := r.pool[r.current]
|
||||
r.current++
|
||||
return result
|
||||
}
|
||||
|
||||
// BalancerForward Forward performs the given http request with round robin algorithm to server and fills the given http response.
|
||||
// This method will return an fiber.Handler
|
||||
func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handler {
|
||||
r := &roundrobin{
|
||||
current: 0,
|
||||
pool: servers,
|
||||
}
|
||||
return func(c *fiber.Ctx) error {
|
||||
server := r.get()
|
||||
if !strings.HasPrefix(server, "http") {
|
||||
server = "http://" + server
|
||||
}
|
||||
c.Request().Header.Add("X-Real-IP", c.IP())
|
||||
return Do(c, server+c.OriginalURL(), clients...)
|
||||
}
|
||||
}
|
689
middleware/proxy/proxy_test.go
Normal file
689
middleware/proxy/proxy_test.go
Normal file
|
@ -0,0 +1,689 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/internal/tlstest"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func createProxyTestServer(t *testing.T, handler fiber.Handler) (*fiber.App, string) {
|
||||
t.Helper()
|
||||
|
||||
target := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
target.Get("/", handler)
|
||||
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
go func() {
|
||||
utils.AssertEqual(t, nil, target.Listener(ln))
|
||||
}()
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
addr := ln.Addr().String()
|
||||
|
||||
return target, addr
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Empty_Host
|
||||
func Test_Proxy_Empty_Upstream_Servers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
utils.AssertEqual(t, "Servers cannot be empty", r)
|
||||
}
|
||||
}()
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{Servers: []string{}}))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Empty_Config
|
||||
func Test_Proxy_Empty_Config(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
utils.AssertEqual(t, "Servers cannot be empty", r)
|
||||
}
|
||||
}()
|
||||
app := fiber.New()
|
||||
app.Use(New(Config{}))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Next
|
||||
func Test_Proxy_Next(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{"127.0.0.1"},
|
||||
Next: func(_ *fiber.Ctx) bool {
|
||||
return true
|
||||
},
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy
|
||||
func Test_Proxy(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
target, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusTeapot)
|
||||
})
|
||||
|
||||
resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
app.Use(Balancer(Config{Servers: []string{addr}}))
|
||||
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
|
||||
req.Host = addr
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Balancer_WithTlsConfig
|
||||
func Test_Proxy_Balancer_WithTlsConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
serverTLSConf, _, err := tlstest.GetTLSConfigs()
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, serverTLSConf)
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/tlsbalaner", func(c *fiber.Ctx) error {
|
||||
return c.SendString("tls balancer")
|
||||
})
|
||||
|
||||
addr := ln.Addr().String()
|
||||
clientTLSConf := &tls.Config{InsecureSkipVerify: true} //nolint:gosec // We're in a test func, so this is fine
|
||||
|
||||
// disable certificate verification in Balancer
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
TlsConfig: clientTLSConf,
|
||||
}))
|
||||
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()
|
||||
|
||||
code, body, errs := fiber.Get("https://" + addr + "/tlsbalaner").TLSConfig(clientTLSConf).String()
|
||||
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "tls balancer", body)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Forward_WithTlsConfig_To_Http
|
||||
func Test_Proxy_Forward_WithTlsConfig_To_Http(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, targetAddr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("hello from target")
|
||||
})
|
||||
|
||||
proxyServerTLSConf, _, err := tlstest.GetTLSConfigs()
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
proxyServerLn, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
proxyServerLn = tls.NewListener(proxyServerLn, proxyServerTLSConf)
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
proxyAddr := proxyServerLn.Addr().String()
|
||||
|
||||
app.Use(Forward("http://" + targetAddr))
|
||||
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(proxyServerLn)) }()
|
||||
|
||||
code, body, errs := fiber.Get("https://" + proxyAddr).
|
||||
InsecureSkipVerify().
|
||||
Timeout(5 * time.Second).
|
||||
String()
|
||||
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "hello from target", body)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Forward
|
||||
func Test_Proxy_Forward(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("forwarded")
|
||||
})
|
||||
|
||||
app.Use(Forward("http://" + addr))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "forwarded", string(b))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Forward_WithTlsConfig
|
||||
func Test_Proxy_Forward_WithTlsConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
serverTLSConf, _, err := tlstest.GetTLSConfigs()
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, serverTLSConf)
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
app.Get("/tlsfwd", func(c *fiber.Ctx) error {
|
||||
return c.SendString("tls forward")
|
||||
})
|
||||
|
||||
addr := ln.Addr().String()
|
||||
clientTLSConf := &tls.Config{InsecureSkipVerify: true} //nolint:gosec // We're in a test func, so this is fine
|
||||
|
||||
// disable certificate verification
|
||||
WithTlsConfig(clientTLSConf)
|
||||
app.Use(Forward("https://" + addr + "/tlsfwd"))
|
||||
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()
|
||||
|
||||
code, body, errs := fiber.Get("https://" + addr).TLSConfig(clientTLSConf).String()
|
||||
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "tls forward", body)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Modify_Response
|
||||
func Test_Proxy_Modify_Response(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.Status(500).SendString("not modified")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
ModifyResponse: func(c *fiber.Ctx) error {
|
||||
c.Response().SetStatusCode(fiber.StatusOK)
|
||||
return c.SendString("modified response")
|
||||
},
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "modified response", string(b))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Modify_Request
|
||||
func Test_Proxy_Modify_Request(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
b := c.Request().Body()
|
||||
return c.SendString(string(b))
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
ModifyRequest: func(c *fiber.Ctx) error {
|
||||
c.Request().SetBody([]byte("modified request"))
|
||||
return nil
|
||||
},
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "modified request", string(b))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Timeout_Slow_Server
|
||||
func Test_Proxy_Timeout_Slow_Server(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
time.Sleep(2 * time.Second)
|
||||
return c.SendString("fiber is awesome")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
Timeout: 3 * time.Second,
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 5000)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "fiber is awesome", string(b))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_With_Timeout
|
||||
func Test_Proxy_With_Timeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
time.Sleep(1 * time.Second)
|
||||
return c.SendString("fiber is awesome")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
Timeout: 100 * time.Millisecond,
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "timeout", string(b))
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Buffer_Size_Response
|
||||
func Test_Proxy_Buffer_Size_Response(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
long := strings.Join(make([]string, 5000), "-")
|
||||
c.Set("Very-Long-Header", long)
|
||||
return c.SendString("ok")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Use(Balancer(Config{Servers: []string{addr}}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
|
||||
|
||||
app = fiber.New()
|
||||
app.Use(Balancer(Config{
|
||||
Servers: []string{addr},
|
||||
ReadBufferSize: 1024 * 8,
|
||||
}))
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Do_RestoreOriginalURL
|
||||
func Test_Proxy_Do_RestoreOriginalURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("proxied")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return Do(c, "http://"+addr)
|
||||
})
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "proxied", string(body))
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Do_WithRealURL
|
||||
func Test_Proxy_Do_WithRealURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return Do(c, "https://www.google.com")
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, strings.Contains(string(body), "https://www.google.com/"))
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Do_WithRedirect
|
||||
func Test_Proxy_Do_WithRedirect(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return Do(c, "https://google.com")
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, strings.Contains(string(body), "https://www.google.com/"))
|
||||
utils.AssertEqual(t, 301, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoRedirects_RestoreOriginalURL
|
||||
func Test_Proxy_DoRedirects_RestoreOriginalURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoRedirects(c, "http://google.com", 1)
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
_, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoRedirects_TooManyRedirects
|
||||
func Test_Proxy_DoRedirects_TooManyRedirects(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoRedirects(c, "http://google.com", 0)
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "too many redirects detected when doing the request", string(body))
|
||||
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoTimeout_RestoreOriginalURL
|
||||
func Test_Proxy_DoTimeout_RestoreOriginalURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("proxied")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoTimeout(c, "http://"+addr, time.Second)
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "proxied", string(body))
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoTimeout_Timeout
|
||||
func Test_Proxy_DoTimeout_Timeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
time.Sleep(time.Second * 5)
|
||||
return c.SendString("proxied")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoTimeout(c, "http://"+addr, time.Second)
|
||||
})
|
||||
|
||||
_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, errors.New("test: timeout error 1000ms"), err1)
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoDeadline_RestoreOriginalURL
|
||||
func Test_Proxy_DoDeadline_RestoreOriginalURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("proxied")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoDeadline(c, "http://"+addr, time.Now().Add(time.Second))
|
||||
})
|
||||
|
||||
resp, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, nil, err1)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "proxied", string(body))
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "/test", resp.Request.URL.String())
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_DoDeadline_PastDeadline
|
||||
func Test_Proxy_DoDeadline_PastDeadline(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
time.Sleep(time.Second * 5)
|
||||
return c.SendString("proxied")
|
||||
})
|
||||
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return DoDeadline(c, "http://"+addr, time.Now().Add(time.Second))
|
||||
})
|
||||
|
||||
_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil))
|
||||
utils.AssertEqual(t, errors.New("test: timeout error 1000ms"), err1)
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Do_HTTP_Prefix_URL
|
||||
func Test_Proxy_Do_HTTP_Prefix_URL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("hello world")
|
||||
})
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
app.Get("/*", func(c *fiber.Ctx) error {
|
||||
path := c.OriginalURL()
|
||||
url := strings.TrimPrefix(path, "/")
|
||||
|
||||
utils.AssertEqual(t, "http://"+addr, url)
|
||||
if err := Do(c, url); err != nil {
|
||||
return err
|
||||
}
|
||||
c.Response().Header.Del(fiber.HeaderServer)
|
||||
return nil
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/http://"+addr, nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
s, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "hello world", string(s))
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Forward_Global_Client
|
||||
func Test_Proxy_Forward_Global_Client(t *testing.T) {
|
||||
t.Parallel()
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
WithClient(&fasthttp.Client{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
})
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
app.Get("/test_global_client", func(c *fiber.Ctx) error {
|
||||
return c.SendString("test_global_client")
|
||||
})
|
||||
|
||||
addr := ln.Addr().String()
|
||||
app.Use(Forward("http://" + addr + "/test_global_client"))
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()
|
||||
|
||||
code, body, errs := fiber.Get("http://" + addr).String()
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "test_global_client", body)
|
||||
}
|
||||
|
||||
// go test -race -run Test_Proxy_Forward_Local_Client
|
||||
func Test_Proxy_Forward_Local_Client(t *testing.T) {
|
||||
t.Parallel()
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
app.Get("/test_local_client", func(c *fiber.Ctx) error {
|
||||
return c.SendString("test_local_client")
|
||||
})
|
||||
|
||||
addr := ln.Addr().String()
|
||||
app.Use(Forward("http://"+addr+"/test_local_client", &fasthttp.Client{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
|
||||
Dial: fasthttp.Dial,
|
||||
}))
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()
|
||||
|
||||
code, body, errs := fiber.Get("http://" + addr).String()
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "test_local_client", body)
|
||||
}
|
||||
|
||||
// go test -run Test_ProxyBalancer_Custom_Client
|
||||
func Test_ProxyBalancer_Custom_Client(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
target, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusTeapot)
|
||||
})
|
||||
|
||||
resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
||||
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
app.Use(Balancer(Config{Client: &fasthttp.LBClient{
|
||||
Clients: []fasthttp.BalancingClient{
|
||||
&fasthttp.HostClient{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
Addr: addr,
|
||||
},
|
||||
},
|
||||
Timeout: time.Second,
|
||||
}}))
|
||||
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
|
||||
req.Host = addr
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusTeapot, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Domain_Forward_Local
|
||||
func Test_Proxy_Domain_Forward_Local(t *testing.T) {
|
||||
t.Parallel()
|
||||
ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
app := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
// target server
|
||||
ln1, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
app1 := fiber.New(fiber.Config{DisableStartupMessage: true})
|
||||
|
||||
app1.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.SendString("test_local_client:" + c.Query("query_test"))
|
||||
})
|
||||
|
||||
proxyAddr := ln.Addr().String()
|
||||
targetAddr := ln1.Addr().String()
|
||||
localDomain := strings.Replace(proxyAddr, "127.0.0.1", "localhost", 1)
|
||||
app.Use(DomainForward(localDomain, "http://"+targetAddr, &fasthttp.Client{
|
||||
NoDefaultUserAgentHeader: true,
|
||||
DisablePathNormalizing: true,
|
||||
|
||||
Dial: fasthttp.Dial,
|
||||
}))
|
||||
|
||||
go func() { utils.AssertEqual(t, nil, app.Listener(ln)) }()
|
||||
go func() { utils.AssertEqual(t, nil, app1.Listener(ln1)) }()
|
||||
|
||||
code, body, errs := fiber.Get("http://" + localDomain + "/test?query_test=true").String()
|
||||
utils.AssertEqual(t, 0, len(errs))
|
||||
utils.AssertEqual(t, fiber.StatusOK, code)
|
||||
utils.AssertEqual(t, "test_local_client:true", body)
|
||||
}
|
||||
|
||||
// go test -run Test_Proxy_Balancer_Forward_Local
|
||||
func Test_Proxy_Balancer_Forward_Local(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
_, addr := createProxyTestServer(t, func(c *fiber.Ctx) error {
|
||||
return c.SendString("forwarded")
|
||||
})
|
||||
|
||||
app.Use(BalancerForward([]string{addr}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
utils.AssertEqual(t, string(b), "forwarded")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue