Procházet zdrojové kódy

Prefer ipv6 setting

tags/0.9
9seconds před 8 roky
rodič
revize
8670a56e39
3 změnil soubory, kde provedl 49 přidání a 13 odebrání
  1. 5
    1
      main.go
  2. 4
    2
      proxy/server.go
  3. 40
    10
      proxy/telegram.go

+ 5
- 1
main.go Zobrazit soubor

67
 		Short('s').
67
 		Short('s').
68
 		Envar("MTG_SERVER").
68
 		Envar("MTG_SERVER").
69
 		String()
69
 		String()
70
+	preferIPv6 = app.Flag("prefer-ipv6", "Use IPv6").
71
+			Short('6').
72
+			Envar("MTG_USE_IPV6").
73
+			Bool()
70
 
74
 
71
 	secret = app.Arg("secret", "Secret of this proxy.").String()
75
 	secret = app.Arg("secret", "Secret of this proxy.").String()
72
 )
76
 )
117
 	go stat.Serve(*statsIP, *statsPort)
121
 	go stat.Serve(*statsIP, *statsPort)
118
 
122
 
119
 	srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
123
 	srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
120
-		*readTimeout, *writeTimeout, stat)
124
+		*readTimeout, *writeTimeout, *preferIPv6, stat)
121
 	if err := srv.Serve(); err != nil {
125
 	if err := srv.Serve(); err != nil {
122
 		logger.Fatal(err.Error())
126
 		logger.Fatal(err.Error())
123
 	}
127
 	}

+ 4
- 2
proxy/server.go Zobrazit soubor

26
 	readTimeout  time.Duration
26
 	readTimeout  time.Duration
27
 	writeTimeout time.Duration
27
 	writeTimeout time.Duration
28
 	stats        *Stats
28
 	stats        *Stats
29
+	ipv6         bool
29
 }
30
 }
30
 
31
 
31
 func (s *Server) Serve() error {
32
 func (s *Server) Serve() error {
124
 }
125
 }
125
 
126
 
126
 func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, error) {
127
 func (s *Server) getTelegramStream(dc int16, ctx context.Context, cancel context.CancelFunc, socketID string) (io.ReadWriteCloser, error) {
127
-	socket, err := dialToTelegram(dc, s.readTimeout)
128
+	socket, err := dialToTelegram(s.ipv6, dc, s.readTimeout)
128
 	if err != nil {
129
 	if err != nil {
129
 		return nil, errors.Annotate(err, "Cannot dial")
130
 		return nil, errors.Annotate(err, "Cannot dial")
130
 	}
131
 	}
149
 }
150
 }
150
 
151
 
151
 func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
152
 func NewServer(ip net.IP, port int, secret []byte, logger *zap.SugaredLogger,
152
-	readTimeout, writeTimeout time.Duration, stat *Stats) *Server {
153
+	readTimeout, writeTimeout time.Duration, ipv6 bool, stat *Stats) *Server {
153
 	return &Server{
154
 	return &Server{
154
 		ip:           ip,
155
 		ip:           ip,
155
 		port:         port,
156
 		port:         port,
159
 		readTimeout:  readTimeout,
160
 		readTimeout:  readTimeout,
160
 		writeTimeout: writeTimeout,
161
 		writeTimeout: writeTimeout,
161
 		stats:        stat,
162
 		stats:        stat,
163
+		ipv6:         ipv6,
162
 	}
164
 	}
163
 }
165
 }

+ 40
- 10
proxy/telegram.go Zobrazit soubor

7
 	"github.com/juju/errors"
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
 const telegramKeepAlive = 30 * time.Second
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
 	if dcIdx < 0 || dcIdx >= 5 {
36
 	if dcIdx < 0 || dcIdx >= 5 {
22
 		return nil, errors.New("Incorrect DC IDX")
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
 	if err != nil {
41
 	if err != nil {
29
 		return nil, errors.Annotate(err, "Cannot dial")
42
 		return nil, errors.Annotate(err, "Cannot dial")
30
 	}
43
 	}
38
 
51
 
39
 	return conn, nil
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
+}

Načítá se…
Zrušit
Uložit