add traffic limiter for proxy handler
This commit is contained in:
@ -8,11 +8,13 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/limiter/traffic"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/relay"
|
||||
ctxvalue "github.com/go-gost/x/internal/ctx"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
sx "github.com/go-gost/x/internal/util/selector"
|
||||
serial "github.com/go-gost/x/internal/util/serial"
|
||||
"github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) (err error) {
|
||||
@ -51,7 +53,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
|
||||
switch h.md.hash {
|
||||
case "host":
|
||||
ctx = sx.ContextWithHash(ctx, &sx.Hash{Source: address})
|
||||
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: address})
|
||||
}
|
||||
|
||||
var cc io.ReadWriteCloser
|
||||
@ -103,9 +105,16 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
}
|
||||
}
|
||||
|
||||
rw := wrapper.WrapReadWriter(h.options.Limiter, conn, conn.RemoteAddr().String(),
|
||||
traffic.NetworkOption(network),
|
||||
traffic.AddrOption(address),
|
||||
traffic.ClientOption(string(ctxvalue.ClientIDFromContext(ctx))),
|
||||
traffic.SrcOption(conn.RemoteAddr().String()),
|
||||
)
|
||||
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), address)
|
||||
xnet.Transport(conn, cc)
|
||||
xnet.Transport(rw, cc)
|
||||
log.WithFields(map[string]any{
|
||||
"duration": time.Since(t),
|
||||
}).Infof("%s >-< %s", conn.RemoteAddr(), address)
|
||||
|
@ -7,9 +7,12 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/limiter/traffic"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/relay"
|
||||
ctxvalue "github.com/go-gost/x/internal/ctx"
|
||||
netpkg "github.com/go-gost/x/internal/net"
|
||||
"github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
)
|
||||
|
||||
func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network string, log logger.Logger) error {
|
||||
@ -84,9 +87,16 @@ func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network
|
||||
conn = rc
|
||||
}
|
||||
|
||||
rw := wrapper.WrapReadWriter(h.options.Limiter, conn, conn.RemoteAddr().String(),
|
||||
traffic.NetworkOption(network),
|
||||
traffic.AddrOption(target.Addr),
|
||||
traffic.ClientOption(string(ctxvalue.ClientIDFromContext(ctx))),
|
||||
traffic.SrcOption(conn.RemoteAddr().String()),
|
||||
)
|
||||
|
||||
t := time.Now()
|
||||
log.Debugf("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
||||
netpkg.Transport(conn, cc)
|
||||
netpkg.Transport(rw, cc)
|
||||
log.WithFields(map[string]any{
|
||||
"duration": time.Since(t),
|
||||
}).Debugf("%s >-< %s", conn.RemoteAddr(), target.Addr)
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
md "github.com/go-gost/core/metadata"
|
||||
"github.com/go-gost/core/service"
|
||||
"github.com/go-gost/relay"
|
||||
auth_util "github.com/go-gost/x/internal/util/auth"
|
||||
ctxvalue "github.com/go-gost/x/internal/ctx"
|
||||
"github.com/go-gost/x/registry"
|
||||
)
|
||||
|
||||
@ -83,8 +83,6 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}()
|
||||
|
||||
ctx = auth_util.ContextWithClientAddr(ctx, auth_util.ClientAddr(conn.RemoteAddr().String()))
|
||||
|
||||
if !h.checkRateLimit(conn.RemoteAddr()) {
|
||||
return ErrRateLimit
|
||||
}
|
||||
@ -136,13 +134,13 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
}
|
||||
|
||||
if h.options.Auther != nil {
|
||||
id, ok := h.options.Auther.Authenticate(ctx, user, pass)
|
||||
clientID, ok := h.options.Auther.Authenticate(ctx, user, pass)
|
||||
if !ok {
|
||||
resp.Status = relay.StatusUnauthorized
|
||||
resp.WriteTo(conn)
|
||||
return ErrUnauthorized
|
||||
}
|
||||
ctx = auth_util.ContextWithID(ctx, auth_util.ID(id))
|
||||
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
|
||||
}
|
||||
|
||||
network := networkID.String()
|
||||
|
Reference in New Issue
Block a user