Browse Source

Use network v2 instead of network v1

tags/v2.1.13
9seconds 2 months ago
parent
commit
1151291535

+ 2
- 2
go.mod View File

21
 	github.com/stretchr/testify v1.11.1
21
 	github.com/stretchr/testify v1.11.1
22
 	github.com/tylertreat/BoomFilters v0.0.0-20251117164519-53813c36cc1b
22
 	github.com/tylertreat/BoomFilters v0.0.0-20251117164519-53813c36cc1b
23
 	golang.org/x/crypto v0.48.0
23
 	golang.org/x/crypto v0.48.0
24
-	golang.org/x/net v0.49.0 // indirect
24
+	golang.org/x/net v0.51.0
25
 	golang.org/x/sys v0.41.0
25
 	golang.org/x/sys v0.41.0
26
 	google.golang.org/protobuf v1.36.11 // indirect
26
 	google.golang.org/protobuf v1.36.11 // indirect
27
 )
27
 )
29
 require (
29
 require (
30
 	github.com/pelletier/go-toml/v2 v2.2.4
30
 	github.com/pelletier/go-toml/v2 v2.2.4
31
 	github.com/pires/go-proxyproto v0.11.0
31
 	github.com/pires/go-proxyproto v0.11.0
32
+	github.com/things-go/go-socks5 v0.1.0
32
 	github.com/txthinking/socks5 v0.0.0-20251011041537-5c31f201a10e
33
 	github.com/txthinking/socks5 v0.0.0-20251011041537-5c31f201a10e
33
 	github.com/yl2chen/cidranger v1.0.2
34
 	github.com/yl2chen/cidranger v1.0.2
34
 )
35
 )
46
 	github.com/pmezard/go-difflib v1.0.0 // indirect
47
 	github.com/pmezard/go-difflib v1.0.0 // indirect
47
 	github.com/prometheus/client_model v0.6.2 // indirect
48
 	github.com/prometheus/client_model v0.6.2 // indirect
48
 	github.com/rogpeppe/go-internal v1.14.1 // indirect
49
 	github.com/rogpeppe/go-internal v1.14.1 // indirect
49
-	github.com/things-go/go-socks5 v0.1.0 // indirect
50
 	github.com/txthinking/runnergroup v0.0.0-20250224021307-5864ffeb65ae // indirect
50
 	github.com/txthinking/runnergroup v0.0.0-20250224021307-5864ffeb65ae // indirect
51
 	go.yaml.in/yaml/v2 v2.4.3 // indirect
51
 	go.yaml.in/yaml/v2 v2.4.3 // indirect
52
 	golang.org/x/sync v0.19.0 // indirect
52
 	golang.org/x/sync v0.19.0 // indirect

+ 2
- 2
go.sum View File

117
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
117
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
118
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
118
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
119
 golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
119
 golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
120
-golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
121
-golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
120
+golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
121
+golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
122
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
122
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
123
 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
123
 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
124
 golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
124
 golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

+ 22
- 30
internal/cli/run_proxy.go View File

4
 	"context"
4
 	"context"
5
 	"fmt"
5
 	"fmt"
6
 	"net"
6
 	"net"
7
-	"net/url"
8
 	"os"
7
 	"os"
9
 
8
 
10
 	"github.com/9seconds/mtg/v2/antireplay"
9
 	"github.com/9seconds/mtg/v2/antireplay"
16
 	"github.com/9seconds/mtg/v2/ipblocklist/files"
15
 	"github.com/9seconds/mtg/v2/ipblocklist/files"
17
 	"github.com/9seconds/mtg/v2/logger"
16
 	"github.com/9seconds/mtg/v2/logger"
18
 	"github.com/9seconds/mtg/v2/mtglib"
17
 	"github.com/9seconds/mtg/v2/mtglib"
19
-	"github.com/9seconds/mtg/v2/network"
18
+	"github.com/9seconds/mtg/v2/network/v2"
20
 	"github.com/9seconds/mtg/v2/stats"
19
 	"github.com/9seconds/mtg/v2/stats"
21
 	"github.com/pires/go-proxyproto"
20
 	"github.com/pires/go-proxyproto"
22
 	"github.com/rs/zerolog"
21
 	"github.com/rs/zerolog"
40
 }
39
 }
41
 
40
 
42
 func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
41
 func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
43
-	tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
44
-	httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
45
-	dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
46
-	userAgent := "mtg/" + version
47
-
48
-	baseDialer, err := network.NewDefaultDialer(tcpTimeout, 0)
49
-	if err != nil {
50
-		return nil, fmt.Errorf("cannot build a default dialer: %w", err)
51
-	}
52
-
53
-	if len(conf.Network.Proxies) == 0 {
54
-		return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck
55
-	}
56
-
57
-	proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
58
-
59
-	for _, v := range conf.Network.Proxies {
60
-		if value := v.Get(nil); value != nil {
61
-			proxyURLs = append(proxyURLs, value)
62
-		}
63
-	}
42
+	base := network.New(
43
+		nil,
44
+		"mtg/"+version,
45
+		conf.Network.Timeout.TCP.Get(0),
46
+		conf.Network.Timeout.HTTP.Get(0),
47
+		conf.Network.Timeout.Idle.Get(0),
48
+	)
64
 
49
 
65
-	if len(proxyURLs) == 1 {
66
-		socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
50
+	proxyDialers := make([]network.Network, len(conf.Network.Proxies))
51
+	for idx, v := range conf.Network.Proxies {
52
+		value, err := network.NewProxyNetwork(base, v.Get(nil))
67
 		if err != nil {
53
 		if err != nil {
68
-			return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
54
+			return nil, fmt.Errorf("cannot use %v for proxy url: %w", v.Get(nil), err)
69
 		}
55
 		}
56
+		proxyDialers[idx] = value
57
+	}
70
 
58
 
71
-		return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck
59
+	switch len(proxyDialers) {
60
+	case 0:
61
+		return base, nil
62
+	case 1:
63
+		return proxyDialers[0], nil
72
 	}
64
 	}
73
 
65
 
74
-	socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
66
+	value, err := network.Join(proxyDialers...)
75
 	if err != nil {
67
 	if err != nil {
76
-		return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
68
+		panic(err)
77
 	}
69
 	}
78
 
70
 
79
-	return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck
71
+	return value, nil
80
 }
72
 }
81
 
73
 
82
 func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache {
74
 func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache {

+ 8
- 4
internal/config/type_proxy_url.go View File

15
 func (t *TypeProxyURL) Set(value string) error {
15
 func (t *TypeProxyURL) Set(value string) error {
16
 	parsedURL, err := url.Parse(value)
16
 	parsedURL, err := url.Parse(value)
17
 	if err != nil {
17
 	if err != nil {
18
-		return fmt.Errorf("value is not corect URL (%s): %w", value, err)
18
+		return fmt.Errorf("value is not correct URL (%s): %w", value, err)
19
 	}
19
 	}
20
 
20
 
21
 	if parsedURL.Host == "" {
21
 	if parsedURL.Host == "" {
22
 		return fmt.Errorf("url has to have a schema: %s", value)
22
 		return fmt.Errorf("url has to have a schema: %s", value)
23
 	}
23
 	}
24
 
24
 
25
-	if parsedURL.Scheme != "socks5" {
25
+	switch parsedURL.Scheme {
26
+	case "socks5", "socks5h":
27
+	default:
26
 		return fmt.Errorf("unsupported schema: %s", parsedURL.Scheme)
28
 		return fmt.Errorf("unsupported schema: %s", parsedURL.Scheme)
27
 	}
29
 	}
28
 
30
 
29
 	if _, _, err := net.SplitHostPort(parsedURL.Host); err != nil {
31
 	if _, _, err := net.SplitHostPort(parsedURL.Host); err != nil {
30
-		parsedURL.Host = net.JoinHostPort(parsedURL.Host,
31
-			typeProxyURLDefaultSOCKS5Port)
32
+		parsedURL.Host = net.JoinHostPort(
33
+			parsedURL.Host,
34
+			typeProxyURLDefaultSOCKS5Port,
35
+		)
32
 	}
36
 	}
33
 
37
 
34
 	t.Value = parsedURL
38
 	t.Value = parsedURL

+ 2
- 2
network/v2/base_http_test.go View File

20
 func (suite *BaseHTTPTestSuite) SetupSuite() {
20
 func (suite *BaseHTTPTestSuite) SetupSuite() {
21
 	suite.http = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21
 	suite.http = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22
 		w.WriteHeader(http.StatusOK)
22
 		w.WriteHeader(http.StatusOK)
23
-		w.Write([]byte(r.Header.Get("User-Agent")))
23
+		w.Write([]byte(r.Header.Get("User-Agent"))) //nolint: errcheck
24
 	}))
24
 	}))
25
 }
25
 }
26
 
26
 
32
 	resp, err := suite.client.Get(suite.http.URL)
32
 	resp, err := suite.client.Get(suite.http.URL)
33
 	suite.NoError(err)
33
 	suite.NoError(err)
34
 
34
 
35
-	defer resp.Body.Close()
35
+	defer resp.Body.Close() //nolint: errcheck
36
 
36
 
37
 	data, err := io.ReadAll(resp.Body)
37
 	data, err := io.ReadAll(resp.Body)
38
 	suite.NoError(err)
38
 	suite.NoError(err)

+ 2
- 2
network/v2/echo_server_test.go View File

20
 func (e *EchoServer) Run() {
20
 func (e *EchoServer) Run() {
21
 	e.wg.Go(func() {
21
 	e.wg.Go(func() {
22
 		<-e.ctx.Done()
22
 		<-e.ctx.Done()
23
-		e.listener.Close()
23
+		e.listener.Close() //nolint: errcheck
24
 	})
24
 	})
25
 
25
 
26
 	e.wg.Go(func() {
26
 	e.wg.Go(func() {
32
 
32
 
33
 			e.wg.Go(func() {
33
 			e.wg.Go(func() {
34
 				<-e.ctx.Done()
34
 				<-e.ctx.Done()
35
-				conn.Close()
35
+				conn.Close() //nolint: errcheck
36
 			})
36
 			})
37
 			e.wg.Go(func() {
37
 			e.wg.Go(func() {
38
 				e.process(conn)
38
 				e.process(conn)

+ 4
- 4
network/v2/socks_proxy_test.go View File

42
 
42
 
43
 	suite.noAuthServer = socks5.NewServer()
43
 	suite.noAuthServer = socks5.NewServer()
44
 	suite.wg.Go(func() {
44
 	suite.wg.Go(func() {
45
-		suite.noAuthServer.Serve(suite.noAuthListener)
45
+		suite.noAuthServer.Serve(suite.noAuthListener) //nolint: errcheck
46
 	})
46
 	})
47
 
47
 
48
 	suite.authServer = socks5.NewServer(
48
 	suite.authServer = socks5.NewServer(
54
 			},
54
 			},
55
 		}))
55
 		}))
56
 	suite.wg.Go(func() {
56
 	suite.wg.Go(func() {
57
-		suite.authServer.Serve(suite.authListener)
57
+		suite.authServer.Serve(suite.authListener) //nolint: errcheck
58
 	})
58
 	})
59
 
59
 
60
 	parsed, err := url.Parse("socks5://" + suite.noAuthListener.Addr().String())
60
 	parsed, err := url.Parse("socks5://" + suite.noAuthListener.Addr().String())
115
 }
115
 }
116
 
116
 
117
 func (suite *SocksProxyTestSuite) TearDownSuite() {
117
 func (suite *SocksProxyTestSuite) TearDownSuite() {
118
-	suite.noAuthListener.Close()
119
-	suite.authListener.Close()
118
+	suite.noAuthListener.Close() //nolint: errcheck
119
+	suite.authListener.Close()   //nolint: errcheck
120
 	suite.wg.Wait()
120
 	suite.wg.Wait()
121
 	suite.EchoServerTestSuite.TearDownSuite()
121
 	suite.EchoServerTestSuite.TearDownSuite()
122
 }
122
 }

Loading…
Cancel
Save