fix: tracker cdn retry issue

This commit is contained in:
liwei committed 2023-03-06 17:07:00 +08:00
1 parent 20130ae6d0
commit 7fdba12376
3 files changed
+30 -11

No files matched your search

+9 -4
View File
@@ -147,12 +147,12 @@ func PutConfig(w http.ResponseWriter, r *http.Request) {
func DoProxy(w http.ResponseWriter, r *http.Request) {
target := r.Header.Get("X-Target-Uri")
if target == "" {
WriteJson(w, model.NewErrorResult("param invalid: X-Target-Uri", model.CodeInvalidParam))
writeError(w, "param invalid: X-Target-Uri")
return
}
targetUrl, err := url.Parse(target)
if err != nil {
WriteJson(w, model.NewErrorResult(err.Error()))
writeError(w, err.Error())
return
}
r.RequestURI = ""
@@ -160,7 +160,7 @@ func DoProxy(w http.ResponseWriter, r *http.Request) {
r.Host = targetUrl.Host
resp, err := http.DefaultClient.Do(r)
if err != nil {
WriteJson(w, model.NewErrorResult(err.Error()))
writeError(w, err.Error())
return
}
defer resp.Body.Close()
@@ -179,11 +179,16 @@ func DoProxy(w http.ResponseWriter, r *http.Request) {
reader = resp.Body
}
if _, err := io.Copy(w, reader); err != nil {
WriteJson(w, model.NewErrorResult(err.Error()))
writeError(w, err.Error())
return
}
}
func writeError(w http.ResponseWriter, msg string) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(msg))
}
func getServerConfig() *download.DownloaderStoreConfig {
_, cfg, _ := Downloader.GetConfig()
return cfg
+14 -4
View File
@@ -229,6 +229,15 @@ func TestDoProxy(t *testing.T) {
t.Errorf("DoProxy() got = %v, want %v", got, want)
}
})
doTest(func() {
code, _ := doHttpRequest0(http.MethodGet, "/api/v1/proxy", map[string]string{
"X-Target-Uri": "https://github.com/GopeedLab/gopeed/raw/695da7ea87d2b455552b709d3cb4d7879484d4d1/NOT_FOUND",
}, nil)
if code != http.StatusNotFound {
t.Errorf("DoProxy() got = %v, want %v", code, http.StatusNotFound)
}
})
}
func TestAuthorization(t *testing.T) {
@@ -307,14 +316,15 @@ func doHttpRequest0(method string, path string, headers map[string]string, body
if err != nil {
panic(err)
}
if response.StatusCode != http.StatusOK {
panic(fmt.Sprintf("http request failed, status code: %d", response.StatusCode))
}
return response.StatusCode, respBody
}
func doHttpRequest[T any](method string, path string, headers map[string]string, body any) (int, *model.Result[T]) {
_, respBody := doHttpRequest0(method, path, headers, body)
statusCode, respBody := doHttpRequest0(method, path, headers, body)
if statusCode != http.StatusOK {
panic(fmt.Sprintf("http request failed, status code: %d", statusCode))
}
var r model.Result[T]
if err := json.Unmarshal(respBody, &r); err != nil {
@@ -120,7 +120,7 @@ class AppController extends GetxController {
var allFail = true;
for (var cdn in allTrackerSubscribeUrlCdns[u]!) {
try {
result.addAll(await _fetchTrackers(u));
result.addAll(await _fetchTrackers(cdn));
allFail = false;
break;
} catch (e) {
@@ -168,10 +168,14 @@ class AppController extends GetxController {
Future<List<String>> _fetchTrackers(String subscribeUrl) async {
final resp = await proxyRequest(subscribeUrl);
if (resp.statusCode != 200) {
throw Exception('Failed to get trackers');
throw Exception(
'Failed to get trackers, status code: ${resp.statusCode}');
}
if (resp.data == null || resp.data!.isEmpty) {
throw Exception('Failed to get trackers, data is null');
}
const ls = LineSplitter();
return ls.convert(resp.data).where((e) => e.isNotEmpty).toList();
return ls.convert(resp.data!).where((e) => e.isNotEmpty).toList();
}
_initDownloaderConfig() async {