mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:34:02 +03:00
Add rpc proxy request
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package bufferpool
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const bufferPoolSize = 4 * 1024
|
||||||
|
|
||||||
|
var bufferPool sync.Pool
|
||||||
|
|
||||||
|
func Get() *bytes.Buffer {
|
||||||
|
buf := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func Return(buf *bytes.Buffer) {
|
||||||
|
bufferPool.Put(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
bufferPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
buf.Grow(bufferPoolSize)
|
||||||
|
|
||||||
|
return buf
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package mtproto
|
||||||
|
|
||||||
|
type Extras struct {
|
||||||
|
QuickAck bool
|
||||||
|
SimpleAck bool
|
||||||
|
}
|
||||||
@@ -1,11 +1,6 @@
|
|||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import "encoding/binary"
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
|
|
||||||
"github.com/9seconds/mtg/mtproto"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RPCProxyRequestFlags uint32
|
type RPCProxyRequestFlags uint32
|
||||||
|
|
||||||
@@ -27,25 +22,3 @@ func (r RPCProxyRequestFlags) Bytes() []byte {
|
|||||||
|
|
||||||
return converted
|
return converted
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRPCRproxyRequestFlags(connectionType mtproto.ConnectionType, quickAck bool, message []byte) RPCProxyRequestFlags {
|
|
||||||
flags := RPCProxyRequestFlagsHasAdTag
|
|
||||||
flags |= RPCProxyRequestFlagsMagic
|
|
||||||
flags |= RPCProxyRequestFlagsExtMode2
|
|
||||||
|
|
||||||
switch connectionType {
|
|
||||||
case mtproto.ConnectionTypeAbridged:
|
|
||||||
flags |= RPCProxyRequestFlagsAbdridged
|
|
||||||
case mtproto.ConnectionTypeIntermediate:
|
|
||||||
flags |= RPCProxyRequestFlagsIntermediate
|
|
||||||
}
|
|
||||||
|
|
||||||
if quickAck {
|
|
||||||
flags |= RPCProxyRequestFlagsQuickAck
|
|
||||||
}
|
|
||||||
if bytes.HasPrefix(message, rpcProxyRequestFlagsEncryptedPrefix[:]) {
|
|
||||||
flags |= RPCProxyRequestFlagsEncrypted
|
|
||||||
}
|
|
||||||
|
|
||||||
return flags
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/mtproto"
|
||||||
|
"github.com/9seconds/mtg/mtproto/bufferpool"
|
||||||
|
"github.com/juju/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
rpcProxyRequestConnectionIDLength = 8
|
||||||
|
rpcProxyRequestIPPortLength = 16 + 4
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rpcProxyRequestTag = []byte{0xee, 0xf1, 0xce, 0x36}
|
||||||
|
rpcProxyRequestExtraSize = []byte{0x18, 0x00, 0x00, 0x00}
|
||||||
|
rpcProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
|
||||||
|
)
|
||||||
|
|
||||||
|
type RPCProxyRequest struct {
|
||||||
|
Flags RPCProxyRequestFlags
|
||||||
|
ConnectionID [rpcProxyRequestConnectionIDLength]byte
|
||||||
|
RemoteIPPort [rpcProxyRequestIPPortLength]byte
|
||||||
|
LocalIPPort [rpcProxyRequestIPPortLength]byte
|
||||||
|
ADTag []byte
|
||||||
|
Message *bytes.Buffer
|
||||||
|
Extras *mtproto.Extras
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RPCProxyRequest) Bytes() *bytes.Buffer {
|
||||||
|
buf := bufferpool.Get()
|
||||||
|
|
||||||
|
flags := r.Flags
|
||||||
|
if r.Extras.QuickAck {
|
||||||
|
flags |= RPCProxyRequestFlagsQuickAck
|
||||||
|
}
|
||||||
|
|
||||||
|
messageBytes := r.Message.Bytes()
|
||||||
|
if bytes.HasPrefix(messageBytes, rpcProxyRequestFlagsEncryptedPrefix[:]) {
|
||||||
|
flags |= RPCProxyRequestFlagsEncrypted
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Write(rpcProxyRequestTag)
|
||||||
|
buf.Write(flags.Bytes())
|
||||||
|
buf.Write(r.ConnectionID[:])
|
||||||
|
buf.Write(r.RemoteIPPort[:])
|
||||||
|
buf.Write(r.LocalIPPort[:])
|
||||||
|
buf.Write(rpcProxyRequestExtraSize)
|
||||||
|
buf.Write(rpcProxyRequestProxyTag)
|
||||||
|
buf.WriteByte(byte(len(r.ADTag)))
|
||||||
|
buf.Write(r.ADTag)
|
||||||
|
|
||||||
|
for i := 0; i < (buf.Len() % 4); i++ {
|
||||||
|
buf.WriteByte(0x00)
|
||||||
|
}
|
||||||
|
if r.Message != nil {
|
||||||
|
buf.Write(messageBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *net.TCPAddr, adTag []byte, extras *mtproto.Extras) (*RPCProxyRequest, error) {
|
||||||
|
flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
|
||||||
|
|
||||||
|
switch connectionType {
|
||||||
|
case mtproto.ConnectionTypeAbridged:
|
||||||
|
flags |= RPCProxyRequestFlagsAbdridged
|
||||||
|
case mtproto.ConnectionTypeIntermediate:
|
||||||
|
flags |= RPCProxyRequestFlagsIntermediate
|
||||||
|
}
|
||||||
|
|
||||||
|
request := RPCProxyRequest{
|
||||||
|
Flags: flags,
|
||||||
|
ADTag: adTag,
|
||||||
|
Extras: extras,
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := rand.Read(request.ConnectionID[:]); err != nil {
|
||||||
|
return nil, errors.Annotate(err, "Cannot generate connection ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
port := make([]byte, 4)
|
||||||
|
copy(request.LocalIPPort[:], local.IP.To16())
|
||||||
|
binary.LittleEndian.PutUint32(port, uint32(local.Port))
|
||||||
|
copy(request.LocalIPPort[16:], port)
|
||||||
|
|
||||||
|
copy(request.RemoteIPPort[:], remote.IP.To16())
|
||||||
|
binary.LittleEndian.PutUint32(port, uint32(remote.Port))
|
||||||
|
copy(request.RemoteIPPort[16:], port)
|
||||||
|
|
||||||
|
return &request, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user