mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-14 12:23:43 +00:00
fix: tracker cdn retry issue
This commit is contained in:
1 parent
20130ae6d0
commit
7fdba12376
3 files changed
+30
-11
No files matched your search
+9
-4
@@ -147,12 +147,12 @@ func PutConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
func DoProxy(w http.ResponseWriter, r *http.Request) {
|
func DoProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
target := r.Header.Get("X-Target-Uri")
|
target := r.Header.Get("X-Target-Uri")
|
||||||
if target == "" {
|
if target == "" {
|
||||||
WriteJson(w, model.NewErrorResult("param invalid: X-Target-Uri", model.CodeInvalidParam))
|
writeError(w, "param invalid: X-Target-Uri")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
targetUrl, err := url.Parse(target)
|
targetUrl, err := url.Parse(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteJson(w, model.NewErrorResult(err.Error()))
|
writeError(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.RequestURI = ""
|
r.RequestURI = ""
|
||||||
@@ -160,7 +160,7 @@ func DoProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
r.Host = targetUrl.Host
|
r.Host = targetUrl.Host
|
||||||
resp, err := http.DefaultClient.Do(r)
|
resp, err := http.DefaultClient.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteJson(w, model.NewErrorResult(err.Error()))
|
writeError(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -179,11 +179,16 @@ func DoProxy(w http.ResponseWriter, r *http.Request) {
|
|||||||
reader = resp.Body
|
reader = resp.Body
|
||||||
}
|
}
|
||||||
if _, err := io.Copy(w, reader); err != nil {
|
if _, err := io.Copy(w, reader); err != nil {
|
||||||
WriteJson(w, model.NewErrorResult(err.Error()))
|
writeError(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, msg string) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
}
|
||||||
|
|
||||||
func getServerConfig() *download.DownloaderStoreConfig {
|
func getServerConfig() *download.DownloaderStoreConfig {
|
||||||
_, cfg, _ := Downloader.GetConfig()
|
_, cfg, _ := Downloader.GetConfig()
|
||||||
return cfg
|
return cfg
|
||||||
|
|||||||
+14
-4
@@ -229,6 +229,15 @@ func TestDoProxy(t *testing.T) {
|
|||||||
t.Errorf("DoProxy() got = %v, want %v", got, want)
|
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) {
|
func TestAuthorization(t *testing.T) {
|
||||||
@@ -307,14 +316,15 @@ func doHttpRequest0(method string, path string, headers map[string]string, body
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if response.StatusCode != http.StatusOK {
|
|
||||||
panic(fmt.Sprintf("http request failed, status code: %d", response.StatusCode))
|
|
||||||
}
|
|
||||||
return response.StatusCode, respBody
|
return response.StatusCode, respBody
|
||||||
}
|
}
|
||||||
|
|
||||||
func doHttpRequest[T any](method string, path string, headers map[string]string, body any) (int, *model.Result[T]) {
|
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]
|
var r model.Result[T]
|
||||||
if err := json.Unmarshal(respBody, &r); err != nil {
|
if err := json.Unmarshal(respBody, &r); err != nil {
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class AppController extends GetxController {
|
|||||||
var allFail = true;
|
var allFail = true;
|
||||||
for (var cdn in allTrackerSubscribeUrlCdns[u]!) {
|
for (var cdn in allTrackerSubscribeUrlCdns[u]!) {
|
||||||
try {
|
try {
|
||||||
result.addAll(await _fetchTrackers(u));
|
result.addAll(await _fetchTrackers(cdn));
|
||||||
allFail = false;
|
allFail = false;
|
||||||
break;
|
break;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -168,10 +168,14 @@ class AppController extends GetxController {
|
|||||||
Future<List<String>> _fetchTrackers(String subscribeUrl) async {
|
Future<List<String>> _fetchTrackers(String subscribeUrl) async {
|
||||||
final resp = await proxyRequest(subscribeUrl);
|
final resp = await proxyRequest(subscribeUrl);
|
||||||
if (resp.statusCode != 200) {
|
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();
|
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 {
|
_initDownloaderConfig() async {
|
||||||
|
|||||||
Reference in new issue
Block a user