diff --git a/backend/utils/ps/ps_test.go b/backend/utils/ps/ps_test.go index 3e925b553..834f4866c 100644 --- a/backend/utils/ps/ps_test.go +++ b/backend/utils/ps/ps_test.go @@ -2,6 +2,7 @@ package ps import ( "fmt" + "github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/process" "strconv" "testing" @@ -61,8 +62,17 @@ func TestPs(t *testing.T) { if err == nil { fmt.Println(cmdLine) } + ss, err := pro.Terminal() + if err == nil { + fmt.Println(ss) + } fmt.Println(fmt.Sprintf("Name: %s PId: %v ParentID: %v Username: %v status:%s startTime: %s numThreads: %v numConnections:%v cpuPercent:%v rss:%s MB IORead: %s IOWrite: %s", name, pro.Pid, parentID, userName, status, startTime, numThreads, numConnections, cpuPercent, rss, ioRead, ioWrite)) } + users, err := host.Users() + if err == nil { + fmt.Println(users) + } + } diff --git a/backend/utils/websocket/process_data.go b/backend/utils/websocket/process_data.go index 6bf67d9c4..83b63d6f5 100644 --- a/backend/utils/websocket/process_data.go +++ b/backend/utils/websocket/process_data.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/files" + "github.com/shirou/gopsutil/v3/host" "github.com/shirou/gopsutil/v3/net" "github.com/shirou/gopsutil/v3/process" "strings" @@ -15,6 +16,7 @@ type WsInput struct { Type string `json:"type"` DownloadProgress PsProcessConfig + SSHSessionConfig } type DownloadProgress struct { @@ -27,6 +29,11 @@ type PsProcessConfig struct { Username string `json:"username"` } +type SSHSessionConfig struct { + LoginUser string `json:"loginUser"` + LoginIP string `json:"loginIP"` +} + type PsProcessData struct { PID int32 `json:"PID"` Name string `json:"name"` @@ -64,6 +71,15 @@ type processConnect struct { Status string `json:"status"` Laddr net.Addr `json:"localaddr"` Raddr net.Addr `json:"remoteaddr"` + PID string `json:"PID"` +} + +type sshSession struct { + Username string `json:"username"` + PID int32 `json:"PID"` + Terminal string `json:"terminal"` + Host string `json:"host"` + LoginTime string `json:"loginTime"` } func ProcessData(c *Client, inputMsg []byte) { @@ -86,6 +102,12 @@ func ProcessData(c *Client, inputMsg []byte) { return } c.Msg <- res + case "ssh": + res, err := getSSHSessions(wsInput.SSHSessionConfig) + if err != nil { + return + } + c.Msg <- res } } @@ -220,3 +242,52 @@ func getProcessData(processConfig PsProcessConfig) (res []byte, err error) { res, err = json.Marshal(result) return } + +func getSSHSessions(config SSHSessionConfig) (res []byte, err error) { + var ( + result []sshSession + users []host.UserStat + processes []*process.Process + ) + processes, err = process.Processes() + if err != nil { + return + } + users, err = host.Users() + if err != nil { + return + } + for _, proc := range processes { + name, _ := proc.Name() + if name != "sshd" || proc.Pid == 0 { + continue + } + connections, _ := proc.Connections() + for _, conn := range connections { + for _, user := range users { + if user.Host == "" { + continue + } + if conn.Raddr.IP == user.Host { + if config.LoginUser != "" && !strings.Contains(user.User, config.LoginUser) { + continue + } + if config.LoginIP != "" && !strings.Contains(user.Host, config.LoginIP) { + continue + } + session := sshSession{ + Username: user.User, + Host: user.Host, + Terminal: user.Terminal, + PID: proc.Pid, + } + t := time.Unix(int64(user.Started), 0) + session.LoginTime = t.Format("2006-1-2 15:04:05") + result = append(result, session) + } + } + } + } + res, err = json.Marshal(result) + return +} diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 6e4619872..da22cea21 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -912,7 +912,12 @@ const message = { publickey: 'Key', belong: 'Belong', local: 'Local', - remote: 'Remote', + config: 'Configuration', + session: 'Session', + loginTime: 'Login Time', + loginIP: 'Login IP', + disconnect: 'Disconnect', + stopSSHWarn: 'Whether to disconnect this SSH connection', }, setting: { all: 'All', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index d6410fd6a..7a12093b5 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -237,7 +237,7 @@ const message = { website: '网站', project: '项目', config: '配置', - ssh: 'SSH 配置', + ssh: 'SSH 管理', firewall: '防火墙', ssl: '证书', database: '数据库', @@ -873,13 +873,19 @@ const message = { keyAuthHelper: '是否启用密钥认证,默认启用。', useDNS: '反向解析', dnsHelper: '控制 SSH 服务器是否启用 DNS 解析功能,从而验证连接方的身份。', - loginLogs: 'SSH 登录日志', + loginLogs: '登录日志', loginMode: '登录方式', authenticating: '密钥', publickey: '密钥', belong: '归属地', local: '内网', remote: '外网', + config: '配置', + session: '会话', + loginTime: '登录时间', + loginIP: '登录IP', + disconnect: '断开', + stopSSHWarn: '是否断开此SSH连接', }, setting: { all: '全部', diff --git a/frontend/src/routers/modules/host.ts b/frontend/src/routers/modules/host.ts index 322a712f1..2dc95c0f0 100644 --- a/frontend/src/routers/modules/host.ts +++ b/frontend/src/routers/modules/host.ts @@ -101,6 +101,16 @@ const hostRouter = { requiresAuth: false, }, }, + { + path: '/hosts/ssh/session', + name: 'SSHSession', + component: () => import('@/views/host/ssh/session/index.vue'), + hidden: true, + meta: { + activeMenu: '/hosts/ssh/ssh', + requiresAuth: false, + }, + }, ], }; diff --git a/frontend/src/views/host/process/process/index.vue b/frontend/src/views/host/process/process/index.vue index 0fce4420f..2debcd055 100644 --- a/frontend/src/views/host/process/process/index.vue +++ b/frontend/src/views/host/process/process/index.vue @@ -62,18 +62,20 @@ ref="tableRef" v-loading="data.length === 0" > - - - + - + diff --git a/frontend/src/views/host/ssh/index.vue b/frontend/src/views/host/ssh/index.vue index dca178b54..5d9e9aefb 100644 --- a/frontend/src/views/host/ssh/index.vue +++ b/frontend/src/views/host/ssh/index.vue @@ -13,12 +13,16 @@ import RouterButton from '@/components/router-button/index.vue'; const buttons = [ { - label: i18n.global.t('menu.ssh'), + label: i18n.global.t('menu.config'), path: '/hosts/ssh/ssh', }, { label: i18n.global.t('ssh.loginLogs'), path: '/hosts/ssh/log', }, + { + label: i18n.global.t('ssh.session'), + path: '/hosts/ssh/session', + }, ]; diff --git a/frontend/src/views/host/ssh/session/index.vue b/frontend/src/views/host/ssh/session/index.vue new file mode 100644 index 000000000..d4de85cc2 --- /dev/null +++ b/frontend/src/views/host/ssh/session/index.vue @@ -0,0 +1,143 @@ + + + diff --git a/frontend/src/views/host/ssh/ssh/index.vue b/frontend/src/views/host/ssh/ssh/index.vue index 96aed57e9..2401158f4 100644 --- a/frontend/src/views/host/ssh/ssh/index.vue +++ b/frontend/src/views/host/ssh/ssh/index.vue @@ -44,7 +44,7 @@ - +