@@ -3,12 +3,13 @@ package websocket
33import (
44 "encoding/json"
55 "fmt"
6- "github.com/1Panel-dev/1Panel/agent/utils/common"
76 "sort"
87 "strings"
98 "sync"
109 "time"
1110
11+ "github.com/1Panel-dev/1Panel/agent/utils/common"
12+
1213 "github.com/1Panel-dev/1Panel/agent/global"
1314 "github.com/1Panel-dev/1Panel/agent/utils/files"
1415 "github.com/shirou/gopsutil/v4/host"
@@ -252,46 +253,67 @@ func getSSHSessions(config SSHSessionConfig) (res []byte, err error) {
252253 users []host.UserStat
253254 processes []* process.Process
254255 )
255- processes , err = process . Processes ()
256+ users , err = host . Users ()
256257 if err != nil {
258+ res , err = json .Marshal (result )
257259 return
258260 }
259- users , err = host .Users ()
260- if err != nil {
261+
262+ usersByHost := make (map [string ][]host.UserStat )
263+ for _ , user := range users {
264+ if user .Host == "" {
265+ continue
266+ }
267+ if config .LoginUser != "" && ! strings .Contains (user .User , config .LoginUser ) {
268+ continue
269+ }
270+ if config .LoginIP != "" && ! strings .Contains (user .Host , config .LoginIP ) {
271+ continue
272+ }
273+ usersByHost [user .Host ] = append (usersByHost [user .Host ], user )
274+ }
275+
276+ if len (usersByHost ) == 0 {
261277 res , err = json .Marshal (result )
262278 return
263279 }
280+
281+ processes , err = process .Processes ()
282+ if err != nil {
283+ return
284+ }
285+
264286 for _ , proc := range processes {
265287 name , _ := proc .Name ()
266288 if name != "sshd" || proc .Pid == 0 {
267289 continue
268290 }
269291 connections , _ := proc .Connections ()
292+ if len (connections ) == 0 {
293+ continue
294+ }
295+
296+ cmdline , cmdErr := proc .Cmdline ()
297+ if cmdErr != nil {
298+ continue
299+ }
300+
270301 for _ , conn := range connections {
271- for _ , user := range users {
272- if user .Host == "" {
273- continue
274- }
275- if conn .Raddr .IP == user .Host {
276- if config .LoginUser != "" && ! strings .Contains (user .User , config .LoginUser ) {
277- continue
278- }
279- if config .LoginIP != "" && ! strings .Contains (user .Host , config .LoginIP ) {
280- continue
281- }
282- if terminal , err := proc .Cmdline (); err == nil {
283- if strings .Contains (terminal , user .Terminal ) {
284- session := sshSession {
285- Username : user .User ,
286- Host : user .Host ,
287- Terminal : user .Terminal ,
288- PID : proc .Pid ,
289- }
290- t := time .Unix (int64 (user .Started ), 0 )
291- session .LoginTime = t .Format ("2006-1-2 15:04:05" )
292- result = append (result , session )
293- }
294- }
302+ matchedUsers , exists := usersByHost [conn .Raddr .IP ]
303+ if ! exists {
304+ continue
305+ }
306+
307+ for _ , user := range matchedUsers {
308+ if strings .Contains (cmdline , user .Terminal ) {
309+ t := time .Unix (int64 (user .Started ), 0 )
310+ result = append (result , sshSession {
311+ Username : user .User ,
312+ Host : user .Host ,
313+ Terminal : user .Terminal ,
314+ PID : proc .Pid ,
315+ LoginTime : t .Format ("2006-1-2 15:04:05" ),
316+ })
295317 }
296318 }
297319 }
0 commit comments