[Redirector] Optimize code

This commit is contained in:
Connection Refused
2021-10-15 01:35:52 +08:00
parent 62154cf07b
commit 91b6692cfb
6 changed files with 91 additions and 144 deletions

View File

@@ -329,66 +329,38 @@ void udpSend(ENDPOINT_ID id, const unsigned char* target, const char* buffer, in
nf_udpPostSend(id, target, buffer, length, options);
return;
}
auto udpConn = udpContext[id];
auto remote = udpContext[id];
udpContextLock.unlock();
UP += length;
if (udpConn->tcpSocket == INVALID_SOCKET)
if (remote->tcpSocket == INVALID_SOCKET || remote->udpSocket == INVALID_SOCKET)
{
auto tcpSocket = SocksHelper::Utils::Connect();
if (tcpSocket == INVALID_SOCKET)
{
printf("[Redirector][EventHandler][udpSend][%llu] Connect to remote server failed\n", id);
if (remote->tcpSocket) closesocket(remote->tcpSocket);
if (remote->udpSocket) closesocket(remote->udpSocket);
remote->tcpSocket = INVALID_SOCKET;
remote->udpSocket = INVALID_SOCKET;
if (!remote->Associate())
return;
}
if (!SocksHelper::Utils::Handshake(tcpSocket))
{
closesocket(tcpSocket);
printf("[Redirector][EventHandler][udpSend][%llu] Handshake failed\n", id);
if (!remote->CreateUDP())
return;
}
udpConn->tcpSocket = tcpSocket;
}
if (udpConn->udpSocket == INVALID_SOCKET)
{
if (!udpConn->Associate())
{
closesocket(udpConn->tcpSocket);
udpConn->tcpSocket = INVALID_SOCKET;
printf("[Redirector][EventHandler][udpSend][%llu] UDP Associate failed\n", id);
return;
}
if (!udpConn->CreateUDP())
{
closesocket(udpConn->tcpSocket);
udpConn->tcpSocket = INVALID_SOCKET;
printf("[Redirector][EventHandler][udpSend][%llu] Create UDP socket failed\n", id);
return;
}
auto data = (PNF_UDP_OPTIONS)new char[sizeof(NF_UDP_OPTIONS) + options->optionsLength]();
memcpy(data, options, sizeof(NF_UDP_OPTIONS) + options->optionsLength - 1);
thread(udpBeginReceive, id, udpConn, data).detach();
thread(udpReceiveHandler, id, remote, data).detach();
}
if (udpConn->Send((PSOCKADDR_IN6)target, buffer, length) != length)
if (remote->Send((PSOCKADDR_IN6)target, buffer, length) != length)
{
closesocket(udpConn->tcpSocket);
closesocket(udpConn->udpSocket);
if (remote->tcpSocket) closesocket(remote->tcpSocket);
if (remote->udpSocket) closesocket(remote->udpSocket);
udpConn->tcpSocket = INVALID_SOCKET;
udpConn->udpSocket = INVALID_SOCKET;
printf("[Redirector][EventHandler][udpSend][%llu] Send data failed\n", id);
remote->tcpSocket = INVALID_SOCKET;
remote->udpSocket = INVALID_SOCKET;
}
}
@@ -417,19 +389,17 @@ void udpClosed(ENDPOINT_ID id, PNF_UDP_CONN_INFO info)
}
}
void udpBeginReceive(ENDPOINT_ID id, SocksHelper::PUDP udpConn, PNF_UDP_OPTIONS options)
void udpReceiveHandler(ENDPOINT_ID id, SocksHelper::PUDP remote, PNF_UDP_OPTIONS options)
{
char buffer[1458];
while (udpConn->udpSocket != INVALID_SOCKET)
while (remote->tcpSocket != INVALID_SOCKET && remote->udpSocket != INVALID_SOCKET)
{
SOCKADDR_IN6 target;
int length = udpConn->Read(&target, buffer, sizeof(buffer), NULL);
int length = remote->Read(&target, buffer, sizeof(buffer), NULL);
if (length == 0 || length == SOCKET_ERROR)
{
break;
}
DL += length;