Procházet zdrojové kódy

Add NativeDialer method to mtglib.Network

tags/v2.2.0^2^2
9seconds před 1 měsícem
rodič
revize
94e4179fb7

+ 1
- 1
internal/cli/run_proxy.go Zobrazit soubor

@@ -52,7 +52,7 @@ func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
52 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 56
 	for idx, v := range conf.Network.Proxies {
57 57
 		value, err := network.NewProxyNetwork(base, v.Get(nil))
58 58
 		if err != nil {

+ 5
- 1
mtglib/init.go Zobrazit soubor

@@ -124,13 +124,17 @@ type Network interface {
124 124
 	// Dial establishes context-free TCP connections.
125 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 128
 	// establishing TCP connections.
129 129
 	DialContext(ctx context.Context, network, address string) (essentials.Conn, error)
130 130
 
131 131
 	// MakeHTTPClient build an HTTP client with given dial function. If nothing is
132 132
 	// provided, then DialContext of this interface is going to be used.
133 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 140
 // AntiReplayCache is an interface that is used to detect replay attacks based

+ 4
- 0
network/network.go Zobrazit soubor

@@ -60,6 +60,10 @@ func (n *network) DialContext(ctx context.Context, protocol, address string) (es
60 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 67
 func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
64 68
 	network, address string) (essentials.Conn, error),
65 69
 ) *http.Client {

+ 0
- 9
network/v2/init.go Zobrazit soubor

@@ -11,10 +11,7 @@ package network
11 11
 
12 12
 import (
13 13
 	"errors"
14
-	"net"
15 14
 	"time"
16
-
17
-	"github.com/9seconds/mtg/v2/mtglib"
18 15
 )
19 16
 
20 17
 const (
@@ -37,9 +34,3 @@ const (
37 34
 )
38 35
 
39 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 Zobrazit soubor

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

+ 2
- 1
network/v2/network.go Zobrazit soubor

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

+ 3
- 2
network/v2/proxy_network.go Zobrazit soubor

@@ -6,11 +6,12 @@ import (
6 6
 	"net/url"
7 7
 
8 8
 	"github.com/9seconds/mtg/v2/essentials"
9
+	"github.com/9seconds/mtg/v2/mtglib"
9 10
 	"golang.org/x/net/proxy"
10 11
 )
11 12
 
12 13
 type proxyNetwork struct {
13
-	Network
14
+	mtglib.Network
14 15
 	client proxy.ContextDialer
15 16
 }
16 17
 
@@ -23,7 +24,7 @@ func (p proxyNetwork) DialContext(ctx context.Context, network, address string)
23 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 28
 	socks, err := proxy.FromURL(proxyURL, base.NativeDialer())
28 29
 	if err != nil {
29 30
 		return nil, fmt.Errorf("cannot build proxy dialer: %w", err)

Načítá se…
Zrušit
Uložit