Change rwc to rwc with addresses

This commit is contained in:
9seconds
2018-07-02 16:12:31 +03:00
parent 6fa5f31ca8
commit 09476cc467
15 changed files with 109 additions and 68 deletions
+5 -3
View File
@@ -7,7 +7,6 @@ import (
"crypto/md5"
"crypto/sha1"
"encoding/binary"
"io"
"net"
"github.com/9seconds/mtg/mtproto/rpc"
@@ -23,14 +22,17 @@ const (
var emptyIP = [4]byte{0x00, 0x00, 0x00, 0x00}
func NewMiddleProxyCipherRWC(conn io.ReadWriteCloser, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) io.ReadWriteCloser {
func NewMiddleProxyCipherRWC(conn wrappers.ReadWriteCloserWithAddr, req *rpc.RPCNonceRequest,
resp *rpc.RPCNonceResponse, client *net.TCPAddr, remote *net.TCPAddr,
secret []byte) wrappers.ReadWriteCloserWithAddr {
encryptor := newCBCCipher(CipherPurposeClient, req, resp, client, remote, secret)
decryptor := newCBCCipher(CipherPurposeServer, req, resp, client, remote, secret)
return wrappers.NewBlockCipherRWC(conn, encryptor, decryptor)
}
func newCBCCipher(purpose CipherPurpose, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse, client *net.TCPAddr, remote *net.TCPAddr, secret []byte) cipher.BlockMode {
func newCBCCipher(purpose CipherPurpose, req *rpc.RPCNonceRequest, resp *rpc.RPCNonceResponse,
client *net.TCPAddr, remote *net.TCPAddr, secret []byte) cipher.BlockMode {
message := bytes.Buffer{}
message.Write(resp.Nonce[:])
message.Write(req.Nonce[:])
+9 -2
View File
@@ -6,8 +6,11 @@ import (
"encoding/binary"
"hash/crc32"
"io"
"net"
"github.com/juju/errors"
"github.com/9seconds/mtg/wrappers"
)
// Frame: { MessageLength(4) | SequenceNumber(4) | Message(???) | CRC32(4) [| padding(4), ...] }
@@ -19,7 +22,7 @@ const (
var frameRWCPadding = [4]byte{0x04, 0x00, 0x00, 0x00}
type FrameRWC struct {
conn io.ReadWriteCloser
conn wrappers.ReadWriteCloserWithAddr
readSeqNo int32
writeSeqNo int32
@@ -102,6 +105,10 @@ func (f *FrameRWC) Close() error {
return f.conn.Close()
}
func (f *FrameRWC) Addr() *net.TCPAddr {
return f.conn.Addr()
}
func (f *FrameRWC) flush(p []byte) (int, error) {
sizeToRead := len(p)
if f.readBuf.Len() < sizeToRead {
@@ -119,7 +126,7 @@ func (f *FrameRWC) flush(p []byte) (int, error) {
return sizeToRead, nil
}
func NewFrameRWC(conn io.ReadWriteCloser, seqNo int32) io.ReadWriteCloser {
func NewFrameRWC(conn wrappers.ReadWriteCloserWithAddr, seqNo int32) wrappers.ReadWriteCloserWithAddr {
return &FrameRWC{
conn: conn,
readSeqNo: seqNo,