Remove borsh borsh new borsch

This commit is contained in:
9seconds
2018-07-05 11:16:53 +03:00
parent 7b43766a08
commit d3e685de97
13 changed files with 124 additions and 123 deletions
+21
View File
@@ -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"
)
type RPCHandshakeResponse struct {
type HandshakeResponse struct {
Type []byte
Flags []byte
SenderPID []byte
PeerPID []byte
}
func (r *RPCHandshakeResponse) Bytes() []byte {
func (r *HandshakeResponse) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(r.Type[:])
@@ -24,23 +24,23 @@ func (r *RPCHandshakeResponse) Bytes() []byte {
return buf.Bytes()
}
func (r *RPCHandshakeResponse) Valid(req *RPCHandshakeRequest) error {
if !bytes.Equal(r.Type, RPCTagHandshake) {
func (r *HandshakeResponse) Valid(req *HandshakeRequest) error {
if !bytes.Equal(r.Type, TagHandshake) {
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 nil
}
func NewRPCHandshakeResponse(data []byte) (*RPCHandshakeResponse, error) {
func NewHandshakeResponse(data []byte) (*HandshakeResponse, error) {
if len(data) != 32 {
return nil, errors.New("Incorrect handshake response length")
}
return &RPCHandshakeResponse{
return &HandshakeResponse{
Type: data[:4],
Flags: data[4:8],
SenderPID: data[8:20],
@@ -9,25 +9,25 @@ import (
"github.com/juju/errors"
)
type RPCNonceRequest struct {
type NonceRequest struct {
KeySelector []byte
CryptoTS []byte
Nonce []byte
}
func (r *RPCNonceRequest) Bytes() []byte {
func (r *NonceRequest) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(RPCTagNonce)
buf.Write(TagNonce)
buf.Write(r.KeySelector)
buf.Write(RPCNonceCryptoAES)
buf.Write(NonceCryptoAES)
buf.Write(r.CryptoTS)
buf.Write(r.Nonce)
return buf.Bytes()
}
func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
func NewNonceRequest(proxySecret []byte) (*NonceRequest, error) {
nonce := make([]byte, 16)
keySelector := 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
binary.LittleEndian.PutUint32(cryptoTS, uint32(timestamp))
return &RPCNonceRequest{
return &NonceRequest{
KeySelector: keySelector,
CryptoTS: cryptoTS,
Nonce: nonce,
@@ -6,17 +6,17 @@ import (
"github.com/juju/errors"
)
type RPCNonceResponse struct {
RPCNonceRequest
type NonceResponse struct {
NonceRequest
RPCType []byte
Crypto []byte
Type []byte
Crypto []byte
}
func (r *RPCNonceResponse) Bytes() []byte {
func (r *NonceResponse) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(r.RPCType)
buf.Write(r.Type)
buf.Write(r.KeySelector)
buf.Write(r.Crypto)
buf.Write(r.CryptoTS)
@@ -25,11 +25,11 @@ func (r *RPCNonceResponse) Bytes() []byte {
return buf.Bytes()
}
func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
if !bytes.Equal(r.RPCType, RPCTagNonce) {
func (r *NonceResponse) Valid(req *NonceRequest) error {
if !bytes.Equal(r.Type, TagNonce) {
return errors.New("Unexpected RPC type")
}
if !bytes.Equal(r.Crypto, RPCNonceCryptoAES) {
if !bytes.Equal(r.Crypto, NonceCryptoAES) {
return errors.New("Unexpected crypto type")
}
if !bytes.Equal(r.KeySelector, req.KeySelector) {
@@ -39,18 +39,18 @@ func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
return nil
}
func NewRPCNonceResponse(data []byte) (*RPCNonceResponse, error) {
func NewNonceResponse(data []byte) (*NonceResponse, error) {
if len(data) != 32 {
return nil, errors.New("Unexpected message length")
}
return &RPCNonceResponse{
RPCNonceRequest: RPCNonceRequest{
return &NonceResponse{
NonceRequest: NonceRequest{
KeySelector: data[4:8],
CryptoTS: data[12:16],
Nonce: data[16:],
},
RPCType: data[:4],
Crypto: data[8:12],
Type: data[:4],
Crypto: data[8:12],
}, nil
}
+24
View File
@@ -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"
)
type RPCProxyRequest struct {
Flags RPCProxyRequestFlags
type ProxyRequest struct {
Flags proxyRequestFlags
ConnectionID []byte
OurIPPort []byte
ClientIPPort []byte
@@ -20,25 +20,25 @@ type RPCProxyRequest struct {
Options *mtproto.ConnectionOpts
}
func (r *RPCProxyRequest) Bytes(message []byte) []byte {
func (r *ProxyRequest) Bytes(message []byte) []byte {
buf := &bytes.Buffer{}
flags := r.Flags
if r.Options.QuickAck {
flags |= RPCProxyRequestFlagsQuickAck
flags |= proxyRequestFlagsQuickAck
}
if bytes.HasPrefix(message, rpcProxyRequestFlagsEncryptedPrefix[:]) {
flags |= RPCProxyRequestFlagsEncrypted
if bytes.HasPrefix(message, proxyRequestFlagsEncryptedPrefix[:]) {
flags |= proxyRequestFlagsEncrypted
}
buf.Write(RPCTagProxyRequest)
buf.Write(TagProxyRequest)
buf.Write(flags.Bytes())
buf.Write(r.ConnectionID[:])
buf.Write(r.ClientIPPort[:])
buf.Write(r.OurIPPort[:])
buf.Write(RPCProxyRequestExtraSize)
buf.Write(RPCProxyRequestProxyTag)
buf.Write(ProxyRequestExtraSize)
buf.Write(ProxyRequestProxyTag)
buf.WriteByte(byte(len(r.ADTag)))
buf.Write(r.ADTag)
buf.Write(bytes.Repeat([]byte{0x00}, buf.Len()%4))
@@ -47,17 +47,17 @@ func (r *RPCProxyRequest) Bytes(message []byte) []byte {
return buf.Bytes()
}
func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*RPCProxyRequest, error) {
flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
func NewProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*ProxyRequest, error) {
flags := proxyRequestFlagsHasAdTag | proxyRequestFlagsMagic | proxyRequestFlagsExtMode2
switch opts.ConnectionType {
case mtproto.ConnectionTypeAbridged:
flags |= RPCProxyRequestFlagsAbdridged
flags |= proxyRequestFlagsAbdridged
case mtproto.ConnectionTypeIntermediate:
flags |= RPCProxyRequestFlagsIntermediate
flags |= proxyRequestFlagsIntermediate
}
request := RPCProxyRequest{
request := &ProxyRequest{
Flags: flags,
ADTag: adTag,
Options: opts,
@@ -79,5 +79,5 @@ func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.Connecti
binary.LittleEndian.PutUint32(port[:], uint32(ownAddr.Port))
copy(request.OurIPPort[16:], port[:])
return &request, nil
return request, nil
}
+16 -16
View File
@@ -1,30 +1,30 @@
package rpc
const (
RPCNonceSeqNo = -2
RPCHandshakeSeqNo = -1
SeqNoNonce = -2
SeqNoHandshake = -1
)
var (
RPCTagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
RPCTagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
RPCTagSimpleAck = []byte{0x9b, 0x40, 0xac, 0x3b}
RPCTagHandshake = []byte{0xf5, 0xee, 0x82, 0x76}
RPCTagNonce = []byte{0xaa, 0x87, 0xcb, 0x7a}
RPCTagProxyRequest = []byte{0xee, 0xf1, 0xce, 0x36}
TagCloseExt = []byte{0xa2, 0x34, 0xb6, 0x5e}
TagProxyAns = []byte{0x0d, 0xda, 0x03, 0x44}
TagSimpleAck = []byte{0x9b, 0x40, 0xac, 0x3b}
TagHandshake = []byte{0xf5, 0xee, 0x82, 0x76}
TagNonce = []byte{0xaa, 0x87, 0xcb, 0x7a}
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}
RPCProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
ProxyRequestExtraSize = []byte{0x18, 0x00, 0x00, 0x00}
ProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
RPCHandshakeSenderPID = []byte{}
RPCHandshakePeerPID = []byte{}
HandshakeSenderPID []byte
HandshakePeerPID []byte
)
func init() {
RPCHandshakeSenderPID = []byte("IPIPPRPDTIME")
RPCHandshakePeerPID = []byte("IPIPPRPDTIME")
HandshakeSenderPID = []byte("IPIPPRPDTIME")
HandshakePeerPID = []byte("IPIPPRPDTIME")
}
-21
View File
@@ -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{}
}
-24
View File
@@ -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
}
+2 -3
View File
@@ -22,7 +22,7 @@ const (
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()
remoteAddr := conn.RemoteAddr()
@@ -35,8 +35,7 @@ func NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.RPC
return wrappers.NewBlockCipherRWC(conn, enc, dec)
}
func makeKeys(purpose CipherPurpose, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse,
client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
func makeKeys(purpose CipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
message := bytes.Buffer{}
message.Write(resp.Nonce[:])
message.Write(req.Nonce[:])
+2 -2
View File
@@ -21,13 +21,13 @@ var proxySecret = []byte{196, 249, 250, 202, 150, 120, 230, 187, 72, 173,
183, 6, 27, 38, 93, 178, 18}
func TestMakeKeys(t *testing.T) {
req, err := rpc.NewRPCNonceRequest(proxySecret)
req, err := rpc.NewNonceRequest(proxySecret)
assert.Nil(t, err)
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)
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})
cltAddr := &net.TCPAddr{
+5 -5
View File
@@ -17,7 +17,7 @@ type ProxyRequestReadWriteCloserWithAddr struct {
wrappers.BufferedReader
conn wrappers.ReadWriteCloserWithAddr
req *rpc.RPCProxyRequest
req *rpc.ProxyRequest
}
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")
}
if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagCloseExt) {
if bytes.Equal(ansBuf.Bytes(), rpc.TagCloseExt) {
return p.readCloseExt()
} else if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagProxyAns) {
} else if bytes.Equal(ansBuf.Bytes(), rpc.TagProxyAns) {
return p.readProxyAns(buf)
} else if bytes.Equal(ansBuf.Bytes(), rpc.RPCTagSimpleAck) {
} else if bytes.Equal(ansBuf.Bytes(), rpc.TagSimpleAck) {
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) {
req, err := rpc.NewRPCProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
req, err := rpc.NewProxyRequest(connOpts.ClientAddr, conn.LocalAddr(), connOpts, adTag)
if err != nil {
return nil, errors.Annotate(err, "Cannot create new RPC proxy request")
}