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

Fix rpc proxy request

tags/0.9
9seconds 7 лет назад
Родитель
Сommit
a274493e3b
2 измененных файлов: 18 добавлений и 26 удалений
  1. 0
    5
      mtproto/rpc/rpc.go
  2. 18
    21
      mtproto/rpc/rpc_proxy_request.go

+ 0
- 5
mtproto/rpc/rpc.go Просмотреть файл

@@ -4,8 +4,3 @@ const (
4 4
 	RPCNonceSeqNo     = -2
5 5
 	RPCHandshakeSeqNo = -1
6 6
 )
7
-
8
-type Extras struct {
9
-	QuickAck  bool
10
-	SimpleAck bool
11
-}

+ 18
- 21
mtproto/rpc/rpc_proxy_request.go Просмотреть файл

@@ -25,17 +25,17 @@ var (
25 25
 type RPCProxyRequest struct {
26 26
 	Flags        RPCProxyRequestFlags
27 27
 	ConnectionID [rpcProxyRequestConnectionIDLength]byte
28
-	RemoteIPPort [rpcProxyRequestIPPortLength]byte
29
-	LocalIPPort  [rpcProxyRequestIPPortLength]byte
28
+	OurIPPort    [rpcProxyRequestIPPortLength]byte
29
+	ClientIPPort [rpcProxyRequestIPPortLength]byte
30 30
 	ADTag        []byte
31
-	Extras       Extras
31
+	Options      *mtproto.ConnectionOpts
32 32
 }
33 33
 
34 34
 func (r *RPCProxyRequest) Bytes(message []byte) []byte {
35 35
 	buf := &bytes.Buffer{}
36 36
 
37 37
 	flags := r.Flags
38
-	if r.Extras.QuickAck {
38
+	if r.Options.QuickAck {
39 39
 		flags |= RPCProxyRequestFlagsQuickAck
40 40
 	}
41 41
 
@@ -46,25 +46,22 @@ func (r *RPCProxyRequest) Bytes(message []byte) []byte {
46 46
 	buf.Write(rpcProxyRequestTag)
47 47
 	buf.Write(flags.Bytes())
48 48
 	buf.Write(r.ConnectionID[:])
49
-	buf.Write(r.RemoteIPPort[:])
50
-	buf.Write(r.LocalIPPort[:])
49
+	buf.Write(r.ClientIPPort[:])
50
+	buf.Write(r.OurIPPort[:])
51 51
 	buf.Write(rpcProxyRequestExtraSize)
52 52
 	buf.Write(rpcProxyRequestProxyTag)
53 53
 	buf.WriteByte(byte(len(r.ADTag)))
54 54
 	buf.Write(r.ADTag)
55
-
56
-	for i := 0; i < (buf.Len() % 4); i++ {
57
-		buf.WriteByte(0x00)
58
-	}
55
+	buf.Write(bytes.Repeat([]byte{0x00}, buf.Len()%4))
59 56
 	buf.Write(message)
60 57
 
61 58
 	return buf.Bytes()
62 59
 }
63 60
 
64
-func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *net.TCPAddr, adTag []byte, extras Extras) (*RPCProxyRequest, error) {
61
+func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*RPCProxyRequest, error) {
65 62
 	flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
66 63
 
67
-	switch connectionType {
64
+	switch opts.ConnectionType {
68 65
 	case mtproto.ConnectionTypeAbridged:
69 66
 		flags |= RPCProxyRequestFlagsAbdridged
70 67
 	case mtproto.ConnectionTypeIntermediate:
@@ -72,9 +69,9 @@ func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *ne
72 69
 	}
73 70
 
74 71
 	request := RPCProxyRequest{
75
-		Flags:  flags,
76
-		ADTag:  adTag,
77
-		Extras: extras,
72
+		Flags:   flags,
73
+		ADTag:   adTag,
74
+		Options: opts,
78 75
 	}
79 76
 
80 77
 	if _, err := rand.Read(request.ConnectionID[:]); err != nil {
@@ -82,13 +79,13 @@ func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *ne
82 79
 	}
83 80
 
84 81
 	port := make([]byte, 4)
85
-	copy(request.LocalIPPort[:], local.IP.To16())
86
-	binary.LittleEndian.PutUint32(port, uint32(local.Port))
87
-	copy(request.LocalIPPort[16:], port)
82
+	copy(request.ClientIPPort[:16], clientAddr.IP.To16())
83
+	binary.LittleEndian.PutUint32(port, uint32(clientAddr.Port))
84
+	copy(request.ClientIPPort[16:], port)
88 85
 
89
-	copy(request.RemoteIPPort[:], remote.IP.To16())
90
-	binary.LittleEndian.PutUint32(port, uint32(remote.Port))
91
-	copy(request.RemoteIPPort[16:], port)
86
+	copy(request.OurIPPort[:16], ownAddr.IP.To16())
87
+	binary.LittleEndian.PutUint32(port, uint32(ownAddr.Port))
88
+	copy(request.OurIPPort[16:], port)
92 89
 
93 90
 	return &request, nil
94 91
 }

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