Skip to content

Commit d661645

Browse files
author
ssonglius11
committed
feat: 增加服务器代理设置
1 parent c189c48 commit d661645

File tree

15 files changed

+205
-59
lines changed

15 files changed

+205
-59
lines changed

backend/app/api/v1/setting.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"encoding/base64"
45
"errors"
56
"os"
67
"path"
@@ -60,6 +61,37 @@ func (b *BaseApi) UpdateSetting(c *gin.Context) {
6061
helper.SuccessWithData(c, nil)
6162
}
6263

64+
// @Tags System Setting
65+
// @Summary Update proxy setting
66+
// @Description 服务器代理配置
67+
// @Accept json
68+
// @Param request body dto.ProxyUpdate true "request"
69+
// @Success 200
70+
// @Security ApiKeyAuth
71+
// @Router /settings/proxy/update [post]
72+
// @x-panel-log {"bodyKeys":["proxyUrl","proxyPort"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"服务器代理配置 [proxyPort]:[proxyPort]","formatEN":"set proxy [proxyPort]:[proxyPort]."}
73+
func (b *BaseApi) UpdateProxy(c *gin.Context) {
74+
var req dto.ProxyUpdate
75+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
76+
return
77+
}
78+
79+
if len(req.ProxyPasswd) != 0 && len(req.ProxyType) != 0 {
80+
pass, err := base64.StdEncoding.DecodeString(req.ProxyPasswd)
81+
if err != nil {
82+
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
83+
return
84+
}
85+
req.ProxyPasswd = string(pass)
86+
}
87+
88+
if err := settingService.UpdateProxy(req); err != nil {
89+
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
90+
return
91+
}
92+
helper.SuccessWithData(c, nil)
93+
}
94+
6395
// @Tags System Setting
6496
// @Summary Update system setting
6597
// @Description 隐藏高级功能菜单

backend/app/dto/setting.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ type SettingInfo struct {
5858
SnapshotIgnore string `json:"snapshotIgnore"`
5959
XpackHideMenu string `json:"xpackHideMenu"`
6060
NoAuthSetting string `json:"noAuthSetting"`
61+
62+
ProxyUrl string `json:"proxyUrl"`
63+
ProxyType string `json:"proxyType"`
64+
ProxyPort string `json:"proxyPort"`
65+
ProxyUser string `json:"proxyUser"`
66+
ProxyPasswd string `json:"proxyPasswd"`
67+
ProxyPasswdKeep string `json:"proxyPasswdKeep"`
6168
}
6269

6370
type SettingUpdate struct {
@@ -160,6 +167,15 @@ type Upgrade struct {
160167
Version string `json:"version" validate:"required"`
161168
}
162169

170+
type ProxyUpdate struct {
171+
ProxyUrl string `json:"proxyUrl"`
172+
ProxyType string `json:"proxyType"`
173+
ProxyPort string `json:"proxyPort"`
174+
ProxyUser string `json:"proxyUser"`
175+
ProxyPasswd string `json:"proxyPasswd"`
176+
ProxyPasswdKeep string `json:"proxyPasswdKeep"`
177+
}
178+
163179
type CleanData struct {
164180
SystemClean []CleanTree `json:"systemClean"`
165181
UploadClean []CleanTree `json:"uploadClean"`

backend/app/service/app.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/1Panel-dev/1Panel/backend/utils/docker"
2727
"github.com/1Panel-dev/1Panel/backend/utils/files"
2828
http2 "github.com/1Panel-dev/1Panel/backend/utils/http"
29+
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
2930
"gopkg.in/yaml.v3"
3031
)
3132

@@ -226,21 +227,16 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response.
226227
if appDetailDTO.DockerCompose == "" {
227228
filename := filepath.Base(appDetailDTO.DownloadUrl)
228229
dockerComposeUrl := fmt.Sprintf("%s%s", strings.TrimSuffix(appDetailDTO.DownloadUrl, filename), "docker-compose.yml")
229-
composeRes, err := http.Get(dockerComposeUrl)
230+
statusCode, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet)
230231
if err != nil {
231232
return appDetailDTO, buserr.WithDetail("ErrGetCompose", err.Error(), err)
232233
}
233-
defer composeRes.Body.Close()
234-
bodyContent, err := io.ReadAll(composeRes.Body)
235-
if err != nil {
236-
return appDetailDTO, buserr.WithDetail("ErrGetCompose", err.Error(), err)
237-
}
238-
if composeRes.StatusCode > 200 {
239-
return appDetailDTO, buserr.WithDetail("ErrGetCompose", string(bodyContent), err)
234+
if statusCode > 200 {
235+
return appDetailDTO, buserr.WithDetail("ErrGetCompose", string(composeRes), err)
240236
}
241-
detail.DockerCompose = string(bodyContent)
237+
detail.DockerCompose = string(composeRes)
242238
_ = appDetailRepo.Update(context.Background(), detail)
243-
appDetailDTO.DockerCompose = string(bodyContent)
239+
appDetailDTO.DockerCompose = string(composeRes)
244240
}
245241

246242
appDetailDTO.HostMode = isHostModel(appDetailDTO.DockerCompose)
@@ -840,19 +836,14 @@ func (a AppService) SyncAppListFromRemote() (err error) {
840836
global.LOG.Infof("Starting synchronization of application details...")
841837
for _, l := range list.Apps {
842838
app := appsMap[l.AppProperty.Key]
843-
iconRes, err := http.Get(l.Icon)
844-
if err != nil {
845-
return err
846-
}
847-
body, err := io.ReadAll(iconRes.Body)
839+
_, iconRes, err := httpUtil.HandleGet(l.Icon, http.MethodGet)
848840
if err != nil {
849841
return err
850842
}
851843
iconStr := ""
852-
if !strings.Contains(string(body), "<xml>") {
853-
iconStr = base64.StdEncoding.EncodeToString(body)
844+
if !strings.Contains(string(iconRes), "<xml>") {
845+
iconStr = base64.StdEncoding.EncodeToString(iconRes)
854846
}
855-
_ = iconRes.Body.Close()
856847

857848
app.Icon = iconStr
858849
app.TagsKey = l.AppProperty.Tags
@@ -872,16 +863,11 @@ func (a AppService) SyncAppListFromRemote() (err error) {
872863

873864
if _, ok := InitTypes[app.Type]; ok {
874865
dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml")
875-
composeRes, err := http.Get(dockerComposeUrl)
876-
if err != nil {
877-
return err
878-
}
879-
defer composeRes.Body.Close()
880-
bodyContent, err := io.ReadAll(composeRes.Body)
866+
_, composeRes, err := httpUtil.HandleGet(dockerComposeUrl, http.MethodGet)
881867
if err != nil {
882868
return err
883869
}
884-
detail.DockerCompose = string(bodyContent)
870+
detail.DockerCompose = string(composeRes)
885871
} else {
886872
detail.DockerCompose = ""
887873
}

backend/app/service/app_utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717

18+
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
1819
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
1920
"github.com/docker/docker/api/types/container"
2021

@@ -504,7 +505,7 @@ func upgradeInstall(installID uint, detailID uint, backup, pullImage bool) error
504505
_ = appDetailRepo.Update(context.Background(), detail)
505506
}
506507
go func() {
507-
_, _ = http.Get(detail.DownloadCallBackUrl)
508+
_, _, _ = httpUtil.HandleGet(detail.DownloadCallBackUrl, http.MethodGet)
508509
}()
509510
}
510511

@@ -782,7 +783,7 @@ func copyData(app model.App, appDetail model.AppDetail, appInstall *model.AppIns
782783
return
783784
}
784785
go func() {
785-
_, _ = http.Get(appDetail.DownloadCallBackUrl)
786+
_, _, _ = httpUtil.HandleGet(appDetail.DownloadCallBackUrl, http.MethodGet)
786787
}()
787788
}
788789
appKey := app.Key

backend/app/service/device_clean.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package service
33
import (
44
"context"
55
"fmt"
6-
"github.com/1Panel-dev/1Panel/backend/utils/docker"
7-
"github.com/docker/docker/api/types"
8-
"github.com/docker/docker/api/types/filters"
96
"os"
107
"path"
118
"sort"
129
"strings"
1310
"time"
1411

12+
"github.com/1Panel-dev/1Panel/backend/utils/docker"
13+
"github.com/docker/docker/api/types"
14+
"github.com/docker/docker/api/types/filters"
15+
1516
"github.com/1Panel-dev/1Panel/backend/app/dto"
1617
"github.com/1Panel-dev/1Panel/backend/global"
1718
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
@@ -514,6 +515,9 @@ func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree {
514515
func loadContainerTree() []dto.CleanTree {
515516
var treeData []dto.CleanTree
516517
client, err := docker.NewDockerClient()
518+
if err != nil {
519+
return treeData
520+
}
517521
diskUsage, err := client.DiskUsage(context.Background(), types.DiskUsageOptions{})
518522
if err != nil {
519523
return treeData

backend/app/service/runtime_utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/1Panel-dev/1Panel/backend/global"
2020
"github.com/1Panel-dev/1Panel/backend/utils/docker"
2121
"github.com/1Panel-dev/1Panel/backend/utils/files"
22+
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
2223
"github.com/pkg/errors"
2324
"github.com/subosito/gotenv"
2425
"gopkg.in/yaml.v3"
@@ -54,12 +55,10 @@ func handleNode(create request.RuntimeCreate, runtime *model.Runtime, fileOp fil
5455
}
5556

5657
go func() {
57-
res, err := http.Get(nodeDetail.DownloadCallBackUrl)
58-
if err != nil {
58+
if _ , _, err := httpUtil.HandleGet(nodeDetail.DownloadCallBackUrl, http.MethodGet); err != nil {
5959
global.LOG.Errorf("http request failed(handleNode), err: %v", err)
6060
return
6161
}
62-
res.Body.Close()
6362
}()
6463
go startRuntime(runtime)
6564

backend/app/service/setting.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ISettingService interface {
3333
GetSettingInfo() (*dto.SettingInfo, error)
3434
LoadInterfaceAddr() ([]string, error)
3535
Update(key, value string) error
36+
UpdateProxy(req dto.ProxyUpdate) error
3637
UpdatePassword(c *gin.Context, old, new string) error
3738
UpdatePort(port uint) error
3839
UpdateBindInfo(req dto.BindInfo) error
@@ -62,6 +63,12 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
6263
if err := json.Unmarshal(arr, &info); err != nil {
6364
return nil, err
6465
}
66+
if info.ProxyPasswdKeep != constant.StatusEnable {
67+
info.ProxyPasswd = ""
68+
} else {
69+
info.ProxyPasswd, _ = encrypt.StringDecrypt(info.ProxyPasswd)
70+
}
71+
6572
info.LocalTime = time.Now().Format("2006-01-02 15:04:05 MST -0700")
6673
return &info, err
6774
}
@@ -162,6 +169,29 @@ func (u *SettingService) UpdateBindInfo(req dto.BindInfo) error {
162169
return nil
163170
}
164171

172+
func (u *SettingService) UpdateProxy(req dto.ProxyUpdate) error {
173+
if err := settingRepo.Update("ProxyUrl", req.ProxyUrl); err != nil {
174+
return err
175+
}
176+
if err := settingRepo.Update("ProxyType", req.ProxyType); err != nil {
177+
return err
178+
}
179+
if err := settingRepo.Update("ProxyPort", req.ProxyPort); err != nil {
180+
return err
181+
}
182+
if err := settingRepo.Update("ProxyUser", req.ProxyUser); err != nil {
183+
return err
184+
}
185+
pass, _ := encrypt.StringEncrypt(req.ProxyPasswd)
186+
if err := settingRepo.Update("ProxyPasswd", pass); err != nil {
187+
return err
188+
}
189+
if err := settingRepo.Update("ProxyPasswdKeep", req.ProxyPasswdKeep); err != nil {
190+
return err
191+
}
192+
return nil
193+
}
194+
165195
func (u *SettingService) UpdatePort(port uint) error {
166196
if common.ScanPort(int(port)) {
167197
return buserr.WithDetail(constant.ErrPortInUsed, port, nil)

backend/app/service/upgrade.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package service
33
import (
44
"encoding/json"
55
"fmt"
6-
"io"
76
"net/http"
87
"os"
98
"path"
@@ -16,6 +15,7 @@ import (
1615
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
1716
"github.com/1Panel-dev/1Panel/backend/utils/common"
1817
"github.com/1Panel-dev/1Panel/backend/utils/files"
18+
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
1919
)
2020

2121
type UpgradeService struct{}
@@ -258,15 +258,13 @@ func (u *UpgradeService) loadVersion(isLatest bool, currentVersion, mode string)
258258
if !isLatest {
259259
path = fmt.Sprintf("%s/%s/latest.current", global.CONF.System.RepoUrl, mode)
260260
}
261-
latestVersionRes, err := http.Get(path)
261+
_, latestVersionRes, err := httpUtil.HandleGet(path, http.MethodGet)
262262
if err != nil {
263263
global.LOG.Errorf("load latest version from oss failed, err: %v", err)
264264
return ""
265265
}
266-
defer latestVersionRes.Body.Close()
267-
versionByte, err := io.ReadAll(latestVersionRes.Body)
268-
version := string(versionByte)
269-
if err != nil || strings.Contains(version, "<") {
266+
version := string(latestVersionRes)
267+
if strings.Contains(version, "<") {
270268
global.LOG.Errorf("load latest version from oss failed, err: %v", version)
271269
return ""
272270
}
@@ -275,7 +273,7 @@ func (u *UpgradeService) loadVersion(isLatest bool, currentVersion, mode string)
275273
}
276274

277275
versionMap := make(map[string]string)
278-
if err := json.Unmarshal(versionByte, &versionMap); err != nil {
276+
if err := json.Unmarshal(latestVersionRes, &versionMap); err != nil {
279277
global.LOG.Errorf("load latest version from oss failed (error unmarshal), err: %v", err)
280278
return ""
281279
}
@@ -321,16 +319,11 @@ func (u *UpgradeService) checkVersion(v2, v1 string) string {
321319
}
322320

323321
func (u *UpgradeService) loadReleaseNotes(path string) (string, error) {
324-
releaseNotes, err := http.Get(path)
322+
_, releaseNotes, err := httpUtil.HandleGet(path, http.MethodGet)
325323
if err != nil {
326324
return "", err
327325
}
328-
defer releaseNotes.Body.Close()
329-
release, err := io.ReadAll(releaseNotes.Body)
330-
if err != nil {
331-
return "", err
332-
}
333-
return string(release), nil
326+
return string(releaseNotes), nil
334327
}
335328

336329
func loadArch() (string, error) {

backend/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func Init() {
8787
migrations.AddRedisCommand,
8888
migrations.AddMonitorMenu,
8989
migrations.AddFtp,
90+
migrations.AddProxy,
9091
})
9192
if err := m.Migrate(); err != nil {
9293
global.LOG.Error(err)

backend/init/migration/migrations/v_1_10.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,28 @@ var AddFtp = &gormigrate.Migration{
213213
return nil
214214
},
215215
}
216+
217+
var AddProxy = &gormigrate.Migration{
218+
ID: "20240528-add-proxy",
219+
Migrate: func(tx *gorm.DB) error {
220+
if err := tx.Create(&model.Setting{Key: "ProxyType", Value: ""}).Error; err != nil {
221+
return err
222+
}
223+
if err := tx.Create(&model.Setting{Key: "ProxyUrl", Value: ""}).Error; err != nil {
224+
return err
225+
}
226+
if err := tx.Create(&model.Setting{Key: "ProxyPort", Value: ""}).Error; err != nil {
227+
return err
228+
}
229+
if err := tx.Create(&model.Setting{Key: "ProxyUser", Value: ""}).Error; err != nil {
230+
return err
231+
}
232+
if err := tx.Create(&model.Setting{Key: "ProxyPasswd", Value: ""}).Error; err != nil {
233+
return err
234+
}
235+
if err := tx.Create(&model.Setting{Key: "ProxyPasswdKeep", Value: ""}).Error; err != nil {
236+
return err
237+
}
238+
return nil
239+
},
240+
}

0 commit comments

Comments
 (0)