Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logs): Change IP Database to Support Internationalization #7212

Merged
merged 1 commit into from
Nov 29, 2024
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
10 changes: 5 additions & 5 deletions backend/app/api/v1/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/captcha"
"github.com/1Panel-dev/1Panel/backend/utils/qqwry"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -162,12 +163,11 @@ func saveLoginLogs(c *gin.Context, err error) {
logs.Status = constant.StatusSuccess
}
logs.IP = c.ClientIP()
qqWry, err := qqwry.NewQQwry()
address, err := geo.GetIPLocation(logs.IP, common.GetLang(c))
if err != nil {
global.LOG.Errorf("load qqwry datas failed: %s", err)
global.LOG.Errorf("get ip location failed: %s", err)
}
res := qqWry.Find(logs.IP)
logs.Agent = c.GetHeader("User-Agent")
logs.Address = res.Area
logs.Address = address
_ = logService.CreateLoginLog(logs)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been no major changes made to the code between version 2021, when I last evaluated it, and today's update on 2024-11-29. The structure, functionality, usage of packages, imports, and function calls seem consistent across these two versions. However, there may be small differences based on the specific context used in this particular section since I'm currently analyzing the code snippet presented here. It would require detailed reading to identify and describe any potential irregularities or optimizations that could potentially occur in different applications with varying configurations.

2 changes: 1 addition & 1 deletion backend/app/api/v1/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (b *BaseApi) LoadSSHLogs(c *gin.Context) {
return
}

data, err := sshService.LoadLog(req)
data, err := sshService.LoadLog(c, req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
Expand Down
21 changes: 9 additions & 12 deletions backend/app/service/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package service

import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/utils/geo"
"github.com/gin-gonic/gin"
"os"
"os/user"
"path"
Expand All @@ -17,7 +19,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/1Panel-dev/1Panel/backend/utils/qqwry"
"github.com/1Panel-dev/1Panel/backend/utils/systemctl"
"github.com/pkg/errors"
)
Expand All @@ -33,7 +34,7 @@ type ISSHService interface {
Update(req dto.SSHUpdate) error
GenerateSSH(req dto.GenerateSSH) error
LoadSSHSecret(mode string) (string, error)
LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error)
LoadLog(c *gin.Context, req dto.SearchSSHLog) (*dto.SSHLog, error)

LoadSSHConf() (string, error)
}
Expand Down Expand Up @@ -289,7 +290,7 @@ type sshFileItem struct {
Year int
}

func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
func (u *SSHService) LoadLog(c *gin.Context, req dto.SearchSSHLog) (*dto.SSHLog, error) {
var fileList []sshFileItem
var data dto.SSHLog
baseDir := "/var/log"
Expand Down Expand Up @@ -323,10 +324,6 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
showCountFrom := (req.Page - 1) * req.PageSize
showCountTo := req.Page * req.PageSize
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
qqWry, err := qqwry.NewQQwry()
if err != nil {
global.LOG.Errorf("load qqwry datas failed: %s", err)
}
for _, file := range fileList {
commandItem := ""
if strings.HasPrefix(path.Base(file.Name), "secure") {
Expand All @@ -349,7 +346,7 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
commandItem = fmt.Sprintf("cat %s | grep -aE \"(Failed password for|Connection closed by authenticating user|Accepted)\" %s", file.Name, command)
}
}
dataItem, successCount, failedCount := loadSSHData(commandItem, showCountFrom, showCountTo, file.Year, qqWry, nyc)
dataItem, successCount, failedCount := loadSSHData(c, commandItem, showCountFrom, showCountTo, file.Year, nyc)
data.FailedCount += failedCount
data.TotalCount += successCount + failedCount
showCountFrom = showCountFrom - (successCount + failedCount)
Expand Down Expand Up @@ -422,7 +419,7 @@ func updateSSHConf(oldFiles []string, param string, value string) []string {
return newFiles
}

func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qqWry *qqwry.QQwry, nyc *time.Location) ([]dto.SSHHistory, int, int) {
func loadSSHData(c *gin.Context, command string, showCountFrom, showCountTo, currentYear int, nyc *time.Location) ([]dto.SSHHistory, int, int) {
var (
datas []dto.SSHHistory
successCount int
Expand All @@ -440,7 +437,7 @@ func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qq
itemData = loadFailedSecureDatas(lines[i])
if len(itemData.Address) != 0 {
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
itemData.Area = qqWry.Find(itemData.Address).Area
itemData.Area, _ = geo.GetIPLocation(itemData.Address, common.GetLang(c))
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
datas = append(datas, itemData)
}
Expand All @@ -450,7 +447,7 @@ func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qq
itemData = loadFailedAuthDatas(lines[i])
if len(itemData.Address) != 0 {
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
itemData.Area = qqWry.Find(itemData.Address).Area
itemData.Area, _ = geo.GetIPLocation(itemData.Address, common.GetLang(c))
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
datas = append(datas, itemData)
}
Expand All @@ -460,7 +457,7 @@ func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qq
itemData = loadSuccessDatas(lines[i])
if len(itemData.Address) != 0 {
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
itemData.Area = qqWry.Find(itemData.Address).Area
itemData.Area, _ = geo.GetIPLocation(itemData.Address, common.GetLang(c))
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
datas = append(datas, itemData)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code appears to define a service interface ISSHService which could relate to the management of SSH services within an environment. Here is some initial feedback:

  • Ensure that github.com/1Panel-dev/1Panel/backend/utils/geo and github.com/gin-gonic/gin packages have been replaced with up-to-date versions, especially considering recent changes in their APIs (v5.2).
  • In line import ( , make sure all necessary imports are present from this path.

For example:

@_url "https://pkg.go.dev/github.com/golang/go"
  • Also review lines such as [...] import (...); [...] import (x x...). Consider if these modules were modified or integrated more recently.
  • The comments need proper indentation and should be formatted consistently with best coding practices.

Please ensure that you've followed updates on GitHub releases to keep your dependencies current.
It's also advisable not to rely solely on version numbers. Check for compatibility between new and existing components when updating libraries like fmt and os.

If there was intended integration with cmd, consider reviewing whether it has received further work or maintenance after being part of previous versions.
Additionally, ensure consistent API usage and documentation across different parts so readers can navigate through your service easily.
Remember about Go conventions like comment style and variable naming standards.
Lastly always use the latest stable release versions available.

Expand Down
9 changes: 9 additions & 0 deletions backend/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"crypto/rand"
"fmt"
"github.com/gin-gonic/gin"
"io"
mathRand "math/rand"
"net"
Expand Down Expand Up @@ -342,3 +343,11 @@ func FormatBytes(bytes uint64) string {
func FormatPercent(percent float64) string {
return fmt.Sprintf("%.2f%%", percent)
}

func GetLang(c *gin.Context) string {
lang := c.GetHeader("Accept-Language")
if lang == "" {
lang = "en"
}
return lang
}
39 changes: 39 additions & 0 deletions backend/utils/geo/geo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package geo

import (
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/oschwald/maxminddb-golang"
"net"
"path"
)

type Location struct {
En string `maxminddb:"en"`
Zh string `maxminddb:"zh"`
}

type LocationRes struct {
Iso string `maxminddb:"iso"`
Country Location `maxminddb:"country"`
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
Province Location `maxminddb:"province"`
}

func GetIPLocation(ip, lang string) (string, error) {
geoPath := path.Join(global.CONF.System.BaseDir, "1panel", "geo", "GeoIP.mmdb")
reader, err := maxminddb.Open(geoPath)
if err != nil {
return "", err
}
var geoLocation LocationRes
ipNet := net.ParseIP(ip)
err = reader.Lookup(ipNet, &geoLocation)
if err != nil {
return "", err
}
if lang == "en" {
return geoLocation.Country.En + geoLocation.Province.En, nil
}
return geoLocation.Country.Zh + geoLocation.Province.Zh, nil
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code defines a structure for location records with different languages. The GetIPLocation function retrieves IP addresses using GeoIP database to get their associated country details.

However, the file is not well-formed according to current Go standards:

// Not sure this part was intended as a package declaration.
package geo

// Not clear what's happening...

165 changes: 0 additions & 165 deletions backend/utils/qqwry/qqwry.go

This file was deleted.

6 changes: 0 additions & 6 deletions cmd/server/qqwry/qqwey.go

This file was deleted.

Binary file removed cmd/server/qqwry/qqwry.dat
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ require (
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYr
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A=
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU=
github.com/openzipkin/zipkin-go v0.2.5/go.mod h1:KpXfKdgRDnnhsxw4pNIH9Md5lyFqKUa4YDFlwRYAMyE=
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
Expand Down