mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:54:01 +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"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -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
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user