mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:34:02 +03:00
Remove borsh borsh new borsch
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type HandshakeRequest struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *HandshakeRequest) Bytes() []byte {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
buf.Write(TagHandshake)
|
||||||
|
buf.Write(HandshakeFlags)
|
||||||
|
buf.Write(HandshakeSenderPID)
|
||||||
|
buf.Write(HandshakePeerPID)
|
||||||
|
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandshakeRequest() *HandshakeRequest {
|
||||||
|
return &HandshakeRequest{}
|
||||||
|
}
|
||||||
@@ -6,14 +6,14 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RPCHandshakeResponse struct {
|
type HandshakeResponse struct {
|
||||||
Type []byte
|
Type []byte
|
||||||
Flags []byte
|
Flags []byte
|
||||||
SenderPID []byte
|
SenderPID []byte
|
||||||
PeerPID []byte
|
PeerPID []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCHandshakeResponse) Bytes() []byte {
|
func (r *HandshakeResponse) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
buf.Write(r.Type[:])
|
buf.Write(r.Type[:])
|
||||||
@@ -24,23 +24,23 @@ func (r *RPCHandshakeResponse) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCHandshakeResponse) Valid(req *RPCHandshakeRequest) error {
|
func (r *HandshakeResponse) Valid(req *HandshakeRequest) error {
|
||||||
if !bytes.Equal(r.Type, RPCTagHandshake) {
|
if !bytes.Equal(r.Type, TagHandshake) {
|
||||||
return errors.New("Unexpected handshake tag")
|
return errors.New("Unexpected handshake tag")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(r.PeerPID, RPCHandshakeSenderPID) {
|
if !bytes.Equal(r.PeerPID, HandshakeSenderPID) {
|
||||||
return errors.New("Incorrect sender PID")
|
return errors.New("Incorrect sender PID")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPCHandshakeResponse(data []byte) (*RPCHandshakeResponse, error) {
|
func NewHandshakeResponse(data []byte) (*HandshakeResponse, error) {
|
||||||
if len(data) != 32 {
|
if len(data) != 32 {
|
||||||
return nil, errors.New("Incorrect handshake response length")
|
return nil, errors.New("Incorrect handshake response length")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RPCHandshakeResponse{
|
return &HandshakeResponse{
|
||||||
Type: data[:4],
|
Type: data[:4],
|
||||||
Flags: data[4:8],
|
Flags: data[4:8],
|
||||||
SenderPID: data[8:20],
|
SenderPID: data[8:20],
|
||||||
@@ -9,25 +9,25 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RPCNonceRequest struct {
|
type NonceRequest struct {
|
||||||
KeySelector []byte
|
KeySelector []byte
|
||||||
CryptoTS []byte
|
CryptoTS []byte
|
||||||
Nonce []byte
|
Nonce []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCNonceRequest) Bytes() []byte {
|
func (r *NonceRequest) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
buf.Write(RPCTagNonce)
|
buf.Write(TagNonce)
|
||||||
buf.Write(r.KeySelector)
|
buf.Write(r.KeySelector)
|
||||||
buf.Write(RPCNonceCryptoAES)
|
buf.Write(NonceCryptoAES)
|
||||||
buf.Write(r.CryptoTS)
|
buf.Write(r.CryptoTS)
|
||||||
buf.Write(r.Nonce)
|
buf.Write(r.Nonce)
|
||||||
|
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
|
func NewNonceRequest(proxySecret []byte) (*NonceRequest, error) {
|
||||||
nonce := make([]byte, 16)
|
nonce := make([]byte, 16)
|
||||||
keySelector := make([]byte, 4)
|
keySelector := make([]byte, 4)
|
||||||
cryptoTS := make([]byte, 4)
|
cryptoTS := make([]byte, 4)
|
||||||
@@ -40,7 +40,7 @@ func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
|
|||||||
timestamp := time.Now().Truncate(time.Second).Unix() % 4294967296 // 256 ^ 4 - do not know how to name
|
timestamp := time.Now().Truncate(time.Second).Unix() % 4294967296 // 256 ^ 4 - do not know how to name
|
||||||
binary.LittleEndian.PutUint32(cryptoTS, uint32(timestamp))
|
binary.LittleEndian.PutUint32(cryptoTS, uint32(timestamp))
|
||||||
|
|
||||||
return &RPCNonceRequest{
|
return &NonceRequest{
|
||||||
KeySelector: keySelector,
|
KeySelector: keySelector,
|
||||||
CryptoTS: cryptoTS,
|
CryptoTS: cryptoTS,
|
||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
@@ -6,17 +6,17 @@ import (
|
|||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RPCNonceResponse struct {
|
type NonceResponse struct {
|
||||||
RPCNonceRequest
|
NonceRequest
|
||||||
|
|
||||||
RPCType []byte
|
Type []byte
|
||||||
Crypto []byte
|
Crypto []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCNonceResponse) Bytes() []byte {
|
func (r *NonceResponse) Bytes() []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
buf.Write(r.RPCType)
|
buf.Write(r.Type)
|
||||||
buf.Write(r.KeySelector)
|
buf.Write(r.KeySelector)
|
||||||
buf.Write(r.Crypto)
|
buf.Write(r.Crypto)
|
||||||
buf.Write(r.CryptoTS)
|
buf.Write(r.CryptoTS)
|
||||||
@@ -25,11 +25,11 @@ func (r *RPCNonceResponse) Bytes() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
|
func (r *NonceResponse) Valid(req *NonceRequest) error {
|
||||||
if !bytes.Equal(r.RPCType, RPCTagNonce) {
|
if !bytes.Equal(r.Type, TagNonce) {
|
||||||
return errors.New("Unexpected RPC type")
|
return errors.New("Unexpected RPC type")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(r.Crypto, RPCNonceCryptoAES) {
|
if !bytes.Equal(r.Crypto, NonceCryptoAES) {
|
||||||
return errors.New("Unexpected crypto type")
|
return errors.New("Unexpected crypto type")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(r.KeySelector, req.KeySelector) {
|
if !bytes.Equal(r.KeySelector, req.KeySelector) {
|
||||||
@@ -39,18 +39,18 @@ func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPCNonceResponse(data []byte) (*RPCNonceResponse, error) {
|
func NewNonceResponse(data []byte) (*NonceResponse, error) {
|
||||||
if len(data) != 32 {
|
if len(data) != 32 {
|
||||||
return nil, errors.New("Unexpected message length")
|
return nil, errors.New("Unexpected message length")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RPCNonceResponse{
|
return &NonceResponse{
|
||||||
RPCNonceRequest: RPCNonceRequest{
|
NonceRequest: NonceRequest{
|
||||||
KeySelector: data[4:8],
|
KeySelector: data[4:8],
|
||||||
CryptoTS: data[12:16],
|
CryptoTS: data[12:16],
|
||||||
Nonce: data[16:],
|
Nonce: data[16:],
|
||||||
},
|
},
|
||||||
RPCType: data[:4],
|
Type: data[:4],
|
||||||
Crypto: data[8:12],
|
Crypto: data[8:12],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
type proxyRequestFlags uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
proxyRequestFlagsHasAdTag proxyRequestFlags = 0x8
|
||||||
|
proxyRequestFlagsEncrypted = 0x2
|
||||||
|
proxyRequestFlagsMagic = 0x1000
|
||||||
|
proxyRequestFlagsExtMode2 = 0x20000
|
||||||
|
proxyRequestFlagsIntermediate = 0x20000000
|
||||||
|
proxyRequestFlagsAbdridged = 0x40000000
|
||||||
|
proxyRequestFlagsQuickAck = 0x80000000
|
||||||
|
)
|
||||||
|
|
||||||
|
var proxyRequestFlagsEncryptedPrefix [8]byte
|
||||||
|
|
||||||
|
func (r proxyRequestFlags) Bytes() []byte {
|
||||||
|
converted := make([]byte, 4)
|
||||||
|
binary.LittleEndian.PutUint32(converted, uint32(r))
|
||||||
|
|
||||||
|
return converted
|
||||||
|
}
|
||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/9seconds/mtg/mtproto"
|
"github.com/9seconds/mtg/mtproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RPCProxyRequest struct {
|
type ProxyRequest struct {
|
||||||
Flags RPCProxyRequestFlags
|
Flags proxyRequestFlags
|
||||||
ConnectionID []byte
|
ConnectionID []byte
|
||||||
OurIPPort []byte
|
OurIPPort []byte
|
||||||
ClientIPPort []byte
|
ClientIPPort []byte
|
||||||
@@ -20,25 +20,25 @@ type RPCProxyRequest struct {
|
|||||||
Options *mtproto.ConnectionOpts
|
Options *mtproto.ConnectionOpts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCProxyRequest) Bytes(message []byte) []byte {
|
func (r *ProxyRequest) Bytes(message []byte) []byte {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
flags := r.Flags
|
flags := r.Flags
|
||||||
if r.Options.QuickAck {
|
if r.Options.QuickAck {
|
||||||
flags |= RPCProxyRequestFlagsQuickAck
|
flags |= proxyRequestFlagsQuickAck
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.HasPrefix(message, rpcProxyRequestFlagsEncryptedPrefix[:]) {
|
if bytes.HasPrefix(message, proxyRequestFlagsEncryptedPrefix[:]) {
|
||||||
flags |= RPCProxyRequestFlagsEncrypted
|
flags |= proxyRequestFlagsEncrypted
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.Write(RPCTagProxyRequest)
|
buf.Write(TagProxyRequest)
|
||||||
buf.Write(flags.Bytes())
|
buf.Write(flags.Bytes())
|
||||||
buf.Write(r.ConnectionID[:])
|
buf.Write(r.ConnectionID[:])
|
||||||
buf.Write(r.ClientIPPort[:])
|
buf.Write(r.ClientIPPort[:])
|
||||||
buf.Write(r.OurIPPort[:])
|
buf.Write(r.OurIPPort[:])
|
||||||
buf.Write(RPCProxyRequestExtraSize)
|
buf.Write(ProxyRequestExtraSize)
|
||||||
buf.Write(RPCProxyRequestProxyTag)
|
buf.Write(ProxyRequestProxyTag)
|
||||||
buf.WriteByte(byte(len(r.ADTag)))
|
buf.WriteByte(byte(len(r.ADTag)))
|
||||||
buf.Write(r.ADTag)
|
buf.Write(r.ADTag)
|
||||||
buf.Write(bytes.Repeat([]byte{0x00}, buf.Len()%4))
|
buf.Write(bytes.Repeat([]byte{0x00}, buf.Len()%4))
|
||||||
@@ -47,17 +47,17 @@ func (r *RPCProxyRequest) Bytes(message []byte) []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*RPCProxyRequest, error) {
|
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
|
||||||
flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
|
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
|
||||||
|
|
||||||
switch opts.ConnectionType {
|
switch opts.ConnectionType {
|
||||||
case mtproto.ConnectionTypeAbridged:
|
case mtproto.ConnectionTypeAbridged:
|
||||||
flags |= RPCProxyRequestFlagsAbdridged
|
flags |= proxyRequestFlagsAbdridged
|
||||||
case mtproto.ConnectionTypeIntermediate:
|
case mtproto.ConnectionTypeIntermediate:
|
||||||
flags |= RPCProxyRequestFlagsIntermediate
|
flags |= proxyRequestFlagsIntermediate
|
||||||
}
|
}
|
||||||
|
|
||||||
request := RPCProxyRequest{
|
request := &ProxyRequest{
|
||||||
Flags: flags,
|
Flags: flags,
|
||||||
ADTag: adTag,
|
ADTag: adTag,
|
||||||
Options: opts,
|
Options: opts,
|
||||||
@@ -79,5 +79,5 @@ func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.Connecti
|
|||||||
binary.LittleEndian.PutUint32(port[:], uint32(ownAddr.Port))
|
binary.LittleEndian.PutUint32(port[:], uint32(ownAddr.Port))
|
||||||
copy(request.OurIPPort[16:], port[:])
|
copy(request.OurIPPort[16:], port[:])
|
||||||
|
|
||||||
return &request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
+16
-16
@@ -1,30 +1,30 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RPCNonceSeqNo = -2
|
SeqNoNonce = -2
|
||||||
RPCHandshakeSeqNo = -1
|
SeqNoHandshake = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
RPCTagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
|
TagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
|
||||||
RPCTagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
|
TagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
|
||||||
RPCTagSimpleAck = []byte{0x9b, 0x40, 0xac, 0x3b}
|
TagSimpleAck = []byte{0x9b, 0x40, 0xac, 0x3b}
|
||||||
RPCTagHandshake = []byte{0xf5, 0xee, 0x82, 0x76}
|
TagHandshake = []byte{0xf5, 0xee, 0x82, 0x76}
|
||||||
RPCTagNonce = []byte{0xaa, 0x87, 0xcb, 0x7a}
|
TagNonce = []byte{0xaa, 0x87, 0xcb, 0x7a}
|
||||||
RPCTagProxyRequest = []byte{0xee, 0xf1, 0xce, 0x36}
|
TagProxyRequest = []byte{0xee, 0xf1, 0xce, 0x36}
|
||||||
|
|
||||||
RPCNonceCryptoAES = []byte{0x01, 0x00, 0x00, 0x00}
|
NonceCryptoAES = []byte{0x01, 0x00, 0x00, 0x00}
|
||||||
|
|
||||||
RPCHandshakeFlags = []byte{0x00, 0x00, 0x00, 0x00}
|
HandshakeFlags = []byte{0x00, 0x00, 0x00, 0x00}
|
||||||
|
|
||||||
RPCProxyRequestExtraSize = []byte{0x18, 0x00, 0x00, 0x00}
|
ProxyRequestExtraSize = []byte{0x18, 0x00, 0x00, 0x00}
|
||||||
RPCProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
|
ProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
|
||||||
|
|
||||||
RPCHandshakeSenderPID = []byte{}
|
HandshakeSenderPID []byte
|
||||||
RPCHandshakePeerPID = []byte{}
|
HandshakePeerPID []byte
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RPCHandshakeSenderPID = []byte("IPIPPRPDTIME")
|
HandshakeSenderPID = []byte("IPIPPRPDTIME")
|
||||||
RPCHandshakePeerPID = []byte("IPIPPRPDTIME")
|
HandshakePeerPID = []byte("IPIPPRPDTIME")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package rpc
|
|
||||||
|
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type RPCHandshakeRequest struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RPCHandshakeRequest) Bytes() []byte {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
|
|
||||||
buf.Write(RPCTagHandshake)
|
|
||||||
buf.Write(RPCHandshakeFlags)
|
|
||||||
buf.Write(RPCHandshakeSenderPID)
|
|
||||||
buf.Write(RPCHandshakePeerPID)
|
|
||||||
|
|
||||||
return buf.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRPCHandshakeRequest() *RPCHandshakeRequest {
|
|
||||||
return &RPCHandshakeRequest{}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package rpc
|
|
||||||
|
|
||||||
import "encoding/binary"
|
|
||||||
|
|
||||||
type RPCProxyRequestFlags uint32
|
|
||||||
|
|
||||||
const (
|
|
||||||
RPCProxyRequestFlagsHasAdTag RPCProxyRequestFlags = 0x8
|
|
||||||
RPCProxyRequestFlagsEncrypted = 0x2
|
|
||||||
RPCProxyRequestFlagsMagic = 0x1000
|
|
||||||
RPCProxyRequestFlagsExtMode2 = 0x20000
|
|
||||||
RPCProxyRequestFlagsIntermediate = 0x20000000
|
|
||||||
RPCProxyRequestFlagsAbdridged = 0x40000000
|
|
||||||
RPCProxyRequestFlagsQuickAck = 0x80000000
|
|
||||||
)
|
|
||||||
|
|
||||||
var rpcProxyRequestFlagsEncryptedPrefix [8]byte
|
|
||||||
|
|
||||||
func (r RPCProxyRequestFlags) Bytes() []byte {
|
|
||||||
converted := make([]byte, 4)
|
|
||||||
binary.LittleEndian.PutUint32(converted, uint32(r))
|
|
||||||
|
|
||||||
return converted
|
|
||||||
}
|
|
||||||
@@ -22,7 +22,7 @@ const (
|
|||||||
|
|
||||||
var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
|
var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
|
||||||
|
|
||||||
func NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse, secret []byte) wrappers.ReadWriteCloserWithAddr {
|
func NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.NonceRequest, resp *rpc.NonceResponse, secret []byte) wrappers.ReadWriteCloserWithAddr {
|
||||||
localAddr := conn.LocalAddr()
|
localAddr := conn.LocalAddr()
|
||||||
remoteAddr := conn.RemoteAddr()
|
remoteAddr := conn.RemoteAddr()
|
||||||
|
|
||||||
@@ -35,8 +35,7 @@ func NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.RPC
|
|||||||
return wrappers.NewBlockCipherRWC(conn, enc, dec)
|
return wrappers.NewBlockCipherRWC(conn, enc, dec)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeKeys(purpose CipherPurpose, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse,
|
func makeKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
||||||
client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
|
|
||||||
message := bytes.Buffer{}
|
message := bytes.Buffer{}
|
||||||
message.Write(resp.Nonce[:])
|
message.Write(resp.Nonce[:])
|
||||||
message.Write(req.Nonce[:])
|
message.Write(req.Nonce[:])
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ var proxySecret = []byte{196, 249, 250, 202, 150, 120, 230, 187, 72, 173,
|
|||||||
183, 6, 27, 38, 93, 178, 18}
|
183, 6, 27, 38, 93, 178, 18}
|
||||||
|
|
||||||
func TestMakeKeys(t *testing.T) {
|
func TestMakeKeys(t *testing.T) {
|
||||||
req, err := rpc.NewRPCNonceRequest(proxySecret)
|
req, err := rpc.NewNonceRequest(proxySecret)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
copy(req.Nonce[:], []byte{24, 49, 53, 111, 198, 10, 235, 180, 230, 112, 92, 78, 1, 201, 106, 105})
|
copy(req.Nonce[:], []byte{24, 49, 53, 111, 198, 10, 235, 180, 230, 112, 92, 78, 1, 201, 106, 105})
|
||||||
binary.LittleEndian.PutUint32(req.CryptoTS[:], 1528396015)
|
binary.LittleEndian.PutUint32(req.CryptoTS[:], 1528396015)
|
||||||
|
|
||||||
resp := &rpc.RPCNonceResponse{}
|
resp := &rpc.NonceResponse{}
|
||||||
copy(resp.Nonce[:], []byte{247, 40, 210, 56, 65, 12, 101, 170, 216, 155, 14, 253, 250, 238, 219, 226})
|
copy(resp.Nonce[:], []byte{247, 40, 210, 56, 65, 12, 101, 170, 216, 155, 14, 253, 250, 238, 219, 226})
|
||||||
|
|
||||||
cltAddr := &net.TCPAddr{
|
cltAddr := &net.TCPAddr{
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type ProxyRequestReadWriteCloserWithAddr struct {
|
|||||||
wrappers.BufferedReader
|
wrappers.BufferedReader
|
||||||
|
|
||||||
conn wrappers.ReadWriteCloserWithAddr
|
conn wrappers.ReadWriteCloserWithAddr
|
||||||
req *rpc.RPCProxyRequest
|
req *rpc.ProxyRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProxyRequestReadWriteCloserWithAddr) Read(buf []byte) (int, error) {
|
func (p *ProxyRequestReadWriteCloserWithAddr) Read(buf []byte) (int, error) {
|
||||||
@@ -29,11 +29,11 @@ func (p *ProxyRequestReadWriteCloserWithAddr) Read(buf []byte) (int, error) {
|
|||||||
return errors.Annotate(err, "Cannot read RPC tag")
|
return errors.Annotate(err, "Cannot read RPC tag")
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagCloseExt) {
|
if bytes.Equal(ansBuf.Bytes(), rpc.TagCloseExt) {
|
||||||
return p.readCloseExt()
|
return p.readCloseExt()
|
||||||
} else if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagProxyAns) {
|
} else if bytes.Equal(ansBuf.Bytes(), rpc.TagProxyAns) {
|
||||||
return p.readProxyAns(buf)
|
return p.readProxyAns(buf)
|
||||||
} else if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagSimpleAck) {
|
} else if bytes.Equal(ansBuf.Bytes(), rpc.TagSimpleAck) {
|
||||||
return p.readSimpleAck()
|
return p.readSimpleAck()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ func (p *ProxyRequestReadWriteCloserWithAddr) RemoteAddr() *net.TCPAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewProxyRequestRWC(conn wrappers.ReadWriteCloserWithAddr, connOpts *mtproto.ConnectionOpts, adTag []byte) (wrappers.ReadWriteCloserWithAddr, error) {
|
func NewProxyRequestRWC(conn wrappers.ReadWriteCloserWithAddr, connOpts *mtproto.ConnectionOpts, adTag []byte) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||||
req, err := rpc.NewRPCProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot create new RPC proxy request")
|
return nil, errors.Annotate(err, "Cannot create new RPC proxy request")
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-10
@@ -50,7 +50,7 @@ func NewMiddleTelegram(conf *config.Config, logger *zap.SugaredLogger) Telegram
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.ReadWriteCloserWithAddr) (wrappers.ReadWriteCloserWithAddr, error) {
|
func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.ReadWriteCloserWithAddr) (wrappers.ReadWriteCloserWithAddr, error) {
|
||||||
rpcNonceConn := mtwrappers.NewFrameRWC(conn, rpc.RPCNonceSeqNo)
|
rpcNonceConn := mtwrappers.NewFrameRWC(conn, rpc.SeqNoNonce)
|
||||||
|
|
||||||
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
rpcNonceReq, err := t.sendRPCNonceRequest(rpcNonceConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -62,7 +62,7 @@ func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
secureConn := mtwrappers.NewMiddleProxyCipherRWC(conn, rpcNonceReq, rpcNonceResp, t.proxySecret)
|
secureConn := mtwrappers.NewMiddleProxyCipherRWC(conn, rpcNonceReq, rpcNonceResp, t.proxySecret)
|
||||||
secureConn = mtwrappers.NewFrameRWC(secureConn, rpc.RPCHandshakeSeqNo)
|
secureConn = mtwrappers.NewFrameRWC(secureConn, rpc.SeqNoHandshake)
|
||||||
|
|
||||||
rpcHandshakeReq, err := t.sendRPCHandshakeRequest(secureConn)
|
rpcHandshakeReq, err := t.sendRPCHandshakeRequest(secureConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,8 +76,8 @@ func (t *middleTelegram) Init(connOpts *mtproto.ConnectionOpts, conn wrappers.Re
|
|||||||
return mtwrappers.NewProxyRequestRWC(secureConn, connOpts, t.conf.AdTag)
|
return mtwrappers.NewProxyRequestRWC(secureConn, connOpts, t.conf.AdTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.RPCNonceRequest, error) {
|
func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.NonceRequest, error) {
|
||||||
rpcNonceReq, err := rpc.NewRPCNonceRequest(t.proxySecret)
|
rpcNonceReq, err := rpc.NewNonceRequest(t.proxySecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot create RPC nonce request")
|
return nil, errors.Annotate(err, "Cannot create RPC nonce request")
|
||||||
}
|
}
|
||||||
@@ -88,14 +88,15 @@ func (t *middleTelegram) sendRPCNonceRequest(conn io.Writer) (*rpc.RPCNonceReque
|
|||||||
return rpcNonceReq, nil
|
return rpcNonceReq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) receiveRPCNonceResponse(conn io.Reader, req *rpc.RPCNonceRequest) (*rpc.RPCNonceResponse, error) {
|
func (t *middleTelegram) receiveRPCNonceResponse(conn io.Reader, req *rpc.NonceRequest) (*rpc.NonceResponse, error) {
|
||||||
var ans [128]byte
|
var ans [128]byte
|
||||||
|
|
||||||
n, err := conn.Read(ans[:])
|
n, err := conn.Read(ans[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
return nil, errors.Annotate(err, "Cannot read RPC nonce response")
|
||||||
}
|
}
|
||||||
rpcNonceResp, err := rpc.NewRPCNonceResponse(ans[:n])
|
|
||||||
|
rpcNonceResp, err := rpc.NewNonceResponse(ans[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot initialize RPC nonce response")
|
return nil, errors.Annotate(err, "Cannot initialize RPC nonce response")
|
||||||
}
|
}
|
||||||
@@ -106,8 +107,8 @@ func (t *middleTelegram) receiveRPCNonceResponse(conn io.Reader, req *rpc.RPCNon
|
|||||||
return rpcNonceResp, nil
|
return rpcNonceResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.RPCHandshakeRequest, error) {
|
func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.HandshakeRequest, error) {
|
||||||
req := rpc.NewRPCHandshakeRequest()
|
req := rpc.NewHandshakeRequest()
|
||||||
if _, err := conn.Write(req.Bytes()); err != nil {
|
if _, err := conn.Write(req.Bytes()); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot send RPC handshake request")
|
return nil, errors.Annotate(err, "Cannot send RPC handshake request")
|
||||||
}
|
}
|
||||||
@@ -115,14 +116,15 @@ func (t *middleTelegram) sendRPCHandshakeRequest(conn io.Writer) (*rpc.RPCHandsh
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *middleTelegram) receiveRPCHandshakeResponse(conn io.Reader, req *rpc.RPCHandshakeRequest) (*rpc.RPCHandshakeResponse, error) {
|
func (t *middleTelegram) receiveRPCHandshakeResponse(conn io.Reader, req *rpc.HandshakeRequest) (*rpc.HandshakeResponse, error) {
|
||||||
var ans [128]byte
|
var ans [128]byte
|
||||||
|
|
||||||
n, err := conn.Read(ans[:])
|
n, err := conn.Read(ans[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
return nil, errors.Annotate(err, "Cannot read RPC handshake response")
|
||||||
}
|
}
|
||||||
rpcHandshakeResp, err := rpc.NewRPCHandshakeResponse(ans[:n])
|
|
||||||
|
rpcHandshakeResp, err := rpc.NewHandshakeResponse(ans[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot initialize RPC handshake response")
|
return nil, errors.Annotate(err, "Cannot initialize RPC handshake response")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user