|
| 1 | +package sessions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/carapace-sh/carapace" |
| 8 | + "github.com/chainreactors/IoM-go/proto/client/clientpb" |
| 9 | + "github.com/chainreactors/malice-network/client/command/common" |
| 10 | + "github.com/chainreactors/malice-network/client/core" |
| 11 | + "github.com/chainreactors/tui" |
| 12 | + "github.com/evertras/bubble-table/table" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/spf13/pflag" |
| 15 | +) |
| 16 | + |
| 17 | +func newSessionLinkCommand(con *core.Console) *cobra.Command { |
| 18 | + linkCommand := &cobra.Command{ |
| 19 | + Use: "link", |
| 20 | + Short: "Manage session parent-child relationships", |
| 21 | + Long: "List and manage manually assigned parent-child relationships between sessions.", |
| 22 | + Example: `~~~ |
| 23 | +session link |
| 24 | +session link --parent 08d6c05a21512a79a1dfeb9d2a8f262f |
| 25 | +~~~`, |
| 26 | + Args: cobra.NoArgs, |
| 27 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 28 | + return listSessionLinksCmd(cmd, con) |
| 29 | + }, |
| 30 | + } |
| 31 | + bindSessionLinkFilterFlags(linkCommand, con) |
| 32 | + |
| 33 | + listCommand := &cobra.Command{ |
| 34 | + Use: "list", |
| 35 | + Short: "List session parent-child relationships", |
| 36 | + Long: "List all session links, or filter them by parent and child session IDs.", |
| 37 | + Example: `~~~ |
| 38 | +session link list |
| 39 | +session link list --child 08d6c05a21512a79a1dfeb9d2a8f262f |
| 40 | +~~~`, |
| 41 | + Args: cobra.NoArgs, |
| 42 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 43 | + return listSessionLinksCmd(cmd, con) |
| 44 | + }, |
| 45 | + } |
| 46 | + bindSessionLinkFilterFlags(listCommand, con) |
| 47 | + |
| 48 | + setCommand := &cobra.Command{ |
| 49 | + Use: "set", |
| 50 | + Aliases: []string{"reparent"}, |
| 51 | + Short: "Set or replace a session parent", |
| 52 | + Long: "Assign a parent to a session. If the child already has a parent, replace the existing relationship.", |
| 53 | + Example: `~~~ |
| 54 | +session link set --parent 08d6c05a21512a79a1dfeb9d2a8f262f --child b2bc23d0325a476ea01308d93f15f9da |
| 55 | +session link reparent --parent c459870f1f854653a81407d7018ee756 --child b2bc23d0325a476ea01308d93f15f9da |
| 56 | +~~~`, |
| 57 | + Args: cobra.NoArgs, |
| 58 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 59 | + return setSessionLinkCmd(cmd, con) |
| 60 | + }, |
| 61 | + } |
| 62 | + common.BindFlag(setCommand, func(flags *pflag.FlagSet) { |
| 63 | + flags.String("parent", "", "parent session id") |
| 64 | + flags.String("child", "", "child session id") |
| 65 | + }) |
| 66 | + _ = setCommand.MarkFlagRequired("parent") |
| 67 | + _ = setCommand.MarkFlagRequired("child") |
| 68 | + bindSessionLinkFlagCompletions(setCommand, con) |
| 69 | + |
| 70 | + unlinkCommand := &cobra.Command{ |
| 71 | + Use: "unlink", |
| 72 | + Short: "Remove a session parent relationship", |
| 73 | + Long: "Detach a child session from its current parent while preserving the child's own descendants.", |
| 74 | + Example: `~~~ |
| 75 | +session link unlink --child b2bc23d0325a476ea01308d93f15f9da |
| 76 | +~~~`, |
| 77 | + Args: cobra.NoArgs, |
| 78 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 79 | + return removeSessionLinkCmd(cmd, con) |
| 80 | + }, |
| 81 | + } |
| 82 | + common.BindFlag(unlinkCommand, func(flags *pflag.FlagSet) { |
| 83 | + flags.String("child", "", "child session id") |
| 84 | + }) |
| 85 | + _ = unlinkCommand.MarkFlagRequired("child") |
| 86 | + common.BindFlagCompletions(unlinkCommand, func(comp carapace.ActionMap) { |
| 87 | + comp["child"] = common.AllSessionIDCompleter(con) |
| 88 | + }) |
| 89 | + |
| 90 | + linkCommand.AddCommand(listCommand, setCommand, unlinkCommand) |
| 91 | + return linkCommand |
| 92 | +} |
| 93 | + |
| 94 | +func bindSessionLinkFilterFlags(cmd *cobra.Command, con *core.Console) { |
| 95 | + common.BindFlag(cmd, func(flags *pflag.FlagSet) { |
| 96 | + flags.String("parent", "", "filter by parent session id") |
| 97 | + flags.String("child", "", "filter by child session id") |
| 98 | + }) |
| 99 | + bindSessionLinkFlagCompletions(cmd, con) |
| 100 | +} |
| 101 | + |
| 102 | +func bindSessionLinkFlagCompletions(cmd *cobra.Command, con *core.Console) { |
| 103 | + common.BindFlagCompletions(cmd, func(comp carapace.ActionMap) { |
| 104 | + comp["parent"] = common.AllSessionIDCompleter(con) |
| 105 | + comp["child"] = common.AllSessionIDCompleter(con) |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +func listSessionLinksCmd(cmd *cobra.Command, con *core.Console) error { |
| 110 | + parentSessionID, err := resolveSessionLinkFlag(con, cmd, "parent") |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + childSessionID, err := resolveSessionLinkFlag(con, cmd, "child") |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + links, err := con.Rpc.ListSessionLinks(con.Context(), &clientpb.SessionLinkRequest{ |
| 120 | + ParentSessionId: parentSessionID, |
| 121 | + ChildSessionId: childSessionID, |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + if len(links.GetLinks()) == 0 { |
| 127 | + con.Log.Console("No session links found\n") |
| 128 | + return nil |
| 129 | + } |
| 130 | + printSessionLinks(con, links.GetLinks()) |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func setSessionLinkCmd(cmd *cobra.Command, con *core.Console) error { |
| 135 | + parentSessionID, err := resolveSessionLinkFlag(con, cmd, "parent") |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + childSessionID, err := resolveSessionLinkFlag(con, cmd, "child") |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + link, err := con.Rpc.SetSessionLink(con.Context(), &clientpb.SessionLinkRequest{ |
| 145 | + ParentSessionId: parentSessionID, |
| 146 | + ChildSessionId: childSessionID, |
| 147 | + }) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + con.Log.Console(fmt.Sprintf("Session %s now uses %s as its parent\n", link.GetChildSessionId(), link.GetParentSessionId())) |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func removeSessionLinkCmd(cmd *cobra.Command, con *core.Console) error { |
| 156 | + childSessionID, err := resolveSessionLinkFlag(con, cmd, "child") |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + if _, err := con.Rpc.RemoveSessionLink(con.Context(), &clientpb.SessionLinkRequest{ChildSessionId: childSessionID}); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + con.Log.Console(fmt.Sprintf("Session %s detached from its parent\n", childSessionID)) |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func resolveSessionLinkFlag(con *core.Console, cmd *cobra.Command, name string) (string, error) { |
| 168 | + sessionID, err := cmd.Flags().GetString(name) |
| 169 | + if err != nil || sessionID == "" { |
| 170 | + return sessionID, err |
| 171 | + } |
| 172 | + return resolveSessionID(con, sessionID) |
| 173 | +} |
| 174 | + |
| 175 | +func printSessionLinks(con *core.Console, links []*clientpb.SessionLink) { |
| 176 | + rows := make([]table.Row, 0, len(links)) |
| 177 | + for _, link := range links { |
| 178 | + if link == nil { |
| 179 | + continue |
| 180 | + } |
| 181 | + updatedAt := "-" |
| 182 | + if link.GetUpdatedAt() > 0 { |
| 183 | + updatedAt = time.Unix(link.GetUpdatedAt(), 0).Format("2006-01-02 15:04:05") |
| 184 | + } |
| 185 | + rows = append(rows, table.NewRow(table.RowData{ |
| 186 | + "Parent": link.GetParentSessionId(), |
| 187 | + "Child": link.GetChildSessionId(), |
| 188 | + "Source": link.GetSource(), |
| 189 | + "Updated": updatedAt, |
| 190 | + })) |
| 191 | + } |
| 192 | + |
| 193 | + tableModel := tui.NewTable([]table.Column{ |
| 194 | + table.NewFlexColumn("Parent", "Parent", 1), |
| 195 | + table.NewFlexColumn("Child", "Child", 1), |
| 196 | + table.NewColumn("Source", "Source", 10), |
| 197 | + table.NewColumn("Updated", "Updated", 19), |
| 198 | + }, true) |
| 199 | + tableModel.SetMultiline() |
| 200 | + tableModel.SetRows(rows) |
| 201 | + tableModel.Title = "session links" |
| 202 | + con.Log.Console(tableModel.View()) |
| 203 | +} |
0 commit comments