Explorar el Código

Add NativeDialer method to mtglib.Network

tags/v2.2.0^2^2
9seconds hace 1 mes
padre
commit
94e4179fb7

+ 1
- 1
internal/cli/run_proxy.go Ver fichero

52
 		conf.Network.Timeout.Idle.Get(0),
52
 		conf.Network.Timeout.Idle.Get(0),
53
 	)
53
 	)
54
 
54
 
55
-	proxyDialers := make([]network.Network, len(conf.Network.Proxies))
55
+	proxyDialers := make([]mtglib.Network, len(conf.Network.Proxies))
56
 	for idx, v := range conf.Network.Proxies {
56
 	for idx, v := range conf.Network.Proxies {
57
 		value, err := network.NewProxyNetwork(base, v.Get(nil))
57
 		value, err := network.NewProxyNetwork(base, v.Get(nil))
58
 		if err != nil {
58
 		if err != nil {

+ 5
- 1
mtglib/init.go Ver fichero

124
 	// Dial establishes context-free TCP connections.
124
 	// Dial establishes context-free TCP connections.
125
 	Dial(network, address string) (essentials.Conn, error)
125
 	Dial(network, address string) (essentials.Conn, error)
126
 
126
 
127
-	// DialContext dials using a context. This is a preferrable way of
127
+	// DialContext dials using a context. This is a preferable way of
128
 	// establishing TCP connections.
128
 	// establishing TCP connections.
129
 	DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
129
 	DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
130
 
130
 
131
 	// MakeHTTPClient build an HTTP client with given dial function. If nothing is
131
 	// MakeHTTPClient build an HTTP client with given dial function. If nothing is
132
 	// provided, then DialContext of this interface is going to be used.
132
 	// provided, then DialContext of this interface is going to be used.
133
 	MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
133
 	MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client
134
+
135
+	// NativeDialer returns a configured instance of native dialer that
136
+	// skips proxy connections or any other irrelevant settings.
137
+	NativeDialer() *net.Dialer
134
 }
138
 }
135
 
139
 
136
 // AntiReplayCache is an interface that is used to detect replay attacks based
140
 // AntiReplayCache is an interface that is used to detect replay attacks based

+ 4
- 0
network/network.go Ver fichero

60
 	return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
60
 	return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
61
 }
61
 }
62
 
62
 
63
+func (n *network) NativeDialer() *net.Dialer {
64
+	return &net.Dialer{}
65
+}
66
+
63
 func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
67
 func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
64
 	network, address string) (essentials.Conn, error),
68
 	network, address string) (essentials.Conn, error),
65
 ) *http.Client {
69
 ) *http.Client {

+ 0
- 9
network/v2/init.go Ver fichero

11
 
11
 
12
 import (
12
 import (
13
 	"errors"
13
 	"errors"
14
-	"net"
15
 	"time"
14
 	"time"
16
-
17
-	"github.com/9seconds/mtg/v2/mtglib"
18
 )
15
 )
19
 
16
 
20
 const (
17
 const (
37
 )
34
 )
38
 
35
 
39
 var ErrCannotDial = errors.New("cannot dial to any address")
36
 var ErrCannotDial = errors.New("cannot dial to any address")
40
-
41
-type Network interface {
42
-	mtglib.Network
43
-
44
-	NativeDialer() *net.Dialer
45
-}

+ 4
- 3
network/v2/multi_network.go Ver fichero

8
 	"net/http"
8
 	"net/http"
9
 
9
 
10
 	"github.com/9seconds/mtg/v2/essentials"
10
 	"github.com/9seconds/mtg/v2/essentials"
11
+	"github.com/9seconds/mtg/v2/mtglib"
11
 )
12
 )
12
 
13
 
13
 type multiNetwork struct {
14
 type multiNetwork struct {
14
-	networks []Network
15
+	networks []mtglib.Network
15
 }
16
 }
16
 
17
 
17
 func (m multiNetwork) Dial(network, address string) (essentials.Conn, error) {
18
 func (m multiNetwork) Dial(network, address string) (essentials.Conn, error) {
22
 	networks := m.networks
23
 	networks := m.networks
23
 
24
 
24
 	if len(networks) > 1 {
25
 	if len(networks) > 1 {
25
-		networks = make([]Network, len(m.networks))
26
+		networks = make([]mtglib.Network, len(m.networks))
26
 		copy(networks, m.networks)
27
 		copy(networks, m.networks)
27
 
28
 
28
 		rand.Shuffle(len(m.networks), func(i, j int) {
29
 		rand.Shuffle(len(m.networks), func(i, j int) {
59
 	return m.networks[0].MakeHTTPClient(dialFunc)
60
 	return m.networks[0].MakeHTTPClient(dialFunc)
60
 }
61
 }
61
 
62
 
62
-func Join(networks ...Network) (Network, error) {
63
+func Join(networks ...mtglib.Network) (mtglib.Network, error) {
63
 	if len(networks) == 0 {
64
 	if len(networks) == 0 {
64
 		return nil, errors.New("cannot join no networks")
65
 		return nil, errors.New("cannot join no networks")
65
 	}
66
 	}

+ 2
- 1
network/v2/network.go Ver fichero

8
 	"time"
8
 	"time"
9
 
9
 
10
 	"github.com/9seconds/mtg/v2/essentials"
10
 	"github.com/9seconds/mtg/v2/essentials"
11
+	"github.com/9seconds/mtg/v2/mtglib"
11
 )
12
 )
12
 
13
 
13
 type network struct {
14
 type network struct {
70
 	tcpTimeout,
71
 	tcpTimeout,
71
 	httpTimeout,
72
 	httpTimeout,
72
 	idleTimeout time.Duration,
73
 	idleTimeout time.Duration,
73
-) Network {
74
+) mtglib.Network {
74
 	if dnsResolver == nil {
75
 	if dnsResolver == nil {
75
 		dnsResolver = net.DefaultResolver
76
 		dnsResolver = net.DefaultResolver
76
 	}
77
 	}

+ 3
- 2
network/v2/proxy_network.go Ver fichero

6
 	"net/url"
6
 	"net/url"
7
 
7
 
8
 	"github.com/9seconds/mtg/v2/essentials"
8
 	"github.com/9seconds/mtg/v2/essentials"
9
+	"github.com/9seconds/mtg/v2/mtglib"
9
 	"golang.org/x/net/proxy"
10
 	"golang.org/x/net/proxy"
10
 )
11
 )
11
 
12
 
12
 type proxyNetwork struct {
13
 type proxyNetwork struct {
13
-	Network
14
+	mtglib.Network
14
 	client proxy.ContextDialer
15
 	client proxy.ContextDialer
15
 }
16
 }
16
 
17
 
23
 	return essentials.WrapNetConn(conn), nil
24
 	return essentials.WrapNetConn(conn), nil
24
 }
25
 }
25
 
26
 
26
-func NewProxyNetwork(base Network, proxyURL *url.URL) (*proxyNetwork, error) {
27
+func NewProxyNetwork(base mtglib.Network, proxyURL *url.URL) (*proxyNetwork, error) {
27
 	socks, err := proxy.FromURL(proxyURL, base.NativeDialer())
28
 	socks, err := proxy.FromURL(proxyURL, base.NativeDialer())
28
 	if err != nil {
29
 	if err != nil {
29
 		return nil, fmt.Errorf("cannot build proxy dialer: %w", err)
30
 		return nil, fmt.Errorf("cannot build proxy dialer: %w", err)

Loading…
Cancelar
Guardar