mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 16:29:17 +08:00
fix: ssh 登录日志排序优化 (#1435)
This commit is contained in:
parent
3c57fa76bf
commit
d8d2ee0f46
@ -229,29 +229,29 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
|
|||||||
if len(req.Info) != 0 {
|
if len(req.Info) != 0 {
|
||||||
command = fmt.Sprintf(" | grep '%s'", req.Info)
|
command = fmt.Sprintf(" | grep '%s'", req.Info)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(fileList); i++ {
|
for i := 0; i < len(fileList); i++ {
|
||||||
|
withAppend := len(data.Logs) < req.Page*req.PageSize
|
||||||
|
if req.Status != constant.StatusSuccess {
|
||||||
if strings.HasPrefix(path.Base(fileList[i]), "secure") {
|
if strings.HasPrefix(path.Base(fileList[i]), "secure") {
|
||||||
commandItem := fmt.Sprintf("cat %s | grep -a 'Failed password for' | grep -v 'invalid' %s", fileList[i], command)
|
commandItem := fmt.Sprintf("cat %s | grep -a 'Failed password for' | grep -v 'invalid' %s", fileList[i], command)
|
||||||
dataItem := loadFailedSecureDatas(commandItem)
|
dataItem, itemTotal := loadFailedSecureDatas(commandItem, withAppend)
|
||||||
data.FailedCount += len(dataItem)
|
data.FailedCount += itemTotal
|
||||||
data.TotalCount += len(dataItem)
|
data.TotalCount += itemTotal
|
||||||
if req.Status != constant.StatusSuccess {
|
|
||||||
data.Logs = append(data.Logs, dataItem...)
|
data.Logs = append(data.Logs, dataItem...)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if strings.HasPrefix(path.Base(fileList[i]), "auth.log") {
|
if strings.HasPrefix(path.Base(fileList[i]), "auth.log") {
|
||||||
commandItem := fmt.Sprintf("cat %s | grep -a 'Connection closed by authenticating user' | grep -a 'preauth' %s", fileList[i], command)
|
commandItem := fmt.Sprintf("cat %s | grep -a 'Connection closed by authenticating user' | grep -a 'preauth' %s", fileList[i], command)
|
||||||
dataItem := loadFailedAuthDatas(commandItem)
|
dataItem, itemTotal := loadFailedAuthDatas(commandItem, withAppend)
|
||||||
data.FailedCount += len(dataItem)
|
data.FailedCount += itemTotal
|
||||||
data.TotalCount += len(dataItem)
|
data.TotalCount += itemTotal
|
||||||
if req.Status != constant.StatusSuccess {
|
|
||||||
data.Logs = append(data.Logs, dataItem...)
|
data.Logs = append(data.Logs, dataItem...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commandItem := fmt.Sprintf("cat %s | grep -a Accepted %s", fileList[i], command)
|
|
||||||
dataItem := loadSuccessDatas(commandItem)
|
|
||||||
data.TotalCount += len(dataItem)
|
|
||||||
if req.Status != constant.StatusFailed {
|
if req.Status != constant.StatusFailed {
|
||||||
|
commandItem := fmt.Sprintf("cat %s | grep -a Accepted %s", fileList[i], command)
|
||||||
|
dataItem, itemTotal := loadSuccessDatas(commandItem, withAppend)
|
||||||
|
data.TotalCount += itemTotal
|
||||||
data.Logs = append(data.Logs, dataItem...)
|
data.Logs = append(data.Logs, dataItem...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,7 +279,7 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
|
|||||||
global.LOG.Errorf("load qqwry datas failed: %s", err)
|
global.LOG.Errorf("load qqwry datas failed: %s", err)
|
||||||
}
|
}
|
||||||
var itemLogs []dto.SSHHistory
|
var itemLogs []dto.SSHHistory
|
||||||
for i := len(data.Logs) - 1; i >= 0; i-- {
|
for i := 0; i < len(data.Logs); i++ {
|
||||||
data.Logs[i].Area = qqWry.Find(data.Logs[i].Address).Area
|
data.Logs[i].Area = qqWry.Find(data.Logs[i].Address).Area
|
||||||
data.Logs[i].Date, _ = time.ParseInLocation("2006 Jan 2 15:04:05", fmt.Sprintf("%d %s", timeNow.Year(), data.Logs[i].DateStr), nyc)
|
data.Logs[i].Date, _ = time.ParseInLocation("2006 Jan 2 15:04:05", fmt.Sprintf("%d %s", timeNow.Year(), data.Logs[i].DateStr), nyc)
|
||||||
itemLogs = append(itemLogs, data.Logs[i])
|
itemLogs = append(itemLogs, data.Logs[i])
|
||||||
@ -293,19 +293,18 @@ func sortFileList(fileNames []string) []string {
|
|||||||
if len(fileNames) < 2 {
|
if len(fileNames) < 2 {
|
||||||
return fileNames
|
return fileNames
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(path.Base(fileNames[0]), "secure") {
|
||||||
var itemFile []string
|
var itemFile []string
|
||||||
if strings.Contains(fileNames[0], "secure") {
|
|
||||||
sort.Slice(fileNames, func(i, j int) bool {
|
|
||||||
return fileNames[i] < fileNames[j]
|
|
||||||
})
|
|
||||||
itemFile = append(itemFile, fileNames[1:]...)
|
|
||||||
itemFile = append(itemFile, fileNames[0])
|
|
||||||
return itemFile
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(fileNames, func(i, j int) bool {
|
sort.Slice(fileNames, func(i, j int) bool {
|
||||||
return fileNames[i] > fileNames[j]
|
return fileNames[i] > fileNames[j]
|
||||||
})
|
})
|
||||||
|
itemFile = append(itemFile, fileNames[len(fileNames)-1])
|
||||||
|
itemFile = append(itemFile, fileNames[:len(fileNames)-2]...)
|
||||||
|
return itemFile
|
||||||
|
}
|
||||||
|
sort.Slice(fileNames, func(i, j int) bool {
|
||||||
|
return fileNames[i] < fileNames[j]
|
||||||
|
})
|
||||||
return fileNames
|
return fileNames
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,16 +338,24 @@ func updateSSHConf(oldFiles []string, param string, value interface{}) []string
|
|||||||
return newFiles
|
return newFiles
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSuccessDatas(command string) []dto.SSHHistory {
|
func loadSuccessDatas(command string, withAppend bool) ([]dto.SSHHistory, int) {
|
||||||
var datas []dto.SSHHistory
|
var (
|
||||||
|
datas []dto.SSHHistory
|
||||||
|
totalNum int
|
||||||
|
)
|
||||||
stdout2, err := cmd.Exec(command)
|
stdout2, err := cmd.Exec(command)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lines := strings.Split(string(stdout2), "\n")
|
lines := strings.Split(string(stdout2), "\n")
|
||||||
for _, line := range lines {
|
if len(lines) == 0 {
|
||||||
parts := strings.Fields(line)
|
return datas, 0
|
||||||
|
}
|
||||||
|
for i := len(lines) - 1; i >= 0; i-- {
|
||||||
|
parts := strings.Fields(lines[i])
|
||||||
if len(parts) < 14 {
|
if len(parts) < 14 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
totalNum++
|
||||||
|
if withAppend {
|
||||||
historyItem := dto.SSHHistory{
|
historyItem := dto.SSHHistory{
|
||||||
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
||||||
AuthMode: parts[6],
|
AuthMode: parts[6],
|
||||||
@ -360,19 +367,28 @@ func loadSuccessDatas(command string) []dto.SSHHistory {
|
|||||||
datas = append(datas, historyItem)
|
datas = append(datas, historyItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return datas
|
}
|
||||||
|
return datas, totalNum
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadFailedAuthDatas(command string) []dto.SSHHistory {
|
func loadFailedAuthDatas(command string, withAppend bool) ([]dto.SSHHistory, int) {
|
||||||
var datas []dto.SSHHistory
|
var (
|
||||||
|
datas []dto.SSHHistory
|
||||||
|
totalNum int
|
||||||
|
)
|
||||||
stdout2, err := cmd.Exec(command)
|
stdout2, err := cmd.Exec(command)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lines := strings.Split(string(stdout2), "\n")
|
lines := strings.Split(string(stdout2), "\n")
|
||||||
for _, line := range lines {
|
if len(lines) == 0 {
|
||||||
parts := strings.Fields(line)
|
return datas, 0
|
||||||
|
}
|
||||||
|
for i := len(lines) - 1; i >= 0; i-- {
|
||||||
|
parts := strings.Fields(lines[i])
|
||||||
if len(parts) < 14 {
|
if len(parts) < 14 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
totalNum++
|
||||||
|
if withAppend {
|
||||||
historyItem := dto.SSHHistory{
|
historyItem := dto.SSHHistory{
|
||||||
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
||||||
AuthMode: parts[8],
|
AuthMode: parts[8],
|
||||||
@ -381,25 +397,34 @@ func loadFailedAuthDatas(command string) []dto.SSHHistory {
|
|||||||
Port: parts[13],
|
Port: parts[13],
|
||||||
Status: constant.StatusFailed,
|
Status: constant.StatusFailed,
|
||||||
}
|
}
|
||||||
if strings.Contains(line, ": ") {
|
if strings.Contains(lines[i], ": ") {
|
||||||
historyItem.Message = strings.Split(line, ": ")[1]
|
historyItem.Message = strings.Split(lines[i], ": ")[1]
|
||||||
}
|
}
|
||||||
datas = append(datas, historyItem)
|
datas = append(datas, historyItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return datas
|
}
|
||||||
|
return datas, totalNum
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadFailedSecureDatas(command string) []dto.SSHHistory {
|
func loadFailedSecureDatas(command string, withAppend bool) ([]dto.SSHHistory, int) {
|
||||||
var datas []dto.SSHHistory
|
var (
|
||||||
|
datas []dto.SSHHistory
|
||||||
|
totalNum int
|
||||||
|
)
|
||||||
stdout2, err := cmd.Exec(command)
|
stdout2, err := cmd.Exec(command)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lines := strings.Split(string(stdout2), "\n")
|
lines := strings.Split(string(stdout2), "\n")
|
||||||
for _, line := range lines {
|
if len(lines) == 0 {
|
||||||
parts := strings.Fields(line)
|
return datas, 0
|
||||||
|
}
|
||||||
|
for i := len(lines) - 1; i >= 0; i-- {
|
||||||
|
parts := strings.Fields(lines[i])
|
||||||
if len(parts) < 14 {
|
if len(parts) < 14 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
totalNum++
|
||||||
|
if withAppend {
|
||||||
historyItem := dto.SSHHistory{
|
historyItem := dto.SSHHistory{
|
||||||
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
DateStr: fmt.Sprintf("%s %s %s", parts[0], parts[1], parts[2]),
|
||||||
AuthMode: parts[6],
|
AuthMode: parts[6],
|
||||||
@ -408,13 +433,14 @@ func loadFailedSecureDatas(command string) []dto.SSHHistory {
|
|||||||
Port: parts[12],
|
Port: parts[12],
|
||||||
Status: constant.StatusFailed,
|
Status: constant.StatusFailed,
|
||||||
}
|
}
|
||||||
if strings.Contains(line, ": ") {
|
if strings.Contains(lines[i], ": ") {
|
||||||
historyItem.Message = strings.Split(line, ": ")[1]
|
historyItem.Message = strings.Split(lines[i], ": ")[1]
|
||||||
}
|
}
|
||||||
datas = append(datas, historyItem)
|
datas = append(datas, historyItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return datas
|
}
|
||||||
|
return datas, totalNum
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGunzip(path string) error {
|
func handleGunzip(path string) error {
|
||||||
|
@ -19,10 +19,10 @@
|
|||||||
<el-option :label="$t('commons.status.success')" value="Success"></el-option>
|
<el-option :label="$t('commons.status.success')" value="Success"></el-option>
|
||||||
<el-option :label="$t('commons.status.failed')" value="Failed"></el-option>
|
<el-option :label="$t('commons.status.failed')" value="Failed"></el-option>
|
||||||
</el-select>
|
</el-select>
|
||||||
<el-tag type="success" size="large" style="margin-left: 15px">
|
<el-tag v-if="searchStatus === 'All'" type="success" size="large" style="margin-left: 15px">
|
||||||
{{ $t('commons.status.success') }}: {{ successfulCount }}
|
{{ $t('commons.status.success') }}: {{ successfulCount }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
<el-tag type="danger" size="large" style="margin-left: 5px">
|
<el-tag v-if="searchStatus === 'All'" type="danger" size="large" style="margin-left: 5px">
|
||||||
{{ $t('commons.status.failed') }}: {{ faliedCount }}
|
{{ $t('commons.status.failed') }}: {{ faliedCount }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
<el-button plain @click="onDeny" :disabled="selects.length === 0" style="margin-left: 5px">
|
<el-button plain @click="onDeny" :disabled="selects.length === 0" style="margin-left: 5px">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user