Sfoglia il codice sorgente

Implement IP preferences

tags/v1.0.3^2
9seconds 6 anni fa
parent
commit
720158f15c
1 ha cambiato i file con 26 aggiunte e 11 eliminazioni
  1. 26
    11
      telegram/base.go

+ 26
- 11
telegram/base.go Vedi File

7
 
7
 
8
 	"go.uber.org/zap"
8
 	"go.uber.org/zap"
9
 
9
 
10
+	"github.com/9seconds/mtg/config"
10
 	"github.com/9seconds/mtg/conntypes"
11
 	"github.com/9seconds/mtg/conntypes"
11
 	"github.com/9seconds/mtg/utils"
12
 	"github.com/9seconds/mtg/utils"
12
 	"github.com/9seconds/mtg/wrappers/stream"
13
 	"github.com/9seconds/mtg/wrappers/stream"
29
 
30
 
30
 func (b *baseTelegram) dial(dc conntypes.DC,
31
 func (b *baseTelegram) dial(dc conntypes.DC,
31
 	protocol conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error) {
32
 	protocol conntypes.ConnectionProtocol) (conntypes.StreamReadWriteCloser, error) {
32
-	addresses := make([]string, 0, 2)
33
-
34
-	if protocol&conntypes.ConnectionProtocolIPv6 != 0 {
35
-		addresses = append(addresses, b.chooseAddress(b.v6Addresses, dc, b.v6DefaultDC))
36
-	}
37
-
38
-	if protocol&conntypes.ConnectionProtocolIPv4 != 0 {
39
-		addresses = append(addresses, b.chooseAddress(b.v4Addresses, dc, b.v4DefaultDC))
40
-	}
41
-
42
-	for _, addr := range addresses {
33
+	for _, addr := range b.getAddresses(dc, protocol) {
43
 		conn, err := b.dialer.Dial("tcp", addr)
34
 		conn, err := b.dialer.Dial("tcp", addr)
44
 		if err != nil {
35
 		if err != nil {
45
 			b.logger.Infow("Cannot dial to Telegram", "address", addr, "error", err)
36
 			b.logger.Infow("Cannot dial to Telegram", "address", addr, "error", err)
57
 	return nil, errors.New("cannot dial to the chosen DC")
48
 	return nil, errors.New("cannot dial to the chosen DC")
58
 }
49
 }
59
 
50
 
51
+func (b *baseTelegram) getAddresses(dc conntypes.DC, protocol conntypes.ConnectionProtocol) []string {
52
+	addresses := make([]string, 0, 2)
53
+	protos := []conntypes.ConnectionProtocol{
54
+		conntypes.ConnectionProtocolIPv6,
55
+		conntypes.ConnectionProtocolIPv4,
56
+	}
57
+
58
+	if config.C.PreferIP == config.PreferIPv4 {
59
+		protos[0], protos[1] = protos[1], protos[0]
60
+	}
61
+
62
+	for _, proto := range protos {
63
+		switch {
64
+		case proto&protocol == 0:
65
+		case proto&conntypes.ConnectionProtocolIPv6 != 0:
66
+			addresses = append(addresses, b.chooseAddress(b.v6Addresses, dc, b.v6DefaultDC))
67
+		case proto&conntypes.ConnectionProtocolIPv4 != 0:
68
+			addresses = append(addresses, b.chooseAddress(b.v4Addresses, dc, b.v4DefaultDC))
69
+		}
70
+	}
71
+
72
+	return addresses
73
+}
74
+
60
 func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
75
 func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
61
 	dc, defaultDC conntypes.DC) string {
76
 	dc, defaultDC conntypes.DC) string {
62
 	addrs, ok := addresses[dc]
77
 	addrs, ok := addresses[dc]

Loading…
Annulla
Salva