mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 14:34:02 +03:00
Merge pull request #416 from dolonet/fix/domain-fronting-idle-timeout
fix: apply idle timeout to domain fronting relay
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/v2/antireplay"
|
"github.com/9seconds/mtg/v2/antireplay"
|
||||||
"github.com/9seconds/mtg/v2/events"
|
"github.com/9seconds/mtg/v2/events"
|
||||||
@@ -262,6 +263,7 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen
|
|||||||
|
|
||||||
AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
|
AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
|
||||||
TolerateTimeSkewness: conf.TolerateTimeSkewness.Value,
|
TolerateTimeSkewness: conf.TolerateTimeSkewness.Value,
|
||||||
|
IdleTimeout: conf.Network.Timeout.Idle.Get(time.Minute),
|
||||||
|
|
||||||
DoppelGangerURLs: doppelGangerURLs,
|
DoppelGangerURLs: doppelGangerURLs,
|
||||||
DoppelGangerPerRaid: conf.Defense.Doppelganger.Repeats.Get(mtglib.DoppelGangerPerRaid),
|
DoppelGangerPerRaid: conf.Defense.Doppelganger.Repeats.Get(mtglib.DoppelGangerPerRaid),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/v2/essentials"
|
"github.com/9seconds/mtg/v2/essentials"
|
||||||
"github.com/pires/go-proxyproto"
|
"github.com/pires/go-proxyproto"
|
||||||
@@ -95,3 +96,21 @@ func newConnProxyProtocol(source, target essentials.Conn) *connProxyProtocol {
|
|||||||
sourceAddr: source.RemoteAddr(),
|
sourceAddr: source.RemoteAddr(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type connIdleTimeout struct {
|
||||||
|
essentials.Conn
|
||||||
|
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c connIdleTimeout) Read(b []byte) (int, error) {
|
||||||
|
c.SetReadDeadline(time.Now().Add(c.timeout)) //nolint: errcheck
|
||||||
|
|
||||||
|
return c.Conn.Read(b) //nolint: wrapcheck
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c connIdleTimeout) Write(b []byte) (int, error) {
|
||||||
|
c.SetWriteDeadline(time.Now().Add(c.timeout)) //nolint: errcheck
|
||||||
|
|
||||||
|
return c.Conn.Write(b) //nolint: wrapcheck
|
||||||
|
}
|
||||||
|
|||||||
+5
-2
@@ -27,6 +27,7 @@ type Proxy struct {
|
|||||||
|
|
||||||
allowFallbackOnUnknownDC bool
|
allowFallbackOnUnknownDC bool
|
||||||
tolerateTimeSkewness time.Duration
|
tolerateTimeSkewness time.Duration
|
||||||
|
idleTimeout time.Duration
|
||||||
domainFrontingPort int
|
domainFrontingPort int
|
||||||
domainFrontingIP string
|
domainFrontingIP string
|
||||||
domainFrontingProxyProtocol bool
|
domainFrontingProxyProtocol bool
|
||||||
@@ -151,6 +152,7 @@ func (p *Proxy) Serve(listener net.Listener) error {
|
|||||||
case errors.Is(err, ants.ErrPoolClosed):
|
case errors.Is(err, ants.ErrPoolClosed):
|
||||||
return nil
|
return nil
|
||||||
case errors.Is(err, ants.ErrPoolOverload):
|
case errors.Is(err, ants.ErrPoolOverload):
|
||||||
|
conn.Close() //nolint: errcheck
|
||||||
logger.Info("connection was concurrency limited")
|
logger.Info("connection was concurrency limited")
|
||||||
p.eventStream.Send(p.ctx, NewEventConcurrencyLimited())
|
p.eventStream.Send(p.ctx, NewEventConcurrencyLimited())
|
||||||
}
|
}
|
||||||
@@ -306,8 +308,8 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
|
|||||||
relay.Relay(
|
relay.Relay(
|
||||||
ctx,
|
ctx,
|
||||||
ctx.logger.Named("domain-fronting"),
|
ctx.logger.Named("domain-fronting"),
|
||||||
frontConn,
|
connIdleTimeout{Conn: frontConn, timeout: p.idleTimeout},
|
||||||
conn,
|
connIdleTimeout{Conn: conn, timeout: p.idleTimeout},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,6 +341,7 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
|
|||||||
domainFrontingPort: opts.getDomainFrontingPort(),
|
domainFrontingPort: opts.getDomainFrontingPort(),
|
||||||
domainFrontingIP: opts.DomainFrontingIP,
|
domainFrontingIP: opts.DomainFrontingIP,
|
||||||
tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
|
tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
|
||||||
|
idleTimeout: opts.getIdleTimeout(),
|
||||||
allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
|
allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
|
||||||
telegram: tg,
|
telegram: tg,
|
||||||
doppelGanger: doppel.NewGanger(
|
doppelGanger: doppel.NewGanger(
|
||||||
|
|||||||
@@ -216,6 +216,14 @@ func (p ProxyOpts) getPreferIP() string {
|
|||||||
return p.PreferIP
|
return p.PreferIP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getIdleTimeout() time.Duration {
|
||||||
|
if p.IdleTimeout == 0 {
|
||||||
|
return time.Minute
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.IdleTimeout
|
||||||
|
}
|
||||||
|
|
||||||
func (p ProxyOpts) getLogger(name string) Logger {
|
func (p ProxyOpts) getLogger(name string) Logger {
|
||||||
return p.Logger.Named(name)
|
return p.Logger.Named(name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user