2023-03-23 20:42:01 +08:00
|
|
|
package updater
|
|
|
|
|
|
|
|
import (
|
2024-05-17 11:49:09 +08:00
|
|
|
"context"
|
2023-03-23 20:42:01 +08:00
|
|
|
"io"
|
2024-05-17 11:49:09 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
2023-03-23 20:42:01 +08:00
|
|
|
)
|
|
|
|
|
2024-09-22 13:57:57 +08:00
|
|
|
const defaultHttpTimeout = time.Second * 90
|
|
|
|
|
2024-05-17 11:49:09 +08:00
|
|
|
func downloadForBytes(url string) ([]byte, error) {
|
2024-09-22 13:57:57 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultHttpTimeout)
|
2024-05-17 11:49:09 +08:00
|
|
|
defer cancel()
|
2024-09-09 16:08:48 +08:00
|
|
|
resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, nil, nil)
|
2024-05-17 11:49:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return io.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveFile(bytes []byte, path string) error {
|
|
|
|
return os.WriteFile(path, bytes, 0o644)
|
|
|
|
}
|