feat: sync appstore use proxy (#8525)

This commit is contained in:
ChengPlay 2025-04-30 18:41:52 +08:00 committed by GitHub
parent f94a04b067
commit 043bfc8b45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 2 deletions

View file

@ -834,9 +834,10 @@ func getAppFromRepo(downloadPath string) error {
global.LOG.Infof("[AppStore] download file from %s", downloadUrl)
fileOp := files.NewFileOp()
packagePath := filepath.Join(global.Dir.ResourceDir, filepath.Base(downloadUrl))
if err := fileOp.DownloadFile(downloadUrl, packagePath); err != nil {
if err := files.DownloadFileWithProxy(downloadUrl, packagePath); err != nil {
return err
}
if err := fileOp.Decompress(packagePath, global.Dir.ResourceDir, files.SdkZip, ""); err != nil {
return err
}

View file

@ -2,7 +2,10 @@ package files
import (
"bufio"
"bytes"
"fmt"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/utils/req_helper"
"io"
"net/http"
"os"
@ -167,3 +170,22 @@ func IsEmptyDir(dir string) bool {
_, err = f.Readdirnames(1)
return err == io.EOF
}
func DownloadFileWithProxy(url, dst string) error {
_, resp, err := req_helper.HandleRequest(url, http.MethodGet, constant.TimeOut5m)
if err != nil {
return err
}
out, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create download file [%s] error, err %s", dst, err.Error())
}
defer out.Close()
reader := bytes.NewReader(resp)
if _, err = io.Copy(out, reader); err != nil {
return fmt.Errorf("save download file [%s] error, err %s", dst, err.Error())
}
return nil
}

View file

@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"errors"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
"io"
"net"
"net/http"
@ -52,7 +53,7 @@ func HandleRequest(url, method string, timeout int) (int, []byte, error) {
}
}()
transport := loadRequestTransport()
transport := xpack.LoadRequestTransport()
client := http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

View file

@ -3,8 +3,12 @@
package xpack
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/model"
@ -64,3 +68,16 @@ func GetAlert(alertBase dto.AlertBase) uint {
func PushAlert(pushAlert dto.PushAlert) error {
return nil
}
func LoadRequestTransport() *http.Transport {
return &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: 60 * time.Second,
KeepAlive: 60 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 15 * time.Second,
}
}