Skip to content

Commit

Permalink
add test download timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Jul 22, 2023
1 parent 387285e commit 9fe08ca
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 98 deletions.
44 changes: 35 additions & 9 deletions go/grpc_server/fulltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log"
"net"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -109,16 +110,41 @@ func DoFullTest(ctx context.Context, in *gen.TestReq, instance interface{}) (out
// 下载
var speed string
if in.FullSpeed {
resp, err := httpClient.Get(in.FullSpeedUrl)
if err == nil {
time_start := time.Now()
n, _ := io.Copy(io.Discard, resp.Body)
time_end := time.Now()
if in.FullSpeedTimeout <= 0 {
in.FullSpeedTimeout = 30
}

speed = fmt.Sprintf("%.2fMiB/s", (float64(n)/time_end.Sub(time_start).Seconds())/1048576)
resp.Body.Close()
} else {
speed = "Error"
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(in.FullSpeedTimeout))
result := make(chan string)
var bodyClose io.Closer

go func() {
req, _ := http.NewRequestWithContext(ctx, "GET", in.FullSpeedUrl, nil)
resp, err := httpClient.Do(req)
if err == nil && resp != nil && resp.Body != nil {
bodyClose = resp.Body
defer resp.Body.Close()
//
time_start := time.Now()
n, _ := io.Copy(io.Discard, resp.Body)
time_end := time.Now()
result <- fmt.Sprintf("%.2fMiB/s", (float64(n)/time_end.Sub(time_start).Seconds())/1048576)
} else {
result <- "Error"
}
close(result)
}()

select {
case <-ctx.Done():
speed = "Timeout"
case s := <-result:
speed = s
}

cancel()
if bodyClose != nil {
bodyClose.Close()
}
}

Expand Down
181 changes: 96 additions & 85 deletions go/grpc_server/gen/libcore.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go/grpc_server/gen/libcore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ message TestReq {
bool full_latency = 8;
bool full_speed = 9;
string full_speed_url = 13;
int32 full_speed_timeout = 14;
bool full_in_out = 10;
bool full_udp_latency = 12;
//
Expand Down
2 changes: 1 addition & 1 deletion go/grpc_server/gen/libcore_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions main/NekoGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ namespace NekoGui {
_add(new configItem("user_agent", &user_agent, itemType::string));
_add(new configItem("test_url", &test_latency_url, itemType::string));
_add(new configItem("test_url_dl", &test_download_url, itemType::string));
_add(new configItem("test_dl_timeout", &test_download_timeout, itemType::integer));
_add(new configItem("current_group", &current_group, itemType::integer));
_add(new configItem("inbound_address", &inbound_address, itemType::string));
_add(new configItem("inbound_socks_port", &inbound_socks_port, itemType::integer));
Expand Down
1 change: 1 addition & 0 deletions main/NekoGui_DataStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace NekoGui {
QString log_level = "warning";
QString test_latency_url = "http://cp.cloudflare.com/";
QString test_download_url = "http://cachefly.cachefly.net/10mb.test";
int test_download_timeout = 30;
int test_concurrent = 5;
int traffic_loop_interval = 1000;
bool connection_statistics = false;
Expand Down
4 changes: 4 additions & 0 deletions translations/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ For NekoBox, this rewrites the underlying(localhost) DNS in Tun Mode, normal mod
<source>Download Test URL</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Timeout (s)</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DialogEditGroup</name>
Expand Down
4 changes: 4 additions & 0 deletions translations/ru_RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ For NekoBox, this rewrites the underlying(localhost) DNS in Tun Mode, normal mod
<source>If you Tun Mode is not working, try to change this option.</source>
<translation>Если TUN-режим не работает как надо, попробуйте изменить эту опцию.</translation>
</message>
<message>
<source>Timeout (s)</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DialogEditGroup</name>
Expand Down
4 changes: 4 additions & 0 deletions translations/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ For NekoBox, this rewrites the underlying(localhost) DNS in Tun Mode, normal mod
<source>Download Test URL</source>
<translation>下载测试 URL</translation>
</message>
<message>
<source>Timeout (s)</source>
<translation>超时(秒)</translation>
</message>
</context>
<context>
<name>DialogEditGroup</name>
Expand Down
2 changes: 2 additions & 0 deletions ui/dialog_basic_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ DialogBasicSettings::DialogBasicSettings(QWidget *parent)
D_LOAD_INT(inbound_socks_port)
D_LOAD_INT_ENABLE(inbound_http_port, http_enable)
D_LOAD_INT(test_concurrent)
D_LOAD_INT(test_download_timeout)
D_LOAD_STRING(test_latency_url)
D_LOAD_STRING(test_download_url)

Expand Down Expand Up @@ -263,6 +264,7 @@ void DialogBasicSettings::accept() {
D_SAVE_INT(inbound_socks_port)
D_SAVE_INT_ENABLE(inbound_http_port, http_enable)
D_SAVE_INT(test_concurrent)
D_SAVE_INT(test_download_timeout)
D_SAVE_STRING(test_latency_url)
D_SAVE_STRING(test_download_url)

Expand Down
16 changes: 13 additions & 3 deletions ui/dialog_basic_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
</item>
<item>
<widget class="QGroupBox" name="horizontalGroupBox">
<layout class="QHBoxLayout" name="horizontalLayout_12">
<layout class="QHBoxLayout" name="horizontalLayout_12" stretch="0,8,1,1">
<item>
<widget class="QLabel" name="label_19">
<property name="text">
Expand All @@ -183,6 +183,16 @@
<item>
<widget class="MyLineEdit" name="test_download_url"/>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>Timeout (s)</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="test_download_timeout"/>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -684,8 +694,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>198</width>
<height>58</height>
<width>568</width>
<height>297</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
Expand Down
1 change: 1 addition & 0 deletions ui/mainwindow_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ void MainWindow::speedtest_current_group(int mode) {
req.set_full_in_out(full_test_flags.contains("4"));

req.set_full_speed_url(NekoGui::dataStore->test_download_url.toStdString());
req.set_full_speed_timeout(NekoGui::dataStore->test_download_timeout);
} else if (mode == libcore::TcpPing) {
req.set_address(profile->bean->DisplayAddress().toStdString());
}
Expand Down

0 comments on commit 9fe08ca

Please sign in to comment.