-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
) | ||
|
@@ -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) | ||
} | ||
|
@@ -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" | ||
|
@@ -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") { | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code appears to define a service interface
For example: @_url "https://pkg.go.dev/github.com/golang/go"
Please ensure that you've followed updates on GitHub releases to keep your dependencies current. If there was intended integration with |
||
|
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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.