mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:44:03 +03:00
Add NativeDialer method to mtglib.Network
This commit is contained in:
@@ -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
-1
@@ -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
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user