Skip to content

Commit 79fea27

Browse files
feat(task): Replace QQWry with GeoIP.mmdb (#7599)
1 parent ba1d65f commit 79fea27

File tree

9 files changed

+90
-27
lines changed

9 files changed

+90
-27
lines changed

agent/app/api/v2/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (b *BaseApi) LoadSSHLogs(c *gin.Context) {
144144
return
145145
}
146146

147-
data, err := sshService.LoadLog(req)
147+
data, err := sshService.LoadLog(c, req)
148148
if err != nil {
149149
helper.InternalServer(c, err)
150150
return

agent/app/model/backup.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package model
22

33
type BackupAccount struct {
44
BaseModel
5-
Name string `gorm:"not null" json:"name"`
6-
Type string `gorm:"not null" json:"type"`
5+
Name string `gorm:"not null;default:''" json:"name"`
6+
Type string `gorm:"not null;default:''" json:"type"`
77
Bucket string `json:"bucket"`
88
AccessKey string `json:"accessKey"`
99
Credential string `json:"credential"`
@@ -22,8 +22,8 @@ type BackupRecord struct {
2222
SourceAccountIDs string `json:"sourceAccountIDs"`
2323
DownloadAccountID uint `json:"downloadAccountID"`
2424

25-
Type string `gorm:"not null" json:"type"`
26-
Name string `gorm:"not null" json:"name"`
25+
Type string `gorm:"not null;default:''" json:"type"`
26+
Name string `gorm:"not null;default:''" json:"name"`
2727
DetailName string `json:"detailName"`
2828
FileDir string `json:"fileDir"`
2929
FileName string `json:"fileName"`

agent/app/service/ssh.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package service
22

33
import (
44
"fmt"
5+
"github.com/1Panel-dev/1Panel/agent/utils/geo"
6+
"github.com/gin-gonic/gin"
57
"os"
68
"os/user"
79
"path"
@@ -17,7 +19,6 @@ import (
1719
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
1820
"github.com/1Panel-dev/1Panel/agent/utils/common"
1921
"github.com/1Panel-dev/1Panel/agent/utils/files"
20-
"github.com/1Panel-dev/1Panel/agent/utils/qqwry"
2122
"github.com/1Panel-dev/1Panel/agent/utils/systemctl"
2223
"github.com/pkg/errors"
2324
)
@@ -33,7 +34,7 @@ type ISSHService interface {
3334
Update(req dto.SSHUpdate) error
3435
GenerateSSH(req dto.GenerateSSH) error
3536
LoadSSHSecret(mode string) (string, error)
36-
LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error)
37+
LoadLog(ctx *gin.Context, req dto.SearchSSHLog) (*dto.SSHLog, error)
3738

3839
LoadSSHConf() (string, error)
3940
}
@@ -282,7 +283,7 @@ type sshFileItem struct {
282283
Year int
283284
}
284285

285-
func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
286+
func (u *SSHService) LoadLog(ctx *gin.Context, req dto.SearchSSHLog) (*dto.SSHLog, error) {
286287
var fileList []sshFileItem
287288
var data dto.SSHLog
288289
baseDir := "/var/log"
@@ -316,10 +317,6 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
316317
showCountFrom := (req.Page - 1) * req.PageSize
317318
showCountTo := req.Page * req.PageSize
318319
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
319-
qqWry, err := qqwry.NewQQwry()
320-
if err != nil {
321-
global.LOG.Errorf("load qqwry datas failed: %s", err)
322-
}
323320
for _, file := range fileList {
324321
commandItem := ""
325322
if strings.HasPrefix(path.Base(file.Name), "secure") {
@@ -342,7 +339,7 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
342339
commandItem = fmt.Sprintf("cat %s | grep -aE \"(Failed password for|Connection closed by authenticating user|Accepted)\" %s", file.Name, command)
343340
}
344341
}
345-
dataItem, successCount, failedCount := loadSSHData(commandItem, showCountFrom, showCountTo, file.Year, qqWry, nyc)
342+
dataItem, successCount, failedCount := loadSSHData(ctx, commandItem, showCountFrom, showCountTo, file.Year, nyc)
346343
data.FailedCount += failedCount
347344
data.TotalCount += successCount + failedCount
348345
showCountFrom = showCountFrom - (successCount + failedCount)
@@ -415,25 +412,29 @@ func updateSSHConf(oldFiles []string, param string, value string) []string {
415412
return newFiles
416413
}
417414

418-
func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qqWry *qqwry.QQwry, nyc *time.Location) ([]dto.SSHHistory, int, int) {
415+
func loadSSHData(ctx *gin.Context, command string, showCountFrom, showCountTo, currentYear int, nyc *time.Location) ([]dto.SSHHistory, int, int) {
419416
var (
420417
datas []dto.SSHHistory
421418
successCount int
422419
failedCount int
423420
)
424-
stdout2, err := cmd.Exec(command)
421+
getLoc, err := geo.NewGeo()
422+
if err != nil {
423+
return datas, 0, 0
424+
}
425+
stdout, err := cmd.Exec(command)
425426
if err != nil {
426427
return datas, 0, 0
427428
}
428-
lines := strings.Split(string(stdout2), "\n")
429+
lines := strings.Split(stdout, "\n")
429430
for i := len(lines) - 1; i >= 0; i-- {
430431
var itemData dto.SSHHistory
431432
switch {
432433
case strings.Contains(lines[i], "Failed password for"):
433434
itemData = loadFailedSecureDatas(lines[i])
434435
if len(itemData.Address) != 0 {
435436
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
436-
itemData.Area = qqWry.Find(itemData.Address).Area
437+
itemData.Area, _ = geo.GetIPLocation(getLoc, itemData.Address, common.GetLang(ctx))
437438
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
438439
datas = append(datas, itemData)
439440
}
@@ -443,7 +444,7 @@ func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qq
443444
itemData = loadFailedAuthDatas(lines[i])
444445
if len(itemData.Address) != 0 {
445446
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
446-
itemData.Area = qqWry.Find(itemData.Address).Area
447+
itemData.Area, _ = geo.GetIPLocation(getLoc, itemData.Address, common.GetLang(ctx))
447448
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
448449
datas = append(datas, itemData)
449450
}
@@ -453,7 +454,7 @@ func loadSSHData(command string, showCountFrom, showCountTo, currentYear int, qq
453454
itemData = loadSuccessDatas(lines[i])
454455
if len(itemData.Address) != 0 {
455456
if successCount+failedCount >= showCountFrom && successCount+failedCount < showCountTo {
456-
itemData.Area = qqWry.Find(itemData.Address).Area
457+
itemData.Area, _ = geo.GetIPLocation(getLoc, itemData.Address, common.GetLang(ctx))
457458
itemData.Date = loadDate(currentYear, itemData.DateStr, nyc)
458459
datas = append(datas, itemData)
459460
}

agent/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/1Panel-dev/1Panel/agent
22

3-
go 1.22.4
3+
go 1.23
44

55
require (
66
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
@@ -171,6 +171,7 @@ require (
171171
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
172172
github.com/opencontainers/go-digest v1.0.0 // indirect
173173
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
174+
github.com/oschwald/maxminddb-golang v1.13.1 // indirect
174175
github.com/pelletier/go-toml v1.9.5 // indirect
175176
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
176177
github.com/pierrec/lz4/v4 v4.1.15 // indirect

agent/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M5
586586
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
587587
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A=
588588
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU=
589+
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
590+
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
589591
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
590592
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
591593
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=

agent/init/migration/migrations/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
var AddTable = &gormigrate.Migration{
20-
ID: "20241226-add-table",
20+
ID: "20241231-add-table",
2121
Migrate: func(tx *gorm.DB) error {
2222
return tx.AutoMigrate(
2323
&model.AppDetail{},
@@ -56,7 +56,6 @@ var AddTable = &gormigrate.Migration{
5656
&model.WebsiteDnsAccount{},
5757
&model.WebsiteDomain{},
5858
&model.WebsiteSSL{},
59-
&model.Task{},
6059
)
6160
},
6261
}

agent/utils/common/common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"crypto/rand"
55
"fmt"
6+
"github.com/gin-gonic/gin"
67
"io"
78
mathRand "math/rand"
89
"net"
@@ -323,3 +324,11 @@ func SplitStr(str string, spi ...string) []string {
323324
func IsValidIP(ip string) bool {
324325
return net.ParseIP(ip) != nil
325326
}
327+
328+
func GetLang(context *gin.Context) string {
329+
lang := context.GetHeader("Accept-Language")
330+
if strings.Contains(lang, "zh") {
331+
return "zh"
332+
}
333+
return "en"
334+
}

agent/utils/geo/geo.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package geo
2+
3+
import (
4+
"github.com/1Panel-dev/1Panel/agent/global"
5+
"github.com/oschwald/maxminddb-golang"
6+
"net"
7+
"path"
8+
)
9+
10+
type Location struct {
11+
En string `maxminddb:"en"`
12+
Zh string `maxminddb:"zh"`
13+
}
14+
15+
type LocationRes struct {
16+
Iso string `maxminddb:"iso"`
17+
Country Location `maxminddb:"country"`
18+
Latitude float64 `maxminddb:"latitude"`
19+
Longitude float64 `maxminddb:"longitude"`
20+
Province Location `maxminddb:"province"`
21+
}
22+
23+
func NewGeo() (*maxminddb.Reader, error) {
24+
geoPath := path.Join(global.CONF.System.BaseDir, "1panel", "geo", "GeoIP.mmdb")
25+
return maxminddb.Open(geoPath)
26+
}
27+
28+
func GetIPLocation(reader *maxminddb.Reader, ip, lang string) (string, error) {
29+
var err error
30+
var geoLocation LocationRes
31+
if reader == nil {
32+
geoPath := path.Join(global.CONF.System.BaseDir, "1panel", "geo", "GeoIP.mmdb")
33+
reader, err = maxminddb.Open(geoPath)
34+
if err != nil {
35+
return "", err
36+
}
37+
}
38+
ipNet := net.ParseIP(ip)
39+
err = reader.Lookup(ipNet, &geoLocation)
40+
if err != nil {
41+
return "", err
42+
}
43+
if lang == "zh" {
44+
return geoLocation.Country.Zh + geoLocation.Province.Zh, nil
45+
}
46+
return geoLocation.Country.En + geoLocation.Province.En, nil
47+
}

frontend/src/components/app-status/index.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,15 @@ const onCheck = async (key: any, name: any) => {
136136
const onOperate = async (operation: string) => {
137137
em('update:maskShow', false);
138138
operateReq.operate = operation;
139-
ElMessageBox.confirm(i18n.global.t(`app.${operation}OperatorHelper`), i18n.global.t('app.' + operation), {
140-
confirmButtonText: i18n.global.t('commons.button.confirm'),
141-
cancelButtonText: i18n.global.t('commons.button.cancel'),
142-
type: 'info',
143-
})
139+
ElMessageBox.confirm(
140+
i18n.global.t('app.operatorHelper', [i18n.global.t('app.' + operation)]),
141+
i18n.global.t('app.' + operation),
142+
{
143+
confirmButtonText: i18n.global.t('commons.button.confirm'),
144+
cancelButtonText: i18n.global.t('commons.button.cancel'),
145+
type: 'info',
146+
},
147+
)
144148
.then(() => {
145149
em('update:maskShow', true);
146150
em('update:loading', true);

0 commit comments

Comments
 (0)