Skip to content

Commit 48c3bfc

Browse files
committed
feat(prompt): starship-style prompt layout and GetSessionCount RPC
Restructure the two-line prompt for both client and implant menus: - Status line: name on hostname os/arch via pipeline age (group) - Input line: sessionID ❯ Add lightweight GetSessionCount RPC to display accurate alive/total session counts from memory and DB respectively.
1 parent 4f2b782 commit 48c3bfc

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

client/core/console.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/chainreactors/IoM-go/client"
1717
"github.com/chainreactors/IoM-go/consts"
18+
"github.com/chainreactors/IoM-go/proto/client/clientpb"
1819
"github.com/chainreactors/logs"
1920
"github.com/chainreactors/malice-network/client/repl"
2021
"github.com/reeflective/console"
@@ -261,7 +262,7 @@ func (c *Console) GetPrompt() string {
261262

262263
session := c.ActiveTarget.Get()
263264
if session != nil {
264-
promptLine = tui.NewSessionColor(session.GroupName, session.SessionId[:8]) + " " + promptLine
265+
promptLine = tui.DefaultNameStyle.Render(session.SessionId[:8]) + " " + promptLine
265266
}
266267

267268
if statusLine == "" {
@@ -304,14 +305,7 @@ func (c *Console) getStatusLine() string {
304305

305306
session := c.ActiveTarget.Get()
306307
if session == nil {
307-
// Client menu: user on v0.5.0 sessions 3/5
308-
var alive, total int
309-
for _, s := range c.Sessions {
310-
total++
311-
if s.IsAlive {
312-
alive++
313-
}
314-
}
308+
// Client menu: user on v0.5.0 sessions alive/total
315309
version := ""
316310
if c.Info != nil {
317311
version = c.Info.Version
@@ -320,32 +314,37 @@ func (c *Console) getStatusLine() string {
320314
if c.Client != nil {
321315
name = c.Client.Name
322316
}
317+
sessionInfo := fmt.Sprintf("%d", len(c.Sessions))
318+
count, err := c.Rpc.GetSessionCount(context.Background(), &clientpb.Empty{})
319+
if err == nil && count != nil {
320+
sessionInfo = fmt.Sprintf("%d/%d", count.Alive, count.Total)
321+
}
323322
return fmt.Sprintf("%s %s %s %s %s",
324323
tui.CyanFg.Render(name),
325324
tui.DarkGrayFg.Render("on"),
326325
tui.GreenFg.Render(version),
327326
tui.DarkGrayFg.Render("sessions"),
328-
tui.YellowFg.Render(fmt.Sprintf("%d/%d", alive, total)),
327+
tui.YellowFg.Render(sessionInfo),
329328
)
330329
}
331330

332-
// Implant menu: [note] hostname os/arch via pipeline age
333-
parts := make([]string, 0, 7)
334-
if session.Note != "" {
335-
parts = append(parts, tui.WhiteFg.Bold(true).Render(session.Note))
336-
}
331+
// Implant menu: name on hostname os/arch via pipeline age (group)
332+
parts := make([]string, 0, 8)
337333
hostname := ""
338334
osInfo := ""
339335
if session.Os != nil {
340336
hostname = session.Os.Hostname
341337
osInfo = session.Os.Name + "/" + session.Os.Arch
342338
}
343339
parts = append(parts,
340+
tui.WhiteFg.Bold(true).Render(session.Name),
341+
tui.DarkGrayFg.Render("on"),
344342
tui.CyanFg.Render(hostname),
345343
tui.GreenFg.Render(osInfo),
346344
tui.DarkGrayFg.Render("via"),
347345
tui.PurpleFg.Render(session.PipelineId),
348346
tui.YellowFg.Render(formatCheckinAge(session.LastCheckin)),
347+
tui.DarkGrayFg.Render("("+session.GroupName+")"),
349348
)
350349
return strings.Join(parts, " ")
351350
}

server/rpc/rpc-session.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ func (rpc *Server) GetSessions(ctx context.Context, req *clientpb.SessionRequest
4040
return sessions, nil
4141
}
4242

43+
func (rpc *Server) GetSessionCount(ctx context.Context, req *clientpb.Empty) (*clientpb.SessionCount, error) {
44+
alive := int32(len(core.Sessions.All()))
45+
total, err := db.NewSessionQuery().WhereRemoved(false).Count()
46+
if err != nil {
47+
return nil, err
48+
}
49+
return &clientpb.SessionCount{
50+
Alive: alive,
51+
Total: int32(total),
52+
}, nil
53+
}
54+
4355
func (rpc *Server) GetSession(ctx context.Context, req *clientpb.SessionRequest) (*clientpb.Session, error) {
4456
session, err := core.Sessions.Get(req.SessionId)
4557
if err == nil {

0 commit comments

Comments
 (0)