소스 검색

Propagate connection protocol

tags/0.9
9seconds 7 년 전
부모
커밋
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
 	if err != nil {
35
 	if err != nil {
36
 		return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
36
 		return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
37
 	}
37
 	}
38
+	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
38
 
39
 
39
 	socket := wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor)
40
 	socket := wrappers.NewStreamCipherRWC(conn, obfs2.Encryptor, obfs2.Decryptor)
40
 
41
 

+ 0
- 1
main.go 파일 보기

16
 
16
 
17
 	"github.com/9seconds/mtg/config"
17
 	"github.com/9seconds/mtg/config"
18
 	"github.com/9seconds/mtg/proxy"
18
 	"github.com/9seconds/mtg/proxy"
19
-	"github.com/9seconds/mtg/telegram"
20
 	"github.com/juju/errors"
19
 	"github.com/juju/errors"
21
 )
20
 )
22
 
21
 

+ 11
- 2
mtproto/connection_options.go 파일 보기

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

+ 1
- 1
telegram/direct.go 파일 보기

41
 		dc = 1
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
 func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {
47
 func (t *directTelegram) Init(connOpts *mtproto.ConnectionOpts, conn io.ReadWriteCloser) (io.ReadWriteCloser, error) {

+ 1
- 1
telegram/middle.go 파일 보기

14
 	middleTelegramCaller
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
 	tg := &middleTelegram{
18
 	tg := &middleTelegram{
19
 		middleTelegramCaller: middleTelegramCaller{
19
 		middleTelegramCaller: middleTelegramCaller{
20
 			baseTelegram: baseTelegram{
20
 			baseTelegram: baseTelegram{

+ 1
- 1
telegram/middle_caller.go 파일 보기

47
 	t.dialerMutex.RLock()
47
 	t.dialerMutex.RLock()
48
 	defer t.dialerMutex.RUnlock()
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
 func (t *middleTelegramCaller) autoUpdate() {
53
 func (t *middleTelegramCaller) autoUpdate() {

+ 10
- 5
telegram/telegram.go 파일 보기

24
 	v6Addresses map[int16][]string
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
 	addrs := make([]string, 2)
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
 	for _, addr := range addrs {
41
 	for _, addr := range addrs {

Loading…
취소
저장