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

Add rpc request/response for handshake

tags/0.9
9seconds 7 лет назад
Родитель
Сommit
b9ee1e1857

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

@@ -1,5 +1,12 @@
1 1
 package rpc
2 2
 
3
+import "bytes"
4
+
5
+const (
6
+	RPCNonceSeqNo     = -2
7
+	RPCHandshakeSeqNo = -1
8
+)
9
+
3 10
 type RPC interface {
4
-	Bytes() []byte
11
+	Bytes() *bytes.Buffer
5 12
 }

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

@@ -0,0 +1,47 @@
1
+package rpc
2
+
3
+import "bytes"
4
+
5
+const (
6
+	rpcHandshakeTagLength       = 4
7
+	rpcHandshakeFlagsLength     = 4
8
+	rpcHandshakeSenderPIDLength = 12
9
+	rpcHandshakePeerPIDLength   = rpcHandshakeSenderPIDLength
10
+
11
+	rpcHandshakeRequestLength = rpcHandshakeTagLength + rpcHandshakeFlagsLength +
12
+		rpcHandshakeSenderPIDLength + rpcHandshakePeerPIDLength
13
+)
14
+
15
+var (
16
+	rpcHandshakeSenderPID [rpcHandshakeSenderPIDLength]byte
17
+	rpcHandshakePeerPID   [rpcHandshakePeerPIDLength]byte
18
+
19
+	rpcHandshakeTag   = [rpcHandshakeTagLength]byte{0xf5, 0xee, 0x82, 0x76}
20
+	rpcHandshakeFlags = [rpcHandshakeFlagsLength]byte{0x00, 0x00, 0x00, 0x00}
21
+
22
+	rpcHandshakeBuffer *bytes.Buffer
23
+)
24
+
25
+type RPCHandshakeRequest struct {
26
+}
27
+
28
+func (r *RPCHandshakeRequest) Bytes() *bytes.Buffer {
29
+	buf := &bytes.Buffer{}
30
+	buf.Grow(rpcHandshakeRequestLength)
31
+
32
+	buf.Write(rpcHandshakeTag[:])
33
+	buf.Write(rpcHandshakeFlags[:])
34
+	buf.Write(rpcHandshakeSenderPID[:])
35
+	buf.Write(rpcHandshakePeerPID[:])
36
+
37
+	return buf
38
+}
39
+
40
+func init() {
41
+	copy(rpcHandshakeSenderPID[:], "IPIPPRPDTIME")
42
+	copy(rpcHandshakePeerPID[:], "IPIPPRPDTIME")
43
+}
44
+
45
+func NewRPCHandshakeRequest() *RPCHandshakeRequest {
46
+	return &RPCHandshakeRequest{}
47
+}

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

@@ -0,0 +1,53 @@
1
+package rpc
2
+
3
+import (
4
+	"bytes"
5
+
6
+	"github.com/juju/errors"
7
+)
8
+
9
+const rpcHandshakeResponseLength = rpcHandshakeRequestLength
10
+
11
+type RPCHandshakeResponse struct {
12
+	Type      [rpcHandshakeTagLength]byte
13
+	Flags     [rpcHandshakeFlagsLength]byte
14
+	SenderPID [rpcHandshakeSenderPIDLength]byte
15
+	PeerPID   [rpcHandshakePeerPIDLength]byte
16
+}
17
+
18
+func (r *RPCHandshakeResponse) Bytes() *bytes.Buffer {
19
+	buf := &bytes.Buffer{}
20
+	buf.Grow(rpcHandshakeResponseLength)
21
+
22
+	buf.Write(r.Type[:])
23
+	buf.Write(r.Flags[:])
24
+	buf.Write(r.SenderPID[:])
25
+	buf.Write(r.PeerPID[:])
26
+
27
+	return buf
28
+}
29
+
30
+func (r *RPCHandshakeResponse) Valid(req *RPCHandshakeRequest) error {
31
+	if r.Type != rpcHandshakeTag {
32
+		return errors.New("Unexpected handshake tag")
33
+	}
34
+	if r.PeerPID != rpcHandshakeSenderPID {
35
+		return errors.New("Incorrect sender PID")
36
+	}
37
+
38
+	return nil
39
+}
40
+
41
+func NewRPCHandshakeResponse(data []byte) (*RPCHandshakeResponse, error) {
42
+	if len(data) != rpcHandshakeResponseLength {
43
+		return nil, errors.New("Incorrect handshake response length")
44
+	}
45
+
46
+	resp := RPCHandshakeResponse{}
47
+	copy(resp.Type[:], data[:4])
48
+	copy(resp.Flags[:], data[4:8])
49
+	copy(resp.SenderPID[:], data[8:20])
50
+	copy(resp.PeerPID[:], data[20:])
51
+
52
+	return &resp, nil
53
+}

+ 2
- 4
mtproto/rpc/rpc_nonce_request.go Просмотреть файл

@@ -10,8 +10,6 @@ import (
10 10
 )
11 11
 
12 12
 const (
13
-	RPCNonceSeqNo = -2
14
-
15 13
 	rpcNonceLength            = 16
16 14
 	rpcNonceKeySelectorLength = 4
17 15
 	rpcNonceCryptoTSLength    = 4
@@ -33,7 +31,7 @@ type RPCNonceRequest struct {
33 31
 	Nonce       [rpcNonceLength]byte
34 32
 }
35 33
 
36
-func (r *RPCNonceRequest) Bytes() []byte {
34
+func (r *RPCNonceRequest) Bytes() *bytes.Buffer {
37 35
 	buf := &bytes.Buffer{}
38 36
 	buf.Grow(rpcNonceRequestLength)
39 37
 
@@ -43,7 +41,7 @@ func (r *RPCNonceRequest) Bytes() []byte {
43 41
 	buf.Write(r.CryptoTS[:])
44 42
 	buf.Write(r.Nonce[:])
45 43
 
46
-	return buf.Bytes()
44
+	return buf
47 45
 }
48 46
 
49 47
 func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {

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

@@ -15,7 +15,7 @@ type RPCNonceResponse struct {
15 15
 	Crypto  [rpcNonceCryptoAESLength]byte
16 16
 }
17 17
 
18
-func (r *RPCNonceResponse) Bytes() []byte {
18
+func (r *RPCNonceResponse) Bytes() *bytes.Buffer {
19 19
 	buf := &bytes.Buffer{}
20 20
 	buf.Grow(rpcNonceResponseLength)
21 21
 
@@ -25,7 +25,7 @@ func (r *RPCNonceResponse) Bytes() []byte {
25 25
 	buf.Write(r.CryptoTS[:])
26 26
 	buf.Write(r.Nonce[:])
27 27
 
28
-	return buf.Bytes()
28
+	return buf
29 29
 }
30 30
 
31 31
 func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {

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