@@ -2,6 +2,8 @@ package service
22
33import (
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 }
0 commit comments