Skip to content
Merged
5 changes: 2 additions & 3 deletions agent/app/service/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/1Panel-dev/1Panel/agent/utils/req_helper"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
"github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -1820,8 +1820,7 @@ func getAppCommonConfig(envs map[string]interface{}) request.AppContainerConfig
config.CpuQuota = 0
}
if memLimit, ok := envs[constant.MemoryLimit]; ok {
re := regexp.MustCompile(`(\d+)([A-Za-z]+)`)
matches := re.FindStringSubmatch(memLimit.(string))
matches := re.GetRegex(re.NumberAlphaPattern).FindStringSubmatch(memLimit.(string))
if len(matches) == 3 {
num, err := strconv.ParseFloat(matches[1], 64)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/common"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/build"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -1166,7 +1166,7 @@ func (u *ContainerService) DownloadContainerLogs(containerType, container, since
errCh := make(chan error)
go func() {
scanner := bufio.NewScanner(stdout)
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;?]*[A-Za-z]|\x1b=|\x1b>`)
var ansiRegex = re.GetRegex(re.AnsiEscapePattern)
for scanner.Scan() {
line := scanner.Text()
cleanLine := ansiRegex.ReplaceAllString(line, "")
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/database_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/compose"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/mysql"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/1Panel-dev/1Panel/agent/utils/mysql/client"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/copier"
Expand Down Expand Up @@ -623,7 +623,7 @@ func updateMyCnf(oldFiles []string, group string, param string, value interface{
isOn := false
hasGroup := false
hasKey := false
regItem, _ := regexp.Compile(`\[*\]`)
regItem := re.GetRegex(re.MysqlGroupPattern)
var newFiles []string
i := 0
for _, line := range oldFiles {
Expand Down
15 changes: 7 additions & 8 deletions agent/app/service/disk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package service
import (
"bufio"
"fmt"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"os"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/re"
)

func organizeDiskInfo(diskInfos []response.DiskBasicInfo) response.CompleteDiskInfo {
Expand Down Expand Up @@ -205,12 +206,10 @@ func getDiskType(rota string) string {
return "Unknown"
}

var kvRe = regexp.MustCompile(`([A-Za-z0-9_]+)=("([^"\\]|\\.)*"|[^ \t]+)`)

func parseKeyValuePairs(line string) map[string]string {
fields := make(map[string]string)

matches := kvRe.FindAllStringSubmatch(line, -1)
matches := re.GetRegex(re.DiskKeyValuePattern).FindAllStringSubmatch(line, -1)
for _, m := range matches {
key := m[1]
raw := m[2]
Expand Down
5 changes: 2 additions & 3 deletions agent/app/service/recycle_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/shirou/gopsutil/v4/disk"
)

Expand Down Expand Up @@ -184,8 +184,7 @@ func createClashDir(clashDir string) error {
}

func getRecycleBinDTOFromName(filename string) (*response.RecycleBinDTO, error) {
r := regexp.MustCompile(`_1p_file_1p_(.+)_p_(\d+)_(\d+)`)
matches := r.FindStringSubmatch(filename)
matches := re.GetRegex(re.RecycleBinFilePattern).FindStringSubmatch(filename)
if len(matches) != 4 {
return nil, fmt.Errorf("invalid filename format")
}
Expand Down
9 changes: 6 additions & 3 deletions agent/app/service/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/1Panel-dev/1Panel/agent/utils/env"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/pkg/errors"
"github.com/subosito/gotenv"
)
Expand Down Expand Up @@ -244,8 +245,10 @@ func (r *RuntimeService) Page(req request.RuntimeSearch) (int64, []response.Runt
runtimeDTO.Params[k] = v
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
if strings.Contains(k, "CONTAINER_PORT") {
r := regexp.MustCompile(`_(\d+)$`)
matches := r.FindStringSubmatch(k)
matches := re.GetRegex(re.TrailingDigitsPattern).FindStringSubmatch(k)
if len(matches) < 2 {
continue
}
containerPort, err := strconv.Atoi(v)
if err != nil {
continue
Expand Down Expand Up @@ -829,7 +832,7 @@ func (r *RuntimeService) GetPHPConfig(id uint) (*response.PHPConfig, error) {
if strings.HasPrefix(line, ";") {
continue
}
matches := regexp.MustCompile(`^\s*([a-z_]+)\s*=\s*(.*)$`).FindStringSubmatch(line)
matches := re.GetRegex(re.PhpAssignmentPattern).FindStringSubmatch(line)
if len(matches) == 3 {
params[matches[1]] = matches[2]
}
Expand Down
8 changes: 5 additions & 3 deletions agent/app/service/runtime_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/compose"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/pkg/errors"
"github.com/subosito/gotenv"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -979,8 +979,10 @@ func handleRuntimeDTO(res *response.RuntimeDTO, runtime model.Runtime) error {
for k, v := range envs {
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
if strings.Contains(k, "CONTAINER_PORT") {
r := regexp.MustCompile(`_(\d+)$`)
matches := r.FindStringSubmatch(k)
matches := re.GetRegex(re.TrailingDigitsPattern).FindStringSubmatch(k)
if len(matches) < 2 {
return fmt.Errorf("invalid container port key: %s", k)
}
containerPort, err := strconv.Atoi(v)
if err != nil {
return err
Expand Down
24 changes: 14 additions & 10 deletions agent/app/service/tensorrt_llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ package service
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"

"github.com/subosito/gotenv"
"gopkg.in/yaml.v3"

"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/app/model"
Expand All @@ -14,14 +23,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/compose"
"github.com/1Panel-dev/1Panel/agent/utils/docker"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/subosito/gotenv"
"gopkg.in/yaml.v3"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/1Panel-dev/1Panel/agent/utils/re"
)

type TensorRTLLMService struct{}
Expand Down Expand Up @@ -60,8 +62,10 @@ func (t TensorRTLLMService) Page(req request.TensorRTLLMSearch) response.TensorR
for k, v := range envs {
if strings.Contains(k, "CONTAINER_PORT") || strings.Contains(k, "HOST_PORT") {
if strings.Contains(k, "CONTAINER_PORT") {
r := regexp.MustCompile(`_(\d+)$`)
matches := r.FindStringSubmatch(k)
matches := re.GetRegex(re.TrailingDigitsPattern).FindStringSubmatch(k)
if len(matches) < 2 {
continue
}
containerPort, err := strconv.Atoi(v)
if err != nil {
continue
Expand Down
20 changes: 8 additions & 12 deletions agent/app/service/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os"
"path"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -46,6 +45,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/components"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"golang.org/x/crypto/bcrypt"
)

Expand Down Expand Up @@ -1859,27 +1859,24 @@ func (w WebsiteService) GetProxyCache(id uint) (res response.NginxProxyCache, er
if len(params) == 0 {
return
}
zoneRegexp := regexp.MustCompile(`keys_zone=proxy_cache_zone_of_[\w.]+:(\d+)([kmgt]?)`)
sizeRegexp := regexp.MustCompile(`max_size=([0-9.]+)([kmgt]?)`)
inactiveRegexp := regexp.MustCompile(`inactive=(\d+)([smhd])`)
for _, param := range params {
if match, _ := regexp.MatchString(`keys_zone=proxy_cache_zone_of_[\w.]+:\d+[kmgt]?`, param); match {
matches := zoneRegexp.FindStringSubmatch(param)
if re.GetRegex(re.ProxyCacheZonePattern).MatchString(param) {
matches := re.GetRegex(re.ProxyCacheZonePattern).FindStringSubmatch(param)
if len(matches) > 0 {
res.ShareCache, _ = strconv.Atoi(matches[1])
res.ShareCacheUnit = matches[2]
}
}

if match, _ := regexp.MatchString(`max_size=\d+(\.\d+)?[kmgt]?`, param); match {
matches := sizeRegexp.FindStringSubmatch(param)
if re.GetRegex(re.ProxyCacheMaxSizeValidationPattern).MatchString(param) {
matches := re.GetRegex(re.ProxyCacheMaxSizePattern).FindStringSubmatch(param)
if len(matches) > 0 {
res.CacheLimit, _ = strconv.ParseFloat(matches[1], 64)
res.CacheLimitUnit = matches[2]
}
}
if match, _ := regexp.MatchString(`inactive=\d+[smhd]`, param); match {
matches := inactiveRegexp.FindStringSubmatch(param)
if re.GetRegex(re.ProxyCacheInactivePattern).MatchString(param) {
matches := re.GetRegex(re.ProxyCacheInactivePattern).FindStringSubmatch(param)
if len(matches) > 0 {
res.CacheExpire, _ = strconv.Atoi(matches[1])
res.CacheExpireUnit = matches[2]
Expand Down Expand Up @@ -2598,8 +2595,7 @@ func (w WebsiteService) GetAntiLeech(id uint) (*response.NginxAntiLeechRes, erro
}
if lDir.GetName() == "expires" {
res.Cache = true
re := regexp.MustCompile(`^(\d+)(\w+)$`)
matches := re.FindStringSubmatch(lDir.GetParameters()[0])
matches := re.GetRegex(re.NumberWordPattern).FindStringSubmatch(lDir.GetParameters()[0])
if matches == nil {
continue
}
Expand Down
5 changes: 2 additions & 3 deletions agent/app/service/website_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
Expand All @@ -34,6 +33,7 @@ import (
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
"github.com/1Panel-dev/1Panel/agent/utils/re"
"github.com/pkg/errors"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -1538,8 +1538,7 @@ func getServer(website model.Website) (*components.Server, error) {
func parseTimeString(input string) (int, string, error) {
input = strings.TrimSpace(input)

re := regexp.MustCompile(`^(\d+)([smhdw]?)$`)
matches := re.FindStringSubmatch(input)
matches := re.GetRegex(re.DurationWithOptionalUnitPattern).FindStringSubmatch(input)

if len(matches) < 2 {
return 0, "", fmt.Errorf("invalid time format: %s", input)
Expand Down
3 changes: 3 additions & 0 deletions agent/init/firewall/firwall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package firewall
import (
"github.com/1Panel-dev/1Panel/agent/utils/firewall"
"github.com/1Panel-dev/1Panel/agent/utils/firewall/client/iptables"
"github.com/1Panel-dev/1Panel/agent/utils/re"
)

func Init() {
client, err := firewall.NewFirewallClient()
if err != nil {
return
}
re.RegisterRegex(iptables.Chian1PanelBasicPortPattern)
re.RegisterRegex(iptables.Chain1PanelBasicAddressPattern)
clientName := client.Name()
if clientName == "ufw" || clientName == "iptables" {
_ = iptables.LoadRulesFromFile(iptables.FilterTab, iptables.Chain1PanelForward, iptables.ForwardFileName)
Expand Down
14 changes: 3 additions & 11 deletions agent/init/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package validator

import (
"regexp"
"unicode"

"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/re"

"github.com/go-playground/validator/v10"
)
Expand All @@ -25,20 +25,12 @@ func Init() {

func checkNamePattern(fl validator.FieldLevel) bool {
value := fl.Field().String()
result, err := regexp.MatchString("^[a-zA-Z\u4e00-\u9fa5]{1}[a-zA-Z0-9_\u4e00-\u9fa5]{0,30}$", value)
if err != nil {
global.LOG.Errorf("regexp matchString failed, %v", err)
}
return result
return re.GetRegex(re.ValidatorNamePattern).MatchString(value)
}

func checkIpPattern(fl validator.FieldLevel) bool {
value := fl.Field().String()
result, err := regexp.MatchString(`^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$`, value)
if err != nil {
global.LOG.Errorf("regexp check ip matchString failed, %v", err)
}
return result
return re.GetRegex(re.ValidatorIPPattern).MatchString(value)
}

func checkPasswordPattern(fl validator.FieldLevel) bool {
Expand Down
2 changes: 2 additions & 0 deletions agent/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (
"github.com/1Panel-dev/1Panel/agent/init/validator"
"github.com/1Panel-dev/1Panel/agent/init/viper"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/re"
)

func Start() {
re.Init()
viper.Init()
dir.Init()
log.Init()
Expand Down
Loading
Loading