fix: ssh 登录日志归属地获取方式修改 (#1221)

This commit is contained in:
ssongliu 2023-06-01 18:27:53 +08:00 committed by GitHub
parent 317017a2b4
commit 7d968348f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 52 additions and 43 deletions

1
.gitignore vendored
View file

@ -31,3 +31,4 @@ dist/
1panel.service
install.sh
docker.sh
cmd/server/web/.DS_Store

View file

@ -4,6 +4,7 @@ import "time"
type SSHInfo struct {
Status string `json:"status"`
Message string `json:"message"`
Port string `json:"port"`
ListenAddress string `json:"listenAddress"`
PasswordAuthentication string `json:"passwordAuthentication"`
@ -38,7 +39,7 @@ type SSHLog struct {
type SSHHistory struct {
Date time.Time `json:"date"`
DateStr string `json:"dateStr"`
IsLocal bool `json:"isLocal"`
Area string `json:"area"`
User string `json:"user"`
AuthMode string `json:"authMode"`
Address string `json:"address"`

View file

@ -2,7 +2,6 @@ package service
import (
"fmt"
"net"
"os"
"os/user"
"path"
@ -13,9 +12,11 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"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"
)
const sshPath = "/etc/ssh/sshd_config"
@ -38,7 +39,8 @@ func NewISSHService() ISSHService {
func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
data := dto.SSHInfo{
Status: constant.StatusDisable,
Status: constant.StatusEnable,
Message: "",
Port: "22",
ListenAddress: "0.0.0.0",
PasswordAuthentication: "yes",
@ -49,7 +51,8 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
sudo := cmd.SudoHandleCmd()
stdout, err := cmd.Execf("%s systemctl status sshd", sudo)
if err != nil {
return &data, nil
data.Message = stdout
data.Status = constant.StatusDisable
}
stdLines := strings.Split(stdout, "\n")
for _, stdline := range stdLines {
@ -58,12 +61,10 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
break
}
}
if data.Status == constant.StatusDisable {
return &data, nil
}
sshConf, err := os.ReadFile(sshPath)
if err != nil {
return &data, err
data.Message = err.Error()
data.Status = constant.StatusDisable
}
lines := strings.Split(string(sshConf), "\n")
for _, line := range lines {
@ -86,7 +87,7 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
data.UseDNS = strings.ReplaceAll(line, "UseDNS ", "")
}
}
return &data, err
return &data, nil
}
func (u *SSHService) OperateSSH(operation string) error {
@ -254,8 +255,13 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
timeNow := time.Now()
nyc, _ := time.LoadLocation(common.LoadTimeZone())
qqWry, err := qqwry.NewQQwry()
if err != nil {
global.LOG.Errorf("load qqwry datas failed: %s", err)
}
for i := 0; i < len(data.Logs); i++ {
data.Logs[i].IsLocal = isPrivateIP(net.ParseIP(data.Logs[i].Address))
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)
if data.Logs[i].Date.After(timeNow) {
data.Logs[i].Date = data.Logs[i].Date.AddDate(-1, 0, 0)
@ -394,16 +400,3 @@ func handleGunzip(path string) error {
}
return nil
}
func isPrivateIP(ip net.IP) bool {
if ip4 := ip.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return true
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return true
case ip4[0] == 192 && ip4[1] == 168:
return true
}
}
return false
}

View file

@ -12347,6 +12347,9 @@ var doc = `{
"dto.SSHHistory": {
"type": "object",
"properties": {
"Area": {
"type": "string"
},
"address": {
"type": "string"
},
@ -12359,9 +12362,6 @@ var doc = `{
"dateStr": {
"type": "string"
},
"isLocal": {
"type": "boolean"
},
"message": {
"type": "string"
},
@ -12382,6 +12382,9 @@ var doc = `{
"listenAddress": {
"type": "string"
},
"message": {
"type": "string"
},
"passwordAuthentication": {
"type": "string"
},

View file

@ -12333,6 +12333,9 @@
"dto.SSHHistory": {
"type": "object",
"properties": {
"Area": {
"type": "string"
},
"address": {
"type": "string"
},
@ -12345,9 +12348,6 @@
"dateStr": {
"type": "string"
},
"isLocal": {
"type": "boolean"
},
"message": {
"type": "string"
},
@ -12368,6 +12368,9 @@
"listenAddress": {
"type": "string"
},
"message": {
"type": "string"
},
"passwordAuthentication": {
"type": "string"
},

View file

@ -1437,6 +1437,8 @@ definitions:
type: object
dto.SSHHistory:
properties:
Area:
type: string
address:
type: string
authMode:
@ -1445,8 +1447,6 @@ definitions:
type: string
dateStr:
type: string
isLocal:
type: boolean
message:
type: string
port:
@ -1460,6 +1460,8 @@ definitions:
properties:
listenAddress:
type: string
message:
type: string
passwordAuthentication:
type: string
permitRootLogin:

View file

@ -106,6 +106,7 @@ export namespace Host {
export interface SSHInfo {
status: string;
message: string;
port: string;
listenAddress: string;
passwordAuthentication: string;
@ -130,7 +131,7 @@ export namespace Host {
}
export interface sshHistory {
date: Date;
isLocal: boolean;
area: string;
user: string;
authMode: string;
address: string;

View file

@ -37,9 +37,7 @@
<template #main>
<ComplexTable :pagination-config="paginationConfig" :data="data" @search="search">
<el-table-column min-width="80" :label="$t('logs.loginIP')" prop="address" />
<el-table-column min-width="60" :label="$t('ssh.belong')" prop="isLocal">
<template #default="{ row }">{{ row.isLocal ? $t('ssh.local') : $t('ssh.remote') }}</template>
</el-table-column>
<el-table-column min-width="60" :label="$t('ssh.belong')" prop="area" />
<el-table-column min-width="60" :label="$t('firewall.port')" prop="port" />
<el-table-column min-width="60" :label="$t('ssh.loginMode')" prop="authMode">
<template #default="{ row }">

View file

@ -13,11 +13,7 @@
<el-form ref="formRef" label-position="top" :model="form" @submit.prevent v-loading="loading">
<el-row type="flex" justify="center">
<el-col :span="22">
<el-form-item
:label="$t('ssh.listenAddress')"
prop="listenAddress"
:rules="Rules.requiredInput"
>
<el-form-item :label="$t('ssh.listenAddress')" prop="listenAddress" :rules="Rules.ip">
<el-input clearable v-model="form.listenAddress" />
</el-form-item>
</el-col>

View file

@ -9,9 +9,19 @@
<el-tag round class="status-content" v-if="form.status === 'Enable'" type="success">
{{ $t('commons.status.running') }}
</el-tag>
<el-tag round class="status-content" v-if="form.status === 'Disable'" type="info">
{{ $t('commons.status.stopped') }}
</el-tag>
<el-popover
v-if="form.status === 'Disable'"
placement="top-start"
trigger="hover"
width="450"
:content="form.message"
>
<template #reference>
<el-tag round class="status-content" v-if="form.status === 'Disable'" type="info">
{{ $t('commons.status.stopped') }}
</el-tag>
</template>
</el-popover>
<span v-if="form.status === 'Enable'" class="buttons">
<el-button type="primary" @click="onOperate('stop')" link>
{{ $t('commons.button.stop') }}
@ -164,6 +174,7 @@ const rootsRef = ref();
const sshConf = ref();
const form = reactive({
status: 'enable',
message: '',
port: 22,
listenAddress: '',
passwordAuthentication: 'yes',