Преглед изворни кода

Add rpc request/response for handshake

tags/0.9
9seconds пре 7 година
родитељ
комит
b9ee1e1857

+ 8
- 1
mtproto/rpc/rpc.go Прегледај датотеку

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

+ 47
- 0
mtproto/rpc/rpc_handshake_request.go Прегледај датотеку

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 Прегледај датотеку

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

+ 2
- 2
mtproto/rpc/rpc_nonce_response.go Прегледај датотеку

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

Loading…
Откажи
Сачувај