Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions backend/init/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import (
"encoding/base64"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/cmd/server/res"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/cmd/server/res"

"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/i18n"
"github.com/1Panel-dev/1Panel/backend/middleware"
Expand All @@ -30,7 +31,7 @@
func toIndexHtml(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.Write(web.IndexByte)

Check failure on line 34 in backend/init/router/router.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogp0DMzo25aKVw8WJ&open=AZrogp0DMzo25aKVw8WJ&pullRequest=11183
c.Writer.Flush()
}

Expand All @@ -48,7 +49,7 @@
return true
}
for _, route := range constant.DynamicRoutes {
if match, _ := regexp.MatchString(route, reqUri); match {

Check failure on line 52 in backend/init/router/router.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogp0DMzo25aKVw8WK&open=AZrogp0DMzo25aKVw8WK&pullRequest=11183
return true
}
}
Expand Down Expand Up @@ -140,7 +141,7 @@
}
c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.Write(web.IndexByte)

Check failure on line 144 in backend/init/router/router.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogp0DMzo25aKVw8WL&open=AZrogp0DMzo25aKVw8WL&pullRequest=11183
c.Writer.Flush()
})
}
Expand All @@ -160,7 +161,7 @@
}

func Routers() *gin.Engine {
Router = gin.Default()
Router = gin.New()
Router.Use(middleware.OperationLog())
// Router.Use(middleware.CSRF())
// Router.Use(middleware.LoadCsrfToken())
Expand Down
7 changes: 6 additions & 1 deletion backend/middleware/ip_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin"
)

func WhiteAllow() gin.HandlerFunc {

Check failure on line 15 in backend/middleware/ip_limit.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogp0SMzo25aKVw8WM&open=AZrogp0SMzo25aKVw8WM&pullRequest=11183
return func(c *gin.Context) {
clientIP := common.GetRealClientIP(c)
if common.IsPrivateIP(clientIP) {
c.Next()
return
}
settingRepo := repo.NewISettingRepo()
status, err := settingRepo.Get(settingRepo.WithByKey("AllowIPs"))
if err != nil {
Expand All @@ -24,7 +30,6 @@
c.Next()
return
}
clientIP := c.ClientIP()
for _, ip := range strings.Split(status.Value, ",") {
if len(ip) == 0 {
continue
Expand Down
16 changes: 16 additions & 0 deletions backend/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
for i := 0; i < n; i++ {
if version1s[i] == version2s[i] {
continue
} else {

Check failure on line 74 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'else' clause; the code should continue after the error check.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WC&open=AZrogpsKMzo25aKVw8WC&pullRequest=11183
v1, err1 := strconv.Atoi(version1s[i])
if err1 != nil {
return version1s[i] > version2s[i]
Expand Down Expand Up @@ -118,7 +118,7 @@
return y
}

func GetSortedVersions(versions []string) []string {

Check warning on line 121 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the 'Get' prefix from this function name.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8V-&open=AZrogpsKMzo25aKVw8V-&pullRequest=11183
sort.Slice(versions, func(i, j int) bool {
return CompareVersion(versions[i], versions[j])
})
Expand Down Expand Up @@ -211,14 +211,14 @@
func IsCrossVersion(version1, version2 string) bool {
version1s := strings.Split(version1, ".")
version2s := strings.Split(version2, ".")
v1num, _ := strconv.Atoi(version1s[0])

Check failure on line 214 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WD&open=AZrogpsKMzo25aKVw8WD&pullRequest=11183
v2num, _ := strconv.Atoi(version2s[0])

Check failure on line 215 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WE&open=AZrogpsKMzo25aKVw8WE&pullRequest=11183
return v2num > v1num
}

func GetUuid() string {

Check warning on line 219 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the 'Get' prefix from this function name.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8V_&open=AZrogpsKMzo25aKVw8V_&pullRequest=11183
b := make([]byte, 16)
_, _ = io.ReadFull(rand.Reader, b)

Check failure on line 221 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this error explicitly or document why it can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WF&open=AZrogpsKMzo25aKVw8WF&pullRequest=11183
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
Expand Down Expand Up @@ -326,7 +326,7 @@
if len(fields) != 5 {
return loc
}
if _, err := time.LoadLocation(fields[2]); err != nil {

Check failure on line 329 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Check this error or remove the variable if the error can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WG&open=AZrogpsKMzo25aKVw8WG&pullRequest=11183
return loc
}
return fields[2]
Expand Down Expand Up @@ -400,7 +400,7 @@
return fmt.Sprintf("%.2f%%", percent)
}

func GetLang(c *gin.Context) string {

Check warning on line 403 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the 'Get' prefix from this function name.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WA&open=AZrogpsKMzo25aKVw8WA&pullRequest=11183
lang := c.GetHeader("Accept-Language")
if lang == "" {
lang = "en"
Expand All @@ -419,10 +419,26 @@
res = append(res, ip)
continue
}
if _, _, err := net.ParseCIDR(ip); err != nil {

Check failure on line 422 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Check this error or remove the variable if the error can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WH&open=AZrogpsKMzo25aKVw8WH&pullRequest=11183
return nil, buserr.New("ErrParseIP")
}
res = append(res, ip)
}
return res, nil
}

func GetRealClientIP(c *gin.Context) string {

Check warning on line 430 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the 'Get' prefix from this function name.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WB&open=AZrogpsKMzo25aKVw8WB&pullRequest=11183
addr := c.Request.RemoteAddr
if ip, _, err := net.SplitHostPort(addr); err == nil {

Check failure on line 432 in backend/utils/common/common.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Check this error or remove the variable if the error can be safely ignored.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZrogpsKMzo25aKVw8WI&open=AZrogpsKMzo25aKVw8WI&pullRequest=11183
return ip
}
return addr
}

func IsPrivateIP(ipStr string) bool {
ip := net.ParseIP(ipStr)
if ip == nil {
return false
}
return ip.IsPrivate() || ip.IsLoopback()
}
Loading