From e492c6573c96e75482f96e35e20dea10bfefe895 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 18 Feb 2025 13:59:10 +0800 Subject: [PATCH] feat: Modified the logic for AI Tool (#7894) --- backend/app/dto/ai.go | 10 +++---- backend/app/service/ai.go | 29 ++++++++++++++++++-- backend/utils/common/common.go | 20 ++++++++++++++ frontend/src/api/interface/ai.ts | 2 +- frontend/src/views/ai/model/domain/index.vue | 5 ++-- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/backend/app/dto/ai.go b/backend/app/dto/ai.go index 9835a8487..c6eeb92fa 100644 --- a/backend/app/dto/ai.go +++ b/backend/app/dto/ai.go @@ -11,11 +11,11 @@ type OllamaModelName struct { } type OllamaBindDomain struct { - Domain string `json:"domain" validate:"required"` - AppInstallID uint `json:"appInstallID" validate:"required"` - SSLID uint `json:"sslID"` - AllowIPs []string `json:"allowIPs"` - WebsiteID uint `json:"websiteID"` + Domain string `json:"domain" validate:"required"` + AppInstallID uint `json:"appInstallID" validate:"required"` + SSLID uint `json:"sslID"` + WebsiteID uint `json:"websiteID"` + IPList string `json:"ipList"` } type OllamaBindDomainReq struct { diff --git a/backend/app/service/ai.go b/backend/app/service/ai.go index 4efe68032..4de584af6 100644 --- a/backend/app/service/ai.go +++ b/backend/app/service/ai.go @@ -3,6 +3,7 @@ package service import ( "context" "fmt" + "github.com/1Panel-dev/1Panel/backend/utils/common" "io" "os" "os/exec" @@ -203,6 +204,16 @@ func (u *AIToolService) BindDomain(req dto.OllamaBindDomain) error { if nginxInstall.ID == 0 { return buserr.New("ErrOpenrestyInstall") } + var ( + ipList []string + err error + ) + if len(req.IPList) > 0 { + ipList, err = common.HandleIPList(req.IPList) + if err != nil { + return err + } + } createWebsiteReq := request.WebsiteCreate{ PrimaryDomain: req.Domain, Alias: strings.ToLower(req.Domain), @@ -218,8 +229,10 @@ func (u *AIToolService) BindDomain(req dto.OllamaBindDomain) error { if err != nil { return err } - if err = ConfigAllowIPs(req.AllowIPs, website); err != nil { - return err + if len(ipList) > 0 { + if err = ConfigAllowIPs(ipList, website); err != nil { + return err + } } if req.SSLID > 0 { sslReq := request.WebsiteHTTPSOp{ @@ -263,12 +276,22 @@ func (u *AIToolService) UpdateBindDomain(req dto.OllamaBindDomain) error { if nginxInstall.ID == 0 { return buserr.New("ErrOpenrestyInstall") } + var ( + ipList []string + err error + ) + if len(req.IPList) > 0 { + ipList, err = common.HandleIPList(req.IPList) + if err != nil { + return err + } + } websiteService := NewIWebsiteService() website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebsiteID)) if err != nil { return err } - if err = ConfigAllowIPs(req.AllowIPs, website); err != nil { + if err = ConfigAllowIPs(ipList, website); err != nil { return err } if req.SSLID > 0 { diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index 88af6e683..9ce53df07 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -3,6 +3,7 @@ package common import ( "crypto/rand" "fmt" + "github.com/1Panel-dev/1Panel/backend/buserr" "github.com/gin-gonic/gin" "io" mathRand "math/rand" @@ -351,3 +352,22 @@ func GetLang(c *gin.Context) string { } return lang } + +func HandleIPList(content string) ([]string, error) { + ipList := strings.Split(content, "\n") + var res []string + for _, ip := range ipList { + if ip == "" { + continue + } + if net.ParseIP(ip) != nil { + res = append(res, ip) + continue + } + if _, _, err := net.ParseCIDR(ip); err != nil { + return nil, buserr.New("ErrParseIP") + } + res = append(res, ip) + } + return res, nil +} diff --git a/frontend/src/api/interface/ai.ts b/frontend/src/api/interface/ai.ts index de23ec21b..fef177397 100644 --- a/frontend/src/api/interface/ai.ts +++ b/frontend/src/api/interface/ai.ts @@ -83,7 +83,7 @@ export namespace AI { export interface BindDomain { domain: string; sslID: number; - allowIPs: string[]; + ipList: string; appInstallID: number; websiteID?: number; } diff --git a/frontend/src/views/ai/model/domain/index.vue b/frontend/src/views/ai/model/domain/index.vue index 9b161429e..7aa847624 100644 --- a/frontend/src/views/ai/model/domain/index.vue +++ b/frontend/src/views/ai/model/domain/index.vue @@ -30,7 +30,7 @@ :rows="3" type="textarea" clearable - v-model.trim="req.ipList" + v-model="req.ipList" :placeholder="$t('xpack.waf.ipGroupHelper')" /> @@ -128,7 +128,7 @@ const req = ref({ websiteID: 0, }); const rules = reactive({ - domain: [Rules.requiredInput], + domain: [Rules.domainWithPort], }); const emit = defineEmits(['search']); @@ -196,7 +196,6 @@ const onSubmit = async (formEl: FormInstance | undefined) => { if (!formEl) return; formEl.validate(async (valid) => { if (!valid) return; - req.value.allowIPs = req.value.ipList.split('\n'); if (operate.value === 'update') { await updateBindDomain(req.value); } else {