Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
Limit local recvsize when OTA is enabled. Fix #98
Browse files Browse the repository at this point in the history
  • Loading branch information
librehat committed Dec 14, 2016
1 parent 9dbc891 commit 469f0e3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
11 changes: 9 additions & 2 deletions lib/tcprelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ TcpRelay::TcpRelay(QTcpSocket *localSocket,
timer, static_cast<void (QTimer::*)()> (&QTimer::start));
connect(remote, &QTcpSocket::bytesWritten, this, &TcpRelay::bytesSend);

local->setReadBufferSize(RecvSize);
// To make sure datagram doesn't exceed remote server's maximum, we can
// limit how many bytes we take from local socket at a time. This is due
// the overhead introduced by OTA.
quint64 localRecvSize = RemoteRecvSize;
if (auth && isLocal) {
localRecvSize -= (Cipher::AUTH_LEN + 2);
}
local->setReadBufferSize(localRecvSize);
local->setSocketOption(QAbstractSocket::LowDelayOption, 1);
local->setSocketOption(QAbstractSocket::KeepAliveOption, 1);

remote->setReadBufferSize(RecvSize);
remote->setReadBufferSize(RemoteRecvSize);
remote->setSocketOption(QAbstractSocket::LowDelayOption, 1);
remote->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tcprelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class QSS_EXPORT TcpRelay : public QObject
void finished();

private:
static const qint64 RecvSize = 65536;
static const qint64 RemoteRecvSize = 65536;

STAGE stage;
Address remoteAddress;
Expand Down
19 changes: 13 additions & 6 deletions lib/udprelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ UdpRelay::UdpRelay(const EncryptorPrivate &ep,
{
encryptor = new Encryptor(ep, this);

listenSocket.setReadBufferSize(RecvSize);
// To make sure datagram doesn't exceed remote server's maximum, we can
// limit how many bytes we take from local socket at a time. This is due
// the overhead introduced by OTA.
quint64 localRecvSize = RemoteRecvSize;
if (auth && isLocal) {
localRecvSize -= (Cipher::AUTH_LEN + 2);
}
listenSocket.setReadBufferSize(localRecvSize);
listenSocket.setSocketOption(QAbstractSocket::LowDelayOption, 1);

connect(&listenSocket, &QUdpSocket::stateChanged,
Expand Down Expand Up @@ -104,7 +111,7 @@ void UdpRelay::onListenStateChanged(QAbstractSocket::SocketState s)

void UdpRelay::onServerUdpSocketReadyRead()
{
if (listenSocket.pendingDatagramSize() > RecvSize) {
if (listenSocket.pendingDatagramSize() > RemoteRecvSize) {
emit info("[UDP] Datagram is too large. discarded.");
return;
}
Expand All @@ -114,7 +121,7 @@ void UdpRelay::onServerUdpSocketReadyRead()
QHostAddress r_addr;
quint16 r_port;
qint64 readSize = listenSocket.readDatagram(data.data(),
RecvSize,
RemoteRecvSize,
&r_addr,
&r_port);
emit bytesRead(readSize);
Expand Down Expand Up @@ -152,7 +159,7 @@ void UdpRelay::onServerUdpSocketReadyRead()
QString dbg;
if (!client) {
client = new QUdpSocket(this);
client->setReadBufferSize(RecvSize);
client->setReadBufferSize(RemoteRecvSize);
client->setSocketOption(QAbstractSocket::LowDelayOption, 1);
cache.insert(remoteAddr, client);
connect(client, &QUdpSocket::readyRead,
Expand Down Expand Up @@ -210,7 +217,7 @@ void UdpRelay::onClientUdpSocketReadyRead()
return;
}

if (sock->pendingDatagramSize() > RecvSize) {
if (sock->pendingDatagramSize() > RemoteRecvSize) {
emit info("[UDP] Datagram is too large. Discarded.");
return;
}
Expand All @@ -219,7 +226,7 @@ void UdpRelay::onClientUdpSocketReadyRead()
data.resize(sock->pendingDatagramSize());
QHostAddress r_addr;
quint16 r_port;
sock->readDatagram(data.data(), RecvSize, &r_addr, &r_port);
sock->readDatagram(data.data(), RemoteRecvSize, &r_addr, &r_port);

QByteArray response;
if (isLocal) {
Expand Down
2 changes: 1 addition & 1 deletion lib/udprelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public slots:

private:
//64KB, same as shadowsocks-python (udprelay)
static const qint64 RecvSize = 65536;
static const qint64 RemoteRecvSize = 65536;

const Address &serverAddress;
const bool &isLocal;
Expand Down

0 comments on commit 469f0e3

Please sign in to comment.