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

Propagate connection protocol

tags/0.9
9seconds 7 лет назад
Родитель
Сommit
f9a2b0513d
7 измененных файлов: 25 добавлений и 11 удалений
  1. 1
    0
      client/direct.go
  2. 0
    1
      main.go
  3. 11
    2
      mtproto/connection_options.go
  4. 1
    1
      telegram/direct.go
  5. 1
    1
      telegram/middle.go
  6. 1
    1
      telegram/middle_caller.go
  7. 10
    5
      telegram/telegram.go

+ 1
- 0
client/direct.go Просмотреть файл

@@ -35,6 +35,7 @@ func DirectInit(conn net.Conn, conf *config.Config) (*mtproto.ConnectionOpts, io
35 35
 	if err != nil {
36 36
 		return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
37 37
 	}
38
+	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
38 39
 
39 40
 	socket := wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor)
40 41
 

+ 0
- 1
main.go Просмотреть файл

@@ -16,7 +16,6 @@ import (
16 16
 
17 17
 	"github.com/9seconds/mtg/config"
18 18
 	"github.com/9seconds/mtg/proxy"
19
-	"github.com/9seconds/mtg/telegram"
20 19
 	"github.com/juju/errors"
21 20
 )
22 21
 

+ 11
- 2
mtproto/connection_options.go Просмотреть файл

@@ -10,11 +10,14 @@ import (
10 10
 // by the user.
11 11
 type ConnectionType uint8
12 12
 
13
+type ConnectionProtocol uint8
14
+
13 15
 // ConnectionOpts presents an options, metadata on connection requested
14 16
 // by the user on handshake.
15 17
 type ConnectionOpts struct {
16
-	DC             int16
17
-	ConnectionType ConnectionType
18
+	DC              int16
19
+	ConnectionType  ConnectionType
20
+	ConnectionProto ConnectionProtocol
18 21
 }
19 22
 
20 23
 // Different connection types which user requests from Telegram.
@@ -24,6 +27,12 @@ const (
24 27
 	ConnectionTypeIntermediate
25 28
 )
26 29
 
30
+const (
31
+	ConnectionProtocolIPv4 ConnectionProtocol = 1
32
+	ConnectionProtocolIPv6                    = ConnectionProtocolIPv4 << 1
33
+	ConnectionProtocolAny                     = ConnectionProtocolIPv4 | ConnectionProtocolIPv6
34
+)
35
+
27 36
 // Connection tags for mtproto handshakes.
28 37
 var (
29 38
 	ConnectionTagAbridged     = []byte{0xef, 0xef, 0xef, 0xef}

+ 1
- 1
telegram/direct.go Просмотреть файл

@@ -41,7 +41,7 @@ func (t *directTelegram) Dial(connOpts *mtproto.ConnectionOpts) (io.ReadWriteClo
41 41
 		dc = 1
42 42
 	}
43 43
 
44
-	return t.baseTelegram.dial(dc - 1)
44
+	return t.baseTelegram.dial(dc-1, connOpts.ConnectionProto)
45 45
 }
46 46
 
47 47
 func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {

+ 1
- 1
telegram/middle.go Просмотреть файл

@@ -14,7 +14,7 @@ type middleTelegram struct {
14 14
 	middleTelegramCaller
15 15
 }
16 16
 
17
-func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) Telegram {
17
+func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) *middleTelegram {
18 18
 	tg := &middleTelegram{
19 19
 		middleTelegramCaller: middleTelegramCaller{
20 20
 			baseTelegram: baseTelegram{

+ 1
- 1
telegram/middle_caller.go Просмотреть файл

@@ -47,7 +47,7 @@ func (t *middleTelegramCaller) Dial(connOpts *mtproto.ConnectionOpts) (io.ReadWr
47 47
 	t.dialerMutex.RLock()
48 48
 	defer t.dialerMutex.RUnlock()
49 49
 
50
-	return t.baseTelegram.dial(dc)
50
+	return t.baseTelegram.dial(dc, connOpts.ConnectionProto)
51 51
 }
52 52
 
53 53
 func (t *middleTelegramCaller) autoUpdate() {

+ 10
- 5
telegram/telegram.go Просмотреть файл

@@ -24,13 +24,18 @@ type baseTelegram struct {
24 24
 	v6Addresses map[int16][]string
25 25
 }
26 26
 
27
-func (b *baseTelegram) dial(dcIdx int16) (io.ReadWriteCloser, error) {
27
+func (b *baseTelegram) dial(dcIdx int16, proto mtproto.ConnectionProtocol) (io.ReadWriteCloser, error) {
28 28
 	addrs := make([]string, 2)
29
-	if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
30
-		addrs = append(addrs, addr[rand.Intn(len(addr))])
29
+
30
+	if proto&mtproto.ConnectionProtocolIPv6 != 0 {
31
+		if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
32
+			addrs = append(addrs, addr[rand.Intn(len(addr))])
33
+		}
31 34
 	}
32
-	if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
33
-		addrs = append(addrs, addr[rand.Intn(len(addr))])
35
+	if proto&mtproto.ConnectionProtocolIPv4 != 0 {
36
+		if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
37
+			addrs = append(addrs, addr[rand.Intn(len(addr))])
38
+		}
34 39
 	}
35 40
 
36 41
 	for _, addr := range addrs {

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