Просмотр исходного кода

Liniting for network

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
21ee1e2c6c
4 измененных файлов: 61 добавлений и 19 удалений
  1. 6
    2
      mtglib/network/default.go
  2. 32
    9
      mtglib/network/network.go
  3. 21
    6
      mtglib/network/shadowsocks.go
  4. 2
    2
      mtglib/network/socks5.go

+ 6
- 2
mtglib/network/default.go Просмотреть файл

21
 
21
 
22
 func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
22
 func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
23
 	switch network {
23
 	switch network {
24
-	case "tcp", "tcp4", "tcp6":
24
+	case "tcp", "tcp4", "tcp6": // nolint: goconst
25
 	default:
25
 	default:
26
 		return nil, fmt.Errorf("unsupported network %s", network)
26
 		return nil, fmt.Errorf("unsupported network %s", network)
27
 	}
27
 	}
35
 
35
 
36
 	if err := tcpConn.SetNoDelay(true); err != nil {
36
 	if err := tcpConn.SetNoDelay(true); err != nil {
37
 		conn.Close()
37
 		conn.Close()
38
+
38
 		return nil, fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
39
 		return nil, fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
39
 	}
40
 	}
40
 
41
 
41
 	if err := tcpConn.SetReadBuffer(d.bufferSize); err != nil {
42
 	if err := tcpConn.SetReadBuffer(d.bufferSize); err != nil {
42
 		tcpConn.Close()
43
 		tcpConn.Close()
44
+
43
 		return nil, fmt.Errorf("cannot set read buffer size: %w", err)
45
 		return nil, fmt.Errorf("cannot set read buffer size: %w", err)
44
 	}
46
 	}
45
 
47
 
46
 	if err := tcpConn.SetWriteBuffer(d.bufferSize); err != nil {
48
 	if err := tcpConn.SetWriteBuffer(d.bufferSize); err != nil {
47
 		tcpConn.Close()
49
 		tcpConn.Close()
50
+
48
 		return nil, fmt.Errorf("cannot set write buffer size: %w", err)
51
 		return nil, fmt.Errorf("cannot set write buffer size: %w", err)
49
 	}
52
 	}
50
 
53
 
51
 	if err := tcpConn.SetKeepAlive(true); err != nil {
54
 	if err := tcpConn.SetKeepAlive(true); err != nil {
52
 		tcpConn.Close()
55
 		tcpConn.Close()
56
+
53
 		return nil, fmt.Errorf("cannot enable keep-alive: %w", err)
57
 		return nil, fmt.Errorf("cannot enable keep-alive: %w", err)
54
 	}
58
 	}
55
 
59
 
61
 	case timeout < 0:
65
 	case timeout < 0:
62
 		return nil, fmt.Errorf("timeout %v should be positive number", timeout)
66
 		return nil, fmt.Errorf("timeout %v should be positive number", timeout)
63
 	case bufferSize < 0:
67
 	case bufferSize < 0:
64
-		return nil, fmt.Errorf("buffer size %s should be positive number", bufferSize)
68
+		return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
65
 	}
69
 	}
66
 
70
 
67
 	if timeout == 0 {
71
 	if timeout == 0 {

+ 32
- 9
mtglib/network/network.go Просмотреть файл

6
 	"math/rand"
6
 	"math/rand"
7
 	"net"
7
 	"net"
8
 	"net/http"
8
 	"net/http"
9
+	"sync"
9
 	"time"
10
 	"time"
10
 
11
 
11
 	doh "github.com/babolivier/go-doh-client"
12
 	doh "github.com/babolivier/go-doh-client"
50
 		return []string{address}, nil
51
 		return []string{address}, nil
51
 	}
52
 	}
52
 
53
 
53
-	var ips []string
54
+	ips := []string{}
55
+	wg := &sync.WaitGroup{}
56
+	mutex := &sync.Mutex{}
54
 
57
 
55
 	switch network {
58
 	switch network {
56
 	case "tcp", "tcp4":
59
 	case "tcp", "tcp4":
57
-		if recs, _, err := d.DNS.LookupA(address); err == nil {
58
-			for _, v := range recs {
59
-				ips = append(ips, v.IP4)
60
+		wg.Add(1)
61
+
62
+		go func() {
63
+			defer wg.Done()
64
+
65
+			if recs, _, err := d.DNS.LookupA(address); err == nil {
66
+				mutex.Lock()
67
+				defer mutex.Unlock()
68
+
69
+				for _, v := range recs {
70
+					ips = append(ips, v.IP4)
71
+				}
60
 			}
72
 			}
61
-		}
73
+		}()
62
 	}
74
 	}
63
 
75
 
64
 	switch network {
76
 	switch network {
65
 	case "tcp", "tcp6":
77
 	case "tcp", "tcp6":
66
-		if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
67
-			for _, v := range recs {
68
-				ips = append(ips, v.IP6)
78
+		wg.Add(1)
79
+
80
+		go func() {
81
+			defer wg.Done()
82
+
83
+			if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
84
+				mutex.Lock()
85
+				defer mutex.Unlock()
86
+
87
+				for _, v := range recs {
88
+					ips = append(ips, v.IP6)
89
+				}
69
 			}
90
 			}
70
-		}
91
+		}()
71
 	}
92
 	}
72
 
93
 
94
+	wg.Wait()
95
+
73
 	if len(ips) == 0 {
96
 	if len(ips) == 0 {
74
 		return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
97
 		return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
75
 	}
98
 	}

+ 21
- 6
mtglib/network/shadowsocks.go Просмотреть файл

10
 	"time"
10
 	"time"
11
 
11
 
12
 	shadowsocks "github.com/shadowsocks/go-shadowsocks2/core"
12
 	shadowsocks "github.com/shadowsocks/go-shadowsocks2/core"
13
+	"golang.org/x/net/proxy"
13
 )
14
 )
14
 
15
 
15
 type shadowsocksDialer struct {
16
 type shadowsocksDialer struct {
18
 	cipher shadowsocks.StreamConnCipher
19
 	cipher shadowsocks.StreamConnCipher
19
 }
20
 }
20
 
21
 
22
+func (s *shadowsocksDialer) Dial(network, address string) (net.Conn, error) {
23
+	return s.DialContext(context.Background(), network, address)
24
+}
25
+
21
 func (s *shadowsocksDialer) DialContext(ctx context.Context,
26
 func (s *shadowsocksDialer) DialContext(ctx context.Context,
22
 	network, address string) (net.Conn, error) {
27
 	network, address string) (net.Conn, error) {
23
 	conn, err := s.Dialer.DialContext(ctx, network, address)
28
 	conn, err := s.Dialer.DialContext(ctx, network, address)
24
 	if err != nil {
29
 	if err != nil {
25
-		return nil, err
30
+		return nil, err // nolint: wrapcheck
26
 	}
31
 	}
27
 
32
 
28
 	return s.cipher.StreamConn(conn), nil
33
 	return s.cipher.StreamConn(conn), nil
29
 }
34
 }
30
 
35
 
31
-func NewShadowsocksDialer(proxyUrl *url.URL,
36
+func NewShadowsocksDialer(proxyURL *url.URL,
32
 	timeout time.Duration, bufferSize int) (Dialer, error) {
37
 	timeout time.Duration, bufferSize int) (Dialer, error) {
33
-	username := proxyUrl.User.Username()
38
+	username := proxyURL.User.Username()
34
 
39
 
35
 	decoded, err := base64.RawURLEncoding.DecodeString(username)
40
 	decoded, err := base64.RawURLEncoding.DecodeString(username)
36
 	if err != nil {
41
 	if err != nil {
47
 		return nil, fmt.Errorf("cannot initialize shadowsocks cipher: %w", err)
52
 		return nil, fmt.Errorf("cannot initialize shadowsocks cipher: %w", err)
48
 	}
53
 	}
49
 
54
 
55
+	socks5URL := *proxyURL
56
+	socks5URL.Scheme = "socks5"
57
+	socks5URL.User = nil
58
+
50
 	dialer, err := NewDefaultDialer(timeout, bufferSize)
59
 	dialer, err := NewDefaultDialer(timeout, bufferSize)
51
 	if err != nil {
60
 	if err != nil {
52
 		return nil, fmt.Errorf("cannot initialize a base dialer: %w", err)
61
 		return nil, fmt.Errorf("cannot initialize a base dialer: %w", err)
53
-
54
 	}
62
 	}
55
 
63
 
56
-	return &shadowsocksDialer{
64
+	ssDialer := &shadowsocksDialer{
57
 		Dialer: dialer,
65
 		Dialer: dialer,
58
 		cipher: cipher,
66
 		cipher: cipher,
59
-	}, nil
67
+	}
68
+
69
+	rv, err := proxy.FromURL(&socks5URL, ssDialer)
70
+	if err != nil {
71
+		return nil, fmt.Errorf("cannot initialize ss proxy dialer: %w", err)
72
+	}
73
+
74
+	return rv.(Dialer), nil
60
 }
75
 }

+ 2
- 2
mtglib/network/socks5.go Просмотреть файл

8
 	"golang.org/x/net/proxy"
8
 	"golang.org/x/net/proxy"
9
 )
9
 )
10
 
10
 
11
-func NewSocks5Dialer(proxyUrl *url.URL, timeout time.Duration, bufferSize int) (Dialer, error) {
11
+func NewSocks5Dialer(proxyURL *url.URL, timeout time.Duration, bufferSize int) (Dialer, error) {
12
 	dialer, err := NewDefaultDialer(timeout, bufferSize)
12
 	dialer, err := NewDefaultDialer(timeout, bufferSize)
13
 	if err != nil {
13
 	if err != nil {
14
 		return nil, fmt.Errorf("cannot initialize base dialer: %w", err)
14
 		return nil, fmt.Errorf("cannot initialize base dialer: %w", err)
15
 	}
15
 	}
16
 
16
 
17
-	rv, err := proxy.FromURL(proxyUrl, dialer.(*defaultDialer))
17
+	rv, err := proxy.FromURL(proxyURL, dialer.(*defaultDialer))
18
 	if err != nil {
18
 	if err != nil {
19
 		return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
19
 		return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
20
 	}
20
 	}

Загрузка…
Отмена
Сохранить