mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
fix: ssh 登录日志归属地获取方式修改 (#1221)
This commit is contained in:
parent
317017a2b4
commit
7d968348f5
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ dist/
|
|||||||
1panel.service
|
1panel.service
|
||||||
install.sh
|
install.sh
|
||||||
docker.sh
|
docker.sh
|
||||||
|
cmd/server/web/.DS_Store
|
||||||
|
@ -4,6 +4,7 @@ import "time"
|
|||||||
|
|
||||||
type SSHInfo struct {
|
type SSHInfo struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
Message string `json:"message"`
|
||||||
Port string `json:"port"`
|
Port string `json:"port"`
|
||||||
ListenAddress string `json:"listenAddress"`
|
ListenAddress string `json:"listenAddress"`
|
||||||
PasswordAuthentication string `json:"passwordAuthentication"`
|
PasswordAuthentication string `json:"passwordAuthentication"`
|
||||||
@ -38,7 +39,7 @@ type SSHLog struct {
|
|||||||
type SSHHistory struct {
|
type SSHHistory struct {
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
DateStr string `json:"dateStr"`
|
DateStr string `json:"dateStr"`
|
||||||
IsLocal bool `json:"isLocal"`
|
Area string `json:"area"`
|
||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
AuthMode string `json:"authMode"`
|
AuthMode string `json:"authMode"`
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
|
@ -2,7 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
@ -13,9 +12,11 @@ import (
|
|||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
"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/cmd"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/utils/qqwry"
|
||||||
)
|
)
|
||||||
|
|
||||||
const sshPath = "/etc/ssh/sshd_config"
|
const sshPath = "/etc/ssh/sshd_config"
|
||||||
@ -38,7 +39,8 @@ func NewISSHService() ISSHService {
|
|||||||
|
|
||||||
func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
|
func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
|
||||||
data := dto.SSHInfo{
|
data := dto.SSHInfo{
|
||||||
Status: constant.StatusDisable,
|
Status: constant.StatusEnable,
|
||||||
|
Message: "",
|
||||||
Port: "22",
|
Port: "22",
|
||||||
ListenAddress: "0.0.0.0",
|
ListenAddress: "0.0.0.0",
|
||||||
PasswordAuthentication: "yes",
|
PasswordAuthentication: "yes",
|
||||||
@ -49,7 +51,8 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
|
|||||||
sudo := cmd.SudoHandleCmd()
|
sudo := cmd.SudoHandleCmd()
|
||||||
stdout, err := cmd.Execf("%s systemctl status sshd", sudo)
|
stdout, err := cmd.Execf("%s systemctl status sshd", sudo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &data, nil
|
data.Message = stdout
|
||||||
|
data.Status = constant.StatusDisable
|
||||||
}
|
}
|
||||||
stdLines := strings.Split(stdout, "\n")
|
stdLines := strings.Split(stdout, "\n")
|
||||||
for _, stdline := range stdLines {
|
for _, stdline := range stdLines {
|
||||||
@ -58,12 +61,10 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if data.Status == constant.StatusDisable {
|
|
||||||
return &data, nil
|
|
||||||
}
|
|
||||||
sshConf, err := os.ReadFile(sshPath)
|
sshConf, err := os.ReadFile(sshPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &data, err
|
data.Message = err.Error()
|
||||||
|
data.Status = constant.StatusDisable
|
||||||
}
|
}
|
||||||
lines := strings.Split(string(sshConf), "\n")
|
lines := strings.Split(string(sshConf), "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
@ -86,7 +87,7 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
|
|||||||
data.UseDNS = strings.ReplaceAll(line, "UseDNS ", "")
|
data.UseDNS = strings.ReplaceAll(line, "UseDNS ", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &data, err
|
return &data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *SSHService) OperateSSH(operation string) error {
|
func (u *SSHService) OperateSSH(operation string) error {
|
||||||
@ -254,8 +255,13 @@ func (u *SSHService) LoadLog(req dto.SearchSSHLog) (*dto.SSHLog, error) {
|
|||||||
|
|
||||||
timeNow := time.Now()
|
timeNow := time.Now()
|
||||||
nyc, _ := time.LoadLocation(common.LoadTimeZone())
|
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++ {
|
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)
|
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) {
|
if data.Logs[i].Date.After(timeNow) {
|
||||||
data.Logs[i].Date = data.Logs[i].Date.AddDate(-1, 0, 0)
|
data.Logs[i].Date = data.Logs[i].Date.AddDate(-1, 0, 0)
|
||||||
@ -394,16 +400,3 @@ func handleGunzip(path string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
|
@ -12347,6 +12347,9 @@ var doc = `{
|
|||||||
"dto.SSHHistory": {
|
"dto.SSHHistory": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"Area": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"address": {
|
"address": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -12359,9 +12362,6 @@ var doc = `{
|
|||||||
"dateStr": {
|
"dateStr": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"isLocal": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"message": {
|
"message": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -12382,6 +12382,9 @@ var doc = `{
|
|||||||
"listenAddress": {
|
"listenAddress": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"message": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"passwordAuthentication": {
|
"passwordAuthentication": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
@ -12333,6 +12333,9 @@
|
|||||||
"dto.SSHHistory": {
|
"dto.SSHHistory": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"Area": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"address": {
|
"address": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -12345,9 +12348,6 @@
|
|||||||
"dateStr": {
|
"dateStr": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
"isLocal": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"message": {
|
"message": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@ -12368,6 +12368,9 @@
|
|||||||
"listenAddress": {
|
"listenAddress": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"message": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"passwordAuthentication": {
|
"passwordAuthentication": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
@ -1437,6 +1437,8 @@ definitions:
|
|||||||
type: object
|
type: object
|
||||||
dto.SSHHistory:
|
dto.SSHHistory:
|
||||||
properties:
|
properties:
|
||||||
|
Area:
|
||||||
|
type: string
|
||||||
address:
|
address:
|
||||||
type: string
|
type: string
|
||||||
authMode:
|
authMode:
|
||||||
@ -1445,8 +1447,6 @@ definitions:
|
|||||||
type: string
|
type: string
|
||||||
dateStr:
|
dateStr:
|
||||||
type: string
|
type: string
|
||||||
isLocal:
|
|
||||||
type: boolean
|
|
||||||
message:
|
message:
|
||||||
type: string
|
type: string
|
||||||
port:
|
port:
|
||||||
@ -1460,6 +1460,8 @@ definitions:
|
|||||||
properties:
|
properties:
|
||||||
listenAddress:
|
listenAddress:
|
||||||
type: string
|
type: string
|
||||||
|
message:
|
||||||
|
type: string
|
||||||
passwordAuthentication:
|
passwordAuthentication:
|
||||||
type: string
|
type: string
|
||||||
permitRootLogin:
|
permitRootLogin:
|
||||||
|
@ -106,6 +106,7 @@ export namespace Host {
|
|||||||
|
|
||||||
export interface SSHInfo {
|
export interface SSHInfo {
|
||||||
status: string;
|
status: string;
|
||||||
|
message: string;
|
||||||
port: string;
|
port: string;
|
||||||
listenAddress: string;
|
listenAddress: string;
|
||||||
passwordAuthentication: string;
|
passwordAuthentication: string;
|
||||||
@ -130,7 +131,7 @@ export namespace Host {
|
|||||||
}
|
}
|
||||||
export interface sshHistory {
|
export interface sshHistory {
|
||||||
date: Date;
|
date: Date;
|
||||||
isLocal: boolean;
|
area: string;
|
||||||
user: string;
|
user: string;
|
||||||
authMode: string;
|
authMode: string;
|
||||||
address: string;
|
address: string;
|
||||||
|
@ -37,9 +37,7 @@
|
|||||||
<template #main>
|
<template #main>
|
||||||
<ComplexTable :pagination-config="paginationConfig" :data="data" @search="search">
|
<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="80" :label="$t('logs.loginIP')" prop="address" />
|
||||||
<el-table-column min-width="60" :label="$t('ssh.belong')" prop="isLocal">
|
<el-table-column min-width="60" :label="$t('ssh.belong')" prop="area" />
|
||||||
<template #default="{ row }">{{ row.isLocal ? $t('ssh.local') : $t('ssh.remote') }}</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column min-width="60" :label="$t('firewall.port')" prop="port" />
|
<el-table-column min-width="60" :label="$t('firewall.port')" prop="port" />
|
||||||
<el-table-column min-width="60" :label="$t('ssh.loginMode')" prop="authMode">
|
<el-table-column min-width="60" :label="$t('ssh.loginMode')" prop="authMode">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
|
@ -13,11 +13,7 @@
|
|||||||
<el-form ref="formRef" label-position="top" :model="form" @submit.prevent v-loading="loading">
|
<el-form ref="formRef" label-position="top" :model="form" @submit.prevent v-loading="loading">
|
||||||
<el-row type="flex" justify="center">
|
<el-row type="flex" justify="center">
|
||||||
<el-col :span="22">
|
<el-col :span="22">
|
||||||
<el-form-item
|
<el-form-item :label="$t('ssh.listenAddress')" prop="listenAddress" :rules="Rules.ip">
|
||||||
:label="$t('ssh.listenAddress')"
|
|
||||||
prop="listenAddress"
|
|
||||||
:rules="Rules.requiredInput"
|
|
||||||
>
|
|
||||||
<el-input clearable v-model="form.listenAddress" />
|
<el-input clearable v-model="form.listenAddress" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
@ -9,9 +9,19 @@
|
|||||||
<el-tag round class="status-content" v-if="form.status === 'Enable'" type="success">
|
<el-tag round class="status-content" v-if="form.status === 'Enable'" type="success">
|
||||||
{{ $t('commons.status.running') }}
|
{{ $t('commons.status.running') }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
<el-tag round class="status-content" v-if="form.status === 'Disable'" type="info">
|
<el-popover
|
||||||
{{ $t('commons.status.stopped') }}
|
v-if="form.status === 'Disable'"
|
||||||
</el-tag>
|
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">
|
<span v-if="form.status === 'Enable'" class="buttons">
|
||||||
<el-button type="primary" @click="onOperate('stop')" link>
|
<el-button type="primary" @click="onOperate('stop')" link>
|
||||||
{{ $t('commons.button.stop') }}
|
{{ $t('commons.button.stop') }}
|
||||||
@ -164,6 +174,7 @@ const rootsRef = ref();
|
|||||||
const sshConf = ref();
|
const sshConf = ref();
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
status: 'enable',
|
status: 'enable',
|
||||||
|
message: '',
|
||||||
port: 22,
|
port: 22,
|
||||||
listenAddress: '',
|
listenAddress: '',
|
||||||
passwordAuthentication: 'yes',
|
passwordAuthentication: 'yes',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user