|
|
@@ -7,24 +7,37 @@ import (
|
|
7
|
7
|
"github.com/juju/errors"
|
|
8
|
8
|
)
|
|
9
|
9
|
|
|
10
|
|
-var telegramDCIPs = [5]string{
|
|
11
|
|
- "149.154.175.50:443",
|
|
12
|
|
- "149.154.167.51:443",
|
|
13
|
|
- "149.154.175.100:443",
|
|
14
|
|
- "149.154.167.91:443",
|
|
15
|
|
- "149.154.171.5:443",
|
|
|
10
|
+type TelegramAddress struct {
|
|
|
11
|
+ v4 string
|
|
|
12
|
+ v6 string
|
|
16
|
13
|
}
|
|
17
|
14
|
|
|
|
15
|
+func (t *TelegramAddress) IPv4() string {
|
|
|
16
|
+ return net.JoinHostPort(t.v4, telegramPort)
|
|
|
17
|
+}
|
|
|
18
|
+
|
|
|
19
|
+func (t *TelegramAddress) IPv6() string {
|
|
|
20
|
+ return net.JoinHostPort(t.v6, telegramPort)
|
|
|
21
|
+}
|
|
|
22
|
+
|
|
|
23
|
+var TelegramAddresses = []TelegramAddress{
|
|
|
24
|
+ TelegramAddress{v4: "149.154.175.50", v6: "2001:b28:f23d:f001::a"},
|
|
|
25
|
+ TelegramAddress{v4: "149.154.167.51", v6: "2001:67c:04e8:f002::a"},
|
|
|
26
|
+ TelegramAddress{v4: "149.154.175.100", v6: "2001:b28:f23d:f003::a"},
|
|
|
27
|
+ TelegramAddress{v4: "149.154.167.91", v6: "2001:67c:04e8:f004::a"},
|
|
|
28
|
+ TelegramAddress{v4: "149.154.171.5", v6: "2001:b28:f23f:f005::a"},
|
|
|
29
|
+}
|
|
|
30
|
+
|
|
|
31
|
+const telegramPort = "443"
|
|
|
32
|
+
|
|
18
|
33
|
const telegramKeepAlive = 30 * time.Second
|
|
19
|
34
|
|
|
20
|
|
-func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
|
|
35
|
+func dialToTelegram(ipv6 bool, dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
|
21
|
36
|
if dcIdx < 0 || dcIdx >= 5 {
|
|
22
|
37
|
return nil, errors.New("Incorrect DC IDX")
|
|
23
|
38
|
}
|
|
24
|
39
|
|
|
25
|
|
- dialer := net.Dialer{Timeout: timeout}
|
|
26
|
|
- rawConn, err := dialer.Dial("tcp", telegramDCIPs[dcIdx])
|
|
27
|
|
- conn := rawConn.(*net.TCPConn)
|
|
|
40
|
+ conn, err := doDial(ipv6, dcIdx, timeout)
|
|
28
|
41
|
if err != nil {
|
|
29
|
42
|
return nil, errors.Annotate(err, "Cannot dial")
|
|
30
|
43
|
}
|
|
|
@@ -38,3 +51,20 @@ func dialToTelegram(dcIdx int16, timeout time.Duration) (net.Conn, error) {
|
|
38
|
51
|
|
|
39
|
52
|
return conn, nil
|
|
40
|
53
|
}
|
|
|
54
|
+
|
|
|
55
|
+func doDial(ipv6 bool, dcIdx int16, timeout time.Duration) (*net.TCPConn, error) {
|
|
|
56
|
+ dialer := net.Dialer{Timeout: timeout}
|
|
|
57
|
+ addr := TelegramAddresses[dcIdx]
|
|
|
58
|
+
|
|
|
59
|
+ if ipv6 {
|
|
|
60
|
+ if conn, err := dialer.Dial("tcp", addr.IPv6()); err == nil {
|
|
|
61
|
+ return conn.(*net.TCPConn), nil
|
|
|
62
|
+ }
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ conn, err := dialer.Dial("tcp", addr.IPv4())
|
|
|
66
|
+ if err == nil {
|
|
|
67
|
+ return conn.(*net.TCPConn), nil
|
|
|
68
|
+ }
|
|
|
69
|
+ return nil, err
|
|
|
70
|
+}
|