Change signature of socks5 dialer

This commit is contained in:
9seconds
2021-03-05 18:26:13 +03:00
parent 5b46152945
commit ecfd550a95
3 changed files with 7 additions and 10 deletions
+1
View File
@@ -6,5 +6,6 @@ import (
)
type Dialer interface {
Dial(network, address string) (net.Conn, error)
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
+2 -8
View File
@@ -3,18 +3,12 @@ package network
import (
"fmt"
"net/url"
"time"
"golang.org/x/net/proxy"
)
func NewSocks5Dialer(proxyURL *url.URL, timeout time.Duration, bufferSize int) (Dialer, error) {
dialer, err := NewDefaultDialer(timeout, bufferSize)
if err != nil {
return nil, fmt.Errorf("cannot initialize base dialer: %w", err)
}
rv, err := proxy.FromURL(proxyURL, dialer.(*defaultDialer))
func NewSocks5Dialer(proxyURL *url.URL, base Dialer) (Dialer, error) {
rv, err := proxy.FromURL(proxyURL, base)
if err != nil {
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
}
+4 -2
View File
@@ -14,6 +14,7 @@ import (
type Socks5TestSuite struct {
HTTPServerTestSuite
baseDialer network.Dialer
socksListener net.Listener
socksProxy *socks5.Server
}
@@ -29,6 +30,7 @@ func (suite *Socks5TestSuite) SetupSuite() {
suite.socksProxy, _ = socks5.New(&socksConf)
suite.socksListener, _ = net.Listen("tcp", "127.0.0.1:0")
suite.baseDialer, _ = network.NewDefaultDialer(0, 0)
go suite.socksProxy.Serve(suite.socksListener)
}
@@ -45,7 +47,7 @@ func (suite *Socks5TestSuite) TestRequestFailed() {
User: url.UserPassword("user2", "password"),
Host: suite.socksListener.Addr().String(),
}
dialer, _ := network.NewSocks5Dialer(proxyURL, 0, 0)
dialer, _ := network.NewSocks5Dialer(proxyURL, suite.baseDialer)
httpClient := http.Client{
Transport: &http.Transport{
@@ -64,7 +66,7 @@ func (suite *Socks5TestSuite) TestRequestOk() {
User: url.UserPassword("user", "password"),
Host: suite.socksListener.Addr().String(),
}
dialer, _ := network.NewSocks5Dialer(proxyURL, 0, 0)
dialer, _ := network.NewSocks5Dialer(proxyURL, suite.baseDialer)
httpClient := http.Client{
Transport: &http.Transport{