Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from tindy2013:master #17

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Optimize codes
  • Loading branch information
tindy2013 committed May 26, 2020
commit a04d2d9dfe128c937560205aa0359909b0a7055a
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void signalHandler(int signum)
killClient(SPEEDTEST_MESSAGE_FOUNDVMESS);
*/
killByHandle();
writeLog(LOG_TYPE_INFO, "Received SIGINT. Exit right now.");
writeLog(LOG_TYPE_INFO, "Received signal. Exit right now.");
logEOF();

exit(signum);
Expand Down
90 changes: 32 additions & 58 deletions src/multithread_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,43 +485,60 @@ int sitePing(nodeInfo &node, std::string localaddr, int localport, std::string u

writeLog(LOG_TYPE_GPING, "Website ping started. Target: '" + target + "' . Proxy: '" + localaddr + ":" + std::to_string(localport) + "' .");
int loopcounter = 0, succeedcounter = 0, failcounter = 0, totduration = 0;
bool failed = true;
while(loopcounter < times_to_ping)
{
failed = true;
rawSitePing[loopcounter] = 0;
if(failcounter >= fail_limit)
{
writeLog(LOG_TYPE_GPING, "Fail limit exceeded. Stop now.");
break;
}
auto start = steady_clock::now();
defer(loopcounter++;)
defer(draw_progress_gping(loopcounter, rawSitePing);)
time_point<steady_clock> start = steady_clock::now(), end;
milliseconds lapse;
int deltatime = 0;
defer(
end = steady_clock::now();
lapse = duration_cast<milliseconds>(end - start);
deltatime = lapse.count();
if(failed)
{
failcounter++;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Fail - time=" + std::to_string(deltatime) + "ms");
}
else
{
succeedcounter++;
rawSitePing[loopcounter] = deltatime;
totduration += deltatime;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Success - time=" + std::to_string(deltatime) + "ms");
}
)
sHost = initSocket(getNetworkType(localaddr), SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == sHost)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "ERROR: Could not create socket.");
goto end;
continue;
}
defer(closesocket(sHost);)
if(startConnect(sHost, localaddr, localport) == SOCKET_ERROR)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "ERROR: Connect to SOCKS5 server " + localaddr + ":" + std::to_string(localport) + " failed.");
goto end;
continue;
}
setTimeout(sHost, 5000);
if(connectSocks5(sHost, username, password) == -1)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "ERROR: SOCKS5 server authentication failed.");
goto end;
continue;
}
if(connectThruSocks(sHost, host, port) == -1)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "ERROR: Connect to " + host + ":" + std::to_string(port) + " through SOCKS5 server failed.");
goto end;
continue;
}

if(useTLS)
Expand All @@ -531,11 +548,7 @@ int sitePing(nodeInfo &node, std::string localaddr, int localport, std::string u

ctx = SSL_CTX_new(TLS_client_method());
if(ctx == NULL)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "OpenSSL: " + std::string(ERR_error_string(ERR_get_error(), NULL)));
}
else
{
defer(SSL_CTX_free(ctx);)
Expand All @@ -544,31 +557,12 @@ int sitePing(nodeInfo &node, std::string localaddr, int localport, std::string u
SSL_set_fd(ssl, sHost);

if(SSL_connect(ssl) != 1)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "ERROR: Connect to " + host + ":" + std::to_string(port) + " through SOCKS5 server failed.");
}
else
{
SSL_write(ssl, request.data(), request.size());
cur_len = SSL_read(ssl, bufRecv, BUF_SIZE - 1);
auto end = steady_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
int deltatime = duration.count();
if(cur_len <= 0)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Fail - time=" + std::to_string(deltatime) + "ms");
}
else
{
succeedcounter++;
rawSitePing[loopcounter] = deltatime;
totduration += deltatime;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Success - time=" + std::to_string(deltatime) + "ms");
}
failed = cur_len <= 0;
}
}
}
Expand All @@ -578,28 +572,8 @@ int sitePing(nodeInfo &node, std::string localaddr, int localport, std::string u
if (SOCKET_ERROR == retVal)
return -1;
cur_len = Recv(sHost, bufRecv, BUF_SIZE - 1, 0);
auto end = steady_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
int deltatime = duration.count();
if(cur_len <= 0)
{
failcounter++;
rawSitePing[loopcounter] = 0;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Fail - time=" + std::to_string(deltatime) + "ms");
}
else
{
succeedcounter++;
rawSitePing[loopcounter] = deltatime;
totduration += deltatime;
writeLog(LOG_TYPE_GPING, "Accessing '" + target + "' - Success - time=" + std::to_string(deltatime) + "ms");

}
failed = cur_len <= 0;
}
end:
draw_progress_gping(loopcounter, rawSitePing);
loopcounter++;
closesocket(sHost);
}
std::cerr<<std::endl;
std::move(std::begin(rawSitePing), std::end(rawSitePing), node.rawSitePing);
Expand Down