Use network v2 instead of network v1

This commit is contained in:
9seconds
2026-02-27 15:52:29 +01:00
parent 42927c8bdc
commit 1151291535
7 changed files with 43 additions and 47 deletions
+2 -2
View File
@@ -21,7 +21,7 @@ require (
github.com/stretchr/testify v1.11.1 github.com/stretchr/testify v1.11.1
github.com/tylertreat/BoomFilters v0.0.0-20251117164519-53813c36cc1b github.com/tylertreat/BoomFilters v0.0.0-20251117164519-53813c36cc1b
golang.org/x/crypto v0.48.0 golang.org/x/crypto v0.48.0
golang.org/x/net v0.49.0 // indirect golang.org/x/net v0.51.0
golang.org/x/sys v0.41.0 golang.org/x/sys v0.41.0
google.golang.org/protobuf v1.36.11 // indirect google.golang.org/protobuf v1.36.11 // indirect
) )
@@ -29,6 +29,7 @@ require (
require ( require (
github.com/pelletier/go-toml/v2 v2.2.4 github.com/pelletier/go-toml/v2 v2.2.4
github.com/pires/go-proxyproto v0.11.0 github.com/pires/go-proxyproto v0.11.0
github.com/things-go/go-socks5 v0.1.0
github.com/txthinking/socks5 v0.0.0-20251011041537-5c31f201a10e github.com/txthinking/socks5 v0.0.0-20251011041537-5c31f201a10e
github.com/yl2chen/cidranger v1.0.2 github.com/yl2chen/cidranger v1.0.2
) )
@@ -46,7 +47,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.2 // indirect github.com/prometheus/client_model v0.6.2 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/things-go/go-socks5 v0.1.0 // indirect
github.com/txthinking/runnergroup v0.0.0-20250224021307-5864ffeb65ae // indirect github.com/txthinking/runnergroup v0.0.0-20250224021307-5864ffeb65ae // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/sync v0.19.0 // indirect golang.org/x/sync v0.19.0 // indirect
+2 -2
View File
@@ -117,8 +117,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+23 -31
View File
@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"net/url"
"os" "os"
"github.com/9seconds/mtg/v2/antireplay" "github.com/9seconds/mtg/v2/antireplay"
@@ -16,7 +15,7 @@ import (
"github.com/9seconds/mtg/v2/ipblocklist/files" "github.com/9seconds/mtg/v2/ipblocklist/files"
"github.com/9seconds/mtg/v2/logger" "github.com/9seconds/mtg/v2/logger"
"github.com/9seconds/mtg/v2/mtglib" "github.com/9seconds/mtg/v2/mtglib"
"github.com/9seconds/mtg/v2/network" "github.com/9seconds/mtg/v2/network/v2"
"github.com/9seconds/mtg/v2/stats" "github.com/9seconds/mtg/v2/stats"
"github.com/pires/go-proxyproto" "github.com/pires/go-proxyproto"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@@ -40,43 +39,36 @@ func makeLogger(conf *config.Config) mtglib.Logger {
} }
func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) { func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout) base := network.New(
httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout) nil,
dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String() "mtg/"+version,
userAgent := "mtg/" + version conf.Network.Timeout.TCP.Get(0),
conf.Network.Timeout.HTTP.Get(0),
conf.Network.Timeout.Idle.Get(0),
)
baseDialer, err := network.NewDefaultDialer(tcpTimeout, 0) proxyDialers := make([]network.Network, len(conf.Network.Proxies))
if err != nil { for idx, v := range conf.Network.Proxies {
return nil, fmt.Errorf("cannot build a default dialer: %w", err) value, err := network.NewProxyNetwork(base, v.Get(nil))
}
if len(conf.Network.Proxies) == 0 {
return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck
}
proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
for _, v := range conf.Network.Proxies {
if value := v.Get(nil); value != nil {
proxyURLs = append(proxyURLs, value)
}
}
if len(proxyURLs) == 1 {
socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err) return nil, fmt.Errorf("cannot use %v for proxy url: %w", v.Get(nil), err)
} }
proxyDialers[idx] = value
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck
} }
socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs) switch len(proxyDialers) {
case 0:
return base, nil
case 1:
return proxyDialers[0], nil
}
value, err := network.Join(proxyDialers...)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err) panic(err)
} }
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) //nolint: wrapcheck return value, nil
} }
func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache { func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache {
+8 -4
View File
@@ -15,20 +15,24 @@ type TypeProxyURL struct {
func (t *TypeProxyURL) Set(value string) error { func (t *TypeProxyURL) Set(value string) error {
parsedURL, err := url.Parse(value) parsedURL, err := url.Parse(value)
if err != nil { if err != nil {
return fmt.Errorf("value is not corect URL (%s): %w", value, err) return fmt.Errorf("value is not correct URL (%s): %w", value, err)
} }
if parsedURL.Host == "" { if parsedURL.Host == "" {
return fmt.Errorf("url has to have a schema: %s", value) return fmt.Errorf("url has to have a schema: %s", value)
} }
if parsedURL.Scheme != "socks5" { switch parsedURL.Scheme {
case "socks5", "socks5h":
default:
return fmt.Errorf("unsupported schema: %s", parsedURL.Scheme) return fmt.Errorf("unsupported schema: %s", parsedURL.Scheme)
} }
if _, _, err := net.SplitHostPort(parsedURL.Host); err != nil { if _, _, err := net.SplitHostPort(parsedURL.Host); err != nil {
parsedURL.Host = net.JoinHostPort(parsedURL.Host, parsedURL.Host = net.JoinHostPort(
typeProxyURLDefaultSOCKS5Port) parsedURL.Host,
typeProxyURLDefaultSOCKS5Port,
)
} }
t.Value = parsedURL t.Value = parsedURL
+2 -2
View File
@@ -20,7 +20,7 @@ type BaseHTTPTestSuite struct {
func (suite *BaseHTTPTestSuite) SetupSuite() { func (suite *BaseHTTPTestSuite) SetupSuite() {
suite.http = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { suite.http = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(r.Header.Get("User-Agent"))) w.Write([]byte(r.Header.Get("User-Agent"))) //nolint: errcheck
})) }))
} }
@@ -32,7 +32,7 @@ func (suite *BaseHTTPTestSuite) TestGet() {
resp, err := suite.client.Get(suite.http.URL) resp, err := suite.client.Get(suite.http.URL)
suite.NoError(err) suite.NoError(err)
defer resp.Body.Close() defer resp.Body.Close() //nolint: errcheck
data, err := io.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
suite.NoError(err) suite.NoError(err)
+2 -2
View File
@@ -20,7 +20,7 @@ type EchoServer struct {
func (e *EchoServer) Run() { func (e *EchoServer) Run() {
e.wg.Go(func() { e.wg.Go(func() {
<-e.ctx.Done() <-e.ctx.Done()
e.listener.Close() e.listener.Close() //nolint: errcheck
}) })
e.wg.Go(func() { e.wg.Go(func() {
@@ -32,7 +32,7 @@ func (e *EchoServer) Run() {
e.wg.Go(func() { e.wg.Go(func() {
<-e.ctx.Done() <-e.ctx.Done()
conn.Close() conn.Close() //nolint: errcheck
}) })
e.wg.Go(func() { e.wg.Go(func() {
e.process(conn) e.process(conn)
+4 -4
View File
@@ -42,7 +42,7 @@ func (suite *SocksProxyTestSuite) SetupSuite() {
suite.noAuthServer = socks5.NewServer() suite.noAuthServer = socks5.NewServer()
suite.wg.Go(func() { suite.wg.Go(func() {
suite.noAuthServer.Serve(suite.noAuthListener) suite.noAuthServer.Serve(suite.noAuthListener) //nolint: errcheck
}) })
suite.authServer = socks5.NewServer( suite.authServer = socks5.NewServer(
@@ -54,7 +54,7 @@ func (suite *SocksProxyTestSuite) SetupSuite() {
}, },
})) }))
suite.wg.Go(func() { suite.wg.Go(func() {
suite.authServer.Serve(suite.authListener) suite.authServer.Serve(suite.authListener) //nolint: errcheck
}) })
parsed, err := url.Parse("socks5://" + suite.noAuthListener.Addr().String()) parsed, err := url.Parse("socks5://" + suite.noAuthListener.Addr().String())
@@ -115,8 +115,8 @@ func (suite *SocksProxyTestSuite) TestRead() {
} }
func (suite *SocksProxyTestSuite) TearDownSuite() { func (suite *SocksProxyTestSuite) TearDownSuite() {
suite.noAuthListener.Close() suite.noAuthListener.Close() //nolint: errcheck
suite.authListener.Close() suite.authListener.Close() //nolint: errcheck
suite.wg.Wait() suite.wg.Wait()
suite.EchoServerTestSuite.TearDownSuite() suite.EchoServerTestSuite.TearDownSuite()
} }