fix baidu
This commit is contained in:
1 parent
eca3a1dee9
commit
85922d5c2e
3 files changed
+29
-4
No files matched your search
@@ -152,12 +152,13 @@ func (d *Driver) ResolveDownload(ctx context.Context, req driver.DownloadRequest
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
downloadURL = d.resolveDownloadRedirect(ctx, downloadURL)
|
||||
headers := http.Header{"User-Agent": []string{defaultUA}}
|
||||
return &domain.DownloadInfo{
|
||||
URL: downloadURL,
|
||||
Headers: headers,
|
||||
Mode: mode,
|
||||
Expiration: 8 * time.Hour,
|
||||
Expiration: time.Hour,
|
||||
ForceProxy: mode != domain.DownloadRedirect,
|
||||
Size: item.Size,
|
||||
FileName: item.Name,
|
||||
@@ -166,6 +167,28 @@ func (d *Driver) ResolveDownload(ctx context.Context, req driver.DownloadRequest
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d *Driver) resolveDownloadRedirect(ctx context.Context, rawURL string) string {
|
||||
client := *d.client
|
||||
client.CheckRedirect = func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, rawURL, nil)
|
||||
if err != nil {
|
||||
return rawURL
|
||||
}
|
||||
req.Header.Set("User-Agent", defaultUA)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return rawURL
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
location, err := resp.Location()
|
||||
if err != nil {
|
||||
return rawURL
|
||||
}
|
||||
return location.String()
|
||||
}
|
||||
|
||||
func (d *Driver) downloadURLWithToken(raw string) (string, error) {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
|
||||
@@ -28,4 +28,5 @@ func writeDynamicRedirectHeaders(header http.Header) {
|
||||
header.Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
||||
header.Set("Pragma", "no-cache")
|
||||
header.Set("Expires", "0")
|
||||
header.Set("Referrer-Policy", "no-referrer")
|
||||
}
|
||||
@@ -111,9 +111,10 @@ func assertDynamicRedirect(t *testing.T, response *http.Response) {
|
||||
t.Fatalf("Location = %q", got)
|
||||
}
|
||||
wantHeaders := map[string]string{
|
||||
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
|
||||
"Pragma": "no-cache",
|
||||
"Expires": "0",
|
||||
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
|
||||
"Pragma": "no-cache",
|
||||
"Expires": "0",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
}
|
||||
for name, want := range wantHeaders {
|
||||
if got := response.Header.Get(name); got != want {
|
||||
|
||||
Reference in new issue
Block a user