Skip to content

Commit

Permalink
Fix a bug
Browse files Browse the repository at this point in the history
Fix a bug when parsing a space-seperated subscription, only the first node will be parsed.
Add HTTPS file download test.
Bump version to v0.2.3-alpha.
  • Loading branch information
tindy2013 committed Aug 17, 2019
1 parent 6708217 commit 9d762d8
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 41 deletions.
19 changes: 11 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int def_thread_count = 4;
bool export_with_maxspeed = false;
bool test_site_ping = false;
string export_sort_method = "none";
bool useTLS = false;

int avail_status[3] = {1, 1, 1};

Expand All @@ -42,7 +43,7 @@ HANDLE hProc = 0;

//declarations

int perform_test(nodeInfo *node, string testfile, string localaddr, int localport, string username, string password, int thread_count);
int perform_test(nodeInfo *node, string testfile, string localaddr, int localport, string username, string password, int thread_count, bool useTLS = false);
int tcping(nodeInfo *node);

//original codes
Expand Down Expand Up @@ -239,21 +240,21 @@ void readConf(string path)
if(vchild.size() < 2)
continue;
itemval = "";
for(unsigned i = 1; i<vchild.size(); i++)
for(unsigned i = 1; i < vchild.size(); i++)
itemval += vchild[i];
if(vchild[0] == "speedtest_mode")
speedtest_mode = vchild[1];
#ifdef _WIN32
//csharp version only works on windows
else if(vchild[0] == "preferred_ss_client" && vchild[1] == "ss-csharp")
else if(vchild[0] == "preferred_ss_client" && itemval == "ss-csharp")
ss_libev = false;
else if(vchild[0] == "preferred_ssr_client" && vchild[1] == "ssr-csharp")
else if(vchild[0] == "preferred_ssr_client" && itemval == "ssr-csharp")
ssr_libev = false;
#endif // _WIN32
else if(vchild[0] == "export_with_maxspeed")
export_with_maxspeed = vchild[1] == "true";
export_with_maxspeed = itemval == "true";
else if(vchild[0] == "override_conf_port")
override_conf_port = vchild[1];
override_conf_port = itemval;
else if(strFind(vchild[0], "exclude_remarks"))
exclude_remarks.push_back(itemval);
else if(strFind(vchild[0], "include_remarks"))
Expand All @@ -262,6 +263,8 @@ void readConf(string path)
def_test_file = itemval;
else if(vchild[0] == "thread_count")
def_thread_count = stoi(itemval);
else if(vchild[0] == "speetest_with_tls")
useTLS = itemval == "true";
/*
else if(vchild[0] == "colorset")
colorgroup = vchild[1];
Expand Down Expand Up @@ -403,7 +406,7 @@ int singleTest(string testfile, nodeInfo *node)
if(speedtest_mode != "pingonly")
{
writeLog(LOG_TYPE_INFO, "Now performing file download speed test...");
perform_test(node, testfile, testserver, testport, username, password, def_thread_count);
perform_test(node, testfile, testserver, testport, username, password, def_thread_count, useTLS);
logdata = "";
for(int i = 0; i < 20; i++)
{
Expand All @@ -414,7 +417,7 @@ int singleTest(string testfile, nodeInfo *node)
{
writeLog(LOG_TYPE_ERROR, "Speedtest returned no speed.");
printMsg(SPEEDTEST_ERROR_RETEST, node, rpcmode);
perform_test(node, testfile, testserver, testport, username, password, def_thread_count);
perform_test(node, testfile, testserver, testport, username, password, def_thread_count, useTLS);
logdata = "";
for(int i = 0; i < 20; i++)
{
Expand Down
119 changes: 90 additions & 29 deletions multithread-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ static inline void draw_progress(int progress, long long this_bytes)
cerr<<" "<<progress * 5<<"% "<<speedCalc(this_bytes)<<"\r";
}

int _thread_download(string host, string uri, string localaddr, int localport, string username, string password)
int _thread_download(string host, string uri, string localaddr, int localport, string username, string password, bool useTLS = false)
{
launch_acc();
running_acc();
char bufRecv[BUF_SIZE];
int retVal, cur_len/*, recv_len = 0*/;
SOCKET sHost;
string request;
string request = "GET " + uri + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36\r\n\r\n";

sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == sHost)
Expand All @@ -104,46 +106,95 @@ int _thread_download(string host, string uri, string localaddr, int localport, s
goto end;
if(connectSocks5(sHost, username, password) == -1)
goto end;
if(connectThruSocks(sHost, host, "", 80) == -1)
goto end;
request = "GET " + uri + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36\r\n\r\n";
retVal = Send(sHost, request.data(), request.size(), 0);
if (SOCKET_ERROR == retVal)

if(useTLS)
{
closesocket(sHost);
return -1;
if(connectThruSocks(sHost, host, "", 443) == -1)
goto end;
SSL_CTX *ctx;
SSL *ssl;

ctx = SSL_CTX_new(TLS_client_method());
ssl = SSL_new(ctx);
if(ctx == NULL)
{
ERR_print_errors_fp(stderr);
goto end;
}

SSL_set_fd(ssl, sHost);
if(SSL_connect(ssl) != 1)
{
ERR_print_errors_fp(stderr);
}
else
{
SSL_write(ssl, request.data(), request.size());
while(1)
{
cur_len = SSL_read(ssl, bufRecv, BUF_SIZE - 1);
if(cur_len < 0)
{
if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
{
continue;
}
else
{
break;
}
}
if(cur_len == 0)
break;
append_recv_bytes(cur_len);
if(safe_read_exit_flag())
break;
}
}
SSL_clear(ssl);

}
while(1)
else
{
cur_len = Recv(sHost, bufRecv, BUF_SIZE, 0);
if(cur_len < 0)
if(connectThruSocks(sHost, host, "", 80) == -1)
goto end;
retVal = Send(sHost, request.data(), request.size(), 0);
if (SOCKET_ERROR == retVal)
{
closesocket(sHost);
return -1;
}
while(1)
{
if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
cur_len = Recv(sHost, bufRecv, BUF_SIZE - 1, 0);
if(cur_len < 0)
{
continue;
if(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
{
continue;
}
else
{
break;
}
}
else
{
if(cur_len == 0)
break;
//recv_len += cur_len;
append_recv_bytes(cur_len);
//if(recv_len >= MAX_FILE_SIZE) break;
if(safe_read_exit_flag())
break;
}
}
if(cur_len == 0)
break;
//recv_len += cur_len;
append_recv_bytes(cur_len);
//if(recv_len >= MAX_FILE_SIZE) break;
if(safe_read_exit_flag())
break;
}

end:
closesocket(sHost);
running_dec();
return 0;
}

int perform_test(nodeInfo *node, string testfile, string localaddr, int localport, string username, string password, int thread_count)
int perform_test(nodeInfo *node, string testfile, string localaddr, int localport, string username, string password, int thread_count, bool useTLS = false)
{
//prep up vars first
string host, uri;
Expand All @@ -153,14 +204,24 @@ int perform_test(nodeInfo *node, string testfile, string localaddr, int localpor
received_bytes = 0;
EXIT_FLAG = false;

if(useTLS)
{
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_algorithms();
}

int running;
thread threads[thread_count];
for(int i = 0; i != thread_count; i++)
{
threads[i]=thread(_thread_download, host, uri, localaddr, localport, username, password);
threads[i]=thread(_thread_download, host, uri, localaddr, localport, username, password, useTLS);
}
while(!safe_read_launched())
sleep(20);
sleep(20); //wait until any one of the threads start up

if(useTLS)
sleep(500); //slow down because TLS takes longer to initialize
auto start = steady_clock::now();
long long last_bytes = 0, this_bytes = 0, max_speed = 0;
for(int i = 1; i < 21; i++)
Expand Down
3 changes: 3 additions & 0 deletions pref.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ preferred_ssr_client=ssr-libev
;recognized value: tcping, googleping, bingping, gstaticping
preferred_ping_method=tcping

;Use TLS for Speedtest file download test method
speetest_with_tls=true

;Override any port in configurations and use the following one
;!!!DO NOT USE THIS UNLESS YOU ARE VERY SURE OF WHAT YOU ARE DOING!!!
;uncomment to enable this feature
Expand Down
4 changes: 2 additions & 2 deletions socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int Send(SOCKET sHost, const char* data, int len, int flags)
#ifdef _WIN32
return send(sHost, data, len, flags);
#else
return send(sHost, data, len, flags|MSG_NOSIGNAL);
return send(sHost, data, len, flags | MSG_NOSIGNAL);
#endif // _WIN32
}

Expand All @@ -78,7 +78,7 @@ int Recv(SOCKET sHost, char* data, int len, int flags)
#ifdef _WIN32
return recv(sHost, data, len, flags);
#else
return recv(sHost, data, len, flags|MSG_NOSIGNAL);
return recv(sHost, data, len, flags | MSG_NOSIGNAL);
#endif // _WIN32
}

Expand Down
2 changes: 2 additions & 0 deletions socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#ifdef _WIN32
#ifndef WINVER
Expand Down
3 changes: 2 additions & 1 deletion speedtestutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,8 @@ void explodeSub(string sub, bool sslibev, bool ssrlibev, string custom_port, int
sub = base64_decode(sub);
strstream<<sub;
int index = 0;
while(getline(strstream, strLink))
char delimiter = split(sub, "\n").size() <= 1 ? ' ' : '\n';
while(getline(strstream, strLink, delimiter))
{
explode(strLink, sslibev, ssrlibev, custom_port, local_port, &node);
if(strLink.size() == 0 || node.linkType == -1)
Expand Down
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef VERSION_H_INCLUDED
#define VERSION_H_INCLUDED

#define VERSION "v0.2.2-alpha"
#define VERSION "v0.2.3-alpha"

#endif // VERSION_H_INCLUDED

0 comments on commit 9d762d8

Please sign in to comment.