Merge pull request #353 from 9seconds/domain-fronting-native

Use native dialer to communicate with fronting domain
This commit is contained in:
Sergei Arkhipov
2026-03-12 21:40:38 +01:00
committed by GitHub
11 changed files with 33 additions and 21 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
conf.Network.Timeout.Idle.Get(0), conf.Network.Timeout.Idle.Get(0),
) )
proxyDialers := make([]network.Network, len(conf.Network.Proxies)) proxyDialers := make([]mtglib.Network, len(conf.Network.Proxies))
for idx, v := range conf.Network.Proxies { for idx, v := range conf.Network.Proxies {
value, err := network.NewProxyNetwork(base, v.Get(nil)) value, err := network.NewProxyNetwork(base, v.Get(nil))
if err != nil { if err != nil {
+5
View File
@@ -2,6 +2,7 @@ package testlib
import ( import (
"context" "context"
"net"
"net/http" "net/http"
"github.com/9seconds/mtg/v2/essentials" "github.com/9seconds/mtg/v2/essentials"
@@ -24,6 +25,10 @@ func (m *MtglibNetworkMock) DialContext(ctx context.Context, network, address st
return args.Get(0).(essentials.Conn), args.Error(1) //nolint: wrapcheck, forcetypeassert return args.Get(0).(essentials.Conn), args.Error(1) //nolint: wrapcheck, forcetypeassert
} }
func (m *MtglibNetworkMock) NativeDialer() *net.Dialer {
return m.Called().Get(0).(*net.Dialer)
}
func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context, func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
network, address string) (essentials.Conn, error), network, address string) (essentials.Conn, error),
) *http.Client { ) *http.Client {
+5 -1
View File
@@ -124,13 +124,17 @@ type Network interface {
// Dial establishes context-free TCP connections. // Dial establishes context-free TCP connections.
Dial(network, address string) (essentials.Conn, error) Dial(network, address string) (essentials.Conn, error)
// DialContext dials using a context. This is a preferrable way of // DialContext dials using a context. This is a preferable way of
// establishing TCP connections. // establishing TCP connections.
DialContext(ctx context.Context, network, address string) (essentials.Conn, error) DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
// MakeHTTPClient build an HTTP client with given dial function. If nothing is // MakeHTTPClient build an HTTP client with given dial function. If nothing is
// provided, then DialContext of this interface is going to be used. // provided, then DialContext of this interface is going to be used.
MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
// NativeDialer returns a configured instance of native dialer that
// skips proxy connections or any other irrelevant settings.
NativeDialer() *net.Dialer
} }
// AntiReplayCache is an interface that is used to detect replay attacks based // AntiReplayCache is an interface that is used to detect replay attacks based
+4 -1
View File
@@ -279,13 +279,16 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
p.eventStream.Send(p.ctx, NewEventDomainFronting(ctx.streamID)) p.eventStream.Send(p.ctx, NewEventDomainFronting(ctx.streamID))
conn.Rewind() conn.Rewind()
frontConn, err := p.network.DialContext(ctx, "tcp", p.DomainFrontingAddress()) nativeDialer := p.network.NativeDialer()
fConn, err := nativeDialer.DialContext(ctx, "tcp", p.DomainFrontingAddress())
if err != nil { if err != nil {
p.logger.WarningError("cannot dial to the fronting domain", err) p.logger.WarningError("cannot dial to the fronting domain", err)
return return
} }
frontConn := essentials.WrapNetConn(fConn)
if p.domainFrontingProxyProtocol { if p.domainFrontingProxyProtocol {
frontConn = newConnProxyProtocol(ctx.clientConn, frontConn) frontConn = newConnProxyProtocol(ctx.clientConn, frontConn)
} }
+4
View File
@@ -60,6 +60,10 @@ func (n *network) DialContext(ctx context.Context, protocol, address string) (es
return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err) return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
} }
func (n *network) NativeDialer() *net.Dialer {
return &net.Dialer{}
}
func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context, func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
network, address string) (essentials.Conn, error), network, address string) (essentials.Conn, error),
) *http.Client { ) *http.Client {
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"testing" "testing"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/network/v2" "github.com/9seconds/mtg/v2/network/v2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@@ -12,7 +13,7 @@ import (
type BaseNetworkTestSuite struct { type BaseNetworkTestSuite struct {
EchoServerTestSuite EchoServerTestSuite
net network.Network net mtglib.Network
} }
func (suite *BaseNetworkTestSuite) SetupSuite() { func (suite *BaseNetworkTestSuite) SetupSuite() {
-9
View File
@@ -11,10 +11,7 @@ package network
import ( import (
"errors" "errors"
"net"
"time" "time"
"github.com/9seconds/mtg/v2/mtglib"
) )
const ( const (
@@ -37,9 +34,3 @@ const (
) )
var ErrCannotDial = errors.New("cannot dial to any address") var ErrCannotDial = errors.New("cannot dial to any address")
type Network interface {
mtglib.Network
NativeDialer() *net.Dialer
}
+4 -3
View File
@@ -8,10 +8,11 @@ import (
"net/http" "net/http"
"github.com/9seconds/mtg/v2/essentials" "github.com/9seconds/mtg/v2/essentials"
"github.com/9seconds/mtg/v2/mtglib"
) )
type multiNetwork struct { type multiNetwork struct {
networks []Network networks []mtglib.Network
} }
func (m multiNetwork) Dial(network, address string) (essentials.Conn, error) { func (m multiNetwork) Dial(network, address string) (essentials.Conn, error) {
@@ -22,7 +23,7 @@ func (m multiNetwork) DialContext(ctx context.Context, network, address string)
networks := m.networks networks := m.networks
if len(networks) > 1 { if len(networks) > 1 {
networks = make([]Network, len(m.networks)) networks = make([]mtglib.Network, len(m.networks))
copy(networks, m.networks) copy(networks, m.networks)
rand.Shuffle(len(m.networks), func(i, j int) { rand.Shuffle(len(m.networks), func(i, j int) {
@@ -59,7 +60,7 @@ func (m multiNetwork) MakeHTTPClient(
return m.networks[0].MakeHTTPClient(dialFunc) return m.networks[0].MakeHTTPClient(dialFunc)
} }
func Join(networks ...Network) (Network, error) { func Join(networks ...mtglib.Network) (mtglib.Network, error) {
if len(networks) == 0 { if len(networks) == 0 {
return nil, errors.New("cannot join no networks") return nil, errors.New("cannot join no networks")
} }
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/9seconds/mtg/v2/essentials" "github.com/9seconds/mtg/v2/essentials"
"github.com/9seconds/mtg/v2/mtglib"
) )
type network struct { type network struct {
@@ -70,7 +71,7 @@ func New(
tcpTimeout, tcpTimeout,
httpTimeout, httpTimeout,
idleTimeout time.Duration, idleTimeout time.Duration,
) Network { ) mtglib.Network {
if dnsResolver == nil { if dnsResolver == nil {
dnsResolver = net.DefaultResolver dnsResolver = net.DefaultResolver
} }
+3 -2
View File
@@ -6,11 +6,12 @@ import (
"net/url" "net/url"
"github.com/9seconds/mtg/v2/essentials" "github.com/9seconds/mtg/v2/essentials"
"github.com/9seconds/mtg/v2/mtglib"
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
) )
type proxyNetwork struct { type proxyNetwork struct {
Network mtglib.Network
client proxy.ContextDialer client proxy.ContextDialer
} }
@@ -23,7 +24,7 @@ func (p proxyNetwork) DialContext(ctx context.Context, network, address string)
return essentials.WrapNetConn(conn), nil return essentials.WrapNetConn(conn), nil
} }
func NewProxyNetwork(base Network, proxyURL *url.URL) (*proxyNetwork, error) { func NewProxyNetwork(base mtglib.Network, proxyURL *url.URL) (*proxyNetwork, error) {
socks, err := proxy.FromURL(proxyURL, base.NativeDialer()) socks, err := proxy.FromURL(proxyURL, base.NativeDialer())
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot build proxy dialer: %w", err) return nil, fmt.Errorf("cannot build proxy dialer: %w", err)
+3 -2
View File
@@ -6,6 +6,7 @@ import (
"sync" "sync"
"testing" "testing"
"github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/network/v2" "github.com/9seconds/mtg/v2/network/v2"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@@ -17,7 +18,7 @@ type SocksProxyTestSuite struct {
EchoServerTestSuite EchoServerTestSuite
wg sync.WaitGroup wg sync.WaitGroup
baseNetwork network.Network baseNetwork mtglib.Network
noAuthURL *url.URL noAuthURL *url.URL
authURL *url.URL authURL *url.URL
@@ -85,7 +86,7 @@ func (suite *SocksProxyTestSuite) TestRead() {
for name, proxies := range testData { for name, proxies := range testData {
suite.T().Run(name, func(t *testing.T) { suite.T().Run(name, func(t *testing.T) {
proxyNetworks := []network.Network{} proxyNetworks := []mtglib.Network{}
for _, u := range proxies { for _, u := range proxies {
value, err := network.NewProxyNetwork(suite.baseNetwork, u) value, err := network.NewProxyNetwork(suite.baseNetwork, u)