Fix rpc proxy request

This commit is contained in:
9seconds
2018-07-04 12:57:13 +03:00
parent 90c10519ee
commit a274493e3b
2 changed files with 18 additions and 26 deletions
-5
View File
@@ -4,8 +4,3 @@ const (
RPCNonceSeqNo = -2 RPCNonceSeqNo = -2
RPCHandshakeSeqNo = -1 RPCHandshakeSeqNo = -1
) )
type Extras struct {
QuickAck bool
SimpleAck bool
}
+18 -21
View File
@@ -25,17 +25,17 @@ var (
type RPCProxyRequest struct { type RPCProxyRequest struct {
Flags RPCProxyRequestFlags Flags RPCProxyRequestFlags
ConnectionID [rpcProxyRequestConnectionIDLength]byte ConnectionID [rpcProxyRequestConnectionIDLength]byte
RemoteIPPort [rpcProxyRequestIPPortLength]byte OurIPPort [rpcProxyRequestIPPortLength]byte
LocalIPPort [rpcProxyRequestIPPortLength]byte ClientIPPort [rpcProxyRequestIPPortLength]byte
ADTag []byte ADTag []byte
Extras Extras Options *mtproto.ConnectionOpts
} }
func (r *RPCProxyRequest) Bytes(message []byte) []byte { func (r *RPCProxyRequest) Bytes(message []byte) []byte {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
flags := r.Flags flags := r.Flags
if r.Extras.QuickAck { if r.Options.QuickAck {
flags |= RPCProxyRequestFlagsQuickAck flags |= RPCProxyRequestFlagsQuickAck
} }
@@ -46,25 +46,22 @@ func (r *RPCProxyRequest) Bytes(message []byte) []byte {
buf.Write(rpcProxyRequestTag) buf.Write(rpcProxyRequestTag)
buf.Write(flags.Bytes()) buf.Write(flags.Bytes())
buf.Write(r.ConnectionID[:]) buf.Write(r.ConnectionID[:])
buf.Write(r.RemoteIPPort[:]) buf.Write(r.ClientIPPort[:])
buf.Write(r.LocalIPPort[:]) buf.Write(r.OurIPPort[:])
buf.Write(rpcProxyRequestExtraSize) buf.Write(rpcProxyRequestExtraSize)
buf.Write(rpcProxyRequestProxyTag) buf.Write(rpcProxyRequestProxyTag)
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))
for i := 0; i < (buf.Len() % 4); i++ {
buf.WriteByte(0x00)
}
buf.Write(message) buf.Write(message)
return buf.Bytes() return buf.Bytes()
} }
func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *net.TCPAddr, adTag []byte, extras Extras) (*RPCProxyRequest, error) { func NewRPCProxyRequest(clientAddr, ownAddr *net.TCPAddr, opts *mtproto.ConnectionOpts, adTag []byte) (*RPCProxyRequest, error) {
flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2 flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
switch connectionType { switch opts.ConnectionType {
case mtproto.ConnectionTypeAbridged: case mtproto.ConnectionTypeAbridged:
flags |= RPCProxyRequestFlagsAbdridged flags |= RPCProxyRequestFlagsAbdridged
case mtproto.ConnectionTypeIntermediate: case mtproto.ConnectionTypeIntermediate:
@@ -72,9 +69,9 @@ func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *ne
} }
request := RPCProxyRequest{ request := RPCProxyRequest{
Flags: flags, Flags: flags,
ADTag: adTag, ADTag: adTag,
Extras: extras, Options: opts,
} }
if _, err := rand.Read(request.ConnectionID[:]); err != nil { if _, err := rand.Read(request.ConnectionID[:]); err != nil {
@@ -82,13 +79,13 @@ func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *ne
} }
port := make([]byte, 4) port := make([]byte, 4)
copy(request.LocalIPPort[:], local.IP.To16()) copy(request.ClientIPPort[:16], clientAddr.IP.To16())
binary.LittleEndian.PutUint32(port, uint32(local.Port)) binary.LittleEndian.PutUint32(port, uint32(clientAddr.Port))
copy(request.LocalIPPort[16:], port) copy(request.ClientIPPort[16:], port)
copy(request.RemoteIPPort[:], remote.IP.To16()) copy(request.OurIPPort[:16], ownAddr.IP.To16())
binary.LittleEndian.PutUint32(port, uint32(remote.Port)) binary.LittleEndian.PutUint32(port, uint32(ownAddr.Port))
copy(request.RemoteIPPort[16:], port) copy(request.OurIPPort[16:], port)
return &request, nil return &request, nil
} }