fix: 解决 HTTP 类型代理失败的问题 (#5306)

pull/5315/head
ssongliu 2024-06-05 23:46:11 +08:00 committed by GitHub
parent 97e800ee15
commit d83402b819
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 41 additions and 27 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -27,6 +26,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
http2 "github.com/1Panel-dev/1Panel/backend/utils/http" http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http" httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -710,16 +710,11 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
} }
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode) versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode)
versionRes, err := http2.GetHttpRes(versionUrl) _, versionRes, err := http2.HandleGet(versionUrl, http.MethodGet)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer versionRes.Body.Close() lastModifiedStr := string(versionRes)
body, err := io.ReadAll(versionRes.Body)
if err != nil {
return nil, err
}
lastModifiedStr := string(body)
lastModified, err := strconv.Atoi(lastModifiedStr) lastModified, err := strconv.Atoi(lastModifiedStr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -845,13 +840,18 @@ func (a AppService) SyncAppListFromRemote() (err error) {
oldAppIds = append(oldAppIds, old.ID) oldAppIds = append(oldAppIds, old.ID)
} }
var transport *http.Transport
ok, transportItem := xpack.LoadRequestTransport()
if ok {
transport = transportItem
}
baseRemoteUrl := fmt.Sprintf("%s/%s/1panel", global.CONF.System.AppRepo, global.CONF.System.Mode) baseRemoteUrl := fmt.Sprintf("%s/%s/1panel", global.CONF.System.AppRepo, global.CONF.System.Mode)
appsMap := getApps(oldApps, list.Apps) appsMap := getApps(oldApps, list.Apps)
global.LOG.Infof("Starting synchronization of application details...") global.LOG.Infof("Starting synchronization of application details...")
for _, l := range list.Apps { for _, l := range list.Apps {
app := appsMap[l.AppProperty.Key] app := appsMap[l.AppProperty.Key]
_, iconRes, err := httpUtil.HandleGet(l.Icon, http.MethodGet) _, iconRes, err := httpUtil.HandleGetWithTransport(l.Icon, http.MethodGet, transport)
if err != nil { if err != nil {
return err return err
} }
@ -878,7 +878,7 @@ func (a AppService) SyncAppListFromRemote() (err error) {
if _, ok := InitTypes[app.Type]; ok { if _, ok := InitTypes[app.Type]; ok {
dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml") dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml")
_, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet) _, composeRes, err := httpUtil.HandleGetWithTransport(dockerComposeUrl, http.MethodGet, transport)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/docker/docker/api/types"
"math" "math"
"net/http" "net/http"
"os" "os"
@ -16,6 +15,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/docker/docker/api/types"
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http" httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
"github.com/1Panel-dev/1Panel/backend/utils/xpack" "github.com/1Panel-dev/1Panel/backend/utils/xpack"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@ -778,7 +779,7 @@ func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.App
} }
}() }()
if err = fileOp.DownloadFile(appDetail.DownloadUrl, filePath); err != nil { if err = fileOp.DownloadFileWithProxy(appDetail.DownloadUrl, filePath); err != nil {
global.LOG.Errorf("download app[%s] error %v", app.Name, err) global.LOG.Errorf("download app[%s] error %v", app.Name, err)
return return
} }

View File

@ -326,13 +326,15 @@ func (u *SnapshotService) HandleSnapshot(isCronjob bool, logPath string, req dto
func (u *SnapshotService) Delete(req dto.SnapshotBatchDelete) error { func (u *SnapshotService) Delete(req dto.SnapshotBatchDelete) error {
snaps, _ := snapshotRepo.GetList(commonRepo.WithIdsIn(req.Ids)) snaps, _ := snapshotRepo.GetList(commonRepo.WithIdsIn(req.Ids))
for _, snap := range snaps { for _, snap := range snaps {
targetAccounts, err := loadClientMap(snap.From) if req.DeleteWithFile {
if err != nil { targetAccounts, err := loadClientMap(snap.From)
return err if err != nil {
} return err
for _, item := range targetAccounts { }
global.LOG.Debugf("remove snapshot file %s.tar.gz from %s", snap.Name, item.backType) for _, item := range targetAccounts {
_, _ = item.client.Delete(path.Join(item.backupPath, "system_snapshot", snap.Name+".tar.gz")) global.LOG.Debugf("remove snapshot file %s.tar.gz from %s", snap.Name, item.backType)
_, _ = item.client.Delete(path.Join(item.backupPath, "system_snapshot", snap.Name+".tar.gz"))
}
} }
_ = snapshotRepo.DeleteStatus(snap.ID) _ = snapshotRepo.DeleteStatus(snap.ID)

View File

@ -108,7 +108,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
defer func() { defer func() {
global.Cron.Start() global.Cron.Start()
}() }()
if err := fileOp.DownloadFile(downloadPath+"/"+fileName, rootDir+"/"+fileName); err != nil { if err := fileOp.DownloadFileWithProxy(downloadPath+"/"+fileName, rootDir+"/"+fileName); err != nil {
global.LOG.Errorf("download service file failed, err: %v", err) global.LOG.Errorf("download service file failed, err: %v", err)
_ = settingRepo.Update("SystemStatus", "Free") _ = settingRepo.Update("SystemStatus", "Free")
return return

View File

@ -85,7 +85,7 @@ var AddTableSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "Language", Value: "zh"}).Error; err != nil { if err := tx.Create(&model.Setting{Key: "Language", Value: "zh"}).Error; err != nil {
return err return err
} }
if err := tx.Create(&model.Setting{Key: "Theme", Value: "auto"}).Error; err != nil { if err := tx.Create(&model.Setting{Key: "Theme", Value: "light"}).Error; err != nil {
return err return err
} }

View File

@ -11,6 +11,15 @@ import (
) )
func HandleGet(url, method string) (int, []byte, error) { func HandleGet(url, method string) (int, []byte, error) {
var transport *http.Transport
ok, transportItem := xpack.LoadRequestTransport()
if ok {
transport = transportItem
}
return HandleGetWithTransport(url, method, transport)
}
func HandleGetWithTransport(url, method string, transport *http.Transport) (int, []byte, error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
global.LOG.Errorf(" A panic occurred during handle request, error message: %v", r) global.LOG.Errorf(" A panic occurred during handle request, error message: %v", r)
@ -18,11 +27,7 @@ func HandleGet(url, method string) (int, []byte, error) {
} }
}() }()
client := http.Client{Timeout: 10 * time.Second} client := http.Client{Timeout: 10 * time.Second, Transport: transport}
ok, transport := xpack.LoadRequestTransport()
if ok {
client.Transport = transport
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
request, err := http.NewRequestWithContext(ctx, method, url, nil) request, err := http.NewRequestWithContext(ctx, method, url, nil)

View File

@ -116,7 +116,13 @@ interface DialogProps {
passwdKeep: string; passwdKeep: string;
} }
const acceptParams = (params: DialogProps): void => { const acceptParams = (params: DialogProps): void => {
form.proxyUrl = params.url || '127.0.0.1'; if (params.url) {
if (params.type === 'http' || params.type === 'https') {
form.proxyUrl = params.url.replaceAll(params.type + '://', '');
}
} else {
form.proxyUrl = '127.0.0.1';
}
form.proxyType = params.type || 'close'; form.proxyType = params.type || 'close';
form.proxyPortItem = params.port ? Number(params.port) : 7890; form.proxyPortItem = params.port ? Number(params.port) : 7890;
form.proxyUser = params.user; form.proxyUser = params.user;